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 5define([ 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 object_union: new unions.ObjectUnion({ 70 f_dummy: new unions.DummyStruct({ 71 f_int8: 8})})}); 72 73 var decoded = structEncodeDecode(s); 74 expect(decoded).toEqual(s); 75 76 var s = new unions.WrapperStruct({ 77 object_union: new unions.ObjectUnion({ 78 f_array_int8: [1, 2, 3]})}); 79 80 var decoded = structEncodeDecode(s); 81 expect(decoded).toEqual(s); 82 83 var s = new unions.WrapperStruct({ 84 object_union: new unions.ObjectUnion({ 85 f_map_int8: new Map([ 86 ["first", 1], 87 ["second", 2], 88 ])})}); 89 90 var decoded = structEncodeDecode(s); 91 expect(decoded).toEqual(s); 92 93 // Encoding a union with no member set is an error. 94 var s = new unions.WrapperStruct({ 95 object_union: new unions.ObjectUnion()}); 96 expect(function() { 97 structEncodeDecode(s); }).toThrow(); 98 } 99 100 function testUnionsInArrayEncoding() { 101 var s = new unions.SmallStruct({ 102 pod_union_array: [ 103 new unions.PodUnion({f_uint32: 32}), 104 new unions.PodUnion({f_uint64: 64}), 105 ] 106 }); 107 108 var decoded = structEncodeDecode(s); 109 expect(decoded).toEqual(s); 110 } 111 112 function testUnionsInMapEncoding() { 113 var s = new unions.SmallStruct({ 114 pod_union_map: new Map([ 115 ["thirty-two", new unions.PodUnion({f_uint32: 32})], 116 ["sixty-four", new unions.PodUnion({f_uint64: 64})], 117 ]) 118 }); 119 120 var decoded = structEncodeDecode(s); 121 expect(decoded).toEqual(s); 122 } 123 124 function testNestedUnionsEncoding() { 125 var s = new unions.WrapperStruct({ 126 object_union: new unions.ObjectUnion({ 127 f_pod_union: new unions.PodUnion({f_uint32: 32}) 128 })}); 129 var decoded = structEncodeDecode(s); 130 expect(decoded).toEqual(s); 131 } 132 133 function structValidate(struct) { 134 var structClass = struct.constructor; 135 var builder = new codec.MessageBuilder(1234, structClass.encodedSize); 136 builder.encodeStruct(structClass, struct); 137 138 var message = builder.finish(); 139 140 var messageValidator = new validator.Validator(message); 141 return structClass.validate(messageValidator, codec.kMessageHeaderSize); 142 } 143 144 function testNullUnionMemberValidation() { 145 var s = new unions.WrapperStruct({ 146 object_union: new unions.ObjectUnion({ 147 f_dummy: null})}); 148 149 var err = structValidate(s); 150 expect(err).toEqual(validator.validationError.UNEXPECTED_NULL_POINTER); 151 152 var s = new unions.WrapperStruct({ 153 object_union: new unions.ObjectUnion({ 154 f_nullable: null})}); 155 156 var err = structValidate(s); 157 expect(err).toEqual(validator.validationError.NONE); 158 } 159 160 function testNullUnionValidation() { 161 var s = new unions.SmallStructNonNullableUnion({ 162 pod_union: null}); 163 164 var err = structValidate(s); 165 expect(err).toEqual(validator.validationError.UNEXPECTED_NULL_UNION); 166 167 var s = new unions.WrapperStruct({ 168 object_union: new unions.ObjectUnion({ 169 f_pod_union: null}) 170 }); 171 172 var err = structValidate(s); 173 expect(err).toEqual(validator.validationError.UNEXPECTED_NULL_UNION); 174 } 175 176 testConstructors(); 177 testBasicEncoding(); 178 testUnionsInArrayEncoding(); 179 testUnionsInMapEncoding(); 180 testNestedUnionsEncoding(); 181 testNullUnionMemberValidation(); 182 testNullUnionValidation(); 183 this.result = "PASS"; 184}); 185