summaryrefslogtreecommitdiffstats
path: root/browser/components/preferences/tests/browser_search_quickactions.js
blob: 70799b5002f1711249915e0f8a54d37a25cfb557 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// This tests the Privacy pane's Firefox QuickActions UI.

"use strict";

ChromeUtils.defineESModuleGetters(this, {
  ActionsProviderQuickActions:
    "resource:///modules/ActionsProviderQuickActions.sys.mjs",
  UrlbarTestUtils: "resource://testing-common/UrlbarTestUtils.sys.mjs",
});

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

  ActionsProviderQuickActions.addAction("testaction", {
    commands: ["testaction"],
    label: "quickactions-downloads2",
  });

  registerCleanupFunction(() => {
    ActionsProviderQuickActions.removeAction("testaction");
  });
});

async function isGroupHidden(tab) {
  return SpecialPowers.spawn(
    tab.linkedBrowser,
    [],
    async () => content.document.getElementById("quickActionsBox").hidden
  );
}

add_task(async function test_show_prefs() {
  Services.prefs.setBoolPref("browser.urlbar.quickactions.showPrefs", false);

  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "about:preferences#search"
  );

  Assert.ok(
    await isGroupHidden(tab),
    "The preferences are hidden when pref disabled"
  );

  Services.prefs.setBoolPref("browser.urlbar.quickactions.showPrefs", true);

  Assert.ok(
    !(await isGroupHidden(tab)),
    "The preferences are shown when pref enabled"
  );

  Services.prefs.clearUserPref("browser.urlbar.quickactions.showPrefs");
  await BrowserTestUtils.removeTab(tab);
});

async function testActionIsShown(window, name) {
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "testact",
    waitForFocus: SimpleTest.waitForFocus,
  });
  try {
    await BrowserTestUtils.waitForMutationCondition(
      window.document,
      {},
      () =>
        !!window.document.querySelector(
          `.urlbarView-action-btn[data-action=${name}]`
        )
    );
    Assert.ok(true, `We found action "${name}"`);
    return true;
  } catch (e) {
    return false;
  }
}

add_task(async function test_prefs() {
  Services.prefs.setBoolPref("browser.urlbar.quickactions.showPrefs", true);
  Services.prefs.setBoolPref("browser.urlbar.suggest.quickactions", false);

  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "about:preferences#search"
  );

  Assert.ok(
    !(await testActionIsShown(window)),
    "Actions are not shown while pref disabled"
  );

  await SpecialPowers.spawn(tab.linkedBrowser, [], async () => {
    let checkbox = content.document.getElementById("enableQuickActions");
    is(
      checkbox.checked,
      false,
      "Checkbox is not checked while feature is disabled"
    );
    checkbox.click();
  });

  Assert.ok(
    await testActionIsShown(window, "testaction"),
    "Actions are shown after user clicks checkbox"
  );

  Services.prefs.clearUserPref("browser.urlbar.quickactions.showPrefs");
  Services.prefs.clearUserPref("browser.urlbar.suggest.quickactions");
  await BrowserTestUtils.removeTab(tab);
});