summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_quickactions_tab_refocus.js
blob: f969528806ffc92e4196872134a2cd4e26cbb529 (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
184
185
186
187
188
189
190
191
192
193
194
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Tests for QuickActions that re-focus tab..
 */

"use strict";

requestLongerTimeout(3);

add_setup(async function setup() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.urlbar.quickactions.enabled", true],
      ["browser.urlbar.suggest.quickactions", true],
      ["browser.urlbar.shortcuts.quickactions", true],
    ],
  });
});

let isSelected = async selector =>
  SpecialPowers.spawn(gBrowser.selectedBrowser, [selector], arg => {
    return ContentTaskUtils.waitForCondition(() =>
      content.document.querySelector(arg)?.hasAttribute("selected")
    );
  });

add_task(async function test_about_pages() {
  const testData = [
    {
      firstInput: "downloads",
      uri: "about:downloads",
    },
    {
      firstInput: "logins",
      uri: "about:logins",
    },
    {
      firstInput: "settings",
      uri: "about:preferences",
    },
    {
      firstInput: "add-ons",
      uri: "about:addons",
      component: "button[name=discover]",
    },
    {
      firstInput: "extensions",
      uri: "about:addons",
      component: "button[name=extension]",
    },
    {
      firstInput: "plugins",
      uri: "about:addons",
      component: "button[name=plugin]",
    },
    {
      firstInput: "themes",
      uri: "about:addons",
      component: "button[name=theme]",
    },
    {
      firstLoad: "about:preferences#home",
      secondInput: "settings",
      uri: "about:preferences#home",
    },
  ];

  for (const {
    firstInput,
    firstLoad,
    secondInput,
    uri,
    component,
  } of testData) {
    info("Setup initial state");
    let firstTab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
    let onLoad = BrowserTestUtils.browserLoaded(
      gBrowser.selectedBrowser,
      false,
      uri
    );
    if (firstLoad) {
      info("Load initial URI");
      BrowserTestUtils.loadURIString(gBrowser.selectedBrowser, uri);
    } else {
      info("Open about page by quick action");
      await UrlbarTestUtils.promiseAutocompleteResultPopup({
        window,
        value: firstInput,
      });
      EventUtils.synthesizeKey("KEY_ArrowDown", {}, window);
      EventUtils.synthesizeKey("KEY_Enter", {}, window);
    }
    await onLoad;

    if (component) {
      info("Check whether the component is in the page");
      Assert.ok(await isSelected(component), "There is expected component");
    }

    info("Do the second quick action in second tab");
    let secondTab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: secondInput || firstInput,
    });
    EventUtils.synthesizeKey("KEY_ArrowDown", {}, window);
    EventUtils.synthesizeKey("KEY_Enter", {}, window);
    Assert.equal(
      gBrowser.selectedTab,
      firstTab,
      "Switched to the tab that is opening the about page"
    );
    Assert.equal(
      gBrowser.selectedBrowser.currentURI.spec,
      uri,
      "URI is not changed"
    );
    Assert.equal(gBrowser.tabs.length, 3, "Not opened a new tab");

    if (component) {
      info("Check whether the component is still in the page");
      Assert.ok(await isSelected(component), "There is expected component");
    }

    BrowserTestUtils.removeTab(secondTab);
    BrowserTestUtils.removeTab(firstTab);
  }
});

add_task(async function test_about_addons_pages() {
  let testData = [
    {
      cmd: "add-ons",
      testFun: async () => isSelected("button[name=discover]"),
    },
    {
      cmd: "plugins",
      testFun: async () => isSelected("button[name=plugin]"),
    },
    {
      cmd: "extensions",
      testFun: async () => isSelected("button[name=extension]"),
    },
    {
      cmd: "themes",
      testFun: async () => isSelected("button[name=theme]"),
    },
  ];

  info("Pick all actions related about:addons");
  let originalTab = gBrowser.selectedTab;
  for (const { cmd, testFun } of testData) {
    await BrowserTestUtils.openNewForegroundTab(gBrowser);
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: cmd,
    });
    EventUtils.synthesizeKey("KEY_ArrowDown", {}, window);
    EventUtils.synthesizeKey("KEY_Enter", {}, window);
    await BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
    Assert.ok(await testFun(), "The page content is correct");
  }
  Assert.equal(
    gBrowser.tabs.length,
    testData.length + 1,
    "Tab length is correct"
  );

  info("Pick all again");
  for (const { cmd, testFun } of testData) {
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: cmd,
    });
    EventUtils.synthesizeKey("KEY_ArrowDown", {}, window);
    EventUtils.synthesizeKey("KEY_Enter", {}, window);
    await BrowserTestUtils.waitForCondition(() => testFun());
    Assert.ok(true, "The tab correspondent action is selected");
  }
  Assert.equal(
    gBrowser.tabs.length,
    testData.length + 1,
    "Tab length is not changed"
  );

  for (const tab of gBrowser.tabs) {
    if (tab !== originalTab) {
      BrowserTestUtils.removeTab(tab);
    }
  }
});