summaryrefslogtreecommitdiffstats
path: root/remote/shared/messagehandler/test/browser/broadcast/browser_filter_top_browsing_context.js
blob: c140c26fc6cfbe9d03cf5a47d38b4abaa1cf47e7 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const COM_TEST_PAGE = "https://example.com/document-builder.sjs?html=COM";
const FRAME_TEST_PAGE = createTestMarkupWithFrames();

add_task(async function test_broadcasting_filter_top_browsing_context() {
  info("Navigate the initial tab to the COM test URL");
  const tab1 = gBrowser.selectedTab;
  await loadURL(tab1.linkedBrowser, COM_TEST_PAGE);
  const browsingContext1 = tab1.linkedBrowser.browsingContext;

  info("Open a second tab on the frame test URL");
  const tab2 = await addTab(FRAME_TEST_PAGE);
  const browsingContext2 = tab2.linkedBrowser.browsingContext;

  const contextsForTab2 =
    tab2.linkedBrowser.browsingContext.getAllBrowsingContextsInSubtree();
  is(
    contextsForTab2.length,
    4,
    "Frame test tab has 3 children contexts (4 in total)"
  );

  const rootMessageHandler = createRootMessageHandler(
    "session-id-broadcasting_filter_top_browsing_context"
  );

  const broadcastValue1 = await sendBroadcastForTopBrowsingContext(
    browsingContext1,
    rootMessageHandler
  );

  ok(
    Array.isArray(broadcastValue1),
    "The broadcast returned an array of values"
  );

  is(broadcastValue1.length, 1, "The broadcast returned one value as expected");

  ok(
    broadcastValue1.includes("broadcast-" + browsingContext1.id),
    "The broadcast returned the expected value from tab1"
  );

  const broadcastValue2 = await sendBroadcastForTopBrowsingContext(
    browsingContext2,
    rootMessageHandler
  );

  ok(
    Array.isArray(broadcastValue2),
    "The broadcast returned an array of values"
  );

  is(broadcastValue2.length, 4, "The broadcast returned 4 values as expected");

  for (const context of contextsForTab2) {
    ok(
      broadcastValue2.includes("broadcast-" + context.id),
      "The broadcast contains the value for browsing context " + context.id
    );
  }

  rootMessageHandler.destroy();
});

function sendBroadcastForTopBrowsingContext(
  topBrowsingContext,
  rootMessageHandler
) {
  return sendTestBroadcastCommand(
    "commandwindowglobalonly",
    "testBroadcast",
    {},
    {
      type: ContextDescriptorType.TopBrowsingContext,
      id: topBrowsingContext.browserId,
    },
    rootMessageHandler
  );
}