Skip to content

Commit

Permalink
Merge branch 'webextensions-add-unit-tests' into 'master'
Browse files Browse the repository at this point in the history
Add unit tests for WebExtensions add-ons

See merge request clear-code/browserselector!72
  • Loading branch information
piroor committed Sep 20, 2024
2 parents e64d416 + a1361ea commit 9be7b70
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .dir-locals.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
((js-mode . ((indent-tabs-mode . nil)
(js-indent-level . 2)
(js-switch-indent-offset . 2))))
6 changes: 6 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ jobs:
cp webextensions/chrome/manifest.json.dev webextensions/chrome/manifest.json
make -C webextensions/chrome
make -C webextensions/firefox
- name: Run tests
run: |
make -C webextensions test
- name: Upload Extensions
uses: actions/upload-artifact@v4
with:
Expand All @@ -59,6 +62,9 @@ jobs:
make -C webextensions/edge
make -C webextensions/chrome
make -C webextensions/firefox
- name: Run tests
run: |
make -C webextensions test
- name: Upload Extensions
uses: actions/upload-artifact@v4
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ ipch/
*_i.[ch]
*_p.[ch]
*.zip
webextensions/testee
2 changes: 2 additions & 0 deletions webextensions/.eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
tools/
*/dev/
*/enterprise-dev/
testee/
test/
20 changes: 19 additions & 1 deletion webextensions/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,29 @@ NPM_BIN_DIR := $(NPM_MOD_DIR)/.bin

all: packages

testee:
mkdir $@

testee/chrome.js: chrome/background.js testee
sed -e "s/Redirector.init();/exports.redirector = Redirector/g" chrome/background.js > $@

testee/edge.js: edge/background.js testee
sed -e "s/Redirector.init();/exports.redirector = Redirector/g" edge/background.js > $@

unittest: install_dependency testee/chrome.js testee/edge.js
npx mocha --require mocha-suppress-logs test

test: unittest

test-verbose: install_dependency testee/chrome.js testee/edge.js
npx mocha test

clean:
rm -f *.zip
rm -f *.xpi
rm -rf testee

packages: clean lint
packages: test clean lint
cd chrome && make && make dev && make enterprise-dev && mv *.zip ../
cd edge && make && make dev && make enterprise-dev && mv *.zip ../
cd firefox && make && mv *.xpi ../
Expand Down
2 changes: 1 addition & 1 deletion webextensions/chrome/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ const Redirector = {
}
}

for (const [pattern, browser] of Object.entries(config.HostNamePatterns)) {
for (const [pattern, browser] of config.HostNamePatterns) {
if (wildmat(host, pattern)) {
console.log(`* Match with '${pattern}' (browser=${browser})`);
return browser.toLowerCase();
Expand Down
2 changes: 1 addition & 1 deletion webextensions/edge/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ const Redirector = {
}
}

for (const [pattern, browser] of Object.entries(config.HostNamePatterns)) {
for (const [pattern, browser] of config.HostNamePatterns) {
if (wildmat(host, pattern)) {
console.log(`* Match with '${pattern}' (browser=${browser})`);
return browser.toLowerCase();
Expand Down
3 changes: 3 additions & 0 deletions webextensions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
"jsonlint-cli": "*",
"lodash": ">=4.17.21",
"minimist": ">=1.2.2",
"mocha": ">=10.4.0",
"mocha-suppress-logs": "^0.5.1",
"morphdom": "^2.5.12",
"node-fetch": ">=2.6.1",
"sinon": "^14.0.0",
"trim-newlines": ">=3.0.1",
"tunnel-agent": ">=0.6.0",
"underscore": ">=1.12.1"
Expand Down
50 changes: 50 additions & 0 deletions webextensions/test/chrome-stub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
global.chrome = {
alarms: {
create: function() {
},
onAlarm: {
addListener: function() {
}
},
},
runtime: {
sendNativeMessage: function() {
},
},
tabs: {
onCreated: {
addListener: function() {
}
},
onRemoved: {
addListener: function() {
}
},
onUpdated: {
addListener: function() {
}
},
},
windows: {
onCreated: {
addListener: function() {
}
},
onRemoved: {
addListener: function() {
}
},
},
webRequest: {
onBeforeRequest: {
addListener: function() {
},
},
},
webNavigation: {
onCommitted: {
addListener: function() {
}
},
},
};
33 changes: 33 additions & 0 deletions webextensions/test/test-get-host.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const assert = require('assert');
const chromestub = require('./chrome-stub.js');

describe('getHost', () => {
const redirectors = [
{ browser: 'chrome', module: require('../testee/chrome.js') },
{ browser: 'edge', module: require('../testee/edge.js') },
].forEach(({browser, module}) => {
describe(browser, () => {
const redirector = module.redirector;
it('http: extract host name', () => {
const url = 'http://www.google.com/';
assert.equal(redirector._getHost(url), 'www.google.com');
});
it('https: extract host name', () => {
const url = 'https://www.google.com/';
assert.equal(redirector._getHost(url), 'www.google.com');
});
it('exclude account', () => {
const url = 'https://foobar:[email protected]/';
assert.equal(redirector._getHost(url), 'www.google.com');
});
// TODO:
// Although C++ implementation intends to exclude port number, JavaScript
// one includes it. So that this test always fails with current code.
// We may need to fix it.
it('exclude port number' /*, () => {
const url = 'https://www.google.com:8080/';
assert.equal(redirector._getHost(url), 'www.google.com');
}*/);
});
});
});
65 changes: 65 additions & 0 deletions webextensions/test/test-is-redirect-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const assert = require('assert');
const chromestub = require('./chrome-stub.js');

describe('isRedirectURL', () => {
const redirectors = [
{ browser: 'chrome', module: require('../testee/chrome.js') },
{ browser: 'edge', module: require('../testee/edge.js') },
].forEach(({browser, module}) => {
const redirector = module.redirector;
describe(browser, () => {
const baseConfig = {
DefaultBrowser: browser,
SecondBrowser: "",
FirefoxCommannd: "",
CloseEmptyTab: 1,
OnlyOnAnchorClick: 0,
UseRegex: 0,
URLPatterns: [],
HostNamePatterns: [],
ZonePatterns: [],
}
function config(URLPatterns = [], HostNamePatterns = [], additionals = {}) {
const config = {...baseConfig, ...additionals};
config.URLPatterns = [...config.URLPatterns, ...URLPatterns];
config.HostNamePatterns = [...config.HostNamePatterns, ...HostNamePatterns];
return config;
}
describe('Empty redirect pattern', () => {
it(`Should not redirect when default browser is ${browser}`, () => {
const url = 'http://www.google.com/';
assert.equal(redirector.isRedirectURL(baseConfig, url), false);
});
it(`Should redirect when default browser is not ${browser}`, () => {
const defaultBrowser = browser === 'edge' ? 'chrome' : 'edge';
const url = 'http://www.google.com/';
assert.equal(redirector.isRedirectURL(config([], [], {DefaultBrowser: defaultBrowser}), url), true);
});
});
describe('URL patterns', () => {
it(`Match redirect pattern`, () => {
const url = 'http://www.example.com/';
const conf = config([['http*://*.example.com/*', 'firefox']])
assert.equal(redirector.isRedirectURL(conf, url), true);
});
it(`Unmatch redirect pattern`, () => {
const url = 'http://www.google.com/';
const conf = config([['http*://*.example.com/*', 'firefox']])
assert.equal(redirector.isRedirectURL(conf, url), false);
});
});
describe('HostName patterns', () => {
it(`Match redirect pattern`, () => {
const url = 'http://www.example.com/';
const conf = config([], [['*.example.com', 'firefox']])
assert.equal(redirector.isRedirectURL(conf, url), true);
});
it(`Unmatch redirect pattern`, () => {
const url = 'http://www.google.com/';
const conf = config([], [['*.example.com', 'firefox']])
assert.equal(redirector.isRedirectURL(conf, url), false);
});
});
});
});
});

0 comments on commit 9be7b70

Please sign in to comment.