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 5define([ 6 "gin/test/expect", 7 "mojo/public/js/codec", 8 "mojo/public/interfaces/bindings/tests/rect.mojom", 9 "mojo/public/interfaces/bindings/tests/sample_service.mojom", 10 "mojo/public/interfaces/bindings/tests/test_structs.mojom", 11 ], function(expect, codec, rect, sample, structs) { 12 testBar(); 13 testFoo(); 14 testNamedRegion(); 15 testTypes(); 16 testAlign(); 17 testUtf8(); 18 testTypedPointerValidation(); 19 this.result = "PASS"; 20 21 function testBar() { 22 var bar = new sample.Bar(); 23 bar.alpha = 1; 24 bar.beta = 2; 25 bar.gamma = 3; 26 bar.type = 0x08070605; 27 bar.extraProperty = "banana"; 28 29 var messageName = 42; 30 var payloadSize = sample.Bar.encodedSize; 31 32 var builder = new codec.MessageBuilder(messageName, payloadSize); 33 builder.encodeStruct(sample.Bar, bar); 34 35 var message = builder.finish(); 36 37 var expectedMemory = new Uint8Array([ 38 24, 0, 0, 0, 39 0, 0, 0, 0, 40 0, 0, 0, 0, 41 42, 0, 0, 0, 42 0, 0, 0, 0, 43 0, 0, 0, 0, 44 45 16, 0, 0, 0, 46 0, 0, 0, 0, 47 48 1, 2, 3, 0, 49 5, 6, 7, 8, 50 ]); 51 52 var actualMemory = new Uint8Array(message.buffer.arrayBuffer); 53 expect(actualMemory).toEqual(expectedMemory); 54 55 var reader = new codec.MessageReader(message); 56 57 expect(reader.payloadSize).toBe(payloadSize); 58 expect(reader.messageName).toBe(messageName); 59 60 var bar2 = reader.decodeStruct(sample.Bar); 61 62 expect(bar2.alpha).toBe(bar.alpha); 63 expect(bar2.beta).toBe(bar.beta); 64 expect(bar2.gamma).toBe(bar.gamma); 65 expect("extraProperty" in bar2).toBeFalsy(); 66 } 67 68 function testFoo() { 69 var foo = new sample.Foo(); 70 foo.x = 0x212B4D5; 71 foo.y = 0x16E93; 72 foo.a = 1; 73 foo.b = 0; 74 foo.c = 3; // This will get truncated to one bit. 75 foo.bar = new sample.Bar(); 76 foo.bar.alpha = 91; 77 foo.bar.beta = 82; 78 foo.bar.gamma = 73; 79 foo.data = [ 80 4, 5, 6, 7, 8, 81 ]; 82 foo.extra_bars = [ 83 new sample.Bar(), new sample.Bar(), new sample.Bar(), 84 ]; 85 for (var i = 0; i < foo.extra_bars.length; ++i) { 86 foo.extra_bars[i].alpha = 1 * i; 87 foo.extra_bars[i].beta = 2 * i; 88 foo.extra_bars[i].gamma = 3 * i; 89 } 90 foo.name = "I am a banana"; 91 // This is supposed to be a handle, but we fake it with an integer. 92 foo.source = 23423782; 93 foo.array_of_array_of_bools = [ 94 [true], [false, true] 95 ]; 96 foo.array_of_bools = [ 97 true, false, true, false, true, false, true, true 98 ]; 99 100 101 var messageName = 31; 102 var payloadSize = 304; 103 104 var builder = new codec.MessageBuilder(messageName, payloadSize); 105 builder.encodeStruct(sample.Foo, foo); 106 107 var message = builder.finish(); 108 109 var expectedMemory = new Uint8Array([ 110 /* 0: */ 24, 0, 0, 0, 0, 0, 0, 0, 111 /* 8: */ 0, 0, 0, 0, 31, 0, 0, 0, 112 /* 16: */ 0, 0, 0, 0, 0, 0, 0, 0, 113 /* 24: */ 96, 0, 0, 0, 0, 0, 0, 0, 114 /* 32: */ 0xD5, 0xB4, 0x12, 0x02, 0x93, 0x6E, 0x01, 0, 115 /* 40: */ 5, 0, 0, 0, 0, 0, 0, 0, 116 /* 48: */ 72, 0, 0, 0, 0, 0, 0, 0, 117 ]); 118 // TODO(abarth): Test more of the message's raw memory. 119 var actualMemory = new Uint8Array(message.buffer.arrayBuffer, 120 0, expectedMemory.length); 121 expect(actualMemory).toEqual(expectedMemory); 122 123 var expectedHandles = [ 124 23423782, 125 ]; 126 127 expect(message.handles).toEqual(expectedHandles); 128 129 var reader = new codec.MessageReader(message); 130 131 expect(reader.payloadSize).toBe(payloadSize); 132 expect(reader.messageName).toBe(messageName); 133 134 var foo2 = reader.decodeStruct(sample.Foo); 135 136 expect(foo2.x).toBe(foo.x); 137 expect(foo2.y).toBe(foo.y); 138 139 expect(foo2.a).toBe(foo.a & 1 ? true : false); 140 expect(foo2.b).toBe(foo.b & 1 ? true : false); 141 expect(foo2.c).toBe(foo.c & 1 ? true : false); 142 143 expect(foo2.bar).toEqual(foo.bar); 144 expect(foo2.data).toEqual(foo.data); 145 146 expect(foo2.extra_bars).toEqual(foo.extra_bars); 147 expect(foo2.name).toBe(foo.name); 148 expect(foo2.source).toEqual(foo.source); 149 150 expect(foo2.array_of_bools).toEqual(foo.array_of_bools); 151 } 152 153 function createRect(x, y, width, height) { 154 var r = new rect.Rect(); 155 r.x = x; 156 r.y = y; 157 r.width = width; 158 r.height = height; 159 return r; 160 } 161 162 // Verify that the references to the imported Rect type in test_structs.mojom 163 // are generated correctly. 164 function testNamedRegion() { 165 var r = new structs.NamedRegion(); 166 r.name = "rectangle"; 167 r.rects = new Array(createRect(1, 2, 3, 4), createRect(10, 20, 30, 40)); 168 169 var builder = new codec.MessageBuilder(1, structs.NamedRegion.encodedSize); 170 builder.encodeStruct(structs.NamedRegion, r); 171 var reader = new codec.MessageReader(builder.finish()); 172 var result = reader.decodeStruct(structs.NamedRegion); 173 174 expect(result.name).toEqual("rectangle"); 175 expect(result.rects[0]).toEqual(createRect(1, 2, 3, 4)); 176 expect(result.rects[1]).toEqual(createRect(10, 20, 30, 40)); 177 } 178 179 function testTypes() { 180 function encodeDecode(cls, input, expectedResult, encodedSize) { 181 var messageName = 42; 182 var payloadSize = encodedSize || cls.encodedSize; 183 184 var builder = new codec.MessageBuilder(messageName, payloadSize); 185 builder.encodeStruct(cls, input) 186 var message = builder.finish(); 187 188 var reader = new codec.MessageReader(message); 189 expect(reader.payloadSize).toBe(payloadSize); 190 expect(reader.messageName).toBe(messageName); 191 var result = reader.decodeStruct(cls); 192 expect(result).toEqual(expectedResult); 193 } 194 encodeDecode(codec.String, "banana", "banana", 24); 195 encodeDecode(codec.NullableString, null, null, 8); 196 encodeDecode(codec.Int8, -1, -1); 197 encodeDecode(codec.Int8, 0xff, -1); 198 encodeDecode(codec.Int16, -1, -1); 199 encodeDecode(codec.Int16, 0xff, 0xff); 200 encodeDecode(codec.Int16, 0xffff, -1); 201 encodeDecode(codec.Int32, -1, -1); 202 encodeDecode(codec.Int32, 0xffff, 0xffff); 203 encodeDecode(codec.Int32, 0xffffffff, -1); 204 encodeDecode(codec.Float, 1.0, 1.0); 205 encodeDecode(codec.Double, 1.0, 1.0); 206 } 207 208 function testAlign() { 209 var aligned = [ 210 0, // 0 211 8, // 1 212 8, // 2 213 8, // 3 214 8, // 4 215 8, // 5 216 8, // 6 217 8, // 7 218 8, // 8 219 16, // 9 220 16, // 10 221 16, // 11 222 16, // 12 223 16, // 13 224 16, // 14 225 16, // 15 226 16, // 16 227 24, // 17 228 24, // 18 229 24, // 19 230 24, // 20 231 ]; 232 for (var i = 0; i < aligned.length; ++i) 233 expect(codec.align(i)).toBe(aligned[i]); 234 } 235 236 function testUtf8() { 237 var str = "B\u03ba\u1f79"; // some UCS-2 codepoints 238 var messageName = 42; 239 var payloadSize = 24; 240 241 var builder = new codec.MessageBuilder(messageName, payloadSize); 242 var encoder = builder.createEncoder(8); 243 encoder.encodeStringPointer(str); 244 var message = builder.finish(); 245 var expectedMemory = new Uint8Array([ 246 /* 0: */ 24, 0, 0, 0, 0, 0, 0, 0, 247 /* 8: */ 0, 0, 0, 0, 42, 0, 0, 0, 248 /* 16: */ 0, 0, 0, 0, 0, 0, 0, 0, 249 /* 24: */ 8, 0, 0, 0, 0, 0, 0, 0, 250 /* 32: */ 14, 0, 0, 0, 6, 0, 0, 0, 251 /* 40: */ 0x42, 0xCE, 0xBA, 0xE1, 0xBD, 0xB9, 0, 0, 252 ]); 253 var actualMemory = new Uint8Array(message.buffer.arrayBuffer); 254 expect(actualMemory.length).toEqual(expectedMemory.length); 255 expect(actualMemory).toEqual(expectedMemory); 256 257 var reader = new codec.MessageReader(message); 258 expect(reader.payloadSize).toBe(payloadSize); 259 expect(reader.messageName).toBe(messageName); 260 var str2 = reader.decoder.decodeStringPointer(); 261 expect(str2).toEqual(str); 262 } 263 264 function testTypedPointerValidation() { 265 var encoder = new codec.MessageBuilder(42, 24).createEncoder(8); 266 function DummyClass() {}; 267 var testCases = [ 268 // method, args, invalid examples, valid examples 269 [encoder.encodeArrayPointer, [DummyClass], [75], 270 [[], null, undefined, new Uint8Array([])]], 271 [encoder.encodeStringPointer, [], [75, new String("foo")], 272 ["", "bar", null, undefined]], 273 [encoder.encodeMapPointer, [DummyClass, DummyClass], [75], 274 [new Map(), null, undefined]], 275 ]; 276 277 testCases.forEach(function(test) { 278 var method = test[0]; 279 var baseArgs = test[1]; 280 var invalidExamples = test[2]; 281 var validExamples = test[3]; 282 283 var encoder = new codec.MessageBuilder(42, 24).createEncoder(8); 284 invalidExamples.forEach(function(invalid) { 285 expect(function() { 286 method.apply(encoder, baseArgs.concat(invalid)); 287 }).toThrow(); 288 }); 289 290 validExamples.forEach(function(valid) { 291 var encoder = new codec.MessageBuilder(42, 24).createEncoder(8); 292 method.apply(encoder, baseArgs.concat(valid)); 293 }); 294 }); 295 } 296}); 297