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

// Test opening to the differerent panes and subcategories in Preferences
add_task(async function () {
  let prefs = await openPreferencesViaOpenPreferencesAPI("panePrivacy");
  is(prefs.selectedPane, "panePrivacy", "Privacy pane was selected");
  prefs = await openPreferencesViaHash("privacy");
  is(
    prefs.selectedPane,
    "panePrivacy",
    "Privacy pane is selected when hash is 'privacy'"
  );
  prefs = await openPreferencesViaOpenPreferencesAPI("nonexistant-category");
  is(
    prefs.selectedPane,
    "paneGeneral",
    "General pane is selected by default when a nonexistant-category is requested"
  );
  prefs = await openPreferencesViaHash("nonexistant-category");
  is(
    prefs.selectedPane,
    "paneGeneral",
    "General pane is selected when hash is a nonexistant-category"
  );
  prefs = await openPreferencesViaHash();
  is(prefs.selectedPane, "paneGeneral", "General pane is selected by default");
  prefs = await openPreferencesViaOpenPreferencesAPI("privacy-reports", {
    leaveOpen: true,
  });
  is(prefs.selectedPane, "panePrivacy", "Privacy pane is selected by default");
  let doc = gBrowser.contentDocument;
  is(
    doc.location.hash,
    "#privacy",
    "The subcategory should be removed from the URI"
  );
  await TestUtils.waitForCondition(
    () => doc.querySelector(".spotlight"),
    "Wait for the reports section is spotlighted."
  );
  is(
    doc.querySelector(".spotlight").getAttribute("data-subcategory"),
    "reports",
    "The reports section is spotlighted."
  );
  BrowserTestUtils.removeTab(gBrowser.selectedTab);
});

// Test opening Preferences with subcategory on an existing Preferences tab. See bug 1358475.
add_task(async function () {
  let prefs = await openPreferencesViaOpenPreferencesAPI("general", {
    leaveOpen: true,
  });
  is(prefs.selectedPane, "paneGeneral", "General pane is selected by default");
  let doc = gBrowser.contentDocument;
  is(
    doc.location.hash,
    "#general",
    "The subcategory should be removed from the URI"
  );
  // The reasons that here just call the `openPreferences` API without the helping function are
  //   - already opened one about:preferences tab up there and
  //   - the goal is to test on the existing tab and
  //   - using `openPreferencesViaOpenPreferencesAPI` would introduce more handling of additional about:blank and unneccessary event
  await openPreferences("privacy-reports");
  let selectedPane = gBrowser.contentWindow.history.state;
  is(selectedPane, "panePrivacy", "Privacy pane should be selected");
  is(
    doc.location.hash,
    "#privacy",
    "The subcategory should be removed from the URI"
  );
  await TestUtils.waitForCondition(
    () => doc.querySelector(".spotlight"),
    "Wait for the reports section is spotlighted."
  );
  is(
    doc.querySelector(".spotlight").getAttribute("data-subcategory"),
    "reports",
    "The reports section is spotlighted."
  );
  BrowserTestUtils.removeTab(gBrowser.selectedTab);
});

// Test opening to a subcategory displays the correct values for preferences
add_task(async function () {
  // Skip if crash reporting isn't enabled since the checkbox will be missing.
  if (!AppConstants.MOZ_CRASHREPORTER) {
    return;
  }

  await SpecialPowers.pushPrefEnv({
    set: [["browser.crashReports.unsubmittedCheck.autoSubmit2", true]],
  });
  await openPreferencesViaOpenPreferencesAPI("privacy-reports", {
    leaveOpen: true,
  });

  let doc = gBrowser.contentDocument;
  ok(
    doc.querySelector("#automaticallySubmitCrashesBox").checked,
    "Checkbox for automatically submitting crashes should be checked when the pref is true and only Reports are requested"
  );

  BrowserTestUtils.removeTab(gBrowser.selectedTab);
  await SpecialPowers.popPrefEnv();
});

add_task(async function () {
  // Skip if crash reporting isn't enabled since the checkbox will be missing.
  if (!AppConstants.MOZ_CRASHREPORTER) {
    return;
  }

  await SpecialPowers.pushPrefEnv({
    set: [["browser.crashReports.unsubmittedCheck.autoSubmit2", false]],
  });
  await openPreferencesViaOpenPreferencesAPI("privacy-reports", {
    leaveOpen: true,
  });

  let doc = gBrowser.contentDocument;
  ok(
    !doc.querySelector("#automaticallySubmitCrashesBox").checked,
    "Checkbox for automatically submitting crashes should not be checked when the pref is false only Reports are requested"
  );

  BrowserTestUtils.removeTab(gBrowser.selectedTab);
  await SpecialPowers.popPrefEnv();
});

function openPreferencesViaHash(aPane) {
  return new Promise(resolve => {
    let finalPrefPaneLoaded = TestUtils.topicObserved(
      "sync-pane-loaded",
      () => true
    );
    gBrowser.selectedTab = BrowserTestUtils.addTab(
      gBrowser,
      "about:preferences" + (aPane ? "#" + aPane : "")
    );
    let newTabBrowser = gBrowser.selectedBrowser;

    newTabBrowser.addEventListener(
      "Initialized",
      function () {
        newTabBrowser.contentWindow.addEventListener(
          "load",
          async function () {
            let win = gBrowser.contentWindow;
            let selectedPane = win.history.state;
            await finalPrefPaneLoaded;
            gBrowser.removeCurrentTab();
            resolve({ selectedPane });
          },
          { once: true }
        );
      },
      { capture: true, once: true }
    );
  });
}