summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/global-search-bar/browser_clickResultItem.js
blob: d76d05203b334ac72f334f7925400553b36cffa0 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const {
  be_in_folder,
  create_folder,
  inboxFolder,
  make_message_sets_in_folders,
} = ChromeUtils.import(
  "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
);

const { GlodaMsgIndexer } = ChromeUtils.import(
  "resource:///modules/gloda/IndexMsg.jsm"
);

let folder;
let threads;

/**
 * Tests the 3 global search bars found in the UI:
 * 1) The one on the mail tab.
 * 2) The one in the search result tab.
 */
let tests = [
  {
    selector: "#unifiedToolbarContent .search-bar global-search-bar",
    isNewSearchBar: true,
    tabCountBefore: 1,
    tabCountAfter: 2,
  },
  {
    selector: ".remote-gloda-search",
    tabCountBefore: 2,
    async before() {
      // Run a search so we can search from the results tab.
      let input = document.querySelector("#unifiedToolbarContent .search-bar");
      EventUtils.synthesizeMouseAtCenter(input, {});
      EventUtils.sendString("us", window);
      EventUtils.synthesizeKey("KEY_Enter", {});

      await BrowserTestUtils.waitForCondition(
        () =>
          window.document.getElementById("tabmail").selectedTab.browser &&
          window.document.getElementById("tabmail").selectedTab.browser.src ==
            "chrome://messenger/content/glodaFacetView.xhtml",
        "search result tab did not open in time"
      );
    },
    tabCountAfter: 3,
  },
];

/**
 * Tests clicking on an item in the various global search bars opens one tab
 * only. See bug 1679113.
 */
add_task(async function testClickingGlobalSearchResultItemOpensOneTab() {
  window.focus();
  folder = await create_folder("SearchedFolder");
  await be_in_folder(folder);
  threads = await make_message_sets_in_folders(
    [folder],
    [
      { from: ["User", "user@example.com"] },
      { from: ["User", "user@example.com"] },
      { from: ["User", "user@example.com"] },
    ]
  );

  await new Promise(callback => {
    GlodaMsgIndexer.indexFolder(folder, { callback, force: true });
  });

  let tabmail = window.document.getElementById("tabmail");
  for (let test of tests) {
    while (tabmail.tabInfo.length > 1) {
      tabmail.closeTab(1);
    }

    if (test.before) {
      await test.before();
    }

    Assert.equal(
      tabmail.tabInfo.length,
      test.tabCountBefore,
      "tab count is as expected before"
    );

    let input = document.querySelector(test.selector);
    if (test.isNewSearchBar) {
      input.reset();
    } else {
      input.value = "";
    }
    input.focus();

    EventUtils.synthesizeKey("u", {});
    EventUtils.synthesizeKey("s", {});
    EventUtils.synthesizeKey("e", {});

    await BrowserTestUtils.waitForCondition(
      () => input.controller.matchCount > 0,
      `"${test.selector}" did not find any matches`
    );

    let target = document.querySelector(
      "#PopupGlodaAutocomplete > richlistbox > richlistitem"
    );
    Assert.ok(target, "target item to click found");
    EventUtils.synthesizeMouseAtCenter(target, {});

    // Give any potentially extra tabs time to appear.
    // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
    await new Promise(resolve => window.setTimeout(resolve, 1000));

    Assert.equal(
      tabmail.tabInfo.length,
      test.tabCountAfter,
      "tab count is as expected after"
    );
    Assert.equal(
      tabmail.selectedTab.browser.src,
      "chrome://messenger/content/glodaFacetView.xhtml",
      "current tab is the search results tab"
    );
  }
});

registerCleanupFunction(async function () {
  let tabmail = window.document.getElementById("tabmail");
  tabmail.selectTabByMode("mail3PaneTab");
  await be_in_folder(inboxFolder);
  folder.deleteSelf(null);
  while (tabmail.tabInfo.length > 1) {
    tabmail.closeTab(1);
  }
});