| 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 import 'dart:async'; | |
| 6 import 'dart:isolate'; | |
| 7 import 'dart:typed_data'; | |
| 8 import 'dart:convert'; | |
| 9 | |
| 10 import 'package:mojo/bindings.dart' as bindings; | |
| 11 import 'package:mojo/core.dart' as core; | |
| 12 import 'package:_mojo_for_test_only/expect.dart'; | |
| 13 import 'package:_mojo_for_test_only/mojo/test/rect.mojom.dart' as rect; | |
| 14 import 'package:_mojo_for_test_only/mojo/test/serialization_test_structs.mojom.d
art' | |
| 15 as serialization; | |
| 16 import 'package:_mojo_for_test_only/mojo/test/test_structs.mojom.dart' | |
| 17 as structs; | |
| 18 import 'package:_mojo_for_test_only/mojo/test/test_unions.mojom.dart' | |
| 19 as unions; | |
| 20 import 'package:_mojo_for_test_only/regression_tests/regression_tests.mojom.dart
' | |
| 21 as regression; | |
| 22 import 'package:_mojo_for_test_only/sample/sample_interfaces.mojom.dart' | |
| 23 as sample; | |
| 24 | |
| 25 class ProviderImpl implements sample.Provider { | |
| 26 sample.ProviderStub _stub; | |
| 27 | |
| 28 ProviderImpl(core.MojoMessagePipeEndpoint endpoint) { | |
| 29 _stub = new sample.ProviderStub.fromEndpoint(endpoint, this); | |
| 30 } | |
| 31 | |
| 32 echoString(String a, Function responseFactory) => | |
| 33 new Future.value(responseFactory(a)); | |
| 34 | |
| 35 echoStrings(String a, String b, Function responseFactory) => | |
| 36 new Future.value(responseFactory(a, b)); | |
| 37 | |
| 38 echoMessagePipeHanlde(core.MojoHandle a, Function responseFactory) => | |
| 39 new Future.value(responseFactory(a)); | |
| 40 | |
| 41 echoEnum(sample.Enum a, Function responseFactory) => | |
| 42 new Future.value(responseFactory(a)); | |
| 43 } | |
| 44 | |
| 45 void providerIsolate(core.MojoMessagePipeEndpoint endpoint) { | |
| 46 new ProviderImpl(endpoint); | |
| 47 } | |
| 48 | |
| 49 Future<bool> testCallResponse() { | |
| 50 var pipe = new core.MojoMessagePipe(); | |
| 51 var client = new sample.ProviderProxy.fromEndpoint(pipe.endpoints[0]); | |
| 52 var c = new Completer(); | |
| 53 Isolate.spawn(providerIsolate, pipe.endpoints[1]).then((_) { | |
| 54 client.ptr.echoString("hello!").then((echoStringResponse) { | |
| 55 Expect.equals("hello!", echoStringResponse.a); | |
| 56 }).then((_) { | |
| 57 client.ptr.echoStrings("hello", "mojo!").then((echoStringsResponse) { | |
| 58 Expect.equals("hello", echoStringsResponse.a); | |
| 59 Expect.equals("mojo!", echoStringsResponse.b); | |
| 60 client.close().then((_) { | |
| 61 c.complete(true); | |
| 62 }); | |
| 63 }); | |
| 64 }); | |
| 65 }); | |
| 66 return c.future; | |
| 67 } | |
| 68 | |
| 69 Future testAwaitCallResponse() async { | |
| 70 var pipe = new core.MojoMessagePipe(); | |
| 71 var client = new sample.ProviderProxy.fromEndpoint(pipe.endpoints[0]); | |
| 72 var isolate = await Isolate.spawn(providerIsolate, pipe.endpoints[1]); | |
| 73 | |
| 74 var echoStringResponse = await client.ptr.echoString("hello!"); | |
| 75 Expect.equals("hello!", echoStringResponse.a); | |
| 76 | |
| 77 var echoStringsResponse = await client.ptr.echoStrings("hello", "mojo!"); | |
| 78 Expect.equals("hello", echoStringsResponse.a); | |
| 79 Expect.equals("mojo!", echoStringsResponse.b); | |
| 80 | |
| 81 await client.close(); | |
| 82 } | |
| 83 | |
| 84 bindings.ServiceMessage messageOfStruct(bindings.Struct s) => | |
| 85 s.serializeWithHeader(new bindings.MessageHeader(0)); | |
| 86 | |
| 87 testSerializeNamedRegion() { | |
| 88 var r = new rect.Rect() | |
| 89 ..x = 1 | |
| 90 ..y = 2 | |
| 91 ..width = 3 | |
| 92 ..height = 4; | |
| 93 var namedRegion = new structs.NamedRegion() | |
| 94 ..name = "name" | |
| 95 ..rects = [r]; | |
| 96 var message = messageOfStruct(namedRegion); | |
| 97 var namedRegion2 = structs.NamedRegion.deserialize(message.payload); | |
| 98 Expect.equals(namedRegion.name, namedRegion2.name); | |
| 99 } | |
| 100 | |
| 101 testSerializeArrayValueTypes() { | |
| 102 var arrayValues = new structs.ArrayValueTypes() | |
| 103 ..f0 = [0, 1, -1, 0x7f, -0x10] | |
| 104 ..f1 = [0, 1, -1, 0x7fff, -0x1000] | |
| 105 ..f2 = [0, 1, -1, 0x7fffffff, -0x10000000] | |
| 106 ..f3 = [0, 1, -1, 0x7fffffffffffffff, -0x1000000000000000] | |
| 107 ..f4 = [0.0, 1.0, -1.0, 4.0e9, -4.0e9] | |
| 108 ..f5 = [0.0, 1.0, -1.0, 4.0e9, -4.0e9]; | |
| 109 var message = messageOfStruct(arrayValues); | |
| 110 var arrayValues2 = structs.ArrayValueTypes.deserialize(message.payload); | |
| 111 Expect.listEquals(arrayValues.f0, arrayValues2.f0); | |
| 112 Expect.listEquals(arrayValues.f1, arrayValues2.f1); | |
| 113 Expect.listEquals(arrayValues.f2, arrayValues2.f2); | |
| 114 Expect.listEquals(arrayValues.f3, arrayValues2.f3); | |
| 115 Expect.listEquals(arrayValues.f4, arrayValues2.f4); | |
| 116 Expect.listEquals(arrayValues.f5, arrayValues2.f5); | |
| 117 } | |
| 118 | |
| 119 testSerializeToJSON() { | |
| 120 var r = new rect.Rect() | |
| 121 ..x = 1 | |
| 122 ..y = 2 | |
| 123 ..width = 3 | |
| 124 ..height = 4; | |
| 125 | |
| 126 var encodedRect = JSON.encode(r); | |
| 127 var goldenEncoding = "{\"x\":1,\"y\":2,\"width\":3,\"height\":4}"; | |
| 128 Expect.equals(goldenEncoding, encodedRect); | |
| 129 } | |
| 130 | |
| 131 testSerializeHandleToJSON() { | |
| 132 var s = new serialization.Struct2(); | |
| 133 | |
| 134 Expect.throws( | |
| 135 () => JSON.encode(s), (e) => e.cause is bindings.MojoCodecError); | |
| 136 } | |
| 137 | |
| 138 testSerializeKeywordStruct() { | |
| 139 var keywordStruct = new structs.DartKeywordStruct() | |
| 140 ..await_ = structs.DartKeywordStructKeywords.await_ | |
| 141 ..is_ = structs.DartKeywordStructKeywords.is_ | |
| 142 ..rethrow_ = structs.DartKeywordStructKeywords.rethrow_; | |
| 143 var message = messageOfStruct(keywordStruct); | |
| 144 var decodedStruct = structs.DartKeywordStruct.deserialize(message.payload); | |
| 145 Expect.equals(keywordStruct.await_, decodedStruct.await_); | |
| 146 Expect.equals(keywordStruct.is_, decodedStruct.is_); | |
| 147 Expect.equals(keywordStruct.rethrow_, decodedStruct.rethrow_); | |
| 148 } | |
| 149 | |
| 150 testSerializeStructs() { | |
| 151 testSerializeNamedRegion(); | |
| 152 testSerializeArrayValueTypes(); | |
| 153 testSerializeToJSON(); | |
| 154 testSerializeHandleToJSON(); | |
| 155 testSerializeKeywordStruct(); | |
| 156 } | |
| 157 | |
| 158 testSerializePodUnions() { | |
| 159 var s = new unions.WrapperStruct()..podUnion = new unions.PodUnion(); | |
| 160 s.podUnion.fUint32 = 32; | |
| 161 | |
| 162 Expect.equals(unions.PodUnionTag.fUint32, s.podUnion.tag); | |
| 163 Expect.equals(32, s.podUnion.fUint32); | |
| 164 | |
| 165 var message = messageOfStruct(s); | |
| 166 var s2 = unions.WrapperStruct.deserialize(message.payload); | |
| 167 | |
| 168 Expect.equals(s.podUnion.fUint32, s2.podUnion.fUint32); | |
| 169 } | |
| 170 | |
| 171 testSerializeStructInUnion() { | |
| 172 var s = new unions.WrapperStruct()..objectUnion = new unions.ObjectUnion(); | |
| 173 s.objectUnion.fDummy = new unions.DummyStruct()..fInt8 = 8; | |
| 174 | |
| 175 var message = messageOfStruct(s); | |
| 176 var s2 = unions.WrapperStruct.deserialize(message.payload); | |
| 177 | |
| 178 Expect.equals(s.objectUnion.fDummy.fInt8, s2.objectUnion.fDummy.fInt8); | |
| 179 } | |
| 180 | |
| 181 testSerializeArrayInUnion() { | |
| 182 var s = new unions.WrapperStruct()..objectUnion = new unions.ObjectUnion(); | |
| 183 s.objectUnion.fArrayInt8 = [1, 2, 3]; | |
| 184 | |
| 185 var message = messageOfStruct(s); | |
| 186 var s2 = unions.WrapperStruct.deserialize(message.payload); | |
| 187 | |
| 188 Expect.listEquals(s.objectUnion.fArrayInt8, s2.objectUnion.fArrayInt8); | |
| 189 } | |
| 190 | |
| 191 testSerializeMapInUnion() { | |
| 192 var s = new unions.WrapperStruct()..objectUnion = new unions.ObjectUnion(); | |
| 193 s.objectUnion.fMapInt8 = {"one": 1, "two": 2,}; | |
| 194 | |
| 195 var message = messageOfStruct(s); | |
| 196 var s2 = unions.WrapperStruct.deserialize(message.payload); | |
| 197 | |
| 198 Expect.equals(1, s.objectUnion.fMapInt8["one"]); | |
| 199 Expect.equals(2, s.objectUnion.fMapInt8["two"]); | |
| 200 } | |
| 201 | |
| 202 testSerializeUnionInArray() { | |
| 203 var s = new unions.SmallStruct() | |
| 204 ..podUnionArray = [ | |
| 205 new unions.PodUnion()..fUint16 = 16, | |
| 206 new unions.PodUnion()..fUint32 = 32, | |
| 207 ]; | |
| 208 | |
| 209 var message = messageOfStruct(s); | |
| 210 | |
| 211 var s2 = unions.SmallStruct.deserialize(message.payload); | |
| 212 | |
| 213 Expect.equals(16, s2.podUnionArray[0].fUint16); | |
| 214 Expect.equals(32, s2.podUnionArray[1].fUint32); | |
| 215 } | |
| 216 | |
| 217 testSerializeUnionInMap() { | |
| 218 var s = new unions.SmallStruct() | |
| 219 ..podUnionMap = { | |
| 220 'one': new unions.PodUnion()..fUint16 = 16, | |
| 221 'two': new unions.PodUnion()..fUint32 = 32, | |
| 222 }; | |
| 223 | |
| 224 var message = messageOfStruct(s); | |
| 225 | |
| 226 var s2 = unions.SmallStruct.deserialize(message.payload); | |
| 227 | |
| 228 Expect.equals(16, s2.podUnionMap['one'].fUint16); | |
| 229 Expect.equals(32, s2.podUnionMap['two'].fUint32); | |
| 230 } | |
| 231 | |
| 232 testSerializeUnionInUnion() { | |
| 233 var s = new unions.WrapperStruct()..objectUnion = new unions.ObjectUnion(); | |
| 234 s.objectUnion.fPodUnion = new unions.PodUnion()..fUint32 = 32; | |
| 235 | |
| 236 var message = messageOfStruct(s); | |
| 237 var s2 = unions.WrapperStruct.deserialize(message.payload); | |
| 238 | |
| 239 Expect.equals(32, s2.objectUnion.fPodUnion.fUint32); | |
| 240 } | |
| 241 | |
| 242 testUnionsToString() { | |
| 243 var podUnion = new unions.PodUnion(); | |
| 244 podUnion.fUint32 = 32; | |
| 245 Expect.equals("PodUnion(fUint32: 32)", podUnion.toString()); | |
| 246 } | |
| 247 | |
| 248 testUnions() { | |
| 249 testSerializePodUnions(); | |
| 250 testSerializeStructInUnion(); | |
| 251 testSerializeArrayInUnion(); | |
| 252 testSerializeMapInUnion(); | |
| 253 testSerializeUnionInArray(); | |
| 254 testSerializeUnionInMap(); | |
| 255 testSerializeUnionInUnion(); | |
| 256 testUnionsToString(); | |
| 257 } | |
| 258 | |
| 259 class CheckEnumCapsImpl implements regression.CheckEnumCaps { | |
| 260 regression.CheckEnumCapsStub _stub; | |
| 261 | |
| 262 CheckEnumCapsImpl(core.MojoMessagePipeEndpoint endpoint) { | |
| 263 _stub = new regression.CheckEnumCapsStub.fromEndpoint(endpoint, this); | |
| 264 } | |
| 265 | |
| 266 setEnumWithInternalAllCaps(regression.EnumWithInternalAllCaps e) {} | |
| 267 } | |
| 268 | |
| 269 checkEnumCapsIsolate(core.MojoMessagePipeEndpoint endpoint) { | |
| 270 new CheckEnumCapsImpl(endpoint); | |
| 271 } | |
| 272 | |
| 273 testCheckEnumCapsImpl() { | |
| 274 var pipe = new core.MojoMessagePipe(); | |
| 275 var client = | |
| 276 new regression.CheckEnumCapsProxy.fromEndpoint(pipe.endpoints[0]); | |
| 277 var c = new Completer(); | |
| 278 Isolate.spawn(checkEnumCapsIsolate, pipe.endpoints[1]).then((_) { | |
| 279 client.ptr.setEnumWithInternalAllCaps( | |
| 280 regression.EnumWithInternalAllCaps.standard); | |
| 281 client.close().then((_) { | |
| 282 c.complete(null); | |
| 283 }); | |
| 284 }); | |
| 285 return c.future; | |
| 286 } | |
| 287 | |
| 288 testSerializeEnum() { | |
| 289 var constants = new structs.ScopedConstants(); | |
| 290 constants.f4 = structs.ScopedConstantsEType.e0; | |
| 291 var message = messageOfStruct(constants); | |
| 292 var constants2 = structs.ScopedConstants.deserialize(message.payload); | |
| 293 Expect.equals(constants.f4, constants2.f4); | |
| 294 } | |
| 295 | |
| 296 testEnums() async { | |
| 297 testSerializeEnum(); | |
| 298 await testCheckEnumCapsImpl(); | |
| 299 } | |
| 300 | |
| 301 void closingProviderIsolate(core.MojoMessagePipeEndpoint endpoint) { | |
| 302 var provider = new ProviderImpl(endpoint); | |
| 303 provider._stub.close(); | |
| 304 } | |
| 305 | |
| 306 Future<bool> runOnClosedTest() { | |
| 307 var testCompleter = new Completer(); | |
| 308 var pipe = new core.MojoMessagePipe(); | |
| 309 var proxy = new sample.ProviderProxy.fromEndpoint(pipe.endpoints[0]); | |
| 310 proxy.impl.onError = (_) => testCompleter.complete(true); | |
| 311 Isolate.spawn(closingProviderIsolate, pipe.endpoints[1]); | |
| 312 return testCompleter.future.then((b) { | |
| 313 Expect.isTrue(b); | |
| 314 }); | |
| 315 } | |
| 316 | |
| 317 class Regression551Impl implements regression.Regression551 { | |
| 318 regression.Regression551Stub _stub; | |
| 319 | |
| 320 Regression551Impl(core.MojoMessagePipeEndpoint endpoint) { | |
| 321 _stub = new regression.Regression551Stub.fromEndpoint(endpoint, this); | |
| 322 } | |
| 323 | |
| 324 dynamic get(List<String> keyPrefixes, Function responseFactory) => | |
| 325 responseFactory(0); | |
| 326 } | |
| 327 | |
| 328 void regression551Isolate(core.MojoMessagePipeEndpoint endpoint) { | |
| 329 new Regression551Impl(endpoint); | |
| 330 } | |
| 331 | |
| 332 Future<bool> testRegression551() { | |
| 333 var pipe = new core.MojoMessagePipe(); | |
| 334 var client = new regression.Regression551Proxy.fromEndpoint(pipe.endpoints[0])
; | |
| 335 var c = new Completer(); | |
| 336 Isolate.spawn(regression551Isolate, pipe.endpoints[1]).then((_) { | |
| 337 client.ptr.get(["hello!"]).then((response) { | |
| 338 Expect.equals(0, response.result); | |
| 339 client.close().then((_) { | |
| 340 c.complete(true); | |
| 341 }); | |
| 342 }); | |
| 343 }); | |
| 344 return c.future; | |
| 345 } | |
| 346 | |
| 347 class ServiceNameImpl implements regression.ServiceName { | |
| 348 regression.ServiceNameStub _stub; | |
| 349 | |
| 350 ServiceNameImpl(core.MojoMessagePipeEndpoint endpoint) { | |
| 351 _stub = new regression.ServiceNameStub.fromEndpoint(endpoint, this); | |
| 352 } | |
| 353 | |
| 354 dynamic serviceName_(Function responseFactory) => | |
| 355 responseFactory(ServiceName.serviceName); | |
| 356 } | |
| 357 | |
| 358 void serviceNameIsolate(core.MojoMessagePipeEndpoint endpoint) { | |
| 359 new ServiceNameImpl(endpoint); | |
| 360 } | |
| 361 | |
| 362 Future<bool> testServiceName() { | |
| 363 var pipe = new core.MojoMessagePipe(); | |
| 364 var client = new regression.ServiceNameProxy.fromEndpoint(pipe.endpoints[0]); | |
| 365 var c = new Completer(); | |
| 366 Isolate.spawn(serviceNameIsolate, pipe.endpoints[1]).then((_) { | |
| 367 client.ptr.serviceName_().then((response) { | |
| 368 Expect.equals(ServiceName.serviceName, response.serviceName_); | |
| 369 client.close().then((_) { | |
| 370 c.complete(true); | |
| 371 }); | |
| 372 }); | |
| 373 }); | |
| 374 return c.future; | |
| 375 } | |
| 376 | |
| 377 | |
| 378 testCamelCase() { | |
| 379 var e = CamelCaseTestEnum.boolThing; | |
| 380 e = CamelCaseTestEnum.doubleThing; | |
| 381 e = CamelCaseTestEnum.floatThing; | |
| 382 e = CamelCaseTestEnum.int8Thing; | |
| 383 e = CamelCaseTestEnum.int16Thing; | |
| 384 e = CamelCaseTestEnum.int32Th1Ng; | |
| 385 e = CamelCaseTestEnum.int64Th1ng; | |
| 386 e = CamelCaseTestEnum.uint8TH1ng; | |
| 387 e = CamelCaseTestEnum.uint16tH1Ng; | |
| 388 e = CamelCaseTestEnum.uint32Th1ng; | |
| 389 e = CamelCaseTestEnum.uint64Th1Ng; | |
| 390 } | |
| 391 | |
| 392 | |
| 393 main() async { | |
| 394 testSerializeStructs(); | |
| 395 testUnions(); | |
| 396 await testEnums(); | |
| 397 await testCallResponse(); | |
| 398 await testAwaitCallResponse(); | |
| 399 await runOnClosedTest(); | |
| 400 await testRegression551(); | |
| 401 await testServiceName(); | |
| 402 testCamelCase(); | |
| 403 } | |
| OLD | NEW |