OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 var appName = 'com.google.chrome.test.echo'; | 5 var appName = 'com.google.chrome.test.echo'; |
6 | 6 |
7 chrome.test.getConfig(function(config) { | 7 chrome.test.getConfig(function(config) { |
8 chrome.test.runTests([ | 8 chrome.test.runTests([ |
9 | 9 |
10 function sendMessageWithCallback() { | 10 function sendMessageWithCallback() { |
11 var message = {"text": "Hi there!", "number": 3}; | 11 var message = {"text": "Hi there!", "number": 3}; |
12 chrome.runtime.sendNativeMessage( | 12 chrome.runtime.sendNativeMessage( |
13 appName, message, | 13 appName, message, |
14 chrome.test.callbackPass(function(nativeResponse) { | 14 chrome.test.callbackPass(function(response) { |
15 var expectedResponse = {"id": 1, "echo": message}; | 15 chrome.test.assertEq(1, response.id); |
16 chrome.test.assertEq(expectedResponse, nativeResponse); | 16 chrome.test.assertEq(message, response.echo); |
| 17 chrome.test.assertEq( |
| 18 response.caller_url, window.location.origin + "/"); |
17 })); | 19 })); |
18 }, | 20 }, |
19 | 21 |
20 // The goal of this test is just not to crash. | 22 // The goal of this test is just not to crash. |
21 function sendMessageWithoutCallback() { | 23 function sendMessageWithoutCallback() { |
22 var message = {"text": "Hi there!", "number": 3}; | 24 var message = {"text": "Hi there!", "number": 3}; |
23 chrome.extension.sendNativeMessage(appName, message); | 25 chrome.extension.sendNativeMessage(appName, message); |
24 chrome.test.succeed(); // Mission Complete | 26 chrome.test.succeed(); // Mission Complete |
25 }, | 27 }, |
26 | 28 |
27 function connect() { | 29 function connect() { |
28 var messagesToSend = [{"text": "foo"}, | 30 var messagesToSend = [{"text": "foo"}, |
29 {"text": "bar", "funCount": 9001}, | 31 {"text": "bar", "funCount": 9001}, |
30 {}]; | 32 {}]; |
31 var expectedResponses = [{"id": 1, "echo": messagesToSend[0]}, | |
32 {"id": 2, "echo": messagesToSend[1]}, | |
33 {"id": 3, "echo": messagesToSend[2]}]; | |
34 var currentMessage = 0; | 33 var currentMessage = 0; |
35 | 34 |
36 port = chrome.extension.connectNative(appName); | 35 port = chrome.extension.connectNative(appName); |
37 port.postMessage(messagesToSend[currentMessage]); | 36 port.postMessage(messagesToSend[currentMessage]); |
38 | 37 |
39 port.onMessage.addListener(function(message) { | 38 port.onMessage.addListener(function(message) { |
40 chrome.test.assertEq(expectedResponses[currentMessage], message); | 39 chrome.test.assertEq(currentMessage + 1, message.id); |
| 40 chrome.test.assertEq(messagesToSend[currentMessage], message.echo); |
| 41 chrome.test.assertEq( |
| 42 message.caller_url, window.location.origin + "/"); |
41 currentMessage++; | 43 currentMessage++; |
42 | 44 |
43 if (currentMessage == expectedResponses.length) | 45 if (currentMessage == messagesToSend.length) |
44 chrome.test.notifyPass(); | 46 chrome.test.notifyPass(); |
45 else | 47 else |
46 port.postMessage(messagesToSend[currentMessage]); | 48 port.postMessage(messagesToSend[currentMessage]); |
47 }); | 49 }); |
48 } | 50 } |
49 ]); | 51 ]); |
50 }); | 52 }); |
OLD | NEW |