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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
const { ContextDescriptorType } = ChromeUtils.importESModule(
"chrome://remote/content/shared/messagehandler/MessageHandler.sys.mjs"
);
const { RootMessageHandler } = ChromeUtils.importESModule(
"chrome://remote/content/shared/messagehandler/RootMessageHandler.sys.mjs"
);
const { SessionData, SessionDataMethod } = ChromeUtils.importESModule(
"chrome://remote/content/shared/messagehandler/sessiondata/SessionData.sys.mjs"
);
add_task(async function test_sessionData() {
const sessionData = new SessionData(new RootMessageHandler("session-id-1"));
equal(sessionData.getSessionData("mod", "event").length, 0);
const globalContext = {
type: ContextDescriptorType.All,
};
const otherContext = { type: "other-type", id: "some-id" };
info("Add a first event for the global context");
let updatedItems = sessionData.applySessionData([
createUpdate(SessionDataMethod.Add, globalContext, ["first.event"]),
]);
equal(updatedItems.length, 1, "One item updated");
equal(updatedItems[0].method, SessionDataMethod.Add, "One item added");
let updatedValues = updatedItems[0].values;
equal(updatedValues.length, 1, "One value added");
equal(updatedValues[0], "first.event", "Expected value was added");
checkEvents(sessionData.getSessionData("mod", "event"), [
{
value: "first.event",
contextDescriptor: globalContext,
},
]);
info("Add the exact same data (same module, type, context, value)");
updatedItems = sessionData.applySessionData([
createUpdate(SessionDataMethod.Add, globalContext, ["first.event"]),
]);
equal(updatedItems.length, 0, "No new item updated");
checkEvents(sessionData.getSessionData("mod", "event"), [
{
value: "first.event",
contextDescriptor: globalContext,
},
]);
info("Add another context for the same event");
updatedItems = sessionData.applySessionData([
createUpdate(SessionDataMethod.Add, otherContext, ["first.event"]),
]);
equal(updatedItems.length, 1, "One item updated");
equal(updatedItems[0].method, SessionDataMethod.Add, "One item added");
updatedValues = updatedItems[0].values;
equal(updatedValues.length, 1, "One value added");
equal(updatedValues[0], "first.event", "Expected value was added");
checkEvents(sessionData.getSessionData("mod", "event"), [
{
value: "first.event",
contextDescriptor: globalContext,
},
{
value: "first.event",
contextDescriptor: otherContext,
},
]);
info("Add a second event for the global context");
updatedItems = sessionData.applySessionData([
createUpdate(SessionDataMethod.Add, globalContext, ["second.event"]),
]);
equal(updatedItems.length, 1, "One item updated");
equal(updatedItems[0].method, SessionDataMethod.Add, "One item added");
updatedValues = updatedItems[0].values;
equal(updatedValues.length, 1, "One value added");
equal(updatedValues[0], "second.event", "Expected value was added");
checkEvents(sessionData.getSessionData("mod", "event"), [
{
value: "first.event",
contextDescriptor: globalContext,
},
{
value: "first.event",
contextDescriptor: otherContext,
},
{
value: "second.event",
contextDescriptor: globalContext,
},
]);
info("Add two events for the global context");
updatedItems = sessionData.applySessionData([
createUpdate(SessionDataMethod.Add, globalContext, [
"third.event",
"fourth.event",
]),
]);
equal(updatedItems.length, 1, "One item updated");
equal(updatedItems[0].method, SessionDataMethod.Add, "One item added");
updatedValues = updatedItems[0].values;
equal(updatedValues.length, 2, "Two values added");
equal(updatedValues[0], "third.event", "Expected value was added");
equal(updatedValues[1], "fourth.event", "Expected value was added");
checkEvents(sessionData.getSessionData("mod", "event"), [
{
value: "first.event",
contextDescriptor: globalContext,
},
{
value: "first.event",
contextDescriptor: otherContext,
},
{
value: "second.event",
contextDescriptor: globalContext,
},
{
value: "third.event",
contextDescriptor: globalContext,
},
{
value: "fourth.event",
contextDescriptor: globalContext,
},
]);
info("Remove the second, third and fourth events");
updatedItems = sessionData.applySessionData([
createUpdate(SessionDataMethod.Remove, globalContext, [
"second.event",
"third.event",
"fourth.event",
]),
]);
equal(updatedItems.length, 1, "One item updated");
equal(updatedItems[0].method, SessionDataMethod.Remove, "One item removed");
updatedValues = updatedItems[0].values;
equal(updatedValues.length, 3, "Three values removed");
equal(updatedValues[0], "second.event", "Expected value was removed");
equal(updatedValues[1], "third.event", "Expected value was removed");
equal(updatedValues[2], "fourth.event", "Expected value was removed");
checkEvents(sessionData.getSessionData("mod", "event"), [
{
value: "first.event",
contextDescriptor: globalContext,
},
{
value: "first.event",
contextDescriptor: otherContext,
},
]);
info("Remove the global context from the first event");
updatedItems = sessionData.applySessionData([
createUpdate(SessionDataMethod.Remove, globalContext, ["first.event"]),
]);
equal(updatedItems.length, 1, "One item updated");
equal(updatedItems[0].method, SessionDataMethod.Remove, "One item removed");
updatedValues = updatedItems[0].values;
equal(updatedValues.length, 1, "One value removed");
equal(updatedValues[0], "first.event", "Expected value was removed");
checkEvents(sessionData.getSessionData("mod", "event"), [
{
value: "first.event",
contextDescriptor: otherContext,
},
]);
info("Remove the other context from the first event");
updatedItems = sessionData.applySessionData([
createUpdate(SessionDataMethod.Remove, otherContext, ["first.event"]),
]);
equal(updatedItems.length, 1, "One item updated");
equal(updatedItems[0].method, SessionDataMethod.Remove, "One item removed");
updatedValues = updatedItems[0].values;
equal(updatedValues.length, 1, "One value removed");
equal(updatedValues[0], "first.event", "Expected value was removed");
checkEvents(sessionData.getSessionData("mod", "event"), []);
info("Add two events for different contexts");
updatedItems = sessionData.applySessionData([
createUpdate(SessionDataMethod.Add, otherContext, ["first.event"]),
createUpdate(SessionDataMethod.Add, globalContext, ["second.event"]),
]);
equal(updatedItems.length, 2, "Two items updated");
equal(updatedItems[0].method, SessionDataMethod.Add, "First item added");
updatedValues = updatedItems[0].values;
equal(updatedValues.length, 1, "One value for first item added");
equal(updatedValues[0], "first.event", "Expected value first item was added");
equal(updatedItems[1].method, SessionDataMethod.Add, "Second item added");
updatedValues = updatedItems[1].values;
equal(updatedValues.length, 1, "One value for second item added");
equal(
updatedValues[0],
"second.event",
"Expected value second item was added"
);
checkEvents(sessionData.getSessionData("mod", "event"), [
{
value: "first.event",
contextDescriptor: otherContext,
},
{
value: "second.event",
contextDescriptor: globalContext,
},
]);
info("Remove two events for different contexts");
updatedItems = sessionData.applySessionData([
createUpdate(SessionDataMethod.Remove, otherContext, ["first.event"]),
createUpdate(SessionDataMethod.Remove, globalContext, ["second.event"]),
]);
equal(updatedItems.length, 2, "Two items updated");
equal(updatedItems[0].method, SessionDataMethod.Remove, "First item removed");
updatedValues = updatedItems[0].values;
equal(updatedValues.length, 1, "One value for first item removed");
equal(
updatedValues[0],
"first.event",
"Expected value first item was removed"
);
equal(
updatedItems[1].method,
SessionDataMethod.Remove,
"Second item removed"
);
updatedValues = updatedItems[1].values;
equal(updatedValues.length, 1, "One value for second item removed");
equal(
updatedValues[0],
"second.event",
"Expected value second item was removed"
);
checkEvents(sessionData.getSessionData("mod", "event"), []);
info("Add and remove event in different order");
updatedItems = sessionData.applySessionData([
createUpdate(SessionDataMethod.Remove, otherContext, ["first.event"]),
createUpdate(SessionDataMethod.Add, otherContext, ["first.event"]),
]);
equal(updatedItems.length, 1, "One item updated");
equal(updatedItems[0].method, SessionDataMethod.Add, "One item added");
updatedValues = updatedItems[0].values;
equal(updatedValues.length, 1, "One value added");
equal(updatedValues[0], "first.event", "Expected value was added");
checkEvents(sessionData.getSessionData("mod", "event"), [
{
value: "first.event",
contextDescriptor: otherContext,
},
]);
updatedItems = sessionData.applySessionData([
createUpdate(SessionDataMethod.Add, otherContext, ["first.event"]),
createUpdate(SessionDataMethod.Remove, otherContext, ["first.event"]),
]);
equal(updatedItems.length, 1, "No item update");
equal(updatedItems[0].method, SessionDataMethod.Remove, "One item removed");
updatedValues = updatedItems[0].values;
equal(updatedValues.length, 1, "One value removed");
equal(updatedValues[0], "first.event", "Expected value was removed");
checkEvents(sessionData.getSessionData("mod", "event"), []);
});
function checkEvents(events, expectedEvents) {
// Check the arrays have the same size.
equal(events.length, expectedEvents.length);
// Check all the expectedEvents can be found in the events array.
for (const expected of expectedEvents) {
ok(
events.some(
event =>
expected.contextDescriptor.type === event.contextDescriptor.type &&
expected.contextDescriptor.id === event.contextDescriptor.id &&
expected.value == event.value
)
);
}
}
function createUpdate(method, contextDescriptor, values) {
return {
method,
moduleName: "mod",
category: "event",
contextDescriptor,
values,
};
}
|