summaryrefslogtreecommitdiffstats
path: root/extensions/test/data/stash_client_unittest.js
blob: fc498f889380edb177444c0f7e3b7e8ea13aad39 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2015 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 JS stash client.
 *
 * They are launched by extensions/renderer/mojo/stash_client_unittest.cc
 */

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

var modulesPromise = Promise.all([
  requireAsync('stash_client'),
  requireAsync('extensions/common/mojo/stash.mojom'),
  requireAsync('mojo/public/js/core'),
]);

function writeAndClose(core, handle) {
  var data = new Int8Array(1);
  data[0] = 'a'.charCodeAt(0);
  var result = core.writeData(handle, data, core.WRITE_DATA_FLAG_NONE);
  test.assertEq(core.RESULT_OK, result.result);
  test.assertEq(1, result.numBytes);
  core.close(handle);
}

function readAndClose(core, handle) {
  var result = core.readData(handle, core.READ_DATA_FLAG_NONE);
  test.assertEq(core.RESULT_OK, result.result);
  core.close(handle);
  test.assertEq(1, result.buffer.byteLength);
  test.assertEq('a'.charCodeAt(0), new Int8Array(result.buffer)[0]);
}

unittestBindings.exportTests([
  function testStash() {
    modulesPromise.then(function(modules) {
      var stashClient = modules[0];
      var stashMojom = modules[1];
      var core = modules[2];
      stashClient.registerClient('test', stashMojom.StashedObject, function() {
        var stashedObject = new stashMojom.StashedObject();
        stashedObject.id = 'test id';
        stashedObject.data = [1, 2, 3, 4];
        var dataPipe = core.createDataPipe({
          flags: core.CREATE_DATA_PIPE_OPTIONS_FLAG_NONE,
          elementNumBytes: 1,
          capacityNumBytes: 10,
        });
        writeAndClose(core, dataPipe.producerHandle);
        stashedObject.stashed_handles = [dataPipe.consumerHandle];
        return [{serialization: stashedObject, monitorHandles: true}];
      });
      stashClient.saveStashForTesting();
    }).then(test.succeed, test.fail);
  },

  function testRetrieve() {
    modulesPromise.then(function(modules) {
      var stashClient = modules[0];
      var stashMojom = modules[1];
      var core = modules[2];
      return stashClient.retrieve('test', stashMojom.StashedObject)
          .then(function(stashedObjects) {
        test.assertEq(1, stashedObjects.length);
        test.assertEq('test id', stashedObjects[0].id);
        test.assertEq([1, 2, 3, 4], stashedObjects[0].data);
        test.assertEq(1, stashedObjects[0].stashed_handles.length);
        readAndClose(core, stashedObjects[0].stashed_handles[0]);
      });
    }).then(test.succeed, test.fail);
  },

], test.runTests, exports);