| 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 | |
| 11 invalidHandleTest() { | 10 invalidHandleTest() { |
| 12 RawMojoHandle invalidHandle = new RawMojoHandle(RawMojoHandle.INVALID); | 11 MojoHandle invalidHandle = new MojoHandle(MojoHandle.INVALID); |
| 13 | 12 |
| 14 // Close. | 13 // Close. |
| 15 MojoResult result = invalidHandle.close(); | 14 MojoResult result = invalidHandle.close(); |
| 16 Expect.isTrue(result.isInvalidArgument); | 15 Expect.isTrue(result.isInvalidArgument); |
| 17 | 16 |
| 18 // Wait. | 17 // Wait. |
| 19 MojoWaitResult mwr = invalidHandle.wait(MojoHandleSignals.READWRITE, 1000000); | 18 MojoWaitResult mwr = invalidHandle.wait(MojoHandleSignals.kReadWrite, 1000000)
; |
| 20 Expect.isTrue(mwr.result.isInvalidArgument); | 19 Expect.isTrue(mwr.result.isInvalidArgument); |
| 21 | 20 |
| 22 MojoWaitManyResult mwmr = RawMojoHandle.waitMany( | 21 MojoWaitManyResult mwmr = MojoHandle.waitMany([invalidHandle.h], |
| 23 [invalidHandle.h], | 22 [MojoHandleSignals.kReadWrite], |
| 24 [MojoHandleSignals.READWRITE], | 23 MojoHandle.DEADLINE_INDEFINITE); |
| 25 RawMojoHandle.DEADLINE_INDEFINITE); | |
| 26 Expect.isTrue(mwmr.result.isInvalidArgument); | 24 Expect.isTrue(mwmr.result.isInvalidArgument); |
| 27 | 25 |
| 28 // Message pipe. | 26 // Message pipe. |
| 29 MojoMessagePipe pipe = new MojoMessagePipe(); | 27 MojoMessagePipe pipe = new MojoMessagePipe(); |
| 30 Expect.isNotNull(pipe); | 28 Expect.isNotNull(pipe); |
| 31 ByteData bd = new ByteData(10); | 29 ByteData bd = new ByteData(10); |
| 32 pipe.endpoints[0].handle.close(); | 30 pipe.endpoints[0].handle.close(); |
| 33 pipe.endpoints[1].handle.close(); | 31 pipe.endpoints[1].handle.close(); |
| 34 result = pipe.endpoints[0].write(bd); | 32 result = pipe.endpoints[0].write(bd); |
| 35 Expect.isTrue(result.isInvalidArgument); | 33 Expect.isTrue(result.isInvalidArgument); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 Expect.isNotNull(pipe); | 79 Expect.isNotNull(pipe); |
| 82 Expect.isTrue(pipe.status.isOk); | 80 Expect.isTrue(pipe.status.isOk); |
| 83 Expect.isNotNull(pipe.endpoints); | 81 Expect.isNotNull(pipe.endpoints); |
| 84 | 82 |
| 85 MojoMessagePipeEndpoint end0 = pipe.endpoints[0]; | 83 MojoMessagePipeEndpoint end0 = pipe.endpoints[0]; |
| 86 MojoMessagePipeEndpoint end1 = pipe.endpoints[1]; | 84 MojoMessagePipeEndpoint end1 = pipe.endpoints[1]; |
| 87 Expect.isTrue(end0.handle.isValid); | 85 Expect.isTrue(end0.handle.isValid); |
| 88 Expect.isTrue(end1.handle.isValid); | 86 Expect.isTrue(end1.handle.isValid); |
| 89 | 87 |
| 90 // Not readable, yet. | 88 // Not readable, yet. |
| 91 MojoWaitResult mwr = end0.handle.wait(MojoHandleSignals.READABLE, 0); | 89 MojoWaitResult mwr = end0.handle.wait(MojoHandleSignals.kReadable, 0); |
| 92 Expect.isTrue(mwr.result.isDeadlineExceeded); | 90 Expect.isTrue(mwr.result.isDeadlineExceeded); |
| 93 | 91 |
| 94 // Should be writable. | 92 // Should be writable. |
| 95 mwr = end0.handle.wait(MojoHandleSignals.WRITABLE, 0); | 93 mwr = end0.handle.wait(MojoHandleSignals.kWritable, 0); |
| 96 Expect.isTrue(mwr.result.isOk); | 94 Expect.isTrue(mwr.result.isOk); |
| 97 | 95 |
| 98 // Try to read. | 96 // Try to read. |
| 99 ByteData data = new ByteData(10); | 97 ByteData data = new ByteData(10); |
| 100 end0.read(data); | 98 end0.read(data); |
| 101 Expect.isTrue(end0.status.isShouldWait); | 99 Expect.isTrue(end0.status.isShouldWait); |
| 102 | 100 |
| 103 // Write end1. | 101 // Write end1. |
| 104 String hello = "hello"; | 102 String hello = "hello"; |
| 105 ByteData helloData = | 103 ByteData helloData = |
| 106 new ByteData.view((new Uint8List.fromList(hello.codeUnits)).buffer); | 104 new ByteData.view((new Uint8List.fromList(hello.codeUnits)).buffer); |
| 107 MojoResult result = end1.write(helloData); | 105 MojoResult result = end1.write(helloData); |
| 108 Expect.isTrue(result.isOk); | 106 Expect.isTrue(result.isOk); |
| 109 | 107 |
| 110 // end0 should now be readable. | 108 // end0 should now be readable. |
| 111 MojoWaitManyResult mwmr = RawMojoHandle.waitMany([end0.handle.h], | 109 MojoWaitManyResult mwmr = MojoHandle.waitMany([end0.handle.h], |
| 112 [MojoHandleSignals.READABLE], | 110 [MojoHandleSignals.kReadable], |
| 113 RawMojoHandle.DEADLINE_INDEFINITE); | 111 MojoHandle.DEADLINE_INDEFINITE); |
| 114 Expect.isTrue(mwmr.result.isOk); | 112 Expect.isTrue(mwmr.result.isOk); |
| 115 | 113 |
| 116 // Read from end0. | 114 // Read from end0. |
| 117 MojoMessagePipeReadResult readResult = end0.read(data); | 115 MojoMessagePipeReadResult readResult = end0.read(data); |
| 118 Expect.isNotNull(readResult); | 116 Expect.isNotNull(readResult); |
| 119 Expect.isTrue(readResult.status.isOk); | 117 Expect.isTrue(readResult.status.isOk); |
| 120 Expect.equals(readResult.bytesRead, helloData.lengthInBytes); | 118 Expect.equals(readResult.bytesRead, helloData.lengthInBytes); |
| 121 Expect.equals(readResult.handlesRead, 0); | 119 Expect.equals(readResult.handlesRead, 0); |
| 122 | 120 |
| 123 String hello_result = new String.fromCharCodes( | 121 String hello_result = new String.fromCharCodes( |
| 124 data.buffer.asUint8List().sublist(0, readResult.bytesRead).toList()); | 122 data.buffer.asUint8List().sublist(0, readResult.bytesRead).toList()); |
| 125 Expect.equals(hello_result, "hello"); | 123 Expect.equals(hello_result, "hello"); |
| 126 | 124 |
| 127 // end0 should no longer be readable. | 125 // end0 should no longer be readable. |
| 128 mwr = end0.handle.wait(MojoHandleSignals.READABLE, 10); | 126 mwr = end0.handle.wait(MojoHandleSignals.kReadable, 10); |
| 129 Expect.isTrue(mwr.result.isDeadlineExceeded); | 127 Expect.isTrue(mwr.result.isDeadlineExceeded); |
| 130 | 128 |
| 131 // Close end0's handle. | 129 // Close end0's handle. |
| 132 result = end0.handle.close(); | 130 result = end0.handle.close(); |
| 133 Expect.isTrue(result.isOk); | 131 Expect.isTrue(result.isOk); |
| 134 | 132 |
| 135 // end1 should no longer be readable or writable. | 133 // end1 should no longer be readable or writable. |
| 136 mwr = end1.handle.wait(MojoHandleSignals.READWRITE, 1000); | 134 mwr = end1.handle.wait(MojoHandleSignals.kReadWrite, 1000); |
| 137 Expect.isTrue(mwr.result.isFailedPrecondition); | 135 Expect.isTrue(mwr.result.isFailedPrecondition); |
| 138 | 136 |
| 139 result = end1.handle.close(); | 137 result = end1.handle.close(); |
| 140 Expect.isTrue(result.isOk); | 138 Expect.isTrue(result.isOk); |
| 141 } | 139 } |
| 142 | 140 |
| 143 | 141 |
| 144 basicDataPipeTest() { | 142 basicDataPipeTest() { |
| 145 MojoDataPipe pipe = new MojoDataPipe(); | 143 MojoDataPipe pipe = new MojoDataPipe(); |
| 146 Expect.isNotNull(pipe); | 144 Expect.isNotNull(pipe); |
| 147 Expect.isTrue(pipe.status.isOk); | 145 Expect.isTrue(pipe.status.isOk); |
| 148 Expect.isTrue(pipe.consumer.handle.isValid); | 146 Expect.isTrue(pipe.consumer.handle.isValid); |
| 149 Expect.isTrue(pipe.producer.handle.isValid); | 147 Expect.isTrue(pipe.producer.handle.isValid); |
| 150 | 148 |
| 151 MojoDataPipeProducer producer = pipe.producer; | 149 MojoDataPipeProducer producer = pipe.producer; |
| 152 MojoDataPipeConsumer consumer = pipe.consumer; | 150 MojoDataPipeConsumer consumer = pipe.consumer; |
| 153 Expect.isTrue(producer.handle.isValid); | 151 Expect.isTrue(producer.handle.isValid); |
| 154 Expect.isTrue(consumer.handle.isValid); | 152 Expect.isTrue(consumer.handle.isValid); |
| 155 | 153 |
| 156 // Consumer should not be readable. | 154 // Consumer should not be readable. |
| 157 MojoWaitResult mwr = consumer.handle.wait(MojoHandleSignals.READABLE, 0); | 155 MojoWaitResult mwr = consumer.handle.wait(MojoHandleSignals.kReadable, 0); |
| 158 Expect.isTrue(mwr.result.isDeadlineExceeded); | 156 Expect.isTrue(mwr.result.isDeadlineExceeded); |
| 159 | 157 |
| 160 // Producer should be writable. | 158 // Producer should be writable. |
| 161 mwr = producer.handle.wait(MojoHandleSignals.WRITABLE, 0); | 159 mwr = producer.handle.wait(MojoHandleSignals.kWritable, 0); |
| 162 Expect.isTrue(mwr.result.isOk); | 160 Expect.isTrue(mwr.result.isOk); |
| 163 | 161 |
| 164 // Try to read from consumer. | 162 // Try to read from consumer. |
| 165 ByteData buffer = new ByteData(20); | 163 ByteData buffer = new ByteData(20); |
| 166 consumer.read(buffer, buffer.lengthInBytes, MojoDataPipeConsumer.FLAG_NONE); | 164 consumer.read(buffer, buffer.lengthInBytes, MojoDataPipeConsumer.FLAG_NONE); |
| 167 Expect.isTrue(consumer.status.isShouldWait); | 165 Expect.isTrue(consumer.status.isShouldWait); |
| 168 | 166 |
| 169 // Try to begin a two-phase read from consumer. | 167 // Try to begin a two-phase read from consumer. |
| 170 ByteData b = consumer.beginRead(20, MojoDataPipeConsumer.FLAG_NONE); | 168 ByteData b = consumer.beginRead(20, MojoDataPipeConsumer.FLAG_NONE); |
| 171 Expect.isNull(b); | 169 Expect.isNull(b); |
| 172 Expect.isTrue(consumer.status.isShouldWait); | 170 Expect.isTrue(consumer.status.isShouldWait); |
| 173 | 171 |
| 174 // Write to producer. | 172 // Write to producer. |
| 175 String hello = "hello "; | 173 String hello = "hello "; |
| 176 ByteData helloData = | 174 ByteData helloData = |
| 177 new ByteData.view((new Uint8List.fromList(hello.codeUnits)).buffer); | 175 new ByteData.view((new Uint8List.fromList(hello.codeUnits)).buffer); |
| 178 int written = producer.write( | 176 int written = producer.write( |
| 179 helloData, helloData.lengthInBytes, MojoDataPipeProducer.FLAG_NONE); | 177 helloData, helloData.lengthInBytes, MojoDataPipeProducer.FLAG_NONE); |
| 180 Expect.isTrue(producer.status.isOk); | 178 Expect.isTrue(producer.status.isOk); |
| 181 Expect.equals(written, helloData.lengthInBytes); | 179 Expect.equals(written, helloData.lengthInBytes); |
| 182 | 180 |
| 183 // Now that we have written, the consumer should be readable. | 181 // Now that we have written, the consumer should be readable. |
| 184 MojoWaitManyResult mwmr = RawMojoHandle.waitMany( | 182 MojoWaitManyResult mwmr = MojoHandle.waitMany([consumer.handle.h], |
| 185 [consumer.handle.h], | 183 [MojoHandleSignals.kReadable], |
| 186 [MojoHandleSignals.READABLE], | 184 MojoHandle.DEADLINE_INDEFINITE); |
| 187 RawMojoHandle.DEADLINE_INDEFINITE); | |
| 188 Expect.isTrue(mwr.result.isOk); | 185 Expect.isTrue(mwr.result.isOk); |
| 189 | 186 |
| 190 // Do a two-phase write to the producer. | 187 // Do a two-phase write to the producer. |
| 191 ByteData twoPhaseWrite = producer.beginWrite( | 188 ByteData twoPhaseWrite = producer.beginWrite( |
| 192 20, MojoDataPipeProducer.FLAG_NONE); | 189 20, MojoDataPipeProducer.FLAG_NONE); |
| 193 Expect.isTrue(producer.status.isOk); | 190 Expect.isTrue(producer.status.isOk); |
| 194 Expect.isNotNull(twoPhaseWrite); | 191 Expect.isNotNull(twoPhaseWrite); |
| 195 Expect.isTrue(twoPhaseWrite.lengthInBytes >= 20); | 192 Expect.isTrue(twoPhaseWrite.lengthInBytes >= 20); |
| 196 | 193 |
| 197 String world = "world"; | 194 String world = "world"; |
| 198 twoPhaseWrite.buffer.asUint8List().setAll(0, world.codeUnits); | 195 twoPhaseWrite.buffer.asUint8List().setAll(0, world.codeUnits); |
| 199 producer.endWrite(Uint8List.BYTES_PER_ELEMENT * world.codeUnits.length); | 196 producer.endWrite(Uint8List.BYTES_PER_ELEMENT * world.codeUnits.length); |
| 200 Expect.isTrue(producer.status.isOk); | 197 Expect.isTrue(producer.status.isOk); |
| 201 | 198 |
| 202 // Read one character from consumer. | 199 // Read one character from consumer. |
| 203 int read = consumer.read(buffer, 1, MojoDataPipeConsumer.FLAG_NONE); | 200 int read = consumer.read(buffer, 1, MojoDataPipeConsumer.FLAG_NONE); |
| 204 Expect.isTrue(consumer.status.isOk); | 201 Expect.isTrue(consumer.status.isOk); |
| 205 Expect.equals(read, 1); | 202 Expect.equals(read, 1); |
| 206 | 203 |
| 207 // Close the producer. | 204 // Close the producer. |
| 208 MojoResult result = producer.handle.close(); | 205 MojoResult result = producer.handle.close(); |
| 209 Expect.isTrue(result.isOk); | 206 Expect.isTrue(result.isOk); |
| 210 | 207 |
| 211 // Consumer should still be readable. | 208 // Consumer should still be readable. |
| 212 mwr = consumer.handle.wait(MojoHandleSignals.READABLE, 0); | 209 mwr = consumer.handle.wait(MojoHandleSignals.kReadable, 0); |
| 213 Expect.isTrue(mwr.result.isOk); | 210 Expect.isTrue(mwr.result.isOk); |
| 214 | 211 |
| 215 // Get the number of remaining bytes. | 212 // Get the number of remaining bytes. |
| 216 int remaining = consumer.read( | 213 int remaining = consumer.read( |
| 217 null, 0, MojoDataPipeConsumer.FLAG_QUERY); | 214 null, 0, MojoDataPipeConsumer.FLAG_QUERY); |
| 218 Expect.isTrue(consumer.status.isOk); | 215 Expect.isTrue(consumer.status.isOk); |
| 219 Expect.equals(remaining, "hello world".length - 1); | 216 Expect.equals(remaining, "hello world".length - 1); |
| 220 | 217 |
| 221 // Do a two-phase read. | 218 // Do a two-phase read. |
| 222 ByteData twoPhaseRead = consumer.beginRead( | 219 ByteData twoPhaseRead = consumer.beginRead( |
| (...skipping 18 matching lines...) Expand all Loading... |
| 241 } | 238 } |
| 242 | 239 |
| 243 | 240 |
| 244 basicSharedBufferTest() { | 241 basicSharedBufferTest() { |
| 245 MojoSharedBuffer mojoBuffer = new MojoSharedBuffer( | 242 MojoSharedBuffer mojoBuffer = new MojoSharedBuffer( |
| 246 100, MojoSharedBuffer.CREATE_FLAG_NONE); | 243 100, MojoSharedBuffer.CREATE_FLAG_NONE); |
| 247 Expect.isNotNull(mojoBuffer); | 244 Expect.isNotNull(mojoBuffer); |
| 248 Expect.isNotNull(mojoBuffer.status); | 245 Expect.isNotNull(mojoBuffer.status); |
| 249 Expect.isTrue(mojoBuffer.status.isOk); | 246 Expect.isTrue(mojoBuffer.status.isOk); |
| 250 Expect.isNotNull(mojoBuffer.handle); | 247 Expect.isNotNull(mojoBuffer.handle); |
| 251 Expect.isTrue(mojoBuffer.handle is RawMojoHandle); | 248 Expect.isTrue(mojoBuffer.handle is MojoHandle); |
| 252 Expect.isTrue(mojoBuffer.handle.isValid); | 249 Expect.isTrue(mojoBuffer.handle.isValid); |
| 253 | 250 |
| 254 mojoBuffer.map(0, 100, MojoSharedBuffer.MAP_FLAG_NONE); | 251 mojoBuffer.map(0, 100, MojoSharedBuffer.MAP_FLAG_NONE); |
| 255 Expect.isNotNull(mojoBuffer.status); | 252 Expect.isNotNull(mojoBuffer.status); |
| 256 Expect.isTrue(mojoBuffer.status.isOk); | 253 Expect.isTrue(mojoBuffer.status.isOk); |
| 257 Expect.isNotNull(mojoBuffer.mapping); | 254 Expect.isNotNull(mojoBuffer.mapping); |
| 258 Expect.isTrue(mojoBuffer.mapping is ByteData); | 255 Expect.isTrue(mojoBuffer.mapping is ByteData); |
| 259 | 256 |
| 260 mojoBuffer.mapping.setInt8(50, 42); | 257 mojoBuffer.mapping.setInt8(50, 42); |
| 261 | 258 |
| 262 MojoSharedBuffer duplicate = new MojoSharedBuffer.duplicate( | 259 MojoSharedBuffer duplicate = new MojoSharedBuffer.duplicate( |
| 263 mojoBuffer, MojoSharedBuffer.DUPLICATE_FLAG_NONE); | 260 mojoBuffer, MojoSharedBuffer.DUPLICATE_FLAG_NONE); |
| 264 Expect.isNotNull(duplicate); | 261 Expect.isNotNull(duplicate); |
| 265 Expect.isNotNull(duplicate.status); | 262 Expect.isNotNull(duplicate.status); |
| 266 Expect.isTrue(duplicate.status.isOk); | 263 Expect.isTrue(duplicate.status.isOk); |
| 267 Expect.isTrue(duplicate.handle is RawMojoHandle); | 264 Expect.isTrue(duplicate.handle is MojoHandle); |
| 268 Expect.isTrue(duplicate.handle.isValid); | 265 Expect.isTrue(duplicate.handle.isValid); |
| 269 | 266 |
| 270 duplicate.map(0, 100, MojoSharedBuffer.MAP_FLAG_NONE); | 267 duplicate.map(0, 100, MojoSharedBuffer.MAP_FLAG_NONE); |
| 271 Expect.isTrue(duplicate.status.isOk); | 268 Expect.isTrue(duplicate.status.isOk); |
| 272 Expect.isNotNull(duplicate.mapping); | 269 Expect.isNotNull(duplicate.mapping); |
| 273 Expect.isTrue(duplicate.mapping is ByteData); | 270 Expect.isTrue(duplicate.mapping is ByteData); |
| 274 | 271 |
| 275 mojoBuffer.close(); | 272 mojoBuffer.close(); |
| 276 mojoBuffer = null; | 273 mojoBuffer = null; |
| 277 | 274 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 300 duplicate = null; | 297 duplicate = null; |
| 301 } | 298 } |
| 302 | 299 |
| 303 | 300 |
| 304 main() { | 301 main() { |
| 305 invalidHandleTest(); | 302 invalidHandleTest(); |
| 306 basicMessagePipeTest(); | 303 basicMessagePipeTest(); |
| 307 basicDataPipeTest(); | 304 basicDataPipeTest(); |
| 308 basicSharedBufferTest(); | 305 basicSharedBufferTest(); |
| 309 } | 306 } |
| OLD | NEW |