1 // Copyright 2014 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.
5 // Tests launched by extensions/renderer/api/serial/data_receiver_unittest.cc
7 var test = require('test').binding;
8 var unittestBindings = require('test_environment_specific_bindings');
10 var fatalErrorValue = 2;
13 function createReceiver() {
15 requireAsync('content/public/renderer/service_provider'),
16 requireAsync('data_receiver'),
17 requireAsync('device/serial/data_stream.mojom'),
18 ]).then(test.callbackPass(function(modules) {
19 var serviceProvider = modules[0];
20 var dataReceiver = modules[1];
21 var dataStream = modules[2];
22 return new dataReceiver.DataReceiver(
23 serviceProvider.connectToService(dataStream.DataSourceProxy.NAME_),
29 unittestBindings.exportTests([
30 function testReceive() {
31 createReceiver().then(test.callbackPass(function(receiver) {
32 receiver.receive().then(test.callbackPass(function(data) {
33 test.assertEq(1, data.byteLength);
34 test.assertEq('a'.charCodeAt(0), new Int8Array(data)[0]);
38 receiver.receive, receiver, [], 'Receive already in progress.');
42 function testReceiveError() {
43 createReceiver().then(test.callbackPass(function(receiver) {
44 receiver.receive().catch(test.callbackPass(function(error) {
45 test.assertEq(1, error.error);
51 function testReceiveDataAndError() {
52 createReceiver().then(test.callbackPass(function(receiver) {
53 receiver.receive().then(test.callbackPass(function(data) {
54 test.assertEq(1, data.byteLength);
55 test.assertEq('a'.charCodeAt(0), new Int8Array(data)[0]);
56 return receiver.receive();
57 })).catch(test.callbackPass(function(error) {
58 test.assertEq(1, error.error);
59 return receiver.receive();
60 })).then(test.callbackPass(function(data) {
61 test.assertEq(1, data.byteLength);
62 test.assertEq('b'.charCodeAt(0), new Int8Array(data)[0]);
68 function testReceiveErrorThenData() {
69 createReceiver().then(test.callbackPass(function(receiver) {
70 receiver.receive().catch(test.callbackPass(function(error) {
71 test.assertEq(1, error.error);
72 return receiver.receive();
73 })).then(test.callbackPass(function(data) {
74 test.assertEq(1, data.byteLength);
75 test.assertEq('a'.charCodeAt(0), new Int8Array(data)[0]);
81 function testSourceShutdown() {
82 createReceiver().then(test.callbackPass(function(receiver) {
83 receiver.receive().catch(test.callbackPass(function(error) {
84 test.assertEq(fatalErrorValue, error.error);
90 ], test.runTests, exports);