summaryrefslogtreecommitdiffstats
path: root/dom/broadcastchannel/tests/test_broadcastchannel_any.html
blob: 2225e7cad9a622ec70d06df6233f98ad08032b77 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for BroadcastChannel</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>

<div id="content"></div>

<script type="application/javascript">

var tests = [
 'hello world',
 123,
 null,
 true,
 new Date(),
 [ 1, 'test', true, new Date() ],
 { a: true, b:  null, c: new Date(), d: [ true, false, {} ] },
 new Blob([123], { type: 'plain/text' })
];

var currentTest = null;

function getType(a) {
  if (a === null || a === undefined)
    return 'null';

  if (Array.isArray(a))
    return 'array';

  if (typeof a == 'object')
    return 'object';

  return 'primitive';
}

function compare(a, b) {
  is (getType(a), getType(b), 'Type matches');

  var type = getType(a);
  if (type == 'array') {
    is (a.length, b.length, 'Array.length matches');
    for (var i = 0; i < a.length; ++i) {
      compare(a[i], b[i]);
    }

    return;
  }

  if (type == 'object') {
    ok (a !== b, 'They should not match');

    var aProps = [];
    for (var p in a) aProps.push(p);

    var bProps = [];
    for (var p in b) bProps.push(p);

    is (aProps.length, bProps.length, 'Props match');
    is (aProps.sort().toSource(), bProps.sort().toSource(), 'Props match - using toSource()');

    for (var p in a) {
      compare(a[p], b[p]);
    }

    return;
  }

  if (type != 'null') {
    is (a.toSource(), b.toSource(), 'Matching using toSource()');
  }
}

function runTest() {
  var count = 2;

  var bc = new BroadcastChannel("foobar");
  ok(bc, "BroadcastChannel can be created");

  bc.onmessage = function(event) {
    ok(count < 2, "Still comparing...");
    info("bc: " + currentTest);
    compare(event.data, currentTest);
    ++count;
    next();
  }

  var bc2 = new BroadcastChannel("foobar");
  ok(bc2, "BroadcastChannel can be created");

  var toSkip = true;
  bc2.onmessage = function(event) {
    toSkip = !toSkip;
    if (toSkip) return;

    ok(count < 2, "Still comparing...");
    info("bc2: " + currentTest);
    compare(event.data, currentTest);
    ++count;
    next();
  }

  function next() {
    if (count < 2) {
      return;
    }

    is(count, 2, "Just 2 comparations");
    count = 0;

    if (!tests.length) {
      SimpleTest.finish();
      return;
    }

    currentTest = tests.shift();
    bc.postMessage(currentTest);
    info("Posted: " + currentTest);
  }

  var worker = new Worker("broadcastchannel_worker_any.js");
  worker.onmessage = function(event) {
    if (event.data == "READY") {
      next();
    }
  };
}

SimpleTest.waitForExplicitFinish();
runTest();

</script>
</body>
</html>