Bug 759989 - Add test to ensure device sensors are shutdown when listeners are removed. r=smaug

This commit is contained in:
Doug Turner 2012-06-07 10:58:33 -07:00
parent 4c1959cf85
commit 3cee32484e
5 changed files with 87 additions and 8 deletions

View File

@ -85,6 +85,7 @@ _TEST_FILES = \
test_bug741666.html \
test_dom_keyboard_event.html \
test_dom_mouse_event.html \
test_bug742376.html \
$(NULL)
#bug 585630

View File

@ -0,0 +1,55 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=402089
-->
<head>
<title>Test for Bug 742376</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=742376">Mozilla Bug 742376</a>
<script class="testbody" type="text/javascript">
/** Test for Bug 742376 **/
function getListenerCount() {
var Cc = SpecialPowers.wrap(Components).classes;
var Ci = SpecialPowers.wrap(Components).interfaces;
var dss = Cc["@mozilla.org/devicesensors;1"].getService(Ci.nsIDeviceSensors);
return dss.listenerCount(Ci.nsIDeviceSensorData.TYPE_ORIENTATION);
}
var startListenerCount = getListenerCount();
function dumbListener(event) {}
function dumbListener2(event) {}
window.addEventListener("deviceorientation", dumbListener, false);
window.addEventListener("deviceorientation", dumbListener2, false);
window.setTimeout(function() {
window.removeEventListener("deviceorientation", dumbListener, false);
window.setTimeout(function() {
window.removeEventListener("deviceorientation", dumbListener2, false);
window.setTimeout(function() {
is (getListenerCount(), startListenerCount, "Must have the same listeners at this point");
SimpleTest.finish();
}, 0);
}, 0);
}, 0);
SimpleTest.waitForExplicitFinish();
</script>
</pre>
</body>
</html>

View File

@ -10076,35 +10076,44 @@ nsGlobalWindow::EnableDeviceSensor(PRUint32 aType)
}
}
if (alreadyEnabled)
return;
mEnabledSensors.AppendElement(aType);
if (alreadyEnabled) {
return;
}
nsCOMPtr<nsIDeviceSensors> ac = do_GetService(NS_DEVICE_SENSORS_CONTRACTID);
if (ac)
if (ac) {
ac->AddWindowListener(aType, this);
}
}
void
nsGlobalWindow::DisableDeviceSensor(PRUint32 aType)
{
PRInt32 doomedElement = -1;
PRInt32 listenerCount = 0;
for (PRUint32 i = 0; i < mEnabledSensors.Length(); i++) {
if (mEnabledSensors[i] == aType) {
doomedElement = i;
break;
listenerCount++;
}
}
if (doomedElement == -1)
if (doomedElement == -1) {
return;
}
mEnabledSensors.RemoveElementAt(doomedElement);
if (listenerCount > 1) {
return;
}
nsCOMPtr<nsIDeviceSensors> ac = do_GetService(NS_DEVICE_SENSORS_CONTRACTID);
if (ac)
if (ac) {
ac->RemoveWindowListener(aType, this);
}
}
NS_IMETHODIMP

View File

@ -118,6 +118,17 @@ nsDeviceSensors::~nsDeviceSensors()
}
}
NS_IMETHODIMP nsDeviceSensors::ListenerCount(PRUint32 aType, PRInt32 *aRetVal)
{
if (!mEnabled) {
*aRetVal = 0;
return NS_OK;
}
*aRetVal = mWindowListeners[aType]->Length();
return NS_OK;
}
NS_IMETHODIMP nsDeviceSensors::AddWindowListener(PRUint32 aType, nsIDOMWindow *aWindow)
{
if (!mEnabled)

View File

@ -24,11 +24,14 @@ interface nsIDeviceSensorData : nsISupports
readonly attribute double z;
};
[scriptable, uuid(b672bfe0-4479-4094-a9ef-1b6847720d07)]
[scriptable, uuid(83306c9f-1c8f-43c4-900a-245d7f219511)]
interface nsIDeviceSensors : nsISupports
{
long listenerCount(in unsigned long aType);
// Holds pointers, not AddRef objects -- it is up to the caller
// to call RemoveWindowListener before the window is deleted.
[noscript] void addWindowListener(in unsigned long aType, in nsIDOMWindow aWindow);
[noscript] void removeWindowListener(in unsigned long aType, in nsIDOMWindow aWindow);
[noscript] void removeWindowAsListener(in nsIDOMWindow aWindow);