bug 782549 - check if the window has device sensor listeners, instead of global listener count (r=dougt)

This commit is contained in:
Mook 2012-10-20 21:40:56 -07:00
Родитель e0405b2d7b
Коммит 37e994d127
3 изменённых файлов: 17 добавлений и 12 удалений

Просмотреть файл

@ -16,16 +16,16 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=402089
/** Test for Bug 742376 **/
function getListenerCount() {
function hasListeners() {
var Cc = SpecialPowers.Cc;
var Ci = SpecialPowers.Ci;
var dss = Cc["@mozilla.org/devicesensors;1"].getService(Ci.nsIDeviceSensors);
return dss.listenerCount(Ci.nsIDeviceSensorData.TYPE_ORIENTATION);
return dss.hasWindowListener(Ci.nsIDeviceSensorData.TYPE_ORIENTATION, window);
}
var startListenerCount = getListenerCount();
is(hasListeners(), false, "Must not have listeners before tests start");
function dumbListener(event) {}
function dumbListener2(event) {}
@ -33,14 +33,17 @@ function dumbListener2(event) {}
window.addEventListener("deviceorientation", dumbListener, false);
window.addEventListener("deviceorientation", dumbListener2, false);
is(hasListeners(), true, "Listeners should have been added");
window.setTimeout(function() {
window.removeEventListener("deviceorientation", dumbListener, false);
is(hasListeners(), true, "Only some listeners should have been removed");
window.setTimeout(function() {
window.removeEventListener("deviceorientation", dumbListener2, false);
window.setTimeout(function() {
is (getListenerCount(), startListenerCount, "Must have the same listeners at this point");
is(hasListeners(), false, "Listeners should have been removed");
SimpleTest.finish();
}, 0);
}, 0);

Просмотреть файл

@ -119,14 +119,13 @@ nsDeviceSensors::~nsDeviceSensors()
}
}
NS_IMETHODIMP nsDeviceSensors::ListenerCount(uint32_t aType, int32_t *aRetVal)
NS_IMETHODIMP nsDeviceSensors::HasWindowListener(uint32_t aType, nsIDOMWindow *aWindow, bool *aRetVal)
{
if (!mEnabled) {
*aRetVal = 0;
return NS_OK;
}
if (!mEnabled)
*aRetVal = false;
else
*aRetVal = mWindowListeners[aType]->IndexOf(aWindow) != NoIndex;
*aRetVal = mWindowListeners[aType]->Length();
return NS_OK;
}

Просмотреть файл

@ -24,10 +24,13 @@ interface nsIDeviceSensorData : nsISupports
readonly attribute double z;
};
[scriptable, uuid(83306c9f-1c8f-43c4-900a-245d7f219511)]
[scriptable, uuid(e46e47c7-55ff-44c4-abce-21b14ba07f86)]
interface nsIDeviceSensors : nsISupports
{
long listenerCount(in unsigned long aType);
/**
* Returns true if the given window has any listeners of the given type
*/
bool hasWindowListener(in unsigned long aType, in nsIDOMWindow aWindow);
// Holds pointers, not AddRef objects -- it is up to the caller
// to call RemoveWindowListener before the window is deleted.