summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/about/browser_aboutHome_search_composing.js
blob: 7668e8a371236d268bf971cf67e0c30e1fba8f7a (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

ignoreAllUncaughtExceptions();

add_task(async function() {
  info("Clicking suggestion list while composing");

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: "about:home" },
    async function(browser) {
      // Add a test engine that provides suggestions and switch to it.
      let currEngine = await Services.search.getDefault();

      let engine;
      await promiseContentSearchChange(browser, async () => {
        engine = await promiseNewEngine("searchSuggestionEngine.xml");
        await Services.search.setDefault(engine);
        return engine.name;
      });

      // Clear any search history results
      await new Promise((resolve, reject) => {
        FormHistory.update(
          { op: "remove" },
          {
            handleError(error) {
              reject(error);
            },
            handleCompletion(reason) {
              if (!reason) {
                resolve();
              } else {
                reject();
              }
            },
          }
        );
      });

      await SpecialPowers.spawn(browser, [], async function() {
        // Start composition and type "x"
        let input = content.document.querySelector([
          "#searchText",
          "#newtab-search-text",
        ]);
        input.focus();
      });

      info("Setting up the mutation observer before synthesizing composition");
      let mutationPromise = SpecialPowers.spawn(browser, [], async function() {
        let searchController = content.wrappedJSObject.gContentSearchController;

        // Wait for the search suggestions to become visible.
        let table = searchController._suggestionsList;
        let input = content.document.querySelector([
          "#searchText",
          "#newtab-search-text",
        ]);

        await new Promise(resolve => {
          let observer = new content.MutationObserver(() => {
            if (input.getAttribute("aria-expanded") == "true") {
              observer.disconnect();
              ok(!table.hidden, "Search suggestion table unhidden");
              resolve();
            }
          });
          observer.observe(input, {
            attributes: true,
            attributeFilter: ["aria-expanded"],
          });
        });

        let row = table.children[1];
        row.setAttribute("id", "TEMPID");

        // ContentSearchUIController looks at the current selectedIndex when
        // performing a search. Synthesizing the mouse event on the suggestion
        // doesn't actually mouseover the suggestion and trigger it to be flagged
        // as selected, so we manually select it first.
        searchController.selectedIndex = 1;
      });

      // FYI: "compositionstart" will be dispatched automatically.
      await BrowserTestUtils.synthesizeCompositionChange(
        {
          composition: {
            string: "x",
            clauses: [
              { length: 1, attr: Ci.nsITextInputProcessor.ATTR_RAW_CLAUSE },
            ],
          },
          caret: { start: 1, length: 0 },
        },
        browser
      );

      info("Waiting for search suggestion table unhidden");
      await mutationPromise;

      // Click the second suggestion.
      let expectedURL = (await Services.search.getDefault()).getSubmission(
        "xbar",
        null,
        "homepage"
      ).uri.spec;
      let loadPromise = BrowserTestUtils.waitForDocLoadAndStopIt(
        expectedURL,
        gBrowser.selectedBrowser
      );
      await BrowserTestUtils.synthesizeMouseAtCenter(
        "#TEMPID",
        {
          button: 0,
        },
        browser
      );
      await loadPromise;

      Services.search.setDefault(currEngine);
      try {
        await Services.search.removeEngine(engine);
      } catch (ex) {}
    }
  );
});