Do not announce robot account token before account ID is available
[chromium-blink-merge.git] / chrome / test / data / extensions / api_test / webrequest_sendmessage / background.js
blob43bf7c5955cc25b913059a2a00a6987dd747f2d4
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 EVENT_MESSAGE_EXTENSION_STRING = "Extension";
7 // Constants as functions, not to be called until after runTests.
8 function getURLHttpSimpleLoad() {
9   return getServerURL('extensions/api_test/webrequest/simpleLoad/a.html');
12 function getServerURL(path) {
13   return 'http://www.a.com:' + testServerPort + '/' + path;
16 function runTests(tests) {
17   chrome.test.getConfig(function(config) {
18     testServerPort = config.testServer.port;
19     chrome.test.runTests(tests);
20   });
23 function size(obj) {
24   var size = 0, key;
25   for (key in obj) {
26     if (obj.hasOwnProperty(key)) size++;
27   }
28   return size;
31 runTests([
32   function testSendMessage() {
33     var expectedEvents = {
34       "onBeforeRequest-Extension": 1,
35       "onBeforeSendHeaders-Extension": 1,
36       "onHeadersReceived-Extension": 1
37       // "onAuthRequired-Extension" is not sent for this test case.
38     };
39     var done = chrome.test.listenForever(
40         chrome.declarativeWebRequest.onMessage,
41         function(details) {
42       if (EVENT_MESSAGE_EXTENSION_STRING != details.message) {
43         chrome.test.fail("Invalid message: " + details.message);
44       }
45       chrome.test.assertEq(getURLHttpSimpleLoad(), details.url);
46       var messageKey = details.stage + "-" + details.message;
47       if (messageKey in expectedEvents) {
48         delete expectedEvents[messageKey];
49         if (size(expectedEvents) == 0) {
50           done();
51         }
52       } else {
53         chrome.test.fail();
54       }
55     });
57     chrome.declarativeWebRequest.onRequest.removeRules(null, function() {
58       var rule = {
59         conditions: [
60           new chrome.declarativeWebRequest.RequestMatcher(
61               {url: {urlEquals: getURLHttpSimpleLoad()}}),
62         ],
63         actions: [
64           new chrome.declarativeWebRequest.SendMessageToExtension(
65               {message: EVENT_MESSAGE_EXTENSION_STRING}),
66         ],
67       };
68       chrome.declarativeWebRequest.onRequest.addRules([rule], function() {
69         chrome.tabs.create({"url": getURLHttpSimpleLoad()});
70       });
71     });
72   }
73 ]);