summaryrefslogtreecommitdiffstats
path: root/browser/components/search/test/browser_oneOffContextMenu_setDefault.js
blob: ff49cb0c62c6543c18f33dbb0d748c52d00cb902 (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
184
185
186
187
188
189
190
191
192
193
194
195
"use strict";

const TEST_ENGINE_NAME = "Foo";
const TEST_ENGINE_BASENAME = "testEngine.xml";
const SEARCHBAR_BASE_ID = "searchbar-engine-one-off-item-";
const URLBAR_BASE_ID = "urlbar-engine-one-off-item-";
const ONEOFF_URLBAR_PREF = "browser.urlbar.oneOffSearches";

const searchbar = document.getElementById("searchbar");
const urlbar = document.getElementById("urlbar");
const searchPopup = document.getElementById("PopupSearchAutoComplete");
const urlbarPopup = document.getElementById("PopupAutoCompleteRichResult");
const searchIcon = document.getAnonymousElementByAttribute(
  searchbar, "anonid", "searchbar-search-button"
);
const searchOneOffBinding = document.getAnonymousElementByAttribute(
  searchPopup, "anonid", "search-one-off-buttons"
);
const urlBarOneOffBinding = document.getAnonymousElementByAttribute(
  urlbarPopup, "anonid", "one-off-search-buttons"
);

let originalEngine = Services.search.currentEngine;

function resetEngine() {
  Services.search.currentEngine = originalEngine;
}

registerCleanupFunction(resetEngine);

add_task(function* init() {
  yield promiseNewEngine(TEST_ENGINE_BASENAME, {
    setAsCurrent: false,
  });
});

add_task(function* test_searchBarChangeEngine() {
  let oneOffButton = yield openPopupAndGetEngineButton(true, searchPopup,
                                                       searchOneOffBinding,
                                                       SEARCHBAR_BASE_ID);

  const setDefaultEngineMenuItem = document.getAnonymousElementByAttribute(
    searchOneOffBinding, "anonid", "search-one-offs-context-set-default"
  );

  // Click the set default engine menu item.
  let promise = promiseCurrentEngineChanged();
  EventUtils.synthesizeMouseAtCenter(setDefaultEngineMenuItem, {});

  // This also checks the engine correctly changed.
  yield promise;

  Assert.equal(oneOffButton.id, SEARCHBAR_BASE_ID + originalEngine.name,
               "Should now have the original engine's id for the button");
  Assert.equal(oneOffButton.getAttribute("tooltiptext"), originalEngine.name,
               "Should now have the original engine's name for the tooltip");
  Assert.equal(oneOffButton.image, originalEngine.iconURI.spec,
               "Should now have the original engine's uri for the image");

  yield promiseClosePopup(searchPopup);
});

add_task(function* test_urlBarChangeEngine() {
  Services.prefs.setBoolPref(ONEOFF_URLBAR_PREF, true);
  registerCleanupFunction(function* () {
    Services.prefs.clearUserPref(ONEOFF_URLBAR_PREF);
  });

  // Ensure the engine is reset.
  resetEngine();

  let oneOffButton = yield openPopupAndGetEngineButton(false, urlbarPopup,
                                                       urlBarOneOffBinding,
                                                       URLBAR_BASE_ID);

  const setDefaultEngineMenuItem = document.getAnonymousElementByAttribute(
    urlBarOneOffBinding, "anonid", "search-one-offs-context-set-default"
  );

  // Click the set default engine menu item.
  let promise = promiseCurrentEngineChanged();
  EventUtils.synthesizeMouseAtCenter(setDefaultEngineMenuItem, {});

  // This also checks the engine correctly changed.
  yield promise;

  let currentEngine = Services.search.currentEngine;

  // For the urlbar, we should keep the new engine's icon.
  Assert.equal(oneOffButton.id, URLBAR_BASE_ID + currentEngine.name,
               "Should now have the original engine's id for the button");
  Assert.equal(oneOffButton.getAttribute("tooltiptext"), currentEngine.name,
               "Should now have the original engine's name for the tooltip");
  Assert.equal(oneOffButton.image, currentEngine.iconURI.spec,
               "Should now have the original engine's uri for the image");

  yield promiseClosePopup(urlbarPopup);
});

/**
 * Promises that an engine change has happened for the current engine, which
 * has resulted in the test engine now being the current engine.
 *
 * @return {Promise} Resolved once the test engine is set as the current engine.
 */
function promiseCurrentEngineChanged() {
  return new Promise(resolve => {
    function observer(aSub, aTopic, aData) {
      if (aData == "engine-current") {
        Assert.ok(Services.search.currentEngine.name, TEST_ENGINE_NAME, "currentEngine set");
        Services.obs.removeObserver(observer, "browser-search-engine-modified");
        resolve();
      }
    }

    Services.obs.addObserver(observer, "browser-search-engine-modified", false);
  });
}

/**
 * Opens the specified urlbar/search popup and gets the test engine from the
 * one-off buttons.
 *
 * @param {Boolean} isSearch true if the search popup should be opened; false
 *                           for the urlbar popup.
 * @param {Object} popup The expected popup.
 * @param {Object} oneOffBinding The expected one-off-binding for the popup.
 * @param {String} baseId The expected string for the id of the current
 *                        engine button, without the engine name.
 * @return {Object} Returns an object that represents the one off button for the
 *                          test engine.
 */
function* openPopupAndGetEngineButton(isSearch, popup, oneOffBinding, baseId) {
  // Open the popup.
  let promise = promiseEvent(popup, "popupshown");
  info("Opening panel");

  // We have to open the popups in differnt ways.
  if (isSearch) {
    // Use the search icon to avoid hitting the network.
    EventUtils.synthesizeMouseAtCenter(searchIcon, {});
  } else {
    // There's no history at this stage, so we need to press a key.
    urlbar.focus();
    EventUtils.synthesizeKey("a", {});
  }
  yield promise;

  const contextMenu = document.getAnonymousElementByAttribute(
    oneOffBinding, "anonid", "search-one-offs-context-menu"
  );
  const oneOffButtons = document.getAnonymousElementByAttribute(
    oneOffBinding, "anonid", "search-panel-one-offs"
  );

  // Get the one-off button for the test engine.
  let oneOffButton;
  for (let node of oneOffButtons.childNodes) {
    if (node.engine && node.engine.name == TEST_ENGINE_NAME) {
      oneOffButton = node;
      break;
    }
  }
  Assert.notEqual(oneOffButton, undefined,
                  "One-off for test engine should exist");
  Assert.equal(oneOffButton.getAttribute("tooltiptext"), TEST_ENGINE_NAME,
               "One-off should have the tooltip set to the engine name");
  Assert.equal(oneOffButton.id, baseId + TEST_ENGINE_NAME,
               "Should have the correct id");

  // Open the context menu on the one-off.
  promise = BrowserTestUtils.waitForEvent(contextMenu, "popupshown");
  EventUtils.synthesizeMouseAtCenter(oneOffButton, {
    type: "contextmenu",
    button: 2,
  });
  yield promise;

  return oneOffButton;
}

/**
 * Closes the popup and moves the mouse away from it.
 *
 * @param {Button} popup The popup to close.
 */
function* promiseClosePopup(popup) {
  // close the panel using the escape key.
  let promise = promiseEvent(popup, "popuphidden");
  EventUtils.synthesizeKey("VK_ESCAPE", {});
  yield promise;

  // Move the cursor out of the panel area to avoid messing with other tests.
  yield EventUtils.synthesizeNativeMouseMove(popup);
}