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
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
Cu.import("resource://services-sync/engines/tabs.js");
Cu.import("resource://services-sync/service.js");
Cu.import("resource://services-sync/util.js");
var clientsEngine = Service.clientsEngine;
function fakeSvcWinMediator() {
// actions on windows are captured in logs
let logs = [];
delete Services.wm;
Services.wm = {
getEnumerator: function() {
return {
cnt: 2,
hasMoreElements: function() {
return this.cnt-- > 0;
},
getNext: function() {
let elt = {addTopics: [], remTopics: [], numAPL: 0, numRPL: 0};
logs.push(elt);
return {
addEventListener: function(topic) {
elt.addTopics.push(topic);
},
removeEventListener: function(topic) {
elt.remTopics.push(topic);
},
gBrowser: {
addProgressListener() {
elt.numAPL++;
},
removeProgressListener() {
elt.numRPL++;
},
},
};
}
};
}
};
return logs;
}
function run_test() {
let engine = Service.engineManager.get("tabs");
_("We assume that tabs have changed at startup.");
let tracker = engine._tracker;
tracker.persistChangedIDs = false;
do_check_true(tracker.modified);
do_check_true(Utils.deepEquals(Object.keys(engine.getChangedIDs()),
[clientsEngine.localID]));
let logs;
_("Test listeners are registered on windows");
logs = fakeSvcWinMediator();
Svc.Obs.notify("weave:engine:start-tracking");
do_check_eq(logs.length, 2);
for (let log of logs) {
do_check_eq(log.addTopics.length, 5);
do_check_true(log.addTopics.indexOf("pageshow") >= 0);
do_check_true(log.addTopics.indexOf("TabOpen") >= 0);
do_check_true(log.addTopics.indexOf("TabClose") >= 0);
do_check_true(log.addTopics.indexOf("TabSelect") >= 0);
do_check_true(log.addTopics.indexOf("unload") >= 0);
do_check_eq(log.remTopics.length, 0);
do_check_eq(log.numAPL, 1, "Added 1 progress listener");
do_check_eq(log.numRPL, 0, "Didn't remove a progress listener");
}
_("Test listeners are unregistered on windows");
logs = fakeSvcWinMediator();
Svc.Obs.notify("weave:engine:stop-tracking");
do_check_eq(logs.length, 2);
for (let log of logs) {
do_check_eq(log.addTopics.length, 0);
do_check_eq(log.remTopics.length, 5);
do_check_true(log.remTopics.indexOf("pageshow") >= 0);
do_check_true(log.remTopics.indexOf("TabOpen") >= 0);
do_check_true(log.remTopics.indexOf("TabClose") >= 0);
do_check_true(log.remTopics.indexOf("TabSelect") >= 0);
do_check_true(log.remTopics.indexOf("unload") >= 0);
do_check_eq(log.numAPL, 0, "Didn't add a progress listener");
do_check_eq(log.numRPL, 1, "Removed 1 progress listener");
}
_("Test tab listener");
for (let evttype of ["TabOpen", "TabClose", "TabSelect"]) {
// Pretend we just synced.
tracker.clearChangedIDs();
do_check_false(tracker.modified);
// Send a fake tab event
tracker.onTab({type: evttype , originalTarget: evttype});
do_check_true(tracker.modified);
do_check_true(Utils.deepEquals(Object.keys(engine.getChangedIDs()),
[clientsEngine.localID]));
}
// Pretend we just synced.
tracker.clearChangedIDs();
do_check_false(tracker.modified);
tracker.onTab({type: "pageshow", originalTarget: "pageshow"});
do_check_true(Utils.deepEquals(Object.keys(engine.getChangedIDs()),
[clientsEngine.localID]));
// Pretend we just synced and saw some progress listeners.
tracker.clearChangedIDs();
do_check_false(tracker.modified);
tracker.onLocationChange({ isTopLevel: false }, undefined, undefined, 0);
do_check_false(tracker.modified, "non-toplevel request didn't flag as modified");
tracker.onLocationChange({ isTopLevel: true }, undefined, undefined,
Ci.nsIWebProgressListener.LOCATION_CHANGE_SAME_DOCUMENT);
do_check_false(tracker.modified, "location change within the same document request didn't flag as modified");
tracker.onLocationChange({ isTopLevel: true }, undefined, undefined, 0);
do_check_true(tracker.modified, "location change for a new top-level document flagged as modified");
do_check_true(Utils.deepEquals(Object.keys(engine.getChangedIDs()),
[clientsEngine.localID]));
}
|