Chromium Code Reviews| 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 chrome.test.assertEq(outputRule0, rules[0]); | |
| 74 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
| |
| 75 chrome.test.succeed(); | |
| 76 }; | |
| 77 declarative.testEvent.addRules([inputRule0, inputRule1], callback); | |
| 78 }, | |
| 79 // Check that getRules() returns all rules if no filter is passed. | |
| 80 function testGetRules() { | |
| 81 var callback = function(rules) { | |
| 82 chrome.test.assertEq(2, rules.length); | |
| 83 // We are not given any gurantee on the order in which rules are returned. | |
| 84 chrome.test.assertTrue( | |
| 85 (chrome.test.checkDeepEq(outputRule0, rules[0]) && | |
| 86 chrome.test.checkDeepEq(outputRule1, rules[1])) || | |
| 87 (chrome.test.checkDeepEq(outputRule0, rules[1]) && | |
| 88 chrome.test.checkDeepEq(outputRule1, rules[0]))) | |
| 89 chrome.test.succeed(); | |
| 90 } | |
| 91 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
| |
| 92 }, | |
| 93 // Check that getRules() returns all rules if rules are filtered by ID. | |
| 94 function testSelectiveGetRules() { | |
| 95 var callback = function(rules) { | |
| 96 chrome.test.assertEq(1, rules.length); | |
| 97 chrome.test.assertEq(outputRule1, rules[0]); | |
| 98 chrome.test.succeed(); | |
| 99 } | |
| 100 declarative.testEvent.getRules(["my_rule_id"], callback); | |
| 101 }, | |
| 102 // Check that we can remove individual rules. | |
| 103 function testSelectiveRemoveRules() { | |
| 104 var callback = function(rules) { | |
| 105 chrome.test.succeed(); | |
| 106 } | |
| 107 declarative.testEvent.removeRules(["my_rule_id"], callback); | |
| 108 }, | |
| 109 // Check that after removal, the rules are really gone. | |
| 110 function testGetRemainingRules() { | |
| 111 var callback = function(rules) { | |
| 112 chrome.test.assertEq(1, rules.length); | |
| 113 chrome.test.assertEq(outputRule0, rules[0]); | |
| 114 chrome.test.succeed(); | |
| 115 } | |
| 116 declarative.testEvent.getRules([], callback); | |
| 117 }, | |
| 118 // Check that rules are assigned unique IDs. | |
| 119 function testIdGeneration() { | |
| 120 var callback = function(rules) { | |
| 121 chrome.test.assertEq(1, rules.length); | |
| 122 // API should have generated id and priority fields. | |
| 123 chrome.test.assertTrue("id" in rules[0]); | |
| 124 // The IDs should be distinct. | |
| 125 chrome.test.assertFalse(outputRule0["id"] === rules[0]["id"]); | |
| 126 chrome.test.succeed(); | |
| 127 }; | |
| 128 declarative.testEvent.addRules([inputRule2], callback); | |
| 129 }, | |
| 130 // Check that we can remove all rules at once. | |
| 131 function testRemovingAllRules() { | |
| 132 var callback = function() { | |
| 133 chrome.test.succeed(); | |
| 134 } | |
| 135 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.
| |
| 136 }, | |
| 137 // Check that the rules are actually gone. | |
| 138 function testAllRulesRemoved() { | |
| 139 var callback = function(rules) { | |
| 140 chrome.test.assertEq(0, rules.length); | |
| 141 chrome.test.succeed(); | |
| 142 } | |
| 143 declarative.testEvent.getRules([], callback); | |
| 144 }, | |
| 145 // Finally we add one additional rule, to check that is is removed | |
| 146 // on page unload. | |
| 147 function testAddRules() { | |
| 148 var callback = function(rules) { | |
| 149 chrome.test.assertEq(1, rules.length); | |
| 150 chrome.test.succeed(); | |
| 151 }; | |
| 152 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.
| |
| 153 }, | |
| 154 ]); | |
| OLD | NEW |