Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / test / data / extensions / api_test / declarative / api / background.js
blob702b65c883df67e8c8358f07d36e735c73eb7e5e
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.
5 var declarative = chrome.declarative;
7 var RequestMatcher = chrome.declarativeWebRequest.RequestMatcher;
8 var CancelRequest = chrome.declarativeWebRequest.CancelRequest;
9 var RedirectRequest = chrome.declarativeWebRequest.RedirectRequest;
10 var SetRequestHeader = chrome.declarativeWebRequest.SetRequestHeader;
12 var inputRule0 = {
13 // No 'id', this should be filled by the API.
14 conditions: [new RequestMatcher({url: {hostPrefix: "test1"}}),
15 new RequestMatcher({url: {hostPrefix: "test2"}})],
16 actions: [new CancelRequest(),
17 new RedirectRequest({redirectUrl: "http://foobar.com"})]
18 // No 'priority', this should be filled by the API.
21 var outputRule0 = {
22 id: "_0_",
23 conditions: [new RequestMatcher({url: {hostPrefix: "test1"}}),
24 new RequestMatcher({url: {hostPrefix: "test2"}})],
25 actions: [new CancelRequest(),
26 new RedirectRequest({redirectUrl: "http://foobar.com"})],
27 priority: 100
30 var inputRule1 = {
31 id: "my_rule_id",
32 conditions: [],
33 actions: [],
34 priority: 10
37 var outputRule1 = inputRule1;
39 var inputRule2 = {
40 // No 'id', this should be filled by the API.
41 conditions: [new RequestMatcher({url: {hostPrefix: "test3"}})],
42 actions: [new CancelRequest()]
43 // No 'priority', this should be filled by the API.
46 var outputRule2 = {
47 id: "_1_",
48 conditions: [new RequestMatcher({url: {hostPrefix: "test3"}})],
49 actions: [new CancelRequest()],
50 priority: 100
53 var invalidRule0 = {
54 conditions: [new RequestMatcher({url: {hostPrefix: "test1"}})]
55 // "actions" is missing but not optional.
58 var invalidRule1 = {
59 conditions: [new RequestMatcher({url: {hostPrefix: "test1"}})],
60 // "actions" contains an invalid action (separate test because this validation
61 // happens on a different code path).
62 actions: [{key: "value"}]
64 var invalidRule2 = {
65 conditions: [new RequestMatcher({url: {hostPrefix: "test1"}})],
66 actions: [new SetRequestHeader({name: '\x00', value: 'whatever'})]
69 var testEvent = chrome.declarativeWebRequest.onRequest;
71 chrome.test.runTests([
72 // Test validation
73 function testInvalidAddRules() {
74 try {
75 testEvent.addRules();
76 chrome.test.fail();
77 } catch(e) {
78 chrome.test.succeed();
81 function testInvalidGetRules() {
82 try {
83 testEvent.getRules(function() {});
84 chrome.test.fail();
85 } catch(e) {
86 chrome.test.succeed();
89 function testInvalidRemoveRules() {
90 try {
91 testEvent.removeRules(function() {});
92 chrome.test.fail();
93 } catch(e) {
94 chrome.test.succeed();
97 // Add adding two simple rules and check that their optional fields are set
98 // correctly in the call back function.
99 function testAddRules() {
100 var callback = function(rules) {
101 chrome.test.assertNoLastError();
102 chrome.test.assertEq(2, rules.length);
103 // API should have generated id and priority fields.
104 chrome.test.assertTrue("id" in rules[0]);
105 chrome.test.assertEq([outputRule0, outputRule1], rules);
106 chrome.test.succeed();
108 testEvent.addRules([inputRule0, inputRule1], callback);
110 // Check that getRules() returns all rules if no filter is passed.
111 function testGetRules() {
112 var callback = function(rules) {
113 chrome.test.assertNoLastError();
114 // We are not given any gurantee on the order in which rules are returned.
115 chrome.test.assertTrue(
116 chrome.test.checkDeepEq([outputRule0, outputRule1], rules) ||
117 chrome.test.checkDeepEq([outputRule1, outputRule0], rules));
118 chrome.test.succeed();
120 testEvent.getRules(null, callback);
122 // Check that getRules() returns all rules if no filter is passed.
123 function testGetRules2() {
124 var callback = function(rules) {
125 chrome.test.assertNoLastError();
126 // We are not given any gurantee on the order in which rules are returned.
127 chrome.test.assertTrue(
128 chrome.test.checkDeepEq([outputRule0, outputRule1], rules) ||
129 chrome.test.checkDeepEq([outputRule1, outputRule0], rules));
130 chrome.test.succeed();
132 testEvent.getRules(undefined, callback);
134 // Check that getRules() returns no rules if empty filter is passed.
135 function testGetRules3() {
136 var callback = function(rules) {
137 chrome.test.assertNoLastError();
138 // We are not given any gurantee on the order in which rules are returned.
139 chrome.test.assertEq([], rules);
140 chrome.test.succeed();
142 testEvent.getRules([], callback);
144 // Check that getRules() returns all rules if rules are filtered by ID.
145 function testSelectiveGetRules() {
146 var callback = function(rules) {
147 chrome.test.assertNoLastError();
148 chrome.test.assertEq([outputRule1], rules);
149 chrome.test.succeed();
151 testEvent.getRules(["my_rule_id"], callback);
153 // Check that we can remove individual rules.
154 function testSelectiveRemoveRules() {
155 var callback = function(rules) {
156 chrome.test.assertNoLastError();
157 chrome.test.succeed();
159 testEvent.removeRules(["my_rule_id"], callback);
161 // Check that after removal, the rules are really gone.
162 function testGetRemainingRules() {
163 var callback = function(rules) {
164 chrome.test.assertNoLastError();
165 chrome.test.assertEq([outputRule0], rules);
166 chrome.test.succeed();
168 testEvent.getRules(null, callback);
170 // Check that rules are assigned unique IDs.
171 function testIdGeneration() {
172 var callback = function(rules) {
173 chrome.test.assertNoLastError();
174 chrome.test.assertEq(1, rules.length);
175 // API should have generated id and priority fields.
176 chrome.test.assertTrue("id" in rules[0]);
177 // The IDs should be distinct.
178 chrome.test.assertFalse(outputRule0["id"] === rules[0]["id"]);
179 chrome.test.succeed();
181 testEvent.addRules([inputRule2], callback);
183 // Check that we can remove all rules at once.
184 function testRemovingAllRules() {
185 var callback = function() {
186 chrome.test.assertNoLastError();
187 chrome.test.succeed();
189 testEvent.removeRules(null, callback);
191 // Check that the rules are actually gone.
192 function testAllRulesRemoved() {
193 var callback = function(rules) {
194 chrome.test.assertNoLastError();
195 chrome.test.assertEq(0, rules.length);
196 chrome.test.succeed();
198 testEvent.getRules(null, callback);
200 // Check that validation is performed.
201 function testValidation() {
202 var fail = function() {
203 chrome.test.fail("An exception was expected");
205 try {
206 testEvent.addRules([invalidRule0], fail);
207 fail();
208 } catch (e) {}
209 try {
210 testEvent.addRules([invalidRule1], fail);
211 fail();
212 } catch (e) {}
213 // None of these rules should have been registered.
214 var callback = function(rules) {
215 chrome.test.assertNoLastError();
216 chrome.test.assertEq(0, rules.length);
217 chrome.test.succeed();
219 testEvent.getRules(null, callback);
221 // Check that errors are propagated
222 function testValidationAsync() {
223 var callback = function() {
224 chrome.test.assertLastError('Invalid header name.');
225 chrome.test.succeed();
227 testEvent.addRules([invalidRule2], callback);
229 // Finally we add one additional rule, to check that is is removed
230 // on page unload.
231 function testAddRules() {
232 var callback = function(rules) {
233 chrome.test.assertNoLastError();
234 chrome.test.assertEq(1, rules.length);
235 chrome.test.succeed();
237 testEvent.addRules([inputRule0], callback);