| 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..69dbd2aa82e4ad58478865ff971027646514dd6c
|
| --- /dev/null
|
| +++ b/chrome/test/data/extensions/api_test/declarative/api/background.js
|
| @@ -0,0 +1,155 @@
|
| +// 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);
|
| + // Callback parameter should have preserved the identities of rules.
|
| + chrome.test.assertTrue(rules[0] === inputRule0);
|
| + chrome.test.assertTrue(rules[1] === inputRule1);
|
| + 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);
|
| + },
|
| + // 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);
|
| + },
|
| + // 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);
|
| + },
|
| + ]);
|
|
|