Chromium Code Reviews| Index: chrome/test/data/extensions/api_test/declarative/api/background.js |
| diff --git a/chrome/test/data/extensions/api_test/declarative/api/background.js b/chrome/test/data/extensions/api_test/declarative/api/background.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0d8077b680c7c6b8ea8f04727cefa0d645c94c76 |
| --- /dev/null |
| +++ b/chrome/test/data/extensions/api_test/declarative/api/background.js |
| @@ -0,0 +1,154 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +var declarative = chrome.experimental.declarative; |
| + |
| +function createTestCondition(opt_testParameter) { |
| + var result = { |
| + "instanceType": "experimental.declarative.TestCondition" |
| + }; |
| + if (opt_testParameter) { |
| + result["testParameter"] = opt_testParameter; |
| + } |
| + return result; |
| +} |
| + |
| +function createTestAction(opt_testParameter) { |
| + var result = { |
| + "instanceType": "experimental.declarative.TestAction" |
| + }; |
| + if (opt_testParameter) { |
| + result["testParameter"] = opt_testParameter; |
| + } |
| + return result; |
| +} |
| + |
| +var inputRule0 = { |
| + // No 'id', this should be filled by the API |
| + "conditions": [createTestCondition("test1"), createTestCondition("test2")], |
| + "actions": [createTestAction("action1"), createTestAction("action2")] |
| + // No 'priority', this should be filled by the API |
| +} |
| + |
| +var outputRule0 = { |
| + "id": "_0_", |
| + "conditions": [createTestCondition("test1"), createTestCondition("test2")], |
| + "actions": [createTestAction("action1"), createTestAction("action2")], |
| + "priority": 100 |
| +} |
| + |
| +var inputRule1 = { |
| + "id": "my_rule_id", |
| + "conditions": [], |
| + "actions": [], |
| + "priority": 10 |
| +} |
| + |
| +var outputRule1 = inputRule1; |
| + |
| +var inputRule2 = { |
| + // No 'id', this should be filled by the API |
| + "conditions": [createTestCondition("test3")], |
| + "actions": [createTestAction("action3")] |
| + // No 'priority', this should be filled by the API |
| +} |
| + |
| +var outputRule2 = { |
| + "id": "_1_", |
| + "conditions": [createTestCondition("test3")], |
| + "actions": [createTestAction("action1")], |
| + "priority": 100 |
| +} |
| + |
| +chrome.test.runTests([ |
| + // Add adding two simple rules and check that their optional fields are set |
| + // correctly in the call back function. |
| + function testAddRules() { |
| + var callback = function(rules) { |
| + chrome.test.assertEq(2, rules.length); |
| + // API should have generated id and priority fields. |
| + chrome.test.assertTrue("id" in rules[0]); |
| + chrome.test.assertEq(100, rules[0].priority); |
| + chrome.test.assertEq(outputRule0, rules[0]); |
| + chrome.test.assertEq(outputRule1, rules[1]); |
|
not at google - send to devlin
2012/02/02 11:59:16
perhaps more concise as
chrome.test.assertEq([out
battre
2012/02/02 19:12:36
I have changed this to
chrome.test.assertEq([outp
not at google - send to devlin
2012/02/03 13:06:20
Isn't the priority=100 check redundant though, sin
battre
2012/02/06 16:21:04
I had the separate test to create simpler to read
|
| + chrome.test.succeed(); |
| + }; |
| + declarative.testEvent.addRules([inputRule0, inputRule1], callback); |
| + }, |
| + // Check that getRules() returns all rules if no filter is passed. |
| + function testGetRules() { |
| + var callback = function(rules) { |
| + chrome.test.assertEq(2, rules.length); |
| + // We are not given any gurantee on the order in which rules are returned. |
| + chrome.test.assertTrue( |
| + (chrome.test.checkDeepEq(outputRule0, rules[0]) && |
| + chrome.test.checkDeepEq(outputRule1, rules[1])) || |
| + (chrome.test.checkDeepEq(outputRule0, rules[1]) && |
| + chrome.test.checkDeepEq(outputRule1, rules[0]))) |
| + chrome.test.succeed(); |
| + } |
| + declarative.testEvent.getRules([], callback); |
|
not at google - send to devlin
2012/02/02 11:59:16
Why is [] get all rules, I would have though this
battre
2012/02/02 19:12:36
This does not work with SchemaGeneratedBindings as
not at google - send to devlin
2012/02/03 13:06:20
That's surprising? The Storage API uses the tacti
battre
2012/02/06 16:21:04
I have no idea why it did not work last time.
Don
|
| + }, |
| + // Check that getRules() returns all rules if rules are filtered by ID. |
| + function testSelectiveGetRules() { |
| + var callback = function(rules) { |
| + chrome.test.assertEq(1, rules.length); |
| + chrome.test.assertEq(outputRule1, rules[0]); |
| + chrome.test.succeed(); |
| + } |
| + declarative.testEvent.getRules(["my_rule_id"], callback); |
| + }, |
| + // Check that we can remove individual rules. |
| + function testSelectiveRemoveRules() { |
| + var callback = function(rules) { |
| + chrome.test.succeed(); |
| + } |
| + declarative.testEvent.removeRules(["my_rule_id"], callback); |
| + }, |
| + // Check that after removal, the rules are really gone. |
| + function testGetRemainingRules() { |
| + var callback = function(rules) { |
| + chrome.test.assertEq(1, rules.length); |
| + chrome.test.assertEq(outputRule0, rules[0]); |
| + chrome.test.succeed(); |
| + } |
| + declarative.testEvent.getRules([], callback); |
| + }, |
| + // Check that rules are assigned unique IDs. |
| + function testIdGeneration() { |
| + var callback = function(rules) { |
| + chrome.test.assertEq(1, rules.length); |
| + // API should have generated id and priority fields. |
| + chrome.test.assertTrue("id" in rules[0]); |
| + // The IDs should be distinct. |
| + chrome.test.assertFalse(outputRule0["id"] === rules[0]["id"]); |
| + chrome.test.succeed(); |
| + }; |
| + declarative.testEvent.addRules([inputRule2], callback); |
| + }, |
| + // Check that we can remove all rules at once. |
| + function testRemovingAllRules() { |
| + var callback = function() { |
| + chrome.test.succeed(); |
| + } |
| + declarative.testEvent.removeRules([], callback); |
|
not at google - send to devlin
2012/02/02 11:59:16
See comment above about getRules.
battre
2012/02/02 19:12:36
see above
battre
2012/02/06 16:21:04
Done.
|
| + }, |
| + // Check that the rules are actually gone. |
| + function testAllRulesRemoved() { |
| + var callback = function(rules) { |
| + chrome.test.assertEq(0, rules.length); |
| + chrome.test.succeed(); |
| + } |
| + declarative.testEvent.getRules([], callback); |
| + }, |
| + // Finally we add one additional rule, to check that is is removed |
| + // on page unload. |
| + function testAddRules() { |
| + var callback = function(rules) { |
| + chrome.test.assertEq(1, rules.length); |
| + chrome.test.succeed(); |
| + }; |
| + declarative.testEvent.addRules([inputRule0], callback); |
|
not at google - send to devlin
2012/02/02 11:59:16
Another important test is that the condition/actio
battre
2012/02/02 19:12:36
Done.
|
| + }, |
| + ]); |