1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/test/test-rules.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,79 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 +'use strict'; 1.8 + 1.9 +const { Rules } = require('sdk/util/rules'); 1.10 +const { on, off, emit } = require('sdk/event/core'); 1.11 + 1.12 +exports.testAdd = function (test, done) { 1.13 + let rules = Rules(); 1.14 + let urls = [ 1.15 + 'http://www.firefox.com', 1.16 + '*.mozilla.org', 1.17 + '*.html5audio.org' 1.18 + ]; 1.19 + let count = 0; 1.20 + on(rules, 'add', function (rule) { 1.21 + if (count < urls.length) { 1.22 + test.ok(rules.get(rule), 'rule added to internal registry'); 1.23 + test.equal(rule, urls[count], 'add event fired with proper params'); 1.24 + if (++count < urls.length) rules.add(urls[count]); 1.25 + else done(); 1.26 + } 1.27 + }); 1.28 + rules.add(urls[0]); 1.29 +}; 1.30 + 1.31 +exports.testRemove = function (test, done) { 1.32 + let rules = Rules(); 1.33 + let urls = [ 1.34 + 'http://www.firefox.com', 1.35 + '*.mozilla.org', 1.36 + '*.html5audio.org' 1.37 + ]; 1.38 + let count = 0; 1.39 + on(rules, 'remove', function (rule) { 1.40 + if (count < urls.length) { 1.41 + test.ok(!rules.get(rule), 'rule removed to internal registry'); 1.42 + test.equal(rule, urls[count], 'remove event fired with proper params'); 1.43 + if (++count < urls.length) rules.remove(urls[count]); 1.44 + else done(); 1.45 + } 1.46 + }); 1.47 + urls.forEach(function (url) rules.add(url)); 1.48 + rules.remove(urls[0]); 1.49 +}; 1.50 + 1.51 +exports.testMatchesAny = function(test) { 1.52 + let rules = Rules(); 1.53 + rules.add('*.mozilla.org'); 1.54 + rules.add('data:*'); 1.55 + matchTest('http://mozilla.org', true); 1.56 + matchTest('http://www.mozilla.org', true); 1.57 + matchTest('http://www.google.com', false); 1.58 + matchTest('data:text/html;charset=utf-8,', true); 1.59 + 1.60 + function matchTest(string, expected) { 1.61 + test.equal(rules.matchesAny(string), expected, 1.62 + 'Expected to find ' + string + ' in rules'); 1.63 + } 1.64 +}; 1.65 + 1.66 +exports.testIterable = function(test) { 1.67 + let rules = Rules(); 1.68 + rules.add('*.mozilla.org'); 1.69 + rules.add('data:*'); 1.70 + rules.add('http://google.com'); 1.71 + rules.add('http://addons.mozilla.org'); 1.72 + rules.remove('http://google.com'); 1.73 + 1.74 + test.equal(rules.length, 3, 'has correct length of keys'); 1.75 + Array.forEach(rules, function (rule, i) { 1.76 + test.equal(rule, ['*.mozilla.org', 'data:*', 'http://addons.mozilla.org'][i]); 1.77 + }); 1.78 + for (let i in rules) 1.79 + test.equal(rules[i], ['*.mozilla.org', 'data:*', 'http://addons.mozilla.org'][i]); 1.80 +}; 1.81 + 1.82 +require('test').run(exports);