summaryrefslogtreecommitdiffstats
path: root/extensions/test/data/keep_alive_client_unittest.js
blob: 7b88fb0a8ecdc4e4061555d8063ddc4b172babae (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
// 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.

/**
 * Unit tests for the keep-alive client.
 *
 * They are launched by extensions/renderer/mojo/keep_alive_client_unittest.cc.
 */

var test = require('test').binding;
var unittestBindings = require('test_environment_specific_bindings');
var utils = require('utils');

var shouldSucceed;

// We need to set custom bindings for a real API and serial.getDevices has a
// simple signature.
var binding = require('binding').Binding.create('serial');
binding.registerCustomHook(function(bindingsAPI) {
  bindingsAPI.apiFunctions.setHandleRequestWithPromise('getDevices',
      function() {
    if (shouldSucceed)
      return Promise.resolve([]);
    else
      return Promise.reject();
  });
});
var apiFunction = binding.generate().getDevices;

unittestBindings.exportTests([
  // Test that a keep alive is created and destroyed for a successful API call.
  function testKeepAliveWithSuccessfulCall() {
    shouldSucceed = true;
    utils.promise(apiFunction).then(test.succeed, test.fail);
  },

  // Test that a keep alive is created and destroyed for an unsuccessful API
  // call.
  function testKeepAliveWithError() {
    shouldSucceed = false;
    utils.promise(apiFunction).then(test.fail, test.succeed);
  },
], test.runTests, exports);