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