Home | History | Annotate | Download | only in sample
      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     "mojom/sample_service"
     10   ], function(console, hexdump, expect, sample) {
     11 
     12   var global = this;
     13 
     14   // Set this variable to true to print the binary message in hex.
     15   var dumpMessageAsHex = false;
     16 
     17   function makeFoo() {
     18     var bar = new sample.Bar();
     19     bar.alpha = 20;
     20     bar.beta = 40;
     21     bar.gamma = 60;
     22     bar.type = sample.BarType.BAR_VERTICAL;
     23 
     24     var extra_bars = new Array(3);
     25     for (var i = 0; i < extra_bars.length; ++i) {
     26       var base = i * 100;
     27       var type = i % 2 ?
     28           sample.BarType.BAR_VERTICAL : sample.BarType.BAR_HORIZONTAL;
     29       extra_bars[i] = new sample.Bar();
     30       extra_bars[i].alpha = base;
     31       extra_bars[i].beta = base + 20;
     32       extra_bars[i].gamma = base + 40;
     33       extra_bars[i].type = type;
     34     }
     35 
     36     var data = new Array(10);
     37     for (var i = 0; i < data.length; ++i) {
     38       data[i] = data.length - i;
     39     }
     40 
     41     var source = 0xFFFF;  // Invent a dummy handle.
     42 
     43     var foo = new sample.Foo();
     44     foo.name = "foopy";
     45     foo.x = 1;
     46     foo.y = 2;
     47     foo.a = false;
     48     foo.b = true;
     49     foo.c = false;
     50     foo.bar = bar;
     51     foo.extra_bars = extra_bars;
     52     foo.data = data;
     53     foo.source = source;
     54     return foo;
     55   }
     56 
     57   // Check that the given |Foo| is identical to the one made by |MakeFoo()|.
     58   function checkFoo(foo) {
     59     expect(foo.name).toBe("foopy");
     60     expect(foo.x).toBe(1);
     61     expect(foo.y).toBe(2);
     62     expect(foo.a).toBeFalsy();
     63     expect(foo.b).toBeTruthy();
     64     expect(foo.c).toBeFalsy();
     65     expect(foo.bar.alpha).toBe(20);
     66     expect(foo.bar.beta).toBe(40);
     67     expect(foo.bar.gamma).toBe(60);
     68     expect(foo.bar.type).toBe(sample.BarType.BAR_VERTICAL);
     69 
     70     expect(foo.extra_bars.length).toBe(3);
     71     for (var i = 0; i < foo.extra_bars.length; ++i) {
     72       var base = i * 100;
     73       var type = i % 2 ?
     74           sample.BarType.BAR_VERTICAL : sample.BarType.BAR_HORIZONTAL;
     75       expect(foo.extra_bars[i].alpha).toBe(base);
     76       expect(foo.extra_bars[i].beta).toBe(base + 20);
     77       expect(foo.extra_bars[i].gamma).toBe(base + 40);
     78       expect(foo.extra_bars[i].type).toBe(type);
     79     }
     80 
     81     expect(foo.data.length).toBe(10);
     82     for (var i = 0; i < foo.data.length; ++i)
     83       expect(foo.data[i]).toBe(foo.data.length - i);
     84 
     85     expect(foo.source).toBe(0xFFFF);
     86   }
     87 
     88   function ServiceImpl() {
     89   }
     90 
     91   ServiceImpl.prototype = Object.create(sample.ServiceStub.prototype);
     92 
     93   ServiceImpl.prototype.frobinate = function(foo, baz, port) {
     94     checkFoo(foo);
     95     expect(baz).toBeTruthy();
     96     expect(port).toBe(10);
     97     global.result = "PASS";
     98   };
     99 
    100   function SimpleMessageReceiver() {
    101   }
    102 
    103   SimpleMessageReceiver.prototype.accept = function(message) {
    104     if (dumpMessageAsHex)
    105       console.log(hexdump.dumpArray(message.memory));
    106     // Imagine some IPC happened here.
    107     var serviceImpl = new ServiceImpl();
    108     serviceImpl.accept(message);
    109   };
    110 
    111   var receiver = new SimpleMessageReceiver();
    112   var serviceProxy = new sample.ServiceProxy(receiver);
    113 
    114   var foo = makeFoo();
    115   checkFoo(foo);
    116 
    117   var port = 10;
    118   serviceProxy.frobinate(foo, true, port);
    119 });
    120