toolkit/components/osfile/tests/mochi/worker_test_osfile_comms.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/osfile/tests/mochi/worker_test_osfile_comms.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,141 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +"use strict";
     1.8 +
     1.9 +importScripts('worker_test_osfile_shared.js');
    1.10 +
    1.11 +// The set of samples for communications test. Declare as a global
    1.12 +// variable to prevent this from being garbage-collected too early.
    1.13 +let samples;
    1.14 +
    1.15 +self.onmessage = function(msg) {
    1.16 +  info("Initializing");
    1.17 +  self.onmessage = function on_unexpected_message(msg) {
    1.18 +    throw new Error("Unexpected message " + JSON.stringify(msg.data));
    1.19 +  };
    1.20 +  importScripts("resource://gre/modules/osfile.jsm");
    1.21 +  info("Initialization complete");
    1.22 +
    1.23 +  samples = [
    1.24 +    { typename: "OS.Shared.Type.char.in_ptr",
    1.25 +      valuedescr: "String",
    1.26 +      value: "This is a test",
    1.27 +      type: OS.Shared.Type.char.in_ptr,
    1.28 +      check: function check_string(candidate, prefix) {
    1.29 +        is(candidate, "This is a test", prefix);
    1.30 +      }},
    1.31 +    { typename: "OS.Shared.Type.char.in_ptr",
    1.32 +      valuedescr: "Typed array",
    1.33 +      value: (function() {
    1.34 +                let view = new Uint8Array(15);
    1.35 +                for (let i = 0; i < 15; ++i) {
    1.36 +                  view[i] = i;
    1.37 +                }
    1.38 +                return view;
    1.39 +              })(),
    1.40 +      type: OS.Shared.Type.char.in_ptr,
    1.41 +      check: function check_ArrayBuffer(candidate, prefix) {
    1.42 +        let cast = ctypes.cast(candidate, ctypes.uint8_t.ptr);
    1.43 +        for (let i = 0; i < 15; ++i) {
    1.44 +          is(cast.contents, i % 256, prefix + "Checking that the contents of the ArrayBuffer were preserved");
    1.45 +          cast = cast.increment();
    1.46 +        }
    1.47 +      }},
    1.48 +    { typename: "OS.Shared.Type.char.in_ptr",
    1.49 +      valuedescr: "Pointer",
    1.50 +      value: new OS.Shared.Type.char.in_ptr.implementation(1),
    1.51 +      type: OS.Shared.Type.char.in_ptr,
    1.52 +      check: function check_ptr(candidate, prefix) {
    1.53 +        let address = ctypes.cast(candidate, ctypes.uintptr_t).value.toString();
    1.54 +        is(address, "1", prefix + "Checking that the pointer address was preserved");
    1.55 +      }},
    1.56 +    { typename: "OS.Shared.Type.char.in_ptr",
    1.57 +      valuedescr: "C array",
    1.58 +      value: (function() {
    1.59 +                let buf = new (ctypes.ArrayType(ctypes.uint8_t, 15))();
    1.60 +                for (let i = 0; i < 15; ++i) {
    1.61 +                  buf[i] = i % 256;
    1.62 +                }
    1.63 +                return buf;
    1.64 +              })(),
    1.65 +      type: OS.Shared.Type.char.in_ptr,
    1.66 +      check: function check_array(candidate, prefix) {
    1.67 +        let cast = ctypes.cast(candidate, ctypes.uint8_t.ptr);
    1.68 +        for (let i = 0; i < 15; ++i) {
    1.69 +          is(cast.contents, i % 256, prefix + "Checking that the contents of the C array were preserved, index " + i);
    1.70 +          cast = cast.increment();
    1.71 +        }
    1.72 +      }
    1.73 +    },
    1.74 +    { typename: "OS.File.Error",
    1.75 +      valuedescr: "OS Error",
    1.76 +      type: OS.File.Error,
    1.77 +      value: new OS.File.Error("foo", 1),
    1.78 +      check: function check_error(candidate, prefix) {
    1.79 +        ok(candidate instanceof OS.File.Error,
    1.80 +          prefix + "Error is an OS.File.Error");
    1.81 +        ok(candidate.unixErrno == 1 || candidate.winLastError == 1,
    1.82 +           prefix + "Error code is correct");
    1.83 +        try {
    1.84 +          let string = candidate.toString();
    1.85 +          info(prefix + ".toString() works " + string);
    1.86 +        } catch (x) {
    1.87 +          ok(false, prefix + ".toString() fails " + x);
    1.88 +        }
    1.89 +      }
    1.90 +    }
    1.91 +  ];
    1.92 +  samples.forEach(function test(sample) {
    1.93 +    let type = sample.type;
    1.94 +    let value = sample.value;
    1.95 +    let check = sample.check;
    1.96 +    info("Testing handling of type " + sample.typename + " communicating " + sample.valuedescr);
    1.97 +
    1.98 +    // 1. Test serialization
    1.99 +    let serialized;
   1.100 +    let exn;
   1.101 +    try {
   1.102 +      serialized = type.toMsg(value);
   1.103 +    } catch (ex) {
   1.104 +      exn = ex;
   1.105 +    }
   1.106 +    is(exn, null, "Can I serialize the following value? " + value +
   1.107 +      " aka " + JSON.stringify(value));
   1.108 +    if (exn) {
   1.109 +      return;
   1.110 +    }
   1.111 +
   1.112 +    // 2. Test deserialization
   1.113 +    let deserialized;
   1.114 +    try {
   1.115 +      deserialized = type.fromMsg(serialized);
   1.116 +    } catch (ex) {
   1.117 +      exn = ex;
   1.118 +    }
   1.119 +    is(exn, null, "Can I deserialize the following message? " + serialized
   1.120 +     + " aka " + JSON.stringify(serialized));
   1.121 +    if (exn) {
   1.122 +      return;
   1.123 +    }
   1.124 +
   1.125 +    // 3. Local test deserialized value
   1.126 +    info("Running test on deserialized value " + serialized);
   1.127 +    check(deserialized, "Local test: ");
   1.128 +
   1.129 +    // 4. Test sending serialized
   1.130 +    info("Attempting to send message");
   1.131 +    try {
   1.132 +      self.postMessage({kind:"value",
   1.133 +        typename: sample.typename,
   1.134 +        value: serialized,
   1.135 +        check: check.toSource()});
   1.136 +    } catch (ex) {
   1.137 +      exn = ex;
   1.138 +    }
   1.139 +    is(exn, null, "Can I send the following message? " + serialized
   1.140 +     + " aka " + JSON.stringify(serialized));
   1.141 +  });
   1.142 +
   1.143 +  finish();
   1.144 + };

mercurial