summaryrefslogtreecommitdiffstats
path: root/test/wpt/tests/service-workers/service-worker/ServiceWorkerGlobalScope/resources/extendable-message-event-utils.js
blob: d6a3b483f5394c2034cfc92a73ffdcdbbdcacc51 (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
var ExtendableMessageEventUtils = {};

// Create a representation of a given ExtendableMessageEvent that is suitable
// for transmission via the `postMessage` API.
ExtendableMessageEventUtils.serialize = function(event) {
  var ports = event.ports.map(function(port) {
        return { constructor: { name: port.constructor.name } };
    });
  return {
    constructor: {
      name: event.constructor.name
    },
    origin: event.origin,
    lastEventId: event.lastEventId,
    source: {
      constructor: {
        name: event.source.constructor.name
      },
      url: event.source.url,
      frameType: event.source.frameType,
      visibilityState: event.source.visibilityState,
      focused: event.source.focused
    },
    ports: ports
  };
};

// Compare the actual and expected values of an ExtendableMessageEvent that has
// been transformed using the `serialize` function defined in this file.
ExtendableMessageEventUtils.assert_equals = function(actual, expected) {
  assert_equals(
    actual.constructor.name, expected.constructor.name, 'event constructor'
  );
  assert_equals(actual.origin, expected.origin, 'event `origin` property');
  assert_equals(
    actual.lastEventId,
    expected.lastEventId,
    'event `lastEventId` property'
  );

  assert_equals(
    actual.source.constructor.name,
    expected.source.constructor.name,
    'event `source` property constructor'
  );
  assert_equals(
    actual.source.url, expected.source.url, 'event `source` property `url`'
  );
  assert_equals(
    actual.source.frameType,
    expected.source.frameType,
    'event `source` property `frameType`'
  );
  assert_equals(
    actual.source.visibilityState,
    expected.source.visibilityState,
    'event `source` property `visibilityState`'
  );
  assert_equals(
    actual.source.focused,
    expected.source.focused,
    'event `source` property `focused`'
  );

  assert_equals(
    actual.ports.length,
    expected.ports.length,
    'event `ports` property length'
  );

  for (var idx = 0; idx < expected.ports.length; ++idx) {
    assert_equals(
      actual.ports[idx].constructor.name,
      expected.ports[idx].constructor.name,
      'MessagePort #' + idx + ' constructor'
    );
  }
};