content/base/test/chrome/cpows_child.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/test/chrome/cpows_child.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,108 @@
     1.4 +dump('loaded child cpow test\n');
     1.5 +
     1.6 +content.document.title = "Hello, Kitty";
     1.7 +
     1.8 +(function start() {
     1.9 +    sync_test();
    1.10 +    async_test();
    1.11 +    rpc_test();
    1.12 +    nested_sync_test();
    1.13 +    // The sync-ness of this call is important, because otherwise
    1.14 +    // we tear down the child's document while we are
    1.15 +    // still in the async test in the parent.
    1.16 +    sendSyncMessage("cpows:done", {});
    1.17 +  }
    1.18 +)();
    1.19 +
    1.20 +function ok(condition, message) {
    1.21 +  dump('condition: ' + condition  + ', ' + message + '\n');
    1.22 +  if (!condition) {
    1.23 +    sendAsyncMessage("cpows:fail", { message: message });
    1.24 +    throw 'failed check: ' + message;
    1.25 +  }
    1.26 +}
    1.27 +
    1.28 +var sync_obj;
    1.29 +var async_obj;
    1.30 +
    1.31 +function make_object()
    1.32 +{
    1.33 +  let o = { };
    1.34 +  o.i = 5;
    1.35 +  o.b = true;
    1.36 +  o.s = "hello";
    1.37 +  o.x = { i: 10 };
    1.38 +  o.f = function () { return 99; };
    1.39 +
    1.40 +  // Doing anything with this Proxy will throw.
    1.41 +  var throwing = new Proxy({}, new Proxy({}, {
    1.42 +      get: function (trap) { throw trap; }
    1.43 +    }));
    1.44 +
    1.45 +  let array = [1, 2, 3];
    1.46 +
    1.47 +  let for_json = { "n": 3, "a": array, "s": "hello", o: { "x": 10 } };
    1.48 +
    1.49 +  return { "data": o,
    1.50 +           "throwing": throwing,
    1.51 +           "document": content.document,
    1.52 +           "array": array,
    1.53 +           "for_json": for_json
    1.54 +         };
    1.55 +}
    1.56 +
    1.57 +function make_json()
    1.58 +{
    1.59 +  return { check: "ok" };
    1.60 +}
    1.61 +
    1.62 +function sync_test()
    1.63 +{
    1.64 +  dump('beginning cpow sync test\n');
    1.65 +  sync_obj = make_object();
    1.66 +  sendSyncMessage("cpows:sync",
    1.67 +    make_json(),
    1.68 +    make_object());
    1.69 +}
    1.70 +
    1.71 +function async_test()
    1.72 +{
    1.73 +  dump('beginning cpow async test\n');
    1.74 +  async_obj = make_object();
    1.75 +  sendAsyncMessage("cpows:async",
    1.76 +    make_json(),
    1.77 +    async_obj);
    1.78 +}
    1.79 +
    1.80 +function rpc_test()
    1.81 +{
    1.82 +  dump('beginning cpow rpc test\n');
    1.83 +  rpc_obj = make_object();
    1.84 +  rpc_obj.data.reenter = function  () {
    1.85 +    sendRpcMessage("cpows:reenter", { }, { data: { valid: true } });
    1.86 +    return "ok";
    1.87 +  }
    1.88 +  sendRpcMessage("cpows:rpc",
    1.89 +    make_json(),
    1.90 +    rpc_obj);
    1.91 +}
    1.92 +
    1.93 +function nested_sync_test()
    1.94 +{
    1.95 +  dump('beginning cpow nested sync test\n');
    1.96 +  sync_obj = make_object();
    1.97 +  sync_obj.data.reenter = function () {
    1.98 +    let caught = false;
    1.99 +    try {
   1.100 +       sendSyncMessage("cpows:reenter_sync", { }, { });
   1.101 +    } catch (e) {
   1.102 +      caught = true;
   1.103 +    }
   1.104 +    if (!ok(caught, "should not allow nested sync"))
   1.105 +      return "fail";
   1.106 +    return "ok";
   1.107 +  }
   1.108 +  sendSyncMessage("cpows:nested_sync",
   1.109 +    make_json(),
   1.110 +    rpc_obj);
   1.111 +}

mercurial