OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 if (!chrome || !chrome.test) | 5 if (!chrome || !chrome.test) |
6 throw new Error('chrome.test is undefined'); | 6 throw new Error('chrome.test is undefined'); |
7 | 7 |
| 8 var portNumber; |
| 9 |
8 // This is a good end-to-end test for two reasons. The first is obvious - it | 10 // This is a good end-to-end test for two reasons. The first is obvious - it |
9 // tests a simple API and makes sure it behaves as expected, as well as testing | 11 // tests a simple API and makes sure it behaves as expected, as well as testing |
10 // that other APIs are unavailable. | 12 // that other APIs are unavailable. |
11 // The second is that chrome.test is itself an extension API, and a rather | 13 // The second is that chrome.test is itself an extension API, and a rather |
12 // complex one. It requires both traditional bindings (renderer parses args, | 14 // complex one. It requires both traditional bindings (renderer parses args, |
13 // passes info to browser process, browser process does work and responds, re- | 15 // passes info to browser process, browser process does work and responds, re- |
14 // enters JS) and custom JS bindings (in order to have our runTests, assert* | 16 // enters JS) and custom JS bindings (in order to have our runTests, assert* |
15 // methods, etc). If any of these stages failed, the test itself would also | 17 // methods, etc). If any of these stages failed, the test itself would also |
16 // fail. | 18 // fail. |
17 chrome.test.runTests([ | 19 var tests = [ |
18 function idleApi() { | 20 function idleApi() { |
19 chrome.test.assertTrue(!!chrome.idle); | 21 chrome.test.assertTrue(!!chrome.idle); |
20 chrome.test.assertTrue(!!chrome.idle.IdleState); | 22 chrome.test.assertTrue(!!chrome.idle.IdleState); |
21 chrome.test.assertTrue(!!chrome.idle.IdleState.IDLE); | 23 chrome.test.assertTrue(!!chrome.idle.IdleState.IDLE); |
22 chrome.test.assertTrue(!!chrome.idle.IdleState.ACTIVE); | 24 chrome.test.assertTrue(!!chrome.idle.IdleState.ACTIVE); |
23 chrome.test.assertTrue(!!chrome.idle.queryState); | 25 chrome.test.assertTrue(!!chrome.idle.queryState); |
24 chrome.idle.queryState(1000, function(state) { | 26 chrome.idle.queryState(1000, function(state) { |
25 // Depending on the machine, this could come back as either idle or | 27 // Depending on the machine, this could come back as either idle or |
26 // active. However, all we're curious about is the bindings themselves | 28 // active. However, all we're curious about is the bindings themselves |
27 // (not the API implementation), so as long as it's a possible response, | 29 // (not the API implementation), so as long as it's a possible response, |
(...skipping 21 matching lines...) Expand all Loading... |
49 chrome.tabs.create({url: 'http://example.com'}, tab => { | 51 chrome.tabs.create({url: 'http://example.com'}, tab => { |
50 resolve(tab.id); | 52 resolve(tab.id); |
51 }); | 53 }); |
52 }); | 54 }); |
53 Promise.all([createdEvent, createdCallback]).then(res => { | 55 Promise.all([createdEvent, createdCallback]).then(res => { |
54 chrome.test.assertEq(2, res.length); | 56 chrome.test.assertEq(2, res.length); |
55 chrome.test.assertEq(res[0], res[1]); | 57 chrome.test.assertEq(res[0], res[1]); |
56 chrome.test.succeed(); | 58 chrome.test.succeed(); |
57 }); | 59 }); |
58 }, | 60 }, |
59 ]); | 61 function testMessaging() { |
| 62 var tabId; |
| 63 var createPort = function() { |
| 64 chrome.test.assertTrue(!!tabId); |
| 65 var port = chrome.tabs.connect(tabId); |
| 66 chrome.test.assertTrue(!!port, 'Port does not exist'); |
| 67 port.onMessage.addListener(message => { |
| 68 chrome.test.assertEq('content script', message); |
| 69 port.disconnect(); |
| 70 chrome.test.succeed(); |
| 71 }); |
| 72 port.postMessage('background page'); |
| 73 }; |
| 74 |
| 75 chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { |
| 76 chrome.test.assertEq('startFlow', message); |
| 77 createPort(); |
| 78 sendResponse('started'); |
| 79 }); |
| 80 var url = 'http://localhost:' + portNumber + |
| 81 '/native_bindings/messaging_test.html'; |
| 82 chrome.tabs.create({url: url}, function(tab) { |
| 83 chrome.test.assertNoLastError(); |
| 84 chrome.test.assertTrue(!!tab); |
| 85 chrome.test.assertTrue(!!tab.id && tab.id >= 0); |
| 86 tabId = tab.id; |
| 87 }); |
| 88 }, |
| 89 ]; |
| 90 |
| 91 chrome.test.getConfig(config => { |
| 92 chrome.test.assertTrue(!!config, 'config does not exist'); |
| 93 chrome.test.assertTrue(!!config.testServer, 'testServer does not exist'); |
| 94 portNumber = config.testServer.port; |
| 95 chrome.test.runTests(tests); |
| 96 }); |
OLD | NEW |