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 appName
= 'com.google.chrome.test.echo';
7 chrome
.test
.getConfig(function(config
) {
9 function invalidHostName() {
10 var message
= {"text": "Hello!"};
11 chrome
.runtime
.sendNativeMessage(
12 'not.installed.app', message
,
13 chrome
.test
.callbackFail(
14 "Specified native messaging host not found.",
16 chrome
.test
.assertEq(undefined, response
);
20 function nonexistentHost() {
21 var message
= {"text": "Hello!"};
22 chrome
.runtime
.sendNativeMessage(
23 'com.google.chrome.test.host_binary_missing', message
,
24 chrome
.test
.callbackFail(
25 "Specified native messaging host not found.",
27 chrome
.test
.assertEq(undefined, response
);
31 function sendMessageWithCallback() {
32 var message
= {"text": "Hi there!", "number": 3};
33 chrome
.runtime
.sendNativeMessage(
35 chrome
.test
.callbackPass(function(response
) {
36 chrome
.test
.assertEq(1, response
.id
);
37 chrome
.test
.assertEq(message
, response
.echo
);
39 response
.caller_url
, window
.location
.origin
+ "/");
43 // The goal of this test is just not to crash.
44 function sendMessageWithoutCallback() {
45 var message
= {"text": "Hi there!", "number": 3};
46 chrome
.extension
.sendNativeMessage(appName
, message
);
47 chrome
.test
.succeed(); // Mission Complete
50 function bigMessage() {
51 // Create a special message for which the test host must try sending a
52 // message that is bigger than the limit.
53 var message
= { "bigMessageTest": true };
54 chrome
.runtime
.sendNativeMessage(
56 chrome
.test
.callbackFail(
57 "Error when communicating with the native messaging host.",
59 chrome
.test
.assertEq(undefined, response
);
64 var messagesToSend
= [{"text": "foo"},
65 {"text": "bar", "funCount": 9001},
67 var currentMessage
= 0;
69 port
= chrome
.extension
.connectNative(appName
);
70 port
.postMessage(messagesToSend
[currentMessage
]);
72 port
.onMessage
.addListener(function(message
) {
73 chrome
.test
.assertEq(currentMessage
+ 1, message
.id
);
74 chrome
.test
.assertEq(messagesToSend
[currentMessage
], message
.echo
);
76 message
.caller_url
, window
.location
.origin
+ "/");
79 if (currentMessage
== messagesToSend
.length
)
80 chrome
.test
.succeed();
82 port
.postMessage(messagesToSend
[currentMessage
]);
86 // Verify that the case when host stops itself is handled properly.
88 port
= chrome
.extension
.connectNative(appName
);
90 port
.onDisconnect
.addListener(
91 chrome
.test
.callback(function() {}, "Native host has exited."));
93 // Send first message that should stop the host.
94 port
.postMessage({ "stopHostTest": true });