summaryrefslogtreecommitdiffstats
path: root/chrome/test/data/extensions/platform_apps/ephemeral_apps/messaging_sender_success/main.js
blob: 7f2643720e49f8a7b884aee6e6a9c2af4249b270 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

var targetAppId = 'idjoelgddomapgkhmbcoejocojmpjnme';

// Send a message to the ephemeral app and expect a response.
function testSendMessage() {
  chrome.runtime.sendMessage(targetAppId, 'hello', null, function(response) {
    if (response == 'ack_message')
      chrome.test.succeed();
    else
      chrome.test.fail();
  });
}

// Open a port to the ephemeral app, send a message and expect a response.
function testConnect() {
  var port = chrome.runtime.connect(targetAppId);
  var gotResponse = false;

  port.onMessage.addListener(function(response) {
    if (response == 'ack_connect_message') {
      chrome.test.succeed();
      gotResponse = true;
    } else {
      chrome.test.fail();
    }

    port.disconnect();
  });

  port.onDisconnect.addListener(function() {
    if (gotResponse)
      chrome.test.succeed();
    else
      chrome.test.fail();
  });

  port.postMessage('hello');
}

chrome.app.runtime.onLaunched.addListener(function() {
  chrome.test.sendMessage('Launched');

  chrome.test.runTests([
    testSendMessage,
    testConnect
  ]);

});