|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 'use strict'; |
|
5 |
|
6 const { Rules } = require('sdk/util/rules'); |
|
7 const { on, off, emit } = require('sdk/event/core'); |
|
8 |
|
9 exports.testAdd = function (test, done) { |
|
10 let rules = Rules(); |
|
11 let urls = [ |
|
12 'http://www.firefox.com', |
|
13 '*.mozilla.org', |
|
14 '*.html5audio.org' |
|
15 ]; |
|
16 let count = 0; |
|
17 on(rules, 'add', function (rule) { |
|
18 if (count < urls.length) { |
|
19 test.ok(rules.get(rule), 'rule added to internal registry'); |
|
20 test.equal(rule, urls[count], 'add event fired with proper params'); |
|
21 if (++count < urls.length) rules.add(urls[count]); |
|
22 else done(); |
|
23 } |
|
24 }); |
|
25 rules.add(urls[0]); |
|
26 }; |
|
27 |
|
28 exports.testRemove = function (test, done) { |
|
29 let rules = Rules(); |
|
30 let urls = [ |
|
31 'http://www.firefox.com', |
|
32 '*.mozilla.org', |
|
33 '*.html5audio.org' |
|
34 ]; |
|
35 let count = 0; |
|
36 on(rules, 'remove', function (rule) { |
|
37 if (count < urls.length) { |
|
38 test.ok(!rules.get(rule), 'rule removed to internal registry'); |
|
39 test.equal(rule, urls[count], 'remove event fired with proper params'); |
|
40 if (++count < urls.length) rules.remove(urls[count]); |
|
41 else done(); |
|
42 } |
|
43 }); |
|
44 urls.forEach(function (url) rules.add(url)); |
|
45 rules.remove(urls[0]); |
|
46 }; |
|
47 |
|
48 exports.testMatchesAny = function(test) { |
|
49 let rules = Rules(); |
|
50 rules.add('*.mozilla.org'); |
|
51 rules.add('data:*'); |
|
52 matchTest('http://mozilla.org', true); |
|
53 matchTest('http://www.mozilla.org', true); |
|
54 matchTest('http://www.google.com', false); |
|
55 matchTest('data:text/html;charset=utf-8,', true); |
|
56 |
|
57 function matchTest(string, expected) { |
|
58 test.equal(rules.matchesAny(string), expected, |
|
59 'Expected to find ' + string + ' in rules'); |
|
60 } |
|
61 }; |
|
62 |
|
63 exports.testIterable = function(test) { |
|
64 let rules = Rules(); |
|
65 rules.add('*.mozilla.org'); |
|
66 rules.add('data:*'); |
|
67 rules.add('http://google.com'); |
|
68 rules.add('http://addons.mozilla.org'); |
|
69 rules.remove('http://google.com'); |
|
70 |
|
71 test.equal(rules.length, 3, 'has correct length of keys'); |
|
72 Array.forEach(rules, function (rule, i) { |
|
73 test.equal(rule, ['*.mozilla.org', 'data:*', 'http://addons.mozilla.org'][i]); |
|
74 }); |
|
75 for (let i in rules) |
|
76 test.equal(rules[i], ['*.mozilla.org', 'data:*', 'http://addons.mozilla.org'][i]); |
|
77 }; |
|
78 |
|
79 require('test').run(exports); |