1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";
Cu.import("resource://testing-common/MockRegistrar.jsm");
let idleService = {
_observers: new Set(),
_activity: {
addCalls: [],
removeCalls: [],
observerFires: [],
},
_reset: function() {
this._observers.clear();
this._activity.addCalls = [];
this._activity.removeCalls = [];
this._activity.observerFires = [];
},
_fireObservers: function(state) {
for (let observer of this._observers.values()) {
observer.observe(observer, state, null);
this._activity.observerFires.push(state);
}
},
QueryInterface: XPCOMUtils.generateQI([Ci.nsIIdleService]),
idleTime: 19999,
addIdleObserver: function(observer, time) {
this._observers.add(observer);
this._activity.addCalls.push(time);
},
removeIdleObserver: function(observer, time) {
this._observers.delete(observer);
this._activity.removeCalls.push(time);
},
};
function checkActivity(expectedActivity) {
let {expectedAdd, expectedRemove, expectedFires} = expectedActivity;
let {addCalls, removeCalls, observerFires} = idleService._activity;
equal(expectedAdd.length, addCalls.length, "idleService.addIdleObserver was called the expected number of times");
equal(expectedRemove.length, removeCalls.length, "idleService.removeIdleObserver was called the expected number of times");
equal(expectedFires.length, observerFires.length, "idle observer was fired the expected number of times");
deepEqual(addCalls, expectedAdd, "expected interval passed to idleService.addIdleObserver");
deepEqual(removeCalls, expectedRemove, "expected interval passed to idleService.removeIdleObserver");
deepEqual(observerFires, expectedFires, "expected topic passed to idle observer");
}
add_task(function* setup() {
let fakeIdleService = MockRegistrar.register("@mozilla.org/widget/idleservice;1", idleService);
do_register_cleanup(() => {
MockRegistrar.unregister(fakeIdleService);
});
});
add_task(function* testQueryStateActive() {
function background() {
browser.idle.queryState(20).then(status => {
browser.test.assertEq("active", status, "Idle status is active");
browser.test.notifyPass("idle");
},
err => {
browser.test.fail(`Error: ${err} :: ${err.stack}`);
browser.test.notifyFail("idle");
});
}
let extension = ExtensionTestUtils.loadExtension({
background,
manifest: {
permissions: ["idle"],
},
});
yield extension.startup();
yield extension.awaitFinish("idle");
yield extension.unload();
});
add_task(function* testQueryStateIdle() {
function background() {
browser.idle.queryState(15).then(status => {
browser.test.assertEq("idle", status, "Idle status is idle");
browser.test.notifyPass("idle");
},
err => {
browser.test.fail(`Error: ${err} :: ${err.stack}`);
browser.test.notifyFail("idle");
});
}
let extension = ExtensionTestUtils.loadExtension({
background,
manifest: {
permissions: ["idle"],
},
});
yield extension.startup();
yield extension.awaitFinish("idle");
yield extension.unload();
});
add_task(function* testOnlySetDetectionInterval() {
function background() {
browser.idle.setDetectionInterval(99);
browser.test.sendMessage("detectionIntervalSet");
}
let extension = ExtensionTestUtils.loadExtension({
background,
manifest: {
permissions: ["idle"],
},
});
idleService._reset();
yield extension.startup();
yield extension.awaitMessage("detectionIntervalSet");
idleService._fireObservers("idle");
checkActivity({expectedAdd: [], expectedRemove: [], expectedFires: []});
yield extension.unload();
});
add_task(function* testSetDetectionIntervalBeforeAddingListener() {
function background() {
browser.idle.setDetectionInterval(99);
browser.idle.onStateChanged.addListener(newState => {
browser.test.assertEq("idle", newState, "listener fired with the expected state");
browser.test.sendMessage("listenerFired");
});
browser.test.sendMessage("listenerAdded");
}
let extension = ExtensionTestUtils.loadExtension({
background,
manifest: {
permissions: ["idle"],
},
});
idleService._reset();
yield extension.startup();
yield extension.awaitMessage("listenerAdded");
idleService._fireObservers("idle");
yield extension.awaitMessage("listenerFired");
checkActivity({expectedAdd: [99], expectedRemove: [], expectedFires: ["idle"]});
yield extension.unload();
});
add_task(function* testSetDetectionIntervalAfterAddingListener() {
function background() {
browser.idle.onStateChanged.addListener(newState => {
browser.test.assertEq("idle", newState, "listener fired with the expected state");
browser.test.sendMessage("listenerFired");
});
browser.idle.setDetectionInterval(99);
browser.test.sendMessage("detectionIntervalSet");
}
let extension = ExtensionTestUtils.loadExtension({
background,
manifest: {
permissions: ["idle"],
},
});
idleService._reset();
yield extension.startup();
yield extension.awaitMessage("detectionIntervalSet");
idleService._fireObservers("idle");
yield extension.awaitMessage("listenerFired");
checkActivity({expectedAdd: [60, 99], expectedRemove: [60], expectedFires: ["idle"]});
yield extension.unload();
});
add_task(function* testOnlyAddingListener() {
function background() {
browser.idle.onStateChanged.addListener(newState => {
browser.test.assertEq("active", newState, "listener fired with the expected state");
browser.test.sendMessage("listenerFired");
});
browser.test.sendMessage("listenerAdded");
}
let extension = ExtensionTestUtils.loadExtension({
background,
manifest: {
permissions: ["idle"],
},
});
idleService._reset();
yield extension.startup();
yield extension.awaitMessage("listenerAdded");
idleService._fireObservers("active");
yield extension.awaitMessage("listenerFired");
// check that "idle-daily" topic does not cause a listener to fire
idleService._fireObservers("idle-daily");
checkActivity({expectedAdd: [60], expectedRemove: [], expectedFires: ["active", "idle-daily"]});
yield extension.unload();
});
|