Bug 852257 - implement Gamepad.timestamp property; r=ted, r=smaug

--HG--
extra : rebase_source : 39bb03690dd1b455c73604eca3eb0f2811449a54
extra : amend_source : 34220433ef534585057c0a7f0fca370b8cb0bc86
This commit is contained in:
Snigdha Agarwal 2015-01-07 14:17:04 -05:00
parent d0c2ece498
commit eb67e9d016
6 changed files with 83 additions and 0 deletions

View File

@ -4,6 +4,7 @@
#include "Gamepad.h"
#include "nsAutoPtr.h"
#include "nsPIDOMWindow.h"
#include "nsTArray.h"
#include "nsVariant.h"
#include "mozilla/dom/GamepadBinding.h"
@ -21,6 +22,18 @@ NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Gamepad, mParent, mButtons)
void
Gamepad::UpdateTimestamp()
{
nsCOMPtr<nsPIDOMWindow> newWindow(do_QueryInterface(mParent));
if(newWindow) {
nsPerformance* perf = newWindow->GetPerformance();
if (perf) {
mTimestamp = perf->GetDOMTiming()->TimeStampToDOMHighRes(TimeStamp::Now());
}
}
}
Gamepad::Gamepad(nsISupports* aParent,
const nsAString& aID, uint32_t aIndex,
GamepadMappingType aMapping,
@ -37,6 +50,7 @@ Gamepad::Gamepad(nsISupports* aParent,
mButtons.InsertElementAt(i, new GamepadButton(mParent));
}
mAxes.InsertElementsAt(0, aNumAxes, 0.0f);
UpdateTimestamp();
}
void
@ -57,6 +71,7 @@ Gamepad::SetButton(uint32_t aButton, bool aPressed, double aValue)
MOZ_ASSERT(aButton < mButtons.Length());
mButtons[aButton]->SetPressed(aPressed);
mButtons[aButton]->SetValue(aValue);
UpdateTimestamp();
}
void
@ -67,6 +82,7 @@ Gamepad::SetAxis(uint32_t aAxis, double aValue)
mAxes[aAxis] = aValue;
GamepadBinding::ClearCachedAxesValue(this);
}
UpdateTimestamp();
}
void
@ -90,6 +106,7 @@ Gamepad::SyncState(Gamepad* aOther)
if (changed) {
GamepadBinding::ClearCachedAxesValue(this);
}
UpdateTimestamp();
}
already_AddRefed<Gamepad>

View File

@ -13,6 +13,7 @@
#include "nsString.h"
#include "nsTArray.h"
#include "nsWrapperCache.h"
#include "nsPerformance.h"
namespace mozilla {
namespace dom {
@ -65,6 +66,11 @@ public:
aID = mID;
}
DOMHighResTimeStamp Timestamp() const
{
return mTimestamp;
}
GamepadMappingType Mapping()
{
return mMapping;
@ -92,6 +98,7 @@ public:
private:
virtual ~Gamepad() {}
void UpdateTimestamp();
protected:
nsCOMPtr<nsISupports> mParent;
@ -107,6 +114,7 @@ protected:
// Current state of buttons, axes.
nsTArray<nsRefPtr<GamepadButton>> mButtons;
nsTArray<double> mAxes;
DOMHighResTimeStamp mTimestamp;
};
} // namespace dom

View File

@ -5,6 +5,7 @@ support-files =
gamepad_frame_state.html
mock_gamepad.js
[test_check_timestamp.html]
[test_gamepad.html]
skip-if = (buildapp == 'b2g' && toolkit != 'gonk') #Bug 931116, b2g desktop specific, initial triage
[test_gamepad_connect_events.html]

View File

@ -0,0 +1,51 @@
<!-- Any copyright is dedicated to the Public Domain.
- http://creativecommons.org/publicdomain/zero/1.0/ -->
<!DOCTYPE HTML>
<html>
<head>
<title>Test Gamepad.timestamp</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<script type="text/javascript" src="mock_gamepad.js"></script>
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();
var index = GamepadService.addGamepad("test gamepad", // id
SpecialPowers.Ci.nsIGamepadServiceTest.NO_MAPPING,
4, // buttons
2);// axes
var timea=0;
window.addEventListener("gamepadbuttondown", buttonpresshandler);
var firstPress = true;
GamepadService.newButtonEvent(index, 0, true);
GamepadService.newButtonEvent(index, 0, true);
function cleanup(){
SpecialPowers.executeSoon(function() {
GamepadService.removeGamepad(index);
SimpleTest.finish();
});
}
function buttonpresshandler(e) {
if(timea == 0){
timea = e.gamepad.timestamp;
}
else{
ok(timea <= e.gamepad.timestamp);
}
GamepadService.newButtonEvent(index, 0, false);
if (!firstPress) {
SpecialPowers.executeSoon(cleanup);
}
else {
firstPress = false;
}
}
</script>
</body>
</html>

View File

@ -20,6 +20,7 @@ var index = GamepadService.addGamepad("test gamepad", // id
// Press a button
GamepadService.newButtonEvent(index, 0, true);
function connecthandler(e) {
ok(e.gamepad.timestamp <= performance.now());
is(e.gamepad.id, "test gamepad", "correct gamepad name");
is(e.gamepad.mapping, "standard", "standard mapping");
is(e.gamepad.buttons.length, 4, "correct number of buttons");

View File

@ -51,4 +51,9 @@ interface Gamepad {
*/
[Pure, Cached, Frozen]
readonly attribute sequence<double> axes;
/**
* Timestamp from when the data of this device was last updated.
*/
readonly attribute DOMHighResTimeStamp timestamp;
};