| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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/interfaces/bindings/tests/test_unions.mojom", | |
| 8 "mojo/public/js/codec", | |
| 9 "mojo/public/js/validator", | |
| 10 ], function(expect, | |
| 11 unions, | |
| 12 codec, | |
| 13 validator) { | |
| 14 function testConstructors() { | |
| 15 var u = new unions.PodUnion(); | |
| 16 expect(u.$data).toEqual(null); | |
| 17 expect(u.$tag).toBeUndefined(); | |
| 18 | |
| 19 u.f_uint32 = 32; | |
| 20 | |
| 21 expect(u.f_uint32).toEqual(32); | |
| 22 expect(u.$tag).toEqual(unions.PodUnion.Tags.f_uint32); | |
| 23 | |
| 24 var u = new unions.PodUnion({f_uint64: 64}); | |
| 25 expect(u.f_uint64).toEqual(64); | |
| 26 expect(u.$tag).toEqual(unions.PodUnion.Tags.f_uint64); | |
| 27 expect(function() {var v = u.f_uint32;}).toThrow(); | |
| 28 | |
| 29 expect(function() { | |
| 30 var u = new unions.PodUnion({ | |
| 31 f_uint64: 64, | |
| 32 f_uint32: 32, | |
| 33 }); | |
| 34 }).toThrow(); | |
| 35 | |
| 36 expect(function() { | |
| 37 var u = new unions.PodUnion({ foo: 64 }); }).toThrow(); | |
| 38 | |
| 39 expect(function() { | |
| 40 var u = new unions.PodUnion([1,2,3,4]); }).toThrow(); | |
| 41 } | |
| 42 | |
| 43 function structEncodeDecode(struct) { | |
| 44 var structClass = struct.constructor; | |
| 45 var builder = new codec.MessageBuilder(1234, structClass.encodedSize); | |
| 46 builder.encodeStruct(structClass, struct); | |
| 47 | |
| 48 var message = builder.finish(); | |
| 49 | |
| 50 var messageValidator = new validator.Validator(message); | |
| 51 var err = structClass.validate(messageValidator, codec.kMessageHeaderSize); | |
| 52 expect(err).toEqual(validator.validationError.NONE); | |
| 53 | |
| 54 var reader = new codec.MessageReader(message); | |
| 55 var view = reader.decoder.buffer.dataView; | |
| 56 | |
| 57 return reader.decodeStruct(structClass); | |
| 58 } | |
| 59 | |
| 60 function testBasicEncoding() { | |
| 61 var s = new unions.WrapperStruct({ | |
| 62 pod_union: new unions.PodUnion({ | |
| 63 f_uint64: 64})}); | |
| 64 | |
| 65 var decoded = structEncodeDecode(s); | |
| 66 expect(decoded).toEqual(s); | |
| 67 | |
| 68 var s = new unions.WrapperStruct({ | |
| 69 pod_union: new unions.PodUnion({ | |
| 70 f_bool : true})}); | |
| 71 | |
| 72 var decoded = structEncodeDecode(s); | |
| 73 expect(decoded.pod_union.$tag).toEqual(unions.PodUnion.Tags.f_bool); | |
| 74 // Use toEqual() instead of toBeTruthy() to make sure the field type is | |
| 75 // actually boolean. | |
| 76 expect(decoded.pod_union.f_bool).toEqual(true); | |
| 77 | |
| 78 var s = new unions.WrapperStruct({ | |
| 79 object_union: new unions.ObjectUnion({ | |
| 80 f_dummy: new unions.DummyStruct({ | |
| 81 f_int8: 8})})}); | |
| 82 | |
| 83 var decoded = structEncodeDecode(s); | |
| 84 expect(decoded).toEqual(s); | |
| 85 | |
| 86 var s = new unions.WrapperStruct({ | |
| 87 object_union: new unions.ObjectUnion({ | |
| 88 f_array_int8: [1, 2, 3]})}); | |
| 89 | |
| 90 var decoded = structEncodeDecode(s); | |
| 91 expect(decoded).toEqual(s); | |
| 92 | |
| 93 var s = new unions.WrapperStruct({ | |
| 94 object_union: new unions.ObjectUnion({ | |
| 95 f_map_int8: new Map([ | |
| 96 ["first", 1], | |
| 97 ["second", 2], | |
| 98 ])})}); | |
| 99 | |
| 100 var decoded = structEncodeDecode(s); | |
| 101 expect(decoded).toEqual(s); | |
| 102 | |
| 103 // Encoding a union with no member set is an error. | |
| 104 var s = new unions.WrapperStruct({ | |
| 105 object_union: new unions.ObjectUnion()}); | |
| 106 expect(function() { | |
| 107 structEncodeDecode(s); }).toThrow(); | |
| 108 } | |
| 109 | |
| 110 function testUnionsInArrayEncoding() { | |
| 111 var s = new unions.SmallStruct({ | |
| 112 pod_union_array: [ | |
| 113 new unions.PodUnion({f_uint32: 32}), | |
| 114 new unions.PodUnion({f_uint64: 64}), | |
| 115 ] | |
| 116 }); | |
| 117 | |
| 118 var decoded = structEncodeDecode(s); | |
| 119 expect(decoded).toEqual(s); | |
| 120 } | |
| 121 | |
| 122 function testUnionsInMapEncoding() { | |
| 123 var s = new unions.SmallStruct({ | |
| 124 pod_union_map: new Map([ | |
| 125 ["thirty-two", new unions.PodUnion({f_uint32: 32})], | |
| 126 ["sixty-four", new unions.PodUnion({f_uint64: 64})], | |
| 127 ]) | |
| 128 }); | |
| 129 | |
| 130 var decoded = structEncodeDecode(s); | |
| 131 expect(decoded).toEqual(s); | |
| 132 } | |
| 133 | |
| 134 function testNestedUnionsEncoding() { | |
| 135 var s = new unions.WrapperStruct({ | |
| 136 object_union: new unions.ObjectUnion({ | |
| 137 f_pod_union: new unions.PodUnion({f_uint32: 32}) | |
| 138 })}); | |
| 139 var decoded = structEncodeDecode(s); | |
| 140 expect(decoded).toEqual(s); | |
| 141 } | |
| 142 | |
| 143 function structValidate(struct) { | |
| 144 var structClass = struct.constructor; | |
| 145 var builder = new codec.MessageBuilder(1234, structClass.encodedSize); | |
| 146 builder.encodeStruct(structClass, struct); | |
| 147 | |
| 148 var message = builder.finish(); | |
| 149 | |
| 150 var messageValidator = new validator.Validator(message); | |
| 151 return structClass.validate(messageValidator, codec.kMessageHeaderSize); | |
| 152 } | |
| 153 | |
| 154 function testNullUnionMemberValidation() { | |
| 155 var s = new unions.WrapperStruct({ | |
| 156 object_union: new unions.ObjectUnion({ | |
| 157 f_dummy: null})}); | |
| 158 | |
| 159 var err = structValidate(s); | |
| 160 expect(err).toEqual(validator.validationError.UNEXPECTED_NULL_POINTER); | |
| 161 | |
| 162 var s = new unions.WrapperStruct({ | |
| 163 object_union: new unions.ObjectUnion({ | |
| 164 f_nullable: null})}); | |
| 165 | |
| 166 var err = structValidate(s); | |
| 167 expect(err).toEqual(validator.validationError.NONE); | |
| 168 } | |
| 169 | |
| 170 function testNullUnionValidation() { | |
| 171 var s = new unions.SmallStructNonNullableUnion({ | |
| 172 pod_union: null}); | |
| 173 | |
| 174 var err = structValidate(s); | |
| 175 expect(err).toEqual(validator.validationError.UNEXPECTED_NULL_UNION); | |
| 176 | |
| 177 var s = new unions.WrapperStruct({ | |
| 178 object_union: new unions.ObjectUnion({ | |
| 179 f_pod_union: null}) | |
| 180 }); | |
| 181 | |
| 182 var err = structValidate(s); | |
| 183 expect(err).toEqual(validator.validationError.UNEXPECTED_NULL_UNION); | |
| 184 } | |
| 185 | |
| 186 testConstructors(); | |
| 187 testBasicEncoding(); | |
| 188 testUnionsInArrayEncoding(); | |
| 189 testUnionsInMapEncoding(); | |
| 190 testNestedUnionsEncoding(); | |
| 191 testNullUnionMemberValidation(); | |
| 192 testNullUnionValidation(); | |
| 193 this.result = "PASS"; | |
| 194 }); | |
| OLD | NEW |