Skip to content

Commit 9be7b70

Browse files
committed
Merge branch 'webextensions-add-unit-tests' into 'master'
Add unit tests for WebExtensions add-ons See merge request clear-code/browserselector!72
2 parents e64d416 + a1361ea commit 9be7b70

11 files changed

Lines changed: 184 additions & 3 deletions

File tree

.dir-locals.el

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
((js-mode . ((indent-tabs-mode . nil)
2+
(js-indent-level . 2)
3+
(js-switch-indent-offset . 2))))

.github/workflows/build.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ jobs:
4545
cp webextensions/chrome/manifest.json.dev webextensions/chrome/manifest.json
4646
make -C webextensions/chrome
4747
make -C webextensions/firefox
48+
- name: Run tests
49+
run: |
50+
make -C webextensions test
4851
- name: Upload Extensions
4952
uses: actions/upload-artifact@v4
5053
with:
@@ -59,6 +62,9 @@ jobs:
5962
make -C webextensions/edge
6063
make -C webextensions/chrome
6164
make -C webextensions/firefox
65+
- name: Run tests
66+
run: |
67+
make -C webextensions test
6268
- name: Upload Extensions
6369
uses: actions/upload-artifact@v4
6470
with:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ ipch/
1010
*_i.[ch]
1111
*_p.[ch]
1212
*.zip
13+
webextensions/testee

webextensions/.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55
tools/
66
*/dev/
77
*/enterprise-dev/
8+
testee/
9+
test/

webextensions/Makefile

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,29 @@ NPM_BIN_DIR := $(NPM_MOD_DIR)/.bin
55

66
all: packages
77

8+
testee:
9+
mkdir $@
10+
11+
testee/chrome.js: chrome/background.js testee
12+
sed -e "s/Redirector.init();/exports.redirector = Redirector/g" chrome/background.js > $@
13+
14+
testee/edge.js: edge/background.js testee
15+
sed -e "s/Redirector.init();/exports.redirector = Redirector/g" edge/background.js > $@
16+
17+
unittest: install_dependency testee/chrome.js testee/edge.js
18+
npx mocha --require mocha-suppress-logs test
19+
20+
test: unittest
21+
22+
test-verbose: install_dependency testee/chrome.js testee/edge.js
23+
npx mocha test
24+
825
clean:
926
rm -f *.zip
1027
rm -f *.xpi
28+
rm -rf testee
1129

12-
packages: clean lint
30+
packages: test clean lint
1331
cd chrome && make && make dev && make enterprise-dev && mv *.zip ../
1432
cd edge && make && make dev && make enterprise-dev && mv *.zip ../
1533
cd firefox && make && mv *.xpi ../

webextensions/chrome/background.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ const Redirector = {
322322
}
323323
}
324324

325-
for (const [pattern, browser] of Object.entries(config.HostNamePatterns)) {
325+
for (const [pattern, browser] of config.HostNamePatterns) {
326326
if (wildmat(host, pattern)) {
327327
console.log(`* Match with '${pattern}' (browser=${browser})`);
328328
return browser.toLowerCase();

webextensions/edge/background.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ const Redirector = {
322322
}
323323
}
324324

325-
for (const [pattern, browser] of Object.entries(config.HostNamePatterns)) {
325+
for (const [pattern, browser] of config.HostNamePatterns) {
326326
if (wildmat(host, pattern)) {
327327
console.log(`* Match with '${pattern}' (browser=${browser})`);
328328
return browser.toLowerCase();

webextensions/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
"jsonlint-cli": "*",
2121
"lodash": ">=4.17.21",
2222
"minimist": ">=1.2.2",
23+
"mocha": ">=10.4.0",
24+
"mocha-suppress-logs": "^0.5.1",
2325
"morphdom": "^2.5.12",
2426
"node-fetch": ">=2.6.1",
27+
"sinon": "^14.0.0",
2528
"trim-newlines": ">=3.0.1",
2629
"tunnel-agent": ">=0.6.0",
2730
"underscore": ">=1.12.1"

webextensions/test/chrome-stub.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
global.chrome = {
2+
alarms: {
3+
create: function() {
4+
},
5+
onAlarm: {
6+
addListener: function() {
7+
}
8+
},
9+
},
10+
runtime: {
11+
sendNativeMessage: function() {
12+
},
13+
},
14+
tabs: {
15+
onCreated: {
16+
addListener: function() {
17+
}
18+
},
19+
onRemoved: {
20+
addListener: function() {
21+
}
22+
},
23+
onUpdated: {
24+
addListener: function() {
25+
}
26+
},
27+
},
28+
windows: {
29+
onCreated: {
30+
addListener: function() {
31+
}
32+
},
33+
onRemoved: {
34+
addListener: function() {
35+
}
36+
},
37+
},
38+
webRequest: {
39+
onBeforeRequest: {
40+
addListener: function() {
41+
},
42+
},
43+
},
44+
webNavigation: {
45+
onCommitted: {
46+
addListener: function() {
47+
}
48+
},
49+
},
50+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const assert = require('assert');
2+
const chromestub = require('./chrome-stub.js');
3+
4+
describe('getHost', () => {
5+
const redirectors = [
6+
{ browser: 'chrome', module: require('../testee/chrome.js') },
7+
{ browser: 'edge', module: require('../testee/edge.js') },
8+
].forEach(({browser, module}) => {
9+
describe(browser, () => {
10+
const redirector = module.redirector;
11+
it('http: extract host name', () => {
12+
const url = 'http://www.google.com/';
13+
assert.equal(redirector._getHost(url), 'www.google.com');
14+
});
15+
it('https: extract host name', () => {
16+
const url = 'https://www.google.com/';
17+
assert.equal(redirector._getHost(url), 'www.google.com');
18+
});
19+
it('exclude account', () => {
20+
const url = 'https://foobar:password@www.google.com/';
21+
assert.equal(redirector._getHost(url), 'www.google.com');
22+
});
23+
// TODO:
24+
// Although C++ implementation intends to exclude port number, JavaScript
25+
// one includes it. So that this test always fails with current code.
26+
// We may need to fix it.
27+
it('exclude port number' /*, () => {
28+
const url = 'https://www.google.com:8080/';
29+
assert.equal(redirector._getHost(url), 'www.google.com');
30+
}*/);
31+
});
32+
});
33+
});

0 commit comments

Comments
 (0)