| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Register a rule to show the page action whenever we see a page with 'example' | 5 // Register a rule to show the page action whenever we see a page with 'example' |
| 6 // in the host. Send messages after registration of the rule is complete and | 6 // in the host. Send messages after registration of the rule is complete and |
| 7 // when the page action is clicked. | 7 // when the page action is clicked. |
| 8 | 8 |
| 9 const kRuleId = 'rule1'; | 9 const kRuleId = 'rule1'; |
| 10 | 10 |
| 11 var imageData = new ImageData(19, 19); |
| 12 |
| 11 var rule = { | 13 var rule = { |
| 12 conditions: [ | 14 conditions: [ |
| 13 new chrome.declarativeContent.PageStateMatcher( | 15 new chrome.declarativeContent.PageStateMatcher( |
| 14 {pageUrl: {hostPrefix: 'example'}}), | 16 {pageUrl: {hostPrefix: 'example'}}), |
| 15 ], actions: [ | 17 ], actions: [ |
| 16 new chrome.declarativeContent.ShowPageAction(), | 18 new chrome.declarativeContent.ShowPageAction(), |
| 19 new chrome.declarativeContent.SetIcon({imageData: imageData}), |
| 17 ], | 20 ], |
| 18 id: kRuleId, | 21 id: kRuleId, |
| 19 }; | 22 }; |
| 20 | 23 |
| 21 chrome.pageAction.onClicked.addListener(function() { | 24 chrome.pageAction.onClicked.addListener(function() { |
| 22 chrome.declarativeContent.onPageChanged.removeRules([kRuleId], function() { | 25 chrome.declarativeContent.onPageChanged.removeRules([kRuleId], function() { |
| 23 chrome.declarativeContent.onPageChanged.getRules(function(rules) { | 26 chrome.declarativeContent.onPageChanged.getRules(function(rules) { |
| 24 chrome.test.assertEq(0, rules.length); | 27 chrome.test.assertEq(0, rules.length); |
| 25 chrome.test.sendMessage('clicked and removed'); | 28 chrome.test.sendMessage('clicked and removed'); |
| 26 }); | 29 }); |
| 27 }); | 30 }); |
| 28 }); | 31 }); |
| 29 | 32 |
| 30 chrome.declarativeContent.onPageChanged.addRules([rule], function() { | 33 chrome.declarativeContent.onPageChanged.addRules([rule], function() { |
| 31 chrome.declarativeContent.onPageChanged.getRules(function(rules) { | 34 chrome.declarativeContent.onPageChanged.getRules(function(rules) { |
| 32 chrome.test.assertEq(1, rules.length); | 35 chrome.test.assertEq(1, rules.length); |
| 33 chrome.test.assertEq(kRuleId, rules[0].id); | 36 chrome.test.assertEq(kRuleId, rules[0].id); |
| 34 chrome.test.sendMessage('ready'); | 37 chrome.test.sendMessage('ready'); |
| 35 }); | 38 }); |
| 36 }); | 39 }); |
| 40 |
| 41 function didThrow(func) { |
| 42 var caught = false; |
| 43 try { |
| 44 func(); |
| 45 } catch (e) { |
| 46 caught = true; |
| 47 } |
| 48 return caught; |
| 49 } |
| 50 |
| 51 chrome.test.runTests([ |
| 52 function validationCheck() { |
| 53 // Test that type constructions are properly validated. |
| 54 chrome.test.assertTrue(didThrow(function() { |
| 55 var matcher = new chrome.declarativeContent.PageStateMatcher( |
| 56 {pageUrl: {fake: 'bogus'}}); |
| 57 })); |
| 58 chrome.test.succeed(); |
| 59 }, |
| 60 ]); |
| OLD | NEW |