summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_tabs_update_highlighted.js
blob: 0adb05e8271896cb7a8608d3e117a846a8d3db1c (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

add_task(async function test_update_highlighted() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["tabs"],
    },

    background: async function () {
      const trackedEvents = ["onActivated", "onHighlighted"];
      async function expectResults(fn, action) {
        let resolve;
        let reject;
        let promise = new Promise((...args) => {
          [resolve, reject] = args;
        });
        let expectedEvents;
        let events = [];
        let listeners = {};
        for (let trackedEvent of trackedEvents) {
          listeners[trackedEvent] = data => {
            events.push([trackedEvent, data]);
            if (expectedEvents && expectedEvents.length >= events.length) {
              resolve();
            }
          };
          browser.tabs[trackedEvent].addListener(listeners[trackedEvent]);
        }
        let expectedData = await fn();
        let expectedHighlighted = expectedData.highlighted;
        let expectedActive = expectedData.active;
        expectedEvents = expectedData.events;
        if (events.length < expectedEvents.length) {
          // Wait up to 1000 ms for the expected number of events.
          // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
          setTimeout(reject, 1000);
          await promise.catch(() => {
            let numMissing = expectedEvents.length - events.length;
            browser.test.fail(`${numMissing} missing events when ${action}`);
          });
        }
        let [{ id: active }] = await browser.tabs.query({ active: true });
        browser.test.assertEq(
          expectedActive,
          active,
          `The expected tab is active when ${action}`
        );
        let highlighted = (await browser.tabs.query({ highlighted: true })).map(
          ({ id }) => id
        );
        browser.test.assertEq(
          JSON.stringify(expectedHighlighted),
          JSON.stringify(highlighted),
          `The expected tabs are highlighted when ${action}`
        );
        let unexpectedEvents = events.splice(expectedEvents.length);
        browser.test.assertEq(
          JSON.stringify(expectedEvents),
          JSON.stringify(events),
          `Should get expected events when ${action}`
        );
        if (unexpectedEvents.length) {
          browser.test.fail(
            `${unexpectedEvents.length} unexpected events when ${action}: ` +
              JSON.stringify(unexpectedEvents)
          );
        }
        for (let trackedEvent of trackedEvents) {
          browser.tabs[trackedEvent].removeListener(listeners[trackedEvent]);
        }
      }

      let { id: windowId } = await browser.windows.getCurrent();
      let { id: tab1 } = await browser.tabs.create({ url: "about:blank?1" });
      let { id: tab2 } = await browser.tabs.create({
        url: "about:blank?2",
        active: true,
      });

      await expectResults(async () => {
        await browser.tabs.update(tab2, { highlighted: true });
        return { active: tab2, highlighted: [tab2], events: [] };
      }, "highlighting active tab");

      await expectResults(async () => {
        await browser.tabs.update(tab2, { highlighted: false });
        return { active: tab2, highlighted: [tab2], events: [] };
      }, "unhighlighting active tab with no multiselection");

      await expectResults(async () => {
        await browser.tabs.update(tab1, { highlighted: true });
        return {
          active: tab1,
          highlighted: [tab1, tab2],
          events: [
            ["onActivated", { tabId: tab1, previousTabId: tab2, windowId }],
            ["onHighlighted", { tabIds: [tab1, tab2], windowId }],
          ],
        };
      }, "highlighting non-highlighted tab");

      await expectResults(async () => {
        await browser.tabs.update(tab2, { highlighted: true });
        return { active: tab1, highlighted: [tab1, tab2], events: [] };
      }, "highlighting inactive highlighted tab");

      await expectResults(async () => {
        await browser.tabs.update(tab1, { highlighted: false });
        return {
          active: tab2,
          highlighted: [tab2],
          events: [
            ["onActivated", { tabId: tab2, previousTabId: tab1, windowId }],
            ["onHighlighted", { tabIds: [tab2], windowId }],
          ],
        };
      }, "unhighlighting active tab with multiselection");

      await expectResults(async () => {
        await browser.tabs.update(tab1, { highlighted: true });
        return {
          active: tab1,
          highlighted: [tab1, tab2],
          events: [
            ["onActivated", { tabId: tab1, previousTabId: tab2, windowId }],
            ["onHighlighted", { tabIds: [tab1, tab2], windowId }],
          ],
        };
      }, "highlighting non-highlighted tab");

      await expectResults(async () => {
        await browser.tabs.update(tab2, { highlighted: false });
        return {
          active: tab1,
          highlighted: [tab1],
          events: [["onHighlighted", { tabIds: [tab1], windowId }]],
        };
      }, "unhighlighting inactive highlighted tab");

      await expectResults(async () => {
        await browser.tabs.update(tab2, { highlighted: true, active: false });
        return {
          active: tab1,
          highlighted: [tab1, tab2],
          events: [["onHighlighted", { tabIds: [tab1, tab2], windowId }]],
        };
      }, "highlighting without activating non-highlighted tab");

      await expectResults(async () => {
        await browser.tabs.update(tab2, { highlighted: true, active: true });
        return {
          active: tab2,
          highlighted: [tab2],
          events: [
            ["onActivated", { tabId: tab2, previousTabId: tab1, windowId }],
            ["onHighlighted", { tabIds: [tab2], windowId }],
          ],
        };
      }, "highlighting and activating inactive highlighted tab");

      await expectResults(async () => {
        await browser.tabs.update(tab1, { active: true, highlighted: true });
        return {
          active: tab1,
          highlighted: [tab1],
          events: [
            ["onActivated", { tabId: tab1, previousTabId: tab2, windowId }],
            ["onHighlighted", { tabIds: [tab1], windowId }],
          ],
        };
      }, "highlighting and activating non-highlighted tab");

      await browser.tabs.remove([tab1, tab2]);
      browser.test.notifyPass("test-finished");
    },
  });

  await extension.startup();
  await extension.awaitFinish("test-finished");
  await extension.unload();
});