Home | History | Annotate | Download | only in js
      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.
      4 
      5 define([
      6     "gin/test/expect",
      7     "mojo/public/js/core",
      8     "gc",
      9   ], function(expect, core, gc) {
     10 
     11   var HANDLE_SIGNAL_READWRITABLE = core.HANDLE_SIGNAL_WRITABLE |
     12                                    core.HANDLE_SIGNAL_READABLE;
     13   var HANDLE_SIGNAL_ALL = core.HANDLE_SIGNAL_WRITABLE |
     14                           core.HANDLE_SIGNAL_READABLE |
     15                           core.HANDLE_SIGNAL_PEER_CLOSED;
     16 
     17   runWithMessagePipe(testNop);
     18   runWithMessagePipe(testReadAndWriteMessage);
     19   runWithMessagePipeWithOptions(testNop);
     20   runWithMessagePipeWithOptions(testReadAndWriteMessage);
     21   runWithDataPipe(testNop);
     22   runWithDataPipe(testReadAndWriteDataPipe);
     23   runWithDataPipeWithOptions(testNop);
     24   runWithDataPipeWithOptions(testReadAndWriteDataPipe);
     25   runWithMessagePipe(testIsHandleMessagePipe);
     26   runWithDataPipe(testIsHandleDataPipe);
     27   gc.collectGarbage();  // should not crash
     28   this.result = "PASS";
     29 
     30   function runWithMessagePipe(test) {
     31     var pipe = core.createMessagePipe();
     32     expect(pipe.result).toBe(core.RESULT_OK);
     33 
     34     test(pipe);
     35 
     36     expect(core.close(pipe.handle0)).toBe(core.RESULT_OK);
     37     expect(core.close(pipe.handle1)).toBe(core.RESULT_OK);
     38   }
     39 
     40   function runWithMessagePipeWithOptions(test) {
     41     var pipe = core.createMessagePipe({
     42         flags: core.CREATE_MESSAGE_PIPE_OPTIONS_FLAG_NONE
     43     });
     44     expect(pipe.result).toBe(core.RESULT_OK);
     45 
     46     test(pipe);
     47 
     48     expect(core.close(pipe.handle0)).toBe(core.RESULT_OK);
     49     expect(core.close(pipe.handle1)).toBe(core.RESULT_OK);
     50   }
     51 
     52   function runWithDataPipe(test) {
     53     var pipe = core.createDataPipe();
     54     expect(pipe.result).toBe(core.RESULT_OK);
     55 
     56     test(pipe);
     57 
     58     expect(core.close(pipe.producerHandle)).toBe(core.RESULT_OK);
     59     expect(core.close(pipe.consumerHandle)).toBe(core.RESULT_OK);
     60   }
     61 
     62   function runWithDataPipeWithOptions(test) {
     63     var pipe = core.createDataPipe({
     64         flags: core.CREATE_DATA_PIPE_OPTIONS_FLAG_NONE,
     65         elementNumBytes: 1,
     66         capacityNumBytes: 64
     67         });
     68     expect(pipe.result).toBe(core.RESULT_OK);
     69 
     70     test(pipe);
     71 
     72     expect(core.close(pipe.producerHandle)).toBe(core.RESULT_OK);
     73     expect(core.close(pipe.consumerHandle)).toBe(core.RESULT_OK);
     74   }
     75 
     76   function testNop(pipe) {
     77   }
     78 
     79   function testReadAndWriteMessage(pipe) {
     80     var wait = core.waitMany([], [], 0);
     81     expect(wait.result).toBe(core.RESULT_INVALID_ARGUMENT);
     82     expect(wait.index).toBe(null);
     83     expect(wait.signalsState).toBe(null);
     84 
     85     wait = core.wait(pipe.handle0, core.HANDLE_SIGNAL_READABLE, 0);
     86     expect(wait.result).toBe(core.RESULT_DEADLINE_EXCEEDED);
     87     expect(wait.signalsState.satisfiedSignals).toBe(
     88            core.HANDLE_SIGNAL_WRITABLE);
     89     expect(wait.signalsState.satisfiableSignals).toBe(HANDLE_SIGNAL_ALL);
     90 
     91     wait = core.waitMany(
     92                   [pipe.handle0, pipe.handle1],
     93                   [core.HANDLE_SIGNAL_READABLE,core.HANDLE_SIGNAL_READABLE],
     94                   0);
     95     expect(wait.result).toBe(core.RESULT_DEADLINE_EXCEEDED);
     96     expect(wait.index).toBe(null);
     97     expect(wait.signalsState[0].satisfiedSignals).toBe(
     98            core.HANDLE_SIGNAL_WRITABLE);
     99     expect(wait.signalsState[0].satisfiableSignals).toBe(HANDLE_SIGNAL_ALL);
    100     expect(wait.signalsState[1].satisfiedSignals).toBe(
    101            core.HANDLE_SIGNAL_WRITABLE);
    102     expect(wait.signalsState[1].satisfiableSignals).toBe(HANDLE_SIGNAL_ALL);
    103 
    104     wait = core.wait(pipe.handle0, core.HANDLE_SIGNAL_WRITABLE, 0);
    105     expect(wait.result).toBe(core.RESULT_OK);
    106     expect(wait.signalsState.satisfiedSignals).toBe(
    107            core.HANDLE_SIGNAL_WRITABLE);
    108     expect(wait.signalsState.satisfiableSignals).toBe(HANDLE_SIGNAL_ALL);
    109 
    110     var senderData = new Uint8Array(42);
    111     for (var i = 0; i < senderData.length; ++i) {
    112       senderData[i] = i * i;
    113     }
    114 
    115     var result = core.writeMessage(
    116       pipe.handle0, senderData, [],
    117       core.WRITE_MESSAGE_FLAG_NONE);
    118 
    119     expect(result).toBe(core.RESULT_OK);
    120 
    121     wait = core.wait(pipe.handle0, core.HANDLE_SIGNAL_WRITABLE, 0);
    122     expect(wait.result).toBe(core.RESULT_OK);
    123     expect(wait.signalsState.satisfiedSignals).toBe(
    124         core.HANDLE_SIGNAL_WRITABLE);
    125     expect(wait.signalsState.satisfiableSignals).toBe(HANDLE_SIGNAL_ALL);
    126 
    127     wait = core.wait(pipe.handle1, core.HANDLE_SIGNAL_READABLE,
    128                      core.DEADLINE_INDEFINITE);
    129     expect(wait.result).toBe(core.RESULT_OK);
    130     expect(wait.signalsState.satisfiedSignals).toBe(HANDLE_SIGNAL_READWRITABLE);
    131     expect(wait.signalsState.satisfiableSignals).toBe(HANDLE_SIGNAL_ALL);
    132 
    133     var read = core.readMessage(pipe.handle1, core.READ_MESSAGE_FLAG_NONE);
    134 
    135     expect(read.result).toBe(core.RESULT_OK);
    136     expect(read.buffer.byteLength).toBe(42);
    137     expect(read.handles.length).toBe(0);
    138 
    139     var memory = new Uint8Array(read.buffer);
    140     for (var i = 0; i < memory.length; ++i)
    141       expect(memory[i]).toBe((i * i) & 0xFF);
    142   }
    143 
    144   function testReadAndWriteDataPipe(pipe) {
    145     var senderData = new Uint8Array(42);
    146     for (var i = 0; i < senderData.length; ++i) {
    147       senderData[i] = i * i;
    148     }
    149 
    150     var write = core.writeData(
    151       pipe.producerHandle, senderData,
    152       core.WRITE_DATA_FLAG_ALL_OR_NONE);
    153 
    154     expect(write.result).toBe(core.RESULT_OK);
    155     expect(write.numBytes).toBe(42);
    156 
    157     var wait = core.wait(pipe.consumerHandle, core.HANDLE_SIGNAL_READABLE,
    158                          core.DEADLINE_INDEFINITE);
    159     expect(wait.result).toBe(core.RESULT_OK);
    160     var peeked = core.readData(
    161          pipe.consumerHandle,
    162          core.READ_DATA_FLAG_PEEK | core.READ_DATA_FLAG_ALL_OR_NONE);
    163     expect(peeked.result).toBe(core.RESULT_OK);
    164     expect(peeked.buffer.byteLength).toBe(42);
    165 
    166     var peeked_memory = new Uint8Array(peeked.buffer);
    167     for (var i = 0; i < peeked_memory.length; ++i)
    168       expect(peeked_memory[i]).toBe((i * i) & 0xFF);
    169 
    170     var read = core.readData(
    171       pipe.consumerHandle, core.READ_DATA_FLAG_ALL_OR_NONE);
    172 
    173     expect(read.result).toBe(core.RESULT_OK);
    174     expect(read.buffer.byteLength).toBe(42);
    175 
    176     var memory = new Uint8Array(read.buffer);
    177     for (var i = 0; i < memory.length; ++i)
    178       expect(memory[i]).toBe((i * i) & 0xFF);
    179   }
    180 
    181   function testIsHandleMessagePipe(pipe) {
    182     expect(core.isHandle(123).toBeFalsy);
    183     expect(core.isHandle("123").toBeFalsy);
    184     expect(core.isHandle({}).toBeFalsy);
    185     expect(core.isHandle([]).toBeFalsy);
    186     expect(core.isHandle(undefined).toBeFalsy);
    187     expect(core.isHandle(pipe).toBeFalsy);
    188     expect(core.isHandle(pipe.handle0)).toBeTruthy();
    189     expect(core.isHandle(pipe.handle1)).toBeTruthy();
    190     expect(core.isHandle(null)).toBeTruthy();
    191   }
    192 
    193   function testIsHandleDataPipe(pipe) {
    194     expect(core.isHandle(pipe.consumerHandle)).toBeTruthy();
    195     expect(core.isHandle(pipe.producerHandle)).toBeTruthy();
    196   }
    197 
    198 });
    199