1 // Copyright 2015 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.
6 * Unit tests for the JS stash client.
8 * They are launched by extensions/renderer/mojo/stash_client_unittest.cc
11 var test = require('test').binding;
12 var unittestBindings = require('test_environment_specific_bindings');
13 var utils = require('utils');
15 var modulesPromise = Promise.all([
16 requireAsync('stash_client'),
17 requireAsync('extensions/common/mojo/stash.mojom'),
18 requireAsync('mojo/public/js/core'),
21 function writeAndClose(core, handle) {
22 var data = new Int8Array(1);
23 data[0] = 'a'.charCodeAt(0);
24 var result = core.writeData(handle, data, core.WRITE_DATA_FLAG_NONE);
25 test.assertEq(core.RESULT_OK, result.result);
26 test.assertEq(1, result.numBytes);
30 function readAndClose(core, handle) {
31 var result = core.readData(handle, core.READ_DATA_FLAG_NONE);
32 test.assertEq(core.RESULT_OK, result.result);
34 test.assertEq(1, result.buffer.byteLength);
35 test.assertEq('a'.charCodeAt(0), new Int8Array(result.buffer)[0]);
38 unittestBindings.exportTests([
39 function testStash() {
40 modulesPromise.then(function(modules) {
41 var stashClient = modules[0];
42 var stashMojom = modules[1];
43 var core = modules[2];
44 stashClient.registerClient('test', stashMojom.StashedObject, function() {
45 var stashedObject = new stashMojom.StashedObject();
46 stashedObject.id = 'test id';
47 stashedObject.data = [1, 2, 3, 4];
48 var dataPipe = core.createDataPipe({
49 flags: core.CREATE_DATA_PIPE_OPTIONS_FLAG_NONE,
53 writeAndClose(core, dataPipe.producerHandle);
54 stashedObject.stashed_handles = [dataPipe.consumerHandle];
55 return [{serialization: stashedObject, monitorHandles: true}];
57 }).then(test.succeed, test.fail);
60 function testRetrieve() {
61 modulesPromise.then(function(modules) {
62 var stashClient = modules[0];
63 var stashMojom = modules[1];
64 var core = modules[2];
65 return stashClient.retrieve('test', stashMojom.StashedObject)
66 .then(function(stashedObjects) {
67 test.assertEq(1, stashedObjects.length);
68 test.assertEq('test id', stashedObjects[0].id);
69 test.assertEq([1, 2, 3, 4], stashedObjects[0].data);
70 test.assertEq(1, stashedObjects[0].stashed_handles.length);
71 readAndClose(core, stashedObjects[0].stashed_handles[0]);
73 }).then(test.succeed, test.fail);
76 ], test.runTests, exports);