| 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": "webRequest.RequestMatcher" |
| 10 }; |
| 11 if (opt_testParameter) { |
| 12 result["url"] = opt_testParameter; |
| 13 } |
| 14 return result; |
| 15 } |
| 16 |
| 17 function createTestAction() { |
| 18 var result = { |
| 19 "instanceType": "webRequest.CancelRequest" |
| 20 }; |
| 21 return result; |
| 22 } |
| 23 |
| 24 function createTestAction2() { |
| 25 var result = { |
| 26 "instanceType": "webRequest.ModifyRequest" |
| 27 }; |
| 28 return result; |
| 29 } |
| 30 |
| 31 var inputRule0 = { |
| 32 // No 'id', this should be filled by the API. |
| 33 "conditions": [createTestCondition("test1"), createTestCondition("test2")], |
| 34 "actions": [createTestAction(), createTestAction2()] |
| 35 // No 'priority', this should be filled by the API. |
| 36 } |
| 37 |
| 38 var outputRule0 = { |
| 39 "id": "_0_", |
| 40 "conditions": [createTestCondition("test1"), createTestCondition("test2")], |
| 41 "actions": [createTestAction(), createTestAction2()], |
| 42 "priority": 100 |
| 43 } |
| 44 |
| 45 var inputRule1 = { |
| 46 "id": "my_rule_id", |
| 47 "conditions": [], |
| 48 "actions": [], |
| 49 "priority": 10 |
| 50 } |
| 51 |
| 52 var outputRule1 = inputRule1; |
| 53 |
| 54 var inputRule2 = { |
| 55 // No 'id', this should be filled by the API. |
| 56 "conditions": [createTestCondition("test3")], |
| 57 "actions": [createTestAction()] |
| 58 // No 'priority', this should be filled by the API. |
| 59 } |
| 60 |
| 61 var outputRule2 = { |
| 62 "id": "_1_", |
| 63 "conditions": [createTestCondition("test3")], |
| 64 "actions": [createTestAction()], |
| 65 "priority": 100 |
| 66 } |
| 67 |
| 68 var invalidRule0 = { |
| 69 "conditions": [createTestCondition("test1")] |
| 70 // "actions" is missing but not optional. |
| 71 }; |
| 72 |
| 73 var invalidRule1 = { |
| 74 "conditions": [createTestCondition("test1")], |
| 75 // "actions" contains an invalid action (separate test because this validation |
| 76 // happens on a different code path). |
| 77 "actions": [{"key": "value"}] |
| 78 }; |
| 79 |
| 80 var testEvent = chrome.webRequest.onRequest; |
| 81 |
| 82 chrome.test.runTests([ |
| 83 // Add adding two simple rules and check that their optional fields are set |
| 84 // correctly in the call back function. |
| 85 function testAddRules() { |
| 86 var callback = function(rules) { |
| 87 chrome.test.assertNoLastError(); |
| 88 chrome.test.assertEq(2, rules.length); |
| 89 // API should have generated id and priority fields. |
| 90 chrome.test.assertTrue("id" in rules[0]); |
| 91 chrome.test.assertEq([outputRule0, outputRule1], rules); |
| 92 chrome.test.succeed(); |
| 93 }; |
| 94 testEvent.addRules([inputRule0, inputRule1], callback); |
| 95 }, |
| 96 // Check that getRules() returns all rules if no filter is passed. |
| 97 function testGetRules() { |
| 98 var callback = function(rules) { |
| 99 chrome.test.assertNoLastError(); |
| 100 // We are not given any gurantee on the order in which rules are returned. |
| 101 chrome.test.assertTrue( |
| 102 chrome.test.checkDeepEq([outputRule0, outputRule1], rules) || |
| 103 chrome.test.checkDeepEq([outputRule1, outputRule0], rules)); |
| 104 chrome.test.succeed(); |
| 105 } |
| 106 testEvent.getRules(null, callback); |
| 107 }, |
| 108 // Check that getRules() returns all rules if no filter is passed. |
| 109 function testGetRules2() { |
| 110 var callback = function(rules) { |
| 111 chrome.test.assertNoLastError(); |
| 112 // We are not given any gurantee on the order in which rules are returned. |
| 113 chrome.test.assertTrue( |
| 114 chrome.test.checkDeepEq([outputRule0, outputRule1], rules) || |
| 115 chrome.test.checkDeepEq([outputRule1, outputRule0], rules)); |
| 116 chrome.test.succeed(); |
| 117 } |
| 118 testEvent.getRules(undefined, callback); |
| 119 }, |
| 120 // Check that getRules() returns no rules if empty filter is passed. |
| 121 function testGetRules3() { |
| 122 var callback = function(rules) { |
| 123 chrome.test.assertNoLastError(); |
| 124 // We are not given any gurantee on the order in which rules are returned. |
| 125 chrome.test.assertEq([], rules); |
| 126 chrome.test.succeed(); |
| 127 } |
| 128 testEvent.getRules([], callback); |
| 129 }, |
| 130 // Check that getRules() returns all rules if rules are filtered by ID. |
| 131 function testSelectiveGetRules() { |
| 132 var callback = function(rules) { |
| 133 chrome.test.assertNoLastError(); |
| 134 chrome.test.assertEq([outputRule1], rules); |
| 135 chrome.test.succeed(); |
| 136 } |
| 137 testEvent.getRules(["my_rule_id"], callback); |
| 138 }, |
| 139 // Check that we can remove individual rules. |
| 140 function testSelectiveRemoveRules() { |
| 141 var callback = function(rules) { |
| 142 chrome.test.assertNoLastError(); |
| 143 chrome.test.succeed(); |
| 144 } |
| 145 testEvent.removeRules(["my_rule_id"], callback); |
| 146 }, |
| 147 // Check that after removal, the rules are really gone. |
| 148 function testGetRemainingRules() { |
| 149 var callback = function(rules) { |
| 150 chrome.test.assertNoLastError(); |
| 151 chrome.test.assertEq([outputRule0], rules); |
| 152 chrome.test.succeed(); |
| 153 } |
| 154 testEvent.getRules(null, callback); |
| 155 }, |
| 156 // Check that rules are assigned unique IDs. |
| 157 function testIdGeneration() { |
| 158 var callback = function(rules) { |
| 159 chrome.test.assertNoLastError(); |
| 160 chrome.test.assertEq(1, rules.length); |
| 161 // API should have generated id and priority fields. |
| 162 chrome.test.assertTrue("id" in rules[0]); |
| 163 // The IDs should be distinct. |
| 164 chrome.test.assertFalse(outputRule0["id"] === rules[0]["id"]); |
| 165 chrome.test.succeed(); |
| 166 }; |
| 167 testEvent.addRules([inputRule2], callback); |
| 168 }, |
| 169 // Check that we can remove all rules at once. |
| 170 function testRemovingAllRules() { |
| 171 var callback = function() { |
| 172 chrome.test.assertNoLastError(); |
| 173 chrome.test.succeed(); |
| 174 } |
| 175 testEvent.removeRules(null, callback); |
| 176 }, |
| 177 // Check that the rules are actually gone. |
| 178 function testAllRulesRemoved() { |
| 179 var callback = function(rules) { |
| 180 chrome.test.assertNoLastError(); |
| 181 chrome.test.assertEq(0, rules.length); |
| 182 chrome.test.succeed(); |
| 183 } |
| 184 testEvent.getRules(null, callback); |
| 185 }, |
| 186 // Check that validation is performed. |
| 187 function testValidation() { |
| 188 var fail = function() { |
| 189 chrome.test.fail("An exception was expected"); |
| 190 }; |
| 191 try { |
| 192 testEvent.addRules([invalidRule0], fail); |
| 193 fail(); |
| 194 } catch (e) {} |
| 195 try { |
| 196 testEvent.addRules([invalidRule1], fail); |
| 197 fail(); |
| 198 } catch (e) {} |
| 199 // None of these rules should have been registered. |
| 200 var callback = function(rules) { |
| 201 chrome.test.assertNoLastError(); |
| 202 chrome.test.assertEq(0, rules.length); |
| 203 chrome.test.succeed(); |
| 204 }; |
| 205 testEvent.getRules(null, callback); |
| 206 }, |
| 207 // Finally we add one additional rule, to check that is is removed |
| 208 // on page unload. |
| 209 function testAddRules() { |
| 210 var callback = function(rules) { |
| 211 chrome.test.assertNoLastError(); |
| 212 chrome.test.assertEq(1, rules.length); |
| 213 chrome.test.succeed(); |
| 214 }; |
| 215 testEvent.addRules([inputRule0], callback); |
| 216 }, |
| 217 ]); |
| OLD | NEW |