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

"use strict";

// This test verifies that searching filters the features to just that subset that
// contains the search terms.
add_task(async function testFilterFeatures() {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.preferences.experimental", true]],
  });

  // Add a number of test features.
  const server = new DefinitionServer();
  let definitions = [
    {
      id: "test-featureA",
      preference: "test.featureA",
      title: "Experimental Feature 1",
      description: "This is a fun experimental feature you can enable",
      result: true,
    },
    {
      id: "test-featureB",
      preference: "test.featureB",
      title: "Experimental Thing 2",
      description: "This is a very boring experimental tool",
      result: false,
    },
    {
      id: "test-featureC",
      preference: "test.featureC",
      title: "Experimental Thing 3",
      description: "This is a fun experimental feature for you can enable",
      result: true,
    },
    {
      id: "test-featureD",
      preference: "test.featureD",
      title: "Experimental Thing 4",
      description: "This is a not a checkbox that you should be enabling",
      result: false,
    },
  ];
  for (let { id, preference } of definitions) {
    server.addDefinition({ id, preference, isPublic: true });
  }

  await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    `about:preferences?definitionsUrl=${encodeURIComponent(
      server.definitionsUrl
    )}#paneExperimental`
  );
  let doc = gBrowser.contentDocument;

  await TestUtils.waitForCondition(
    () => doc.getElementById(definitions[definitions.length - 1].id),
    "wait for the first public feature to get added to the DOM"
  );

  // Manually modify the labels of the features that were just added, so that the test
  // can rely on consistent search terms.
  for (let definition of definitions) {
    let mainItem = doc.getElementById(definition.id);
    mainItem.label = definition.title;
    mainItem.removeAttribute("data-l10n-id");
    let descItem = doc.getElementById(definition.id + "-description");
    descItem.textContent = definition.description;
    descItem.removeAttribute("data-l10n-id");
  }

  // First, check that all of the items are visible by default.
  for (let definition of definitions) {
    checkVisibility(
      doc.getElementById(definition.id),
      true,
      `${definition.id} should be initially visible`
    );
  }

  // After searching, only a subset should be visible.
  await enterSearch(doc, "feature");

  for (let definition of definitions) {
    checkVisibility(
      doc.getElementById(definition.id),
      definition.result,
      `${definition.id} should be ${
        definition.result ? "visible" : "hidden"
      } after first search`
    );
    info("Text for item was: " + doc.getElementById(definition.id).textContent);
  }

  // Further restrict the search to only a single item.
  await enterSearch(doc, " you");

  let shouldBeVisible = true;
  for (let definition of definitions) {
    checkVisibility(
      doc.getElementById(definition.id),
      shouldBeVisible,
      `${definition.id} should be ${
        shouldBeVisible ? "visible" : "hidden"
      } after further search`
    );
    shouldBeVisible = false;
  }

  // Reset the search entirely.
  let searchInput = doc.getElementById("searchInput");
  searchInput.value = "";
  searchInput.doCommand();

  // Clearing the search will go to the general pane so switch back to the experimental pane.
  EventUtils.synthesizeMouseAtCenter(
    doc.getElementById("category-experimental"),
    {},
    gBrowser.contentWindow
  );

  for (let definition of definitions) {
    checkVisibility(
      doc.getElementById(definition.id),
      true,
      `${definition.id} should be visible after search cleared`
    );
  }

  // Simulate entering a search and then clicking one of the category labels. The search
  // should reset each time.
  for (let category of ["category-search", "category-experimental"]) {
    await enterSearch(doc, "feature");

    for (let definition of definitions) {
      checkVisibility(
        doc.getElementById(definition.id),
        definition.result,
        `${definition.id} should be ${
          definition.result ? "visible" : "hidden"
        } after next search`
      );
    }

    EventUtils.synthesizeMouseAtCenter(
      doc.getElementById(category),
      {},
      gBrowser.contentWindow
    );

    for (let definition of definitions) {
      checkVisibility(
        doc.getElementById(definition.id),
        true,
        `${definition.id} should be visible after category change to ${category}`
      );
    }
  }

  BrowserTestUtils.removeTab(gBrowser.selectedTab);
});

function checkVisibility(element, expected, desc) {
  return expected
    ? is_element_visible(element, desc)
    : is_element_hidden(element, desc);
}

function enterSearch(doc, query) {
  let searchInput = doc.getElementById("searchInput");
  searchInput.focus();

  let searchCompletedPromise = BrowserTestUtils.waitForEvent(
    gBrowser.contentWindow,
    "PreferencesSearchCompleted",
    evt => evt.detail == query
  );

  EventUtils.sendString(query);

  return searchCompletedPromise;
}