1 // Copyright 2013 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 "console", 7 "mojo/apps/js/test/hexdump", 8 "gin/test/expect", 9 "mojo/public/interfaces/bindings/tests/sample_service.mojom", 10 "mojo/public/interfaces/bindings/tests/sample_import.mojom", 11 "mojo/public/interfaces/bindings/tests/sample_import2.mojom", 12 ], function(console, hexdump, expect, sample, imported, imported2) { 13 14 var global = this; 15 16 // Set this variable to true to print the binary message in hex. 17 var dumpMessageAsHex = false; 18 19 function makeFoo() { 20 var bar = new sample.Bar(); 21 bar.alpha = 20; 22 bar.beta = 40; 23 bar.gamma = 60; 24 bar.type = sample.Bar.Type.VERTICAL; 25 26 var extra_bars = new Array(3); 27 for (var i = 0; i < extra_bars.length; ++i) { 28 var base = i * 100; 29 var type = i % 2 ? 30 sample.Bar.Type.VERTICAL : sample.Bar.Type.HORIZONTAL; 31 extra_bars[i] = new sample.Bar(); 32 extra_bars[i].alpha = base; 33 extra_bars[i].beta = base + 20; 34 extra_bars[i].gamma = base + 40; 35 extra_bars[i].type = type; 36 } 37 38 var data = new Array(10); 39 for (var i = 0; i < data.length; ++i) { 40 data[i] = data.length - i; 41 } 42 43 var source = 0xFFFF; // Invent a dummy handle. 44 45 var foo = new sample.Foo(); 46 foo.name = "foopy"; 47 foo.x = 1; 48 foo.y = 2; 49 foo.a = false; 50 foo.b = true; 51 foo.c = false; 52 foo.bar = bar; 53 foo.extra_bars = extra_bars; 54 foo.data = data; 55 foo.source = source; 56 return foo; 57 } 58 59 // Check that the given |Foo| is identical to the one made by |MakeFoo()|. 60 function checkFoo(foo) { 61 expect(foo.name).toBe("foopy"); 62 expect(foo.x).toBe(1); 63 expect(foo.y).toBe(2); 64 expect(foo.a).toBeFalsy(); 65 expect(foo.b).toBeTruthy(); 66 expect(foo.c).toBeFalsy(); 67 expect(foo.bar.alpha).toBe(20); 68 expect(foo.bar.beta).toBe(40); 69 expect(foo.bar.gamma).toBe(60); 70 expect(foo.bar.type).toBe(sample.Bar.Type.VERTICAL); 71 72 expect(foo.extra_bars.length).toBe(3); 73 for (var i = 0; i < foo.extra_bars.length; ++i) { 74 var base = i * 100; 75 var type = i % 2 ? 76 sample.Bar.Type.VERTICAL : sample.Bar.Type.HORIZONTAL; 77 expect(foo.extra_bars[i].alpha).toBe(base); 78 expect(foo.extra_bars[i].beta).toBe(base + 20); 79 expect(foo.extra_bars[i].gamma).toBe(base + 40); 80 expect(foo.extra_bars[i].type).toBe(type); 81 } 82 83 expect(foo.data.length).toBe(10); 84 for (var i = 0; i < foo.data.length; ++i) 85 expect(foo.data[i]).toBe(foo.data.length - i); 86 87 expect(foo.source).toBe(0xFFFF); 88 } 89 90 // Check that values are set to the defaults if we don't override them. 91 function checkDefaultValues() { 92 var bar = new sample.Bar(); 93 expect(bar.alpha).toBe(255); 94 expect(bar.type).toBe(sample.Bar.Type.VERTICAL); 95 96 var foo = new sample.Foo(); 97 expect(foo.name).toBe("Fooby"); 98 expect(foo.a).toBeTruthy(); 99 expect(foo.data).toBeNull(); 100 101 var defaults = new sample.DefaultsTest(); 102 expect(defaults.a0).toBe(-12); 103 expect(defaults.a1).toBe(sample.kTwelve); 104 expect(defaults.a2).toBe(1234); 105 expect(defaults.a3).toBe(34567); 106 expect(defaults.a4).toBe(123456); 107 expect(defaults.a5).toBe(3456789012); 108 expect(defaults.a6).toBe(-111111111111); 109 // JS doesn't have a 64 bit integer type so this is just checking that the 110 // expected and actual values have the same closest double value. 111 expect(defaults.a7).toBe(9999999999999999999); 112 expect(defaults.a8).toBe(0x12345); 113 expect(defaults.a9).toBe(-0x12345); 114 expect(defaults.a10).toBe(1234); 115 expect(defaults.a11).toBe(true); 116 expect(defaults.a12).toBe(false); 117 expect(defaults.a13).toBe(123.25); 118 expect(defaults.a14).toBe(1234567890.123); 119 expect(defaults.a15).toBe(1E10); 120 expect(defaults.a16).toBe(-1.2E+20); 121 expect(defaults.a17).toBe(1.23E-20); 122 expect(defaults.a20).toBe(sample.Bar.Type.BOTH); 123 expect(defaults.a21).toBeNull(); 124 expect(defaults.a22).toBeTruthy(); 125 expect(defaults.a22.shape).toBe(imported.Shape.RECTANGLE); 126 expect(defaults.a22.color).toBe(imported2.Color.BLACK); 127 expect(defaults.a21).toBeNull(); 128 expect(defaults.a23).toBe(0xFFFFFFFFFFFFFFFF); 129 expect(defaults.a24).toBe(0x123456789); 130 expect(defaults.a25).toBe(-0x123456789); 131 } 132 133 function ServiceImpl() { 134 } 135 136 ServiceImpl.prototype = Object.create(sample.ServiceStub.prototype); 137 138 ServiceImpl.prototype.frobinate = function(foo, baz, port) { 139 checkFoo(foo); 140 expect(baz).toBe(sample.ServiceStub.BazOptions.EXTRA); 141 expect(port).toBe(10); 142 global.result = "PASS"; 143 }; 144 145 function SimpleMessageReceiver() { 146 } 147 148 SimpleMessageReceiver.prototype.accept = function(message) { 149 if (dumpMessageAsHex) { 150 var uint8Array = new Uint8Array(message.buffer.arrayBuffer); 151 console.log(hexdump.dumpArray(uint8Array)); 152 } 153 // Imagine some IPC happened here. 154 var serviceImpl = new ServiceImpl(); 155 serviceImpl.accept(message); 156 }; 157 158 var receiver = new SimpleMessageReceiver(); 159 var serviceProxy = new sample.ServiceProxy(receiver); 160 161 checkDefaultValues(); 162 163 var foo = makeFoo(); 164 checkFoo(foo); 165 166 var port = 10; 167 serviceProxy.frobinate(foo, sample.ServiceProxy.BazOptions.EXTRA, port); 168 }); 169