Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(91)

Side by Side Diff: mojo/public/js/core_unittests.js

Issue 2250183003: Make the fuchsia mojo/public repo the source of truth. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/public/js/core.js ('k') | mojo/public/js/router.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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/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_DEADLINE_EXCEEDED);
82 expect(wait.index).toBe(null);
83 expect(wait.signalsState.length).toBe(0);
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.waitMany(
122 [pipe.handle0, pipe.handle1],
123 [core.HANDLE_SIGNAL_WRITABLE,core.HANDLE_SIGNAL_WRITABLE],
124 0);
125 expect(wait.result).toBe(core.RESULT_OK);
126 expect(wait.index).toBe(0);
127 expect(wait.signalsState[0].satisfiedSignals).toBe(
128 core.HANDLE_SIGNAL_WRITABLE);
129 expect(wait.signalsState[0].satisfiableSignals).toBe(HANDLE_SIGNAL_ALL);
130 expect(wait.signalsState[1].satisfiedSignals).toBe(
131 HANDLE_SIGNAL_READWRITABLE);
132 expect(wait.signalsState[1].satisfiableSignals).toBe(HANDLE_SIGNAL_ALL);
133
134 var read = core.readMessage(
135 pipe.handle1, core.READ_MESSAGE_FLAG_NONE);
136
137 expect(read.result).toBe(core.RESULT_OK);
138 expect(read.buffer.byteLength).toBe(42);
139 expect(read.handles.length).toBe(0);
140
141 var memory = new Uint8Array(read.buffer);
142 for (var i = 0; i < memory.length; ++i)
143 expect(memory[i]).toBe((i * i) & 0xFF);
144 }
145
146 function testReadAndWriteDataPipe(pipe) {
147 var senderData = new Uint8Array(42);
148 for (var i = 0; i < senderData.length; ++i) {
149 senderData[i] = i * i;
150 }
151
152 var write = core.writeData(
153 pipe.producerHandle, senderData,
154 core.WRITE_DATA_FLAG_ALL_OR_NONE);
155
156 expect(write.result).toBe(core.RESULT_OK);
157 expect(write.numBytes).toBe(42);
158
159 var peeked = core.readData(
160 pipe.consumerHandle,
161 core.READ_DATA_FLAG_PEEK | core.READ_DATA_FLAG_ALL_OR_NONE);
162 expect(peeked.result).toBe(core.RESULT_OK);
163 expect(peeked.buffer.byteLength).toBe(42);
164
165 var peeked_memory = new Uint8Array(peeked.buffer);
166 for (var i = 0; i < peeked_memory.length; ++i)
167 expect(peeked_memory[i]).toBe((i * i) & 0xFF);
168
169 var read = core.readData(
170 pipe.consumerHandle, core.READ_DATA_FLAG_ALL_OR_NONE);
171
172 expect(read.result).toBe(core.RESULT_OK);
173 expect(read.buffer.byteLength).toBe(42);
174
175 var memory = new Uint8Array(read.buffer);
176 for (var i = 0; i < memory.length; ++i)
177 expect(memory[i]).toBe((i * i) & 0xFF);
178 }
179
180 function testIsHandleMessagePipe(pipe) {
181 expect(core.isHandle(123).toBeFalsy);
182 expect(core.isHandle("123").toBeFalsy);
183 expect(core.isHandle({}).toBeFalsy);
184 expect(core.isHandle([]).toBeFalsy);
185 expect(core.isHandle(undefined).toBeFalsy);
186 expect(core.isHandle(pipe).toBeFalsy);
187 expect(core.isHandle(pipe.handle0)).toBeTruthy();
188 expect(core.isHandle(pipe.handle1)).toBeTruthy();
189 expect(core.isHandle(null)).toBeTruthy();
190 }
191
192 function testIsHandleDataPipe(pipe) {
193 expect(core.isHandle(pipe.consumerHandle)).toBeTruthy();
194 expect(core.isHandle(pipe.producerHandle)).toBeTruthy();
195 }
196
197 });
OLDNEW
« no previous file with comments | « mojo/public/js/core.js ('k') | mojo/public/js/router.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698