| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 var declarative = chrome.experimental.declarative; |
| 6 |
| 7 function createTestCondition(opt_testParameter) { |
| 8 var result = { |
| 9 "instanceType": "experimental.declarative.TestCondition" |
| 10 }; |
| 11 if (opt_testParameter) { |
| 12 result["testParameter"] = opt_testParameter; |
| 13 } |
| 14 return result; |
| 15 } |
| 16 |
| 17 function createTestAction(opt_testParameter) { |
| 18 var result = { |
| 19 "instanceType": "experimental.declarative.TestAction" |
| 20 }; |
| 21 if (opt_testParameter) { |
| 22 result["testParameter"] = opt_testParameter; |
| 23 } |
| 24 return result; |
| 25 } |
| 26 |
| 27 var inputRule0 = { |
| 28 // No 'id', this should be filled by the API |
| 29 "conditions": [createTestCondition("test1"), createTestCondition("test2")], |
| 30 "actions": [createTestAction("action1"), createTestAction("action2")] |
| 31 // No 'priority', this should be filled by the API |
| 32 } |
| 33 |
| 34 var outputRule0 = { |
| 35 "id": "_0_", |
| 36 "conditions": [createTestCondition("test1"), createTestCondition("test2")], |
| 37 "actions": [createTestAction("action1"), createTestAction("action2")], |
| 38 "priority": 100 |
| 39 } |
| 40 |
| 41 var inputRule1 = { |
| 42 "id": "my_rule_id", |
| 43 "conditions": [], |
| 44 "actions": [], |
| 45 "priority": 10 |
| 46 } |
| 47 |
| 48 var outputRule1 = inputRule1; |
| 49 |
| 50 var inputRule2 = { |
| 51 // No 'id', this should be filled by the API |
| 52 "conditions": [createTestCondition("test3")], |
| 53 "actions": [createTestAction("action3")] |
| 54 // No 'priority', this should be filled by the API |
| 55 } |
| 56 |
| 57 var outputRule2 = { |
| 58 "id": "_1_", |
| 59 "conditions": [createTestCondition("test3")], |
| 60 "actions": [createTestAction("action1")], |
| 61 "priority": 100 |
| 62 } |
| 63 |
| 64 chrome.test.runTests([ |
| 65 // Add adding two simple rules and check that their optional fields are set |
| 66 // correctly in the call back function. |
| 67 function testAddRules() { |
| 68 var callback = function(rules) { |
| 69 chrome.test.assertEq(2, rules.length); |
| 70 // API should have generated id and priority fields. |
| 71 chrome.test.assertTrue("id" in rules[0]); |
| 72 chrome.test.assertEq(100, rules[0].priority); |
| 73 // Callback parameter should have preserved the identities of rules. |
| 74 chrome.test.assertTrue(rules[0] === inputRule0); |
| 75 chrome.test.assertTrue(rules[1] === inputRule1); |
| 76 chrome.test.succeed(); |
| 77 }; |
| 78 declarative.testEvent.addRules([inputRule0, inputRule1], callback); |
| 79 }, |
| 80 // Check that getRules() returns all rules if no filter is passed. |
| 81 function testGetRules() { |
| 82 var callback = function(rules) { |
| 83 chrome.test.assertEq(2, rules.length); |
| 84 // We are not given any gurantee on the order in which rules are returned. |
| 85 chrome.test.assertTrue( |
| 86 (chrome.test.checkDeepEq(outputRule0, rules[0]) && |
| 87 chrome.test.checkDeepEq(outputRule1, rules[1])) || |
| 88 (chrome.test.checkDeepEq(outputRule0, rules[1]) && |
| 89 chrome.test.checkDeepEq(outputRule1, rules[0]))) |
| 90 chrome.test.succeed(); |
| 91 } |
| 92 declarative.testEvent.getRules([], callback); |
| 93 }, |
| 94 // Check that getRules() returns all rules if rules are filtered by ID. |
| 95 function testSelectiveGetRules() { |
| 96 var callback = function(rules) { |
| 97 chrome.test.assertEq(1, rules.length); |
| 98 chrome.test.assertEq(outputRule1, rules[0]); |
| 99 chrome.test.succeed(); |
| 100 } |
| 101 declarative.testEvent.getRules(["my_rule_id"], callback); |
| 102 }, |
| 103 // Check that we can remove individual rules. |
| 104 function testSelectiveRemoveRules() { |
| 105 var callback = function(rules) { |
| 106 chrome.test.succeed(); |
| 107 } |
| 108 declarative.testEvent.removeRules(["my_rule_id"], callback); |
| 109 }, |
| 110 // Check that after removal, the rules are really gone. |
| 111 function testGetRemainingRules() { |
| 112 var callback = function(rules) { |
| 113 chrome.test.assertEq(1, rules.length); |
| 114 chrome.test.assertEq(outputRule0, rules[0]); |
| 115 chrome.test.succeed(); |
| 116 } |
| 117 declarative.testEvent.getRules([], callback); |
| 118 }, |
| 119 // Check that rules are assigned unique IDs. |
| 120 function testIdGeneration() { |
| 121 var callback = function(rules) { |
| 122 chrome.test.assertEq(1, rules.length); |
| 123 // API should have generated id and priority fields. |
| 124 chrome.test.assertTrue("id" in rules[0]); |
| 125 // The IDs should be distinct. |
| 126 chrome.test.assertFalse(outputRule0["id"] === rules[0]["id"]); |
| 127 chrome.test.succeed(); |
| 128 }; |
| 129 declarative.testEvent.addRules([inputRule2], callback); |
| 130 }, |
| 131 // Check that we can remove all rules at once. |
| 132 function testRemovingAllRules() { |
| 133 var callback = function() { |
| 134 chrome.test.succeed(); |
| 135 } |
| 136 declarative.testEvent.removeRules([], callback); |
| 137 }, |
| 138 // Check that the rules are actually gone. |
| 139 function testAllRulesRemoved() { |
| 140 var callback = function(rules) { |
| 141 chrome.test.assertEq(0, rules.length); |
| 142 chrome.test.succeed(); |
| 143 } |
| 144 declarative.testEvent.getRules([], callback); |
| 145 }, |
| 146 // Finally we add one additional rule, to check that is is removed |
| 147 // on page unload. |
| 148 function testAddRules() { |
| 149 var callback = function(rules) { |
| 150 chrome.test.assertEq(1, rules.length); |
| 151 chrome.test.succeed(); |
| 152 }; |
| 153 declarative.testEvent.addRules([inputRule0], callback); |
| 154 }, |
| 155 ]); |
| OLD | NEW |