408c032cc8809bf726df163d650797e06a261944
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / native_messaging / test.js
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.
4
5 var appName = 'com.google.chrome.test.echo';
6
7 chrome.test.getConfig(function(config) {
8     chrome.test.runTests([
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.",
15                 function(response) {
16                   chrome.test.assertEq(undefined, response);
17                 }));
18       },
19
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.",
26                 function(response) {
27                   chrome.test.assertEq(undefined, response);
28                 }));
29       },
30
31       function sendMessageWithCallback() {
32         var message = {"text": "Hi there!", "number": 3};
33         chrome.runtime.sendNativeMessage(
34             appName, message,
35             chrome.test.callbackPass(function(response) {
36           chrome.test.assertEq(1, response.id);
37           chrome.test.assertEq(message, response.echo);
38           chrome.test.assertEq(
39               response.caller_url, window.location.origin + "/");
40         }));
41       },
42
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
48       },
49
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(
55             appName, message,
56             chrome.test.callbackFail(
57                 "Error when communicating with the native messaging host.",
58                 function(response) {
59                   chrome.test.assertEq(undefined, response);
60                 }));
61       },
62
63       function connect() {
64         var messagesToSend = [{"text": "foo"},
65                               {"text": "bar", "funCount": 9001},
66                               {}];
67         var currentMessage = 0;
68
69         port = chrome.extension.connectNative(appName);
70         port.postMessage(messagesToSend[currentMessage]);
71
72         port.onMessage.addListener(function(message) {
73           chrome.test.assertEq(currentMessage + 1, message.id);
74           chrome.test.assertEq(messagesToSend[currentMessage], message.echo);
75           chrome.test.assertEq(
76               message.caller_url, window.location.origin + "/");
77           currentMessage++;
78
79           if (currentMessage == messagesToSend.length)
80             chrome.test.succeed();
81           else
82             port.postMessage(messagesToSend[currentMessage]);
83         });
84       },
85
86       // Verify that the case when host stops itself is handled properly.
87       function stopHost() {
88         port = chrome.extension.connectNative(appName);
89
90         port.onMessage.addListener(function(message) {
91           port.onDisconnect.addListener(chrome.test.callback(
92               function() {},
93               "Native host has exited."));
94         });
95
96         // Send first message that should stop the host.
97         port.postMessage({ "stopHostTest": true });
98       }
99     ]);
100 });