-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathopenURLsTests.js
More file actions
248 lines (211 loc) · 9.44 KB
/
openURLsTests.js
File metadata and controls
248 lines (211 loc) · 9.44 KB
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
/*
* Copyright The Morphic: Open in Same Tab copyright holders
* See the AUTHORS.md file at the top-level directory of this distribution and at
* https://github.com/GPII/gpii-open-same-tab-extension/blob/master/AUTHORS.md
*
* Licensed under the New BSD license. You may not use this file except in
* compliance with this license.
*
* You may obtain a copy of the license at
* https://github.com/GPII/gpii-open-same-tab-extension/blob/master/LICENSE.txt
*/
/* global jqUnit, openURLs, browser, sinon */
"use strict";
(function () {
// Due to a https://github.com/acvetkov/sinon-chrome/issues/100 sinon-chrome's webextension stubs
// are in the `chrome` namespace instead of `browser`. The following maps the `chrome` namespaced stubs
// to `browser`
window.browser = window.chrome;
/************************************************************************************************************
* openURLS.getFilter tests
************************************************************************************************************/
const getFilterTestCases = [{
name: "Simple identifier",
identifiers: {
foo: "bar",
abc: "123"
},
filter: {
urls: [
"*://bar/*",
"*://123/*"
]
}
}, {
name: "Subdomain identifier",
identifiers: {
domain: "sub.domain.com"
},
filter: {
urls: ["*://sub.domain.com/*"]
}
}, {
name: "No identifier",
identifiers: {},
filter: {
urls: []
}
}];
jqUnit.test("test openURLs.getFilter()", () => {
jqUnit.expect(getFilterTestCases.length);
getFilterTestCases.forEach(testCase => {
var actualFilter = openURLs.getFilter(testCase.identifiers);
jqUnit.assertDeepEq(`${testCase.name}: the filter is generated correctly`, testCase.filter, actualFilter);
});
});
/************************************************************************************************************
* openURLs.openURL tests
************************************************************************************************************/
const openTabTestsProps = {
testCases: [{
name: "Open Existing",
existing: true
}, {
name: "Refresh Existing",
existing: true,
refresh: true
}, {
name: "Open New"
}, {
name: "Open New (with refresh arg)",
refresh: true
}, {
name: "Open New - no loading tab",
noLoading: true
}, {
name: "Open New (with refresh arg) - no loading tab",
refresh: true,
noLoading: true
}],
matchingTab: {
windowId: 88,
index: 2,
id: 23
},
loadingTab: {
id: 24
},
blankTab: {
id: 1
},
url: new URL("https://actual.org/url"),
queryURL: "*://actual.org/url"
};
openTabTestsProps.testCases.forEach(testCase => {
jqUnit.asyncTest(`test openURLs.openTab - ${testCase.name}`, async () => {
browser.tabs.query.onFirstCall().resolves(testCase.existing ? [openTabTestsProps.matchingTab] : []);
browser.tabs.query.onSecondCall().resolves(testCase.noLoading ? [] : [openTabTestsProps.loadingTab]);
browser.tabs.query.onThirdCall().resolves([openTabTestsProps.blankTab]);
let loadingTab = testCase.noLoading ? openTabTestsProps.blankTab : openTabTestsProps.loadingTab;
await openURLs.openTab(openTabTestsProps.url, testCase.refresh);
jqUnit.assertTrue("Tabs queried for matching URLs", browser.tabs.query.calledWith({url: openTabTestsProps.queryURL}));
jqUnit.assertTrue("Tabs queried for loading URLs", browser.tabs.query.calledWith({"status": "loading", "windowId": browser.windows.WINDOW_ID_CURRENT}));
if (testCase.existing) {
jqUnit.assertTrue("Window for matching tab focused", browser.windows.update.calledOnceWithExactly(openTabTestsProps.matchingTab.windowId, {focused: true}));
jqUnit.assertTrue("Matching tab highlighted", browser.tabs.highlight.calledOnceWithExactly({
windowId: openTabTestsProps.matchingTab.windowId,
tabs: openTabTestsProps.matchingTab.index
}));
jqUnit.assertTrue("Removed loading tab", browser.tabs.remove.calledOnceWithExactly(loadingTab.id));
jqUnit.assertTrue("Tab isn't updated", browser.tabs.update.notCalled);
if (testCase.refresh) {
jqUnit.assertTrue("Matching tab is refreshed", browser.tabs.reload.calledOnceWithExactly(openTabTestsProps.matchingTab.id));
} else {
jqUnit.assertTrue("Tab isn't reloaded", browser.tabs.reload.notCalled);
}
} else {
jqUnit.assertTrue("Window isn't explicitly focused", browser.windows.update.notCalled);
jqUnit.assertTrue("Tab isn't highlighted", browser.tabs.highlight.notCalled);
jqUnit.assertTrue("Loading tab isnt' removed", browser.tabs.remove.notCalled);
jqUnit.assertTrue("Tab isn't reloaded", browser.tabs.reload.notCalled);
let isUpdatedCalled = browser.tabs.update.calledOnceWithExactly(loadingTab.id, {url: openTabTestsProps.url.href});
jqUnit.assertTrue("Loading tab is updated with correct URL", isUpdatedCalled);
}
// teardown
browser.flush();
jqUnit.start();
});
});
/************************************************************************************************************
* openURLs.handleRequest tests
************************************************************************************************************/
const handlestRequestTestCases = [{
name: "Open same tab",
details: {
url: "https://opensametab.morphic.org/redirect/https%3A%2F%2Factual.org/url"
},
expected: {
response: {cancel: true},
args: [new URL("https://actual.org/url"), false]
}
}, {
name: "Refresh tab",
details: {
url: "http://refreshsametab.morphic.org/redirect/http%3A%2F%2Factual.org/url"
},
expected: {
response: {cancel: true},
args: [new URL("http://actual.org/url"), true]
}
}, {
name: "Unfiltered URL",
details: {
url: "http://morphic.org/redirect/https%3A%2F%2Factual.org/url"
},
expected: {
response: {cancel: true},
args: [new URL("http://morphic.org/redirect/https%3A%2F%2Factual.org/url"), false]
}
}, {
name: "Bad URL",
details: {
url: "http://morphic.org/redirect/stupid"
},
expected: {
response: {cancel: false},
args: null
}
}];
jqUnit.test("test openURLs.handleRequest", () => {
let openTabStub = sinon.stub(openURLs, "openTab");
handlestRequestTestCases.forEach(testCase => {
let response = openURLs.handleRequest(testCase.details);
jqUnit.assertDeepEq(`${testCase.name}: the response is returned correctly`, testCase.expected.response, response);
let isOpenTabCalledProperly = !testCase.expected.args || openTabStub.calledOnceWithExactly.apply(openTabStub, testCase.expected.args);
jqUnit.assertTrue(`${testCase.name}: the openURLs.openTab method was called with the correct args`, isOpenTabCalledProperly);
// clean up
openTabStub.reset();
});
// clean up
openTabStub.restore();
});
/************************************************************************************************************
* openURLs.bindListener tests
************************************************************************************************************/
jqUnit.test("test openURLs.bindListener", () => {
const filter = {
urls: [
"*://opensametab.morphic.org/*",
"*://refreshsametab.morphic.org/*"
]
};
const opts = ["blocking"];
openURLs.bindListener();
let isCalledProperly = browser.webRequest.onBeforeRequest.addListener.calledOnceWithExactly(openURLs.handleRequest, filter, opts);
jqUnit.assertTrue("The browser.webRequest.onBeforeRequest.addListener method was called with the correct args", isCalledProperly);
// cleanup
browser.flush();
});
jqUnit.test("trigger onBeforeRequest event", () => {
let handelRequestStub = sinon.stub(openURLs, "handleRequest");
const details = {test: "test"};
browser.webRequest.onBeforeRequest.dispatch(details);
jqUnit.assertFalse("The handleRequest method should not have been triggered before the onBeforeRequest listener is bound", handelRequestStub.called);
openURLs.bindListener();
browser.webRequest.onBeforeRequest.dispatch(details);
jqUnit.assertTrue("The onBeforeRequest handler should be called once, after triggering the onBeforeRequest event", handelRequestStub.calledOnceWithExactly(details));
// cleanup
browser.flush();
handelRequestStub.restore();
});
})();