OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 import 'dart:mojo_core'; | 5 import 'dart:mojo_core'; |
6 import 'dart:typed_data'; | 6 import 'dart:typed_data'; |
7 | 7 |
8 import 'package:mojo/dart/testing/expect.dart'; | 8 import 'package:mojo/dart/testing/expect.dart'; |
9 | 9 |
10 | 10 |
11 invalidHandleTest() { | 11 invalidHandleTest() { |
12 RawMojoHandle invalidHandle = new RawMojoHandle(RawMojoHandle.INVALID); | 12 RawMojoHandle invalidHandle = new RawMojoHandle(RawMojoHandle.INVALID); |
13 | 13 |
14 // Close. | 14 // Close. |
15 MojoResult result = invalidHandle.close(); | 15 MojoResult result = invalidHandle.close(); |
16 Expect.isTrue(result.isInvalidArgument); | 16 Expect.isTrue(result.isInvalidArgument); |
17 | 17 |
18 // Wait. | 18 // Wait. |
19 result = invalidHandle.wait(MojoHandleSignals.READWRITE, 1000000); | 19 result = invalidHandle.wait(MojoHandleSignals.kReadWrite, 1000000); |
20 Expect.isTrue(result.isInvalidArgument); | 20 Expect.isTrue(result.isInvalidArgument); |
21 | 21 |
22 int res = RawMojoHandle.waitMany([invalidHandle.h], | 22 int res = RawMojoHandle.waitMany([invalidHandle.h], |
23 [MojoHandleSignals.READWRITE], | 23 [MojoHandleSignals.kReadWrite], |
24 RawMojoHandle.DEADLINE_INDEFINITE); | 24 RawMojoHandle.DEADLINE_INDEFINITE); |
25 Expect.equals(res, MojoResult.kInvalidArgument); | 25 Expect.equals(res, MojoResult.kInvalidArgument); |
26 | 26 |
27 // Message pipe. | 27 // Message pipe. |
28 MojoMessagePipe pipe = new MojoMessagePipe(); | 28 MojoMessagePipe pipe = new MojoMessagePipe(); |
29 Expect.isNotNull(pipe); | 29 Expect.isNotNull(pipe); |
30 ByteData bd = new ByteData(10); | 30 ByteData bd = new ByteData(10); |
31 pipe.endpoints[0].handle.close(); | 31 pipe.endpoints[0].handle.close(); |
32 pipe.endpoints[1].handle.close(); | 32 pipe.endpoints[1].handle.close(); |
33 result = pipe.endpoints[0].write(bd); | 33 result = pipe.endpoints[0].write(bd); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 Expect.isNotNull(pipe); | 80 Expect.isNotNull(pipe); |
81 Expect.isTrue(pipe.status.isOk); | 81 Expect.isTrue(pipe.status.isOk); |
82 Expect.isNotNull(pipe.endpoints); | 82 Expect.isNotNull(pipe.endpoints); |
83 | 83 |
84 MojoMessagePipeEndpoint end0 = pipe.endpoints[0]; | 84 MojoMessagePipeEndpoint end0 = pipe.endpoints[0]; |
85 MojoMessagePipeEndpoint end1 = pipe.endpoints[1]; | 85 MojoMessagePipeEndpoint end1 = pipe.endpoints[1]; |
86 Expect.isTrue(end0.handle.isValid); | 86 Expect.isTrue(end0.handle.isValid); |
87 Expect.isTrue(end1.handle.isValid); | 87 Expect.isTrue(end1.handle.isValid); |
88 | 88 |
89 // Not readable, yet. | 89 // Not readable, yet. |
90 MojoResult result = end0.handle.wait(MojoHandleSignals.READABLE, 0); | 90 MojoResult result = end0.handle.wait(MojoHandleSignals.kReadable, 0); |
91 Expect.isTrue(result.isDeadlineExceeded); | 91 Expect.isTrue(result.isDeadlineExceeded); |
92 | 92 |
93 // Should be writable. | 93 // Should be writable. |
94 result = end0.handle.wait(MojoHandleSignals.WRITABLE, 0); | 94 result = end0.handle.wait(MojoHandleSignals.kWritable, 0); |
95 Expect.isTrue(result.isOk); | 95 Expect.isTrue(result.isOk); |
96 | 96 |
97 // Try to read. | 97 // Try to read. |
98 ByteData data = new ByteData(10); | 98 ByteData data = new ByteData(10); |
99 end0.read(data); | 99 end0.read(data); |
100 Expect.isTrue(end0.status.isShouldWait); | 100 Expect.isTrue(end0.status.isShouldWait); |
101 | 101 |
102 // Write end1. | 102 // Write end1. |
103 String hello = "hello"; | 103 String hello = "hello"; |
104 ByteData helloData = | 104 ByteData helloData = |
105 new ByteData.view((new Uint8List.fromList(hello.codeUnits)).buffer); | 105 new ByteData.view((new Uint8List.fromList(hello.codeUnits)).buffer); |
106 result = end1.write(helloData); | 106 result = end1.write(helloData); |
107 Expect.isTrue(result.isOk); | 107 Expect.isTrue(result.isOk); |
108 | 108 |
109 // end0 should now be readable. | 109 // end0 should now be readable. |
110 int res = RawMojoHandle.waitMany([end0.handle.h], | 110 int res = RawMojoHandle.waitMany([end0.handle.h], |
111 [MojoHandleSignals.READABLE], | 111 [MojoHandleSignals.kReadable], |
112 RawMojoHandle.DEADLINE_INDEFINITE); | 112 RawMojoHandle.DEADLINE_INDEFINITE); |
113 Expect.equals(res, MojoResult.kOk); | 113 Expect.equals(res, MojoResult.kOk); |
114 | 114 |
115 // Read from end0. | 115 // Read from end0. |
116 MojoMessagePipeReadResult readResult = end0.read(data); | 116 MojoMessagePipeReadResult readResult = end0.read(data); |
117 Expect.isNotNull(readResult); | 117 Expect.isNotNull(readResult); |
118 Expect.isTrue(readResult.status.isOk); | 118 Expect.isTrue(readResult.status.isOk); |
119 Expect.equals(readResult.bytesRead, helloData.lengthInBytes); | 119 Expect.equals(readResult.bytesRead, helloData.lengthInBytes); |
120 Expect.equals(readResult.handlesRead, 0); | 120 Expect.equals(readResult.handlesRead, 0); |
121 | 121 |
122 String hello_result = new String.fromCharCodes( | 122 String hello_result = new String.fromCharCodes( |
123 data.buffer.asUint8List().sublist(0, readResult.bytesRead).toList()); | 123 data.buffer.asUint8List().sublist(0, readResult.bytesRead).toList()); |
124 Expect.equals(hello_result, "hello"); | 124 Expect.equals(hello_result, "hello"); |
125 | 125 |
126 // end0 should no longer be readable. | 126 // end0 should no longer be readable. |
127 result = end0.handle.wait(MojoHandleSignals.READABLE, 10); | 127 result = end0.handle.wait(MojoHandleSignals.kReadable, 10); |
128 Expect.isTrue(result.isDeadlineExceeded); | 128 Expect.isTrue(result.isDeadlineExceeded); |
129 | 129 |
130 // Close end0's handle. | 130 // Close end0's handle. |
131 result = end0.handle.close(); | 131 result = end0.handle.close(); |
132 Expect.isTrue(result.isOk); | 132 Expect.isTrue(result.isOk); |
133 | 133 |
134 // end1 should no longer be readable or writable. | 134 // end1 should no longer be readable or writable. |
135 result = end1.handle.wait(MojoHandleSignals.READWRITE, 1000); | 135 result = end1.handle.wait(MojoHandleSignals.kReadWrite, 1000); |
136 Expect.isTrue(result.isFailedPrecondition); | 136 Expect.isTrue(result.isFailedPrecondition); |
137 | 137 |
138 result = end1.handle.close(); | 138 result = end1.handle.close(); |
139 Expect.isTrue(result.isOk); | 139 Expect.isTrue(result.isOk); |
140 } | 140 } |
141 | 141 |
142 | 142 |
143 basicDataPipeTest() { | 143 basicDataPipeTest() { |
144 MojoDataPipe pipe = new MojoDataPipe(); | 144 MojoDataPipe pipe = new MojoDataPipe(); |
145 Expect.isNotNull(pipe); | 145 Expect.isNotNull(pipe); |
146 Expect.isTrue(pipe.status.isOk); | 146 Expect.isTrue(pipe.status.isOk); |
147 Expect.isTrue(pipe.consumer.handle.isValid); | 147 Expect.isTrue(pipe.consumer.handle.isValid); |
148 Expect.isTrue(pipe.producer.handle.isValid); | 148 Expect.isTrue(pipe.producer.handle.isValid); |
149 | 149 |
150 MojoDataPipeProducer producer = pipe.producer; | 150 MojoDataPipeProducer producer = pipe.producer; |
151 MojoDataPipeConsumer consumer = pipe.consumer; | 151 MojoDataPipeConsumer consumer = pipe.consumer; |
152 Expect.isTrue(producer.handle.isValid); | 152 Expect.isTrue(producer.handle.isValid); |
153 Expect.isTrue(consumer.handle.isValid); | 153 Expect.isTrue(consumer.handle.isValid); |
154 | 154 |
155 // Consumer should not be readable. | 155 // Consumer should not be readable. |
156 MojoResult result = consumer.handle.wait(MojoHandleSignals.READABLE, 0); | 156 MojoResult result = consumer.handle.wait(MojoHandleSignals.kReadable, 0); |
157 Expect.isTrue(result.isDeadlineExceeded); | 157 Expect.isTrue(result.isDeadlineExceeded); |
158 | 158 |
159 // Producer should be writable. | 159 // Producer should be writable. |
160 result = producer.handle.wait(MojoHandleSignals.WRITABLE, 0); | 160 result = producer.handle.wait(MojoHandleSignals.kWritable, 0); |
161 Expect.isTrue(result.isOk); | 161 Expect.isTrue(result.isOk); |
162 | 162 |
163 // Try to read from consumer. | 163 // Try to read from consumer. |
164 ByteData buffer = new ByteData(20); | 164 ByteData buffer = new ByteData(20); |
165 consumer.read(buffer, buffer.lengthInBytes, MojoDataPipeConsumer.FLAG_NONE); | 165 consumer.read(buffer, buffer.lengthInBytes, MojoDataPipeConsumer.FLAG_NONE); |
166 Expect.isTrue(consumer.status.isShouldWait); | 166 Expect.isTrue(consumer.status.isShouldWait); |
167 | 167 |
168 // Try to begin a two-phase read from consumer. | 168 // Try to begin a two-phase read from consumer. |
169 ByteData b = consumer.beginRead(20, MojoDataPipeConsumer.FLAG_NONE); | 169 ByteData b = consumer.beginRead(20, MojoDataPipeConsumer.FLAG_NONE); |
170 Expect.isNull(b); | 170 Expect.isNull(b); |
171 Expect.isTrue(consumer.status.isShouldWait); | 171 Expect.isTrue(consumer.status.isShouldWait); |
172 | 172 |
173 // Write to producer. | 173 // Write to producer. |
174 String hello = "hello "; | 174 String hello = "hello "; |
175 ByteData helloData = | 175 ByteData helloData = |
176 new ByteData.view((new Uint8List.fromList(hello.codeUnits)).buffer); | 176 new ByteData.view((new Uint8List.fromList(hello.codeUnits)).buffer); |
177 int written = producer.write( | 177 int written = producer.write( |
178 helloData, helloData.lengthInBytes, MojoDataPipeProducer.FLAG_NONE); | 178 helloData, helloData.lengthInBytes, MojoDataPipeProducer.FLAG_NONE); |
179 Expect.isTrue(producer.status.isOk); | 179 Expect.isTrue(producer.status.isOk); |
180 Expect.equals(written, helloData.lengthInBytes); | 180 Expect.equals(written, helloData.lengthInBytes); |
181 | 181 |
182 // Now that we have written, the consumer should be readable. | 182 // Now that we have written, the consumer should be readable. |
183 int res = RawMojoHandle.waitMany([consumer.handle.h], | 183 int res = RawMojoHandle.waitMany([consumer.handle.h], |
184 [MojoHandleSignals.READABLE], | 184 [MojoHandleSignals.kReadable], |
185 RawMojoHandle.DEADLINE_INDEFINITE); | 185 RawMojoHandle.DEADLINE_INDEFINITE); |
186 Expect.equals(res, MojoResult.kOk); | 186 Expect.equals(res, MojoResult.kOk); |
187 | 187 |
188 // Do a two-phase write to the producer. | 188 // Do a two-phase write to the producer. |
189 ByteData twoPhaseWrite = producer.beginWrite( | 189 ByteData twoPhaseWrite = producer.beginWrite( |
190 20, MojoDataPipeProducer.FLAG_NONE); | 190 20, MojoDataPipeProducer.FLAG_NONE); |
191 Expect.isTrue(producer.status.isOk); | 191 Expect.isTrue(producer.status.isOk); |
192 Expect.isNotNull(twoPhaseWrite); | 192 Expect.isNotNull(twoPhaseWrite); |
193 Expect.isTrue(twoPhaseWrite.lengthInBytes >= 20); | 193 Expect.isTrue(twoPhaseWrite.lengthInBytes >= 20); |
194 | 194 |
195 String world = "world"; | 195 String world = "world"; |
196 twoPhaseWrite.buffer.asUint8List().setAll(0, world.codeUnits); | 196 twoPhaseWrite.buffer.asUint8List().setAll(0, world.codeUnits); |
197 producer.endWrite(Uint8List.BYTES_PER_ELEMENT * world.codeUnits.length); | 197 producer.endWrite(Uint8List.BYTES_PER_ELEMENT * world.codeUnits.length); |
198 Expect.isTrue(producer.status.isOk); | 198 Expect.isTrue(producer.status.isOk); |
199 | 199 |
200 // Read one character from consumer. | 200 // Read one character from consumer. |
201 int read = consumer.read(buffer, 1, MojoDataPipeConsumer.FLAG_NONE); | 201 int read = consumer.read(buffer, 1, MojoDataPipeConsumer.FLAG_NONE); |
202 Expect.isTrue(consumer.status.isOk); | 202 Expect.isTrue(consumer.status.isOk); |
203 Expect.equals(read, 1); | 203 Expect.equals(read, 1); |
204 | 204 |
205 // Close the producer. | 205 // Close the producer. |
206 result = producer.handle.close(); | 206 result = producer.handle.close(); |
207 Expect.isTrue(result.isOk); | 207 Expect.isTrue(result.isOk); |
208 | 208 |
209 // Consumer should still be readable. | 209 // Consumer should still be readable. |
210 result = consumer.handle.wait(MojoHandleSignals.READABLE, 0); | 210 result = consumer.handle.wait(MojoHandleSignals.kReadable, 0); |
211 Expect.isTrue(result.isOk); | 211 Expect.isTrue(result.isOk); |
212 | 212 |
213 // Get the number of remaining bytes. | 213 // Get the number of remaining bytes. |
214 int remaining = consumer.read( | 214 int remaining = consumer.read( |
215 null, 0, MojoDataPipeConsumer.FLAG_QUERY); | 215 null, 0, MojoDataPipeConsumer.FLAG_QUERY); |
216 Expect.isTrue(consumer.status.isOk); | 216 Expect.isTrue(consumer.status.isOk); |
217 Expect.equals(remaining, "hello world".length - 1); | 217 Expect.equals(remaining, "hello world".length - 1); |
218 | 218 |
219 // Do a two-phase read. | 219 // Do a two-phase read. |
220 ByteData twoPhaseRead = consumer.beginRead( | 220 ByteData twoPhaseRead = consumer.beginRead( |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 duplicate = null; | 298 duplicate = null; |
299 } | 299 } |
300 | 300 |
301 | 301 |
302 main() { | 302 main() { |
303 invalidHandleTest(); | 303 invalidHandleTest(); |
304 basicMessagePipeTest(); | 304 basicMessagePipeTest(); |
305 basicDataPipeTest(); | 305 basicDataPipeTest(); |
306 basicSharedBufferTest(); | 306 basicSharedBufferTest(); |
307 } | 307 } |
OLD | NEW |