summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fs/script-tests/FileSystemBaseHandle-postMessage-BroadcastChannel.js
blob: 681037db2fb129319935313f399cbafc08149d85 (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
'use strict';

// This script depends on the following scripts:
//    /fs/resources/messaging-helpers.js
//    /fs/resources/messaging-serialize-helpers.js
//    /fs/resources/test-helpers.js
//    /service-workers/service-worker/resources/test-helpers.sub.js

// Sets up a new broadcast channel in |target|. Posts a message instructing
// |target| to open the broadcast channel using |broadcast_channel_name|.
async function create_broadcast_channel(
  test, broadcast_channel_name, receiver, target, target_origin) {
  target.postMessage(
    { type: 'create-broadcast-channel', broadcast_channel_name },
    { targetOrigin: target_origin });
  const event_watcher = new EventWatcher(test, receiver, 'message');

  // Wait until |target| is listening to the broad cast channel.
  const message_event = await event_watcher.wait_for('message');
  assert_equals(message_event.data.type, 'broadcast-channel-created',
    'The message target must receive a "broadcast-channel-created" message ' +
    'response.');
}

// This test is very similar to 'FileSystemBaseHandle-postMessage.js'. It
// starts by creating three message targets for the broadcast channel:
// an iframe, dedicated worker and a service worker. After setup, an array
// of FileSystemHandles is sent across the broadcast channel. The test
// expects three responses -- one from each message target.
directory_test(async (t, root) => {
  const broadcast_channel_name = 'file-system-file-handle-channel';
  const broadcast_channel = new BroadcastChannel(broadcast_channel_name);
  const broadcast_channel_event_watcher =
    new EventWatcher(t, broadcast_channel, 'message');

  const iframe = await add_iframe(t, { src: kDocumentMessageTarget });
  await create_broadcast_channel(
    t, broadcast_channel_name, self, iframe.contentWindow, '*');

  const scope = `${kServiceWorkerMessageTarget}` +
    '?post-message-to-broadcast-channel-with-file-handle';

  const registration = await create_service_worker(
    t, kServiceWorkerMessageTarget, scope);

  await create_broadcast_channel(
    t, broadcast_channel_name,
    navigator.serviceWorker, registration.installing);

  const dedicated_worker =
    create_dedicated_worker(t, kDedicatedWorkerMessageTarget);

  await create_broadcast_channel(
    t, broadcast_channel_name, dedicated_worker, dedicated_worker);

  const handles = await create_file_system_handles(t, root);

  broadcast_channel.postMessage(
    { type: 'receive-file-system-handles', cloned_handles: handles });

  const expected_response_count = 3;
  const responses = [];
  for (let i = 0; i < expected_response_count; ++i) {
    const message_event =
      await broadcast_channel_event_watcher.wait_for('message');
    responses.push(message_event.data);
  }

  const expected_serialized_handles = await serialize_handles(handles);

  for (let i = 0; i < responses.length; ++i) {
    assert_equals(responses[i].type, 'receive-serialized-file-system-handles',
      'The test runner must receive a "serialized-file-system-handles" ' +
      `message response. Actual response: ${responses[i]}`);

    assert_equals_serialized_handles(
      responses[i].serialized_handles, expected_serialized_handles);

    await assert_equals_cloned_handles(responses[i].cloned_handles, handles);
  }
}, 'Send and receive messages using a broadcast channel in an iframe, ' +
'dedicated worker and service worker.');