| OLD | NEW |
| (Empty) |
| 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/bindings/core", | |
| 8 "gc", | |
| 9 ], function(expect, core, gc) { | |
| 10 runWithMessagePipe(testNop); | |
| 11 runWithMessagePipe(testReadAndWriteMessage); | |
| 12 runWithMessagePipeWithOptions(testNop); | |
| 13 runWithMessagePipeWithOptions(testReadAndWriteMessage); | |
| 14 runWithDataPipe(testNop); | |
| 15 runWithDataPipe(testReadAndWriteDataPipe); | |
| 16 runWithDataPipeWithOptions(testNop); | |
| 17 runWithDataPipeWithOptions(testReadAndWriteDataPipe); | |
| 18 testHandleToString(); | |
| 19 gc.collectGarbage(); // should not crash | |
| 20 this.result = "PASS"; | |
| 21 | |
| 22 function runWithMessagePipe(test) { | |
| 23 var pipe = core.createMessagePipe(); | |
| 24 expect(pipe.result).toBe(core.RESULT_OK); | |
| 25 | |
| 26 test(pipe); | |
| 27 | |
| 28 expect(core.close(pipe.handle0)).toBe(core.RESULT_OK); | |
| 29 expect(core.close(pipe.handle1)).toBe(core.RESULT_OK); | |
| 30 } | |
| 31 | |
| 32 function runWithMessagePipeWithOptions(test) { | |
| 33 var pipe = core.createMessagePipe({ | |
| 34 flags: core.CREATE_MESSAGE_PIPE_OPTIONS_FLAG_NONE | |
| 35 }); | |
| 36 expect(pipe.result).toBe(core.RESULT_OK); | |
| 37 | |
| 38 test(pipe); | |
| 39 | |
| 40 expect(core.close(pipe.handle0)).toBe(core.RESULT_OK); | |
| 41 expect(core.close(pipe.handle1)).toBe(core.RESULT_OK); | |
| 42 } | |
| 43 | |
| 44 function runWithDataPipe(test) { | |
| 45 var pipe = core.createDataPipe(); | |
| 46 expect(pipe.result).toBe(core.RESULT_OK); | |
| 47 | |
| 48 test(pipe); | |
| 49 | |
| 50 expect(core.close(pipe.producerHandle)).toBe(core.RESULT_OK); | |
| 51 expect(core.close(pipe.consumerHandle)).toBe(core.RESULT_OK); | |
| 52 } | |
| 53 | |
| 54 function runWithDataPipeWithOptions(test) { | |
| 55 var pipe = core.createDataPipe({ | |
| 56 flags: core.CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, | |
| 57 elementNumBytes: 1, | |
| 58 capacityNumBytes: 64 | |
| 59 }); | |
| 60 expect(pipe.result).toBe(core.RESULT_OK); | |
| 61 | |
| 62 test(pipe); | |
| 63 | |
| 64 expect(core.close(pipe.producerHandle)).toBe(core.RESULT_OK); | |
| 65 expect(core.close(pipe.consumerHandle)).toBe(core.RESULT_OK); | |
| 66 } | |
| 67 | |
| 68 function testNop(pipe) { | |
| 69 } | |
| 70 | |
| 71 function testReadAndWriteMessage(pipe) { | |
| 72 var senderData = new Uint8Array(42); | |
| 73 for (var i = 0; i < senderData.length; ++i) { | |
| 74 senderData[i] = i * i; | |
| 75 } | |
| 76 | |
| 77 var result = core.writeMessage( | |
| 78 pipe.handle0, senderData, [], | |
| 79 core.WRITE_MESSAGE_FLAG_NONE); | |
| 80 | |
| 81 expect(result).toBe(core.RESULT_OK); | |
| 82 | |
| 83 var read = core.readMessage( | |
| 84 pipe.handle1, core.READ_MESSAGE_FLAG_NONE); | |
| 85 | |
| 86 expect(read.result).toBe(core.RESULT_OK); | |
| 87 expect(read.buffer.byteLength).toBe(42); | |
| 88 expect(read.handles.length).toBe(0); | |
| 89 | |
| 90 var memory = new Uint8Array(read.buffer); | |
| 91 for (var i = 0; i < memory.length; ++i) | |
| 92 expect(memory[i]).toBe((i * i) & 0xFF); | |
| 93 } | |
| 94 | |
| 95 function testReadAndWriteDataPipe(pipe) { | |
| 96 var senderData = new Uint8Array(42); | |
| 97 for (var i = 0; i < senderData.length; ++i) { | |
| 98 senderData[i] = i * i; | |
| 99 } | |
| 100 | |
| 101 var write = core.writeData( | |
| 102 pipe.producerHandle, senderData, | |
| 103 core.WRITE_DATA_FLAG_ALL_OR_NONE); | |
| 104 | |
| 105 expect(write.result).toBe(core.RESULT_OK); | |
| 106 expect(write.numBytes).toBe(42); | |
| 107 | |
| 108 var read = core.readData( | |
| 109 pipe.consumerHandle, core.READ_DATA_FLAG_ALL_OR_NONE); | |
| 110 | |
| 111 expect(read.result).toBe(core.RESULT_OK); | |
| 112 expect(read.buffer.byteLength).toBe(42); | |
| 113 | |
| 114 var memory = new Uint8Array(read.buffer); | |
| 115 for (var i = 0; i < memory.length; ++i) | |
| 116 expect(memory[i]).toBe((i * i) & 0xFF); | |
| 117 } | |
| 118 | |
| 119 function testHandleToString() { | |
| 120 var pipe = core.createDataPipe(); | |
| 121 expect(pipe.consumerHandle.toString).toBeDefined(); | |
| 122 | |
| 123 var openHandleRE = /^\[mojo\:\:Handle \d+\]$/ // e.g. "[mojo::Handle 123]" | |
| 124 var openHandleString = pipe.consumerHandle.toString(); | |
| 125 expect(openHandleString.match(openHandleRE)[0]).toEqual(openHandleString); | |
| 126 | |
| 127 expect(core.close(pipe.producerHandle)).toBe(core.RESULT_OK); | |
| 128 expect(core.close(pipe.consumerHandle)).toBe(core.RESULT_OK); | |
| 129 | |
| 130 expect(pipe.consumerHandle.toString()).toEqual("[mojo::Handle null]"); | |
| 131 } | |
| 132 | |
| 133 }); | |
| OLD | NEW |