test-rules.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. const { Rules } = require('sdk/util/rules');
  6. const { on, off, emit } = require('sdk/event/core');
  7. exports.testAdd = function (test, done) {
  8. let rules = Rules();
  9. let urls = [
  10. 'http://www.firefox.com',
  11. '*.mozilla.org',
  12. '*.html5audio.org'
  13. ];
  14. let count = 0;
  15. on(rules, 'add', function (rule) {
  16. if (count < urls.length) {
  17. test.ok(rules.get(rule), 'rule added to internal registry');
  18. test.equal(rule, urls[count], 'add event fired with proper params');
  19. if (++count < urls.length) rules.add(urls[count]);
  20. else done();
  21. }
  22. });
  23. rules.add(urls[0]);
  24. };
  25. exports.testRemove = function (test, done) {
  26. let rules = Rules();
  27. let urls = [
  28. 'http://www.firefox.com',
  29. '*.mozilla.org',
  30. '*.html5audio.org'
  31. ];
  32. let count = 0;
  33. on(rules, 'remove', function (rule) {
  34. if (count < urls.length) {
  35. test.ok(!rules.get(rule), 'rule removed to internal registry');
  36. test.equal(rule, urls[count], 'remove event fired with proper params');
  37. if (++count < urls.length) rules.remove(urls[count]);
  38. else done();
  39. }
  40. });
  41. urls.forEach(function (url) rules.add(url));
  42. rules.remove(urls[0]);
  43. };
  44. exports.testMatchesAny = function(test) {
  45. let rules = Rules();
  46. rules.add('*.mozilla.org');
  47. rules.add('data:*');
  48. matchTest('http://mozilla.org', true);
  49. matchTest('http://www.mozilla.org', true);
  50. matchTest('http://www.google.com', false);
  51. matchTest('data:text/html;charset=utf-8,', true);
  52. function matchTest(string, expected) {
  53. test.equal(rules.matchesAny(string), expected,
  54. 'Expected to find ' + string + ' in rules');
  55. }
  56. };
  57. exports.testIterable = function(test) {
  58. let rules = Rules();
  59. rules.add('*.mozilla.org');
  60. rules.add('data:*');
  61. rules.add('http://google.com');
  62. rules.add('http://addons.mozilla.org');
  63. rules.remove('http://google.com');
  64. test.equal(rules.length, 3, 'has correct length of keys');
  65. Array.forEach(rules, function (rule, i) {
  66. test.equal(rule, ['*.mozilla.org', 'data:*', 'http://addons.mozilla.org'][i]);
  67. });
  68. for (let i in rules)
  69. test.equal(rules[i], ['*.mozilla.org', 'data:*', 'http://addons.mozilla.org'][i]);
  70. };
  71. require('test').run(exports);