- add sources.
[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.callback(function(response) {
14               chrome.test.assertEq(typeof response, "undefined");
15             }, "Specified native messaging host not found."));
16       },
17
18       function sendMessageWithCallback() {
19         var message = {"text": "Hi there!", "number": 3};
20         chrome.runtime.sendNativeMessage(
21             appName, message,
22             chrome.test.callbackPass(function(response) {
23           chrome.test.assertEq(1, response.id);
24           chrome.test.assertEq(message, response.echo);
25           chrome.test.assertEq(
26               response.caller_url, window.location.origin + "/");
27         }));
28       },
29
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
35       },
36
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(
42             appName, message,
43             chrome.test.callback(function(response) {
44               chrome.test.assertEq(typeof response, "undefined");
45             },
46             "Error when communicating with the native messaging host."));
47       },
48
49       function connect() {
50         var messagesToSend = [{"text": "foo"},
51                               {"text": "bar", "funCount": 9001},
52                               {}];
53         var currentMessage = 0;
54
55         port = chrome.extension.connectNative(appName);
56         port.postMessage(messagesToSend[currentMessage]);
57
58         port.onMessage.addListener(function(message) {
59           chrome.test.assertEq(currentMessage + 1, message.id);
60           chrome.test.assertEq(messagesToSend[currentMessage], message.echo);
61           chrome.test.assertEq(
62               message.caller_url, window.location.origin + "/");
63           currentMessage++;
64
65           if (currentMessage == messagesToSend.length)
66             chrome.test.succeed();
67           else
68             port.postMessage(messagesToSend[currentMessage]);
69         });
70       },
71
72       // Verify that the case when host stops itself is handled properly.
73       function stopHost() {
74         port = chrome.extension.connectNative(appName);
75
76         port.onMessage.addListener(function(message) {
77           port.onDisconnect.addListener(chrome.test.callback(
78               function() {},
79               "Native host has exited."));
80         });
81
82         // Send first message that should stop the host.
83         port.postMessage({ "stopHostTest": true });
84       }
85     ]);
86 });