summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/sharedWorker_sharedWorker.js
blob: 5e8e93392b11baac0969aa75894895aaa881c994 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */
"use strict";

if (!("self" in this)) {
  throw new Error("No 'self' exists on SharedWorkerGlobalScope!");
}
if (this !== self) {
  throw new Error("'self' not equal to global object!");
}
if (!(self instanceof SharedWorkerGlobalScope)) {
  throw new Error("self not a SharedWorkerGlobalScope instance!");
}

var propsToCheck = [
  "location",
  "navigator",
  "close",
  "importScripts",
  "setTimeout",
  "clearTimeout",
  "setInterval",
  "clearInterval",
  "dump",
  "atob",
  "btoa"
];

for (var index = 0; index < propsToCheck.length; index++) {
  var prop = propsToCheck[index];
  if (!(prop in self)) {
    throw new Error("SharedWorkerGlobalScope has no '" + prop + "' property!");
  }
}

onconnect = function(event) {
  if (!("SharedWorkerGlobalScope" in self)) {
    throw new Error("SharedWorkerGlobalScope should be visible!");
  }
  if (!(self instanceof SharedWorkerGlobalScope)) {
    throw new Error("The global should be a SharedWorkerGlobalScope!");
  }
  if (!(self instanceof WorkerGlobalScope)) {
    throw new Error("The global should be a WorkerGlobalScope!");
  }
  if ("DedicatedWorkerGlobalScope" in self) {
    throw new Error("DedicatedWorkerGlobalScope should not be visible!");
  }
  if (!(event instanceof MessageEvent)) {
    throw new Error("'connect' event is not a MessageEvent!");
  }
  if (!("ports" in event)) {
    throw new Error("'connect' event doesn't have a 'ports' property!");
  }
  if (event.ports.length != 1) {
    throw new Error("'connect' event has a 'ports' property with length '" +
                    event.ports.length + "'!");
  }
  if (!event.ports[0]) {
    throw new Error("'connect' event has a null 'ports[0]' property!");
  }
  if (!(event.ports[0] instanceof MessagePort)) {
    throw new Error("'connect' event has a 'ports[0]' property that isn't a " +
                    "MessagePort!");
  }
  if (!(event.ports[0] == event.source)) {
    throw new Error("'connect' event source property is incorrect!");
  }
  if (event.data) {
    throw new Error("'connect' event has data: " + event.data);
  }

  // The expression closures should trigger a warning in debug builds, but NOT
  // fire error events at us. If we ever actually remove expression closures
  // (in bug 1083458), we'll need something else to test this case.
  (function() "Expected console warning: expression closures are deprecated");

  event.ports[0].onmessage = function(event) {
    if (!(event instanceof MessageEvent)) {
      throw new Error("'message' event is not a MessageEvent!");
    }
    if (!("ports" in event)) {
      throw new Error("'message' event doesn't have a 'ports' property!");
    }
    if (event.ports === null) {
      throw new Error("'message' event has a null 'ports' property!");
    }
    event.target.postMessage(event.data);
    throw new Error(event.data);
  };
};