michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: 'use strict'; michael@0: michael@0: const { Rules } = require('sdk/util/rules'); michael@0: const { on, off, emit } = require('sdk/event/core'); michael@0: michael@0: exports.testAdd = function (test, done) { michael@0: let rules = Rules(); michael@0: let urls = [ michael@0: 'http://www.firefox.com', michael@0: '*.mozilla.org', michael@0: '*.html5audio.org' michael@0: ]; michael@0: let count = 0; michael@0: on(rules, 'add', function (rule) { michael@0: if (count < urls.length) { michael@0: test.ok(rules.get(rule), 'rule added to internal registry'); michael@0: test.equal(rule, urls[count], 'add event fired with proper params'); michael@0: if (++count < urls.length) rules.add(urls[count]); michael@0: else done(); michael@0: } michael@0: }); michael@0: rules.add(urls[0]); michael@0: }; michael@0: michael@0: exports.testRemove = function (test, done) { michael@0: let rules = Rules(); michael@0: let urls = [ michael@0: 'http://www.firefox.com', michael@0: '*.mozilla.org', michael@0: '*.html5audio.org' michael@0: ]; michael@0: let count = 0; michael@0: on(rules, 'remove', function (rule) { michael@0: if (count < urls.length) { michael@0: test.ok(!rules.get(rule), 'rule removed to internal registry'); michael@0: test.equal(rule, urls[count], 'remove event fired with proper params'); michael@0: if (++count < urls.length) rules.remove(urls[count]); michael@0: else done(); michael@0: } michael@0: }); michael@0: urls.forEach(function (url) rules.add(url)); michael@0: rules.remove(urls[0]); michael@0: }; michael@0: michael@0: exports.testMatchesAny = function(test) { michael@0: let rules = Rules(); michael@0: rules.add('*.mozilla.org'); michael@0: rules.add('data:*'); michael@0: matchTest('http://mozilla.org', true); michael@0: matchTest('http://www.mozilla.org', true); michael@0: matchTest('http://www.google.com', false); michael@0: matchTest('data:text/html;charset=utf-8,', true); michael@0: michael@0: function matchTest(string, expected) { michael@0: test.equal(rules.matchesAny(string), expected, michael@0: 'Expected to find ' + string + ' in rules'); michael@0: } michael@0: }; michael@0: michael@0: exports.testIterable = function(test) { michael@0: let rules = Rules(); michael@0: rules.add('*.mozilla.org'); michael@0: rules.add('data:*'); michael@0: rules.add('http://google.com'); michael@0: rules.add('http://addons.mozilla.org'); michael@0: rules.remove('http://google.com'); michael@0: michael@0: test.equal(rules.length, 3, 'has correct length of keys'); michael@0: Array.forEach(rules, function (rule, i) { michael@0: test.equal(rule, ['*.mozilla.org', 'data:*', 'http://addons.mozilla.org'][i]); michael@0: }); michael@0: for (let i in rules) michael@0: test.equal(rules[i], ['*.mozilla.org', 'data:*', 'http://addons.mozilla.org'][i]); michael@0: }; michael@0: michael@0: require('test').run(exports);