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.callback(function(response) {
14 chrome.test.assertEq(typeof response, "undefined");
15 }, "Specified native messaging host not found."));
18 function sendMessageWithCallback() {
19 var message = {"text": "Hi there!", "number": 3};
20 chrome.runtime.sendNativeMessage(
22 chrome.test.callbackPass(function(response) {
23 chrome.test.assertEq(1, response.id);
24 chrome.test.assertEq(message, response.echo);
26 response.caller_url, window.location.origin + "/");
30 // The goal of this test is just not to crash.
31 function sendMessageWithoutCallback() {
32 var message = {"text": "Hi there!", "number": 3};
33 chrome.extension.sendNativeMessage(appName, message);
34 chrome.test.succeed(); // Mission Complete
37 function bigMessage() {
38 // Create a special message for which the test host must try sending a
39 // message that is bigger than the limit.
40 var message = { "bigMessageTest": true };
41 chrome.runtime.sendNativeMessage(
43 chrome.test.callback(function(response) {
44 chrome.test.assertEq(typeof response, "undefined");
46 "Error when communicating with the native messaging host."));
50 var messagesToSend = [{"text": "foo"},
51 {"text": "bar", "funCount": 9001},
53 var currentMessage = 0;
55 port = chrome.extension.connectNative(appName);
56 port.postMessage(messagesToSend[currentMessage]);
58 port.onMessage.addListener(function(message) {
59 chrome.test.assertEq(currentMessage + 1, message.id);
60 chrome.test.assertEq(messagesToSend[currentMessage], message.echo);
62 message.caller_url, window.location.origin + "/");
65 if (currentMessage == messagesToSend.length)
66 chrome.test.succeed();
68 port.postMessage(messagesToSend[currentMessage]);
72 // Verify that the case when host stops itself is handled properly.
74 port = chrome.extension.connectNative(appName);
76 port.onMessage.addListener(function(message) {
77 port.onDisconnect.addListener(chrome.test.callback(
79 "Native host has exited."));
82 // Send first message that should stop the host.
83 port.postMessage({ "stopHostTest": true });