michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: "use strict"; michael@0: michael@0: importScripts('worker_test_osfile_shared.js'); michael@0: michael@0: // The set of samples for communications test. Declare as a global michael@0: // variable to prevent this from being garbage-collected too early. michael@0: let samples; michael@0: michael@0: self.onmessage = function(msg) { michael@0: info("Initializing"); michael@0: self.onmessage = function on_unexpected_message(msg) { michael@0: throw new Error("Unexpected message " + JSON.stringify(msg.data)); michael@0: }; michael@0: importScripts("resource://gre/modules/osfile.jsm"); michael@0: info("Initialization complete"); michael@0: michael@0: samples = [ michael@0: { typename: "OS.Shared.Type.char.in_ptr", michael@0: valuedescr: "String", michael@0: value: "This is a test", michael@0: type: OS.Shared.Type.char.in_ptr, michael@0: check: function check_string(candidate, prefix) { michael@0: is(candidate, "This is a test", prefix); michael@0: }}, michael@0: { typename: "OS.Shared.Type.char.in_ptr", michael@0: valuedescr: "Typed array", michael@0: value: (function() { michael@0: let view = new Uint8Array(15); michael@0: for (let i = 0; i < 15; ++i) { michael@0: view[i] = i; michael@0: } michael@0: return view; michael@0: })(), michael@0: type: OS.Shared.Type.char.in_ptr, michael@0: check: function check_ArrayBuffer(candidate, prefix) { michael@0: let cast = ctypes.cast(candidate, ctypes.uint8_t.ptr); michael@0: for (let i = 0; i < 15; ++i) { michael@0: is(cast.contents, i % 256, prefix + "Checking that the contents of the ArrayBuffer were preserved"); michael@0: cast = cast.increment(); michael@0: } michael@0: }}, michael@0: { typename: "OS.Shared.Type.char.in_ptr", michael@0: valuedescr: "Pointer", michael@0: value: new OS.Shared.Type.char.in_ptr.implementation(1), michael@0: type: OS.Shared.Type.char.in_ptr, michael@0: check: function check_ptr(candidate, prefix) { michael@0: let address = ctypes.cast(candidate, ctypes.uintptr_t).value.toString(); michael@0: is(address, "1", prefix + "Checking that the pointer address was preserved"); michael@0: }}, michael@0: { typename: "OS.Shared.Type.char.in_ptr", michael@0: valuedescr: "C array", michael@0: value: (function() { michael@0: let buf = new (ctypes.ArrayType(ctypes.uint8_t, 15))(); michael@0: for (let i = 0; i < 15; ++i) { michael@0: buf[i] = i % 256; michael@0: } michael@0: return buf; michael@0: })(), michael@0: type: OS.Shared.Type.char.in_ptr, michael@0: check: function check_array(candidate, prefix) { michael@0: let cast = ctypes.cast(candidate, ctypes.uint8_t.ptr); michael@0: for (let i = 0; i < 15; ++i) { michael@0: is(cast.contents, i % 256, prefix + "Checking that the contents of the C array were preserved, index " + i); michael@0: cast = cast.increment(); michael@0: } michael@0: } michael@0: }, michael@0: { typename: "OS.File.Error", michael@0: valuedescr: "OS Error", michael@0: type: OS.File.Error, michael@0: value: new OS.File.Error("foo", 1), michael@0: check: function check_error(candidate, prefix) { michael@0: ok(candidate instanceof OS.File.Error, michael@0: prefix + "Error is an OS.File.Error"); michael@0: ok(candidate.unixErrno == 1 || candidate.winLastError == 1, michael@0: prefix + "Error code is correct"); michael@0: try { michael@0: let string = candidate.toString(); michael@0: info(prefix + ".toString() works " + string); michael@0: } catch (x) { michael@0: ok(false, prefix + ".toString() fails " + x); michael@0: } michael@0: } michael@0: } michael@0: ]; michael@0: samples.forEach(function test(sample) { michael@0: let type = sample.type; michael@0: let value = sample.value; michael@0: let check = sample.check; michael@0: info("Testing handling of type " + sample.typename + " communicating " + sample.valuedescr); michael@0: michael@0: // 1. Test serialization michael@0: let serialized; michael@0: let exn; michael@0: try { michael@0: serialized = type.toMsg(value); michael@0: } catch (ex) { michael@0: exn = ex; michael@0: } michael@0: is(exn, null, "Can I serialize the following value? " + value + michael@0: " aka " + JSON.stringify(value)); michael@0: if (exn) { michael@0: return; michael@0: } michael@0: michael@0: // 2. Test deserialization michael@0: let deserialized; michael@0: try { michael@0: deserialized = type.fromMsg(serialized); michael@0: } catch (ex) { michael@0: exn = ex; michael@0: } michael@0: is(exn, null, "Can I deserialize the following message? " + serialized michael@0: + " aka " + JSON.stringify(serialized)); michael@0: if (exn) { michael@0: return; michael@0: } michael@0: michael@0: // 3. Local test deserialized value michael@0: info("Running test on deserialized value " + serialized); michael@0: check(deserialized, "Local test: "); michael@0: michael@0: // 4. Test sending serialized michael@0: info("Attempting to send message"); michael@0: try { michael@0: self.postMessage({kind:"value", michael@0: typename: sample.typename, michael@0: value: serialized, michael@0: check: check.toSource()}); michael@0: } catch (ex) { michael@0: exn = ex; michael@0: } michael@0: is(exn, null, "Can I send the following message? " + serialized michael@0: + " aka " + JSON.stringify(serialized)); michael@0: }); michael@0: michael@0: finish(); michael@0: };