summaryrefslogtreecommitdiffstats
path: root/browser/components/search/test/browser_oneOffContextMenu.js
blob: 69207923bb4dbc388fbd0b3a34aade66380fb617 (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
"use strict";

const TEST_ENGINE_NAME = "Foo";
const TEST_ENGINE_BASENAME = "testEngine.xml";

const searchbar = document.getElementById("searchbar");
const searchPopup = document.getElementById("PopupSearchAutoComplete");
const searchIcon = document.getAnonymousElementByAttribute(
  searchbar, "anonid", "searchbar-search-button"
);
const oneOffBinding = document.getAnonymousElementByAttribute(
  searchPopup, "anonid", "search-one-off-buttons"
);
const contextMenu = document.getAnonymousElementByAttribute(
  oneOffBinding, "anonid", "search-one-offs-context-menu"
);
const oneOffButtons = document.getAnonymousElementByAttribute(
  oneOffBinding, "anonid", "search-panel-one-offs"
);
const searchInNewTabMenuItem = document.getAnonymousElementByAttribute(
  oneOffBinding, "anonid", "search-one-offs-context-open-in-new-tab"
);

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

add_task(function* extendedTelemetryDisabled() {
  yield SpecialPowers.pushPrefEnv({set: [["toolkit.telemetry.enabled", false]]});
  yield doTest();
  checkTelemetry("other");
});

add_task(function* extendedTelemetryEnabled() {
  yield SpecialPowers.pushPrefEnv({set: [["toolkit.telemetry.enabled", true]]});
  yield doTest();
  checkTelemetry("other-" + TEST_ENGINE_NAME);
});

function* doTest() {
  // Open the popup.
  let promise = promiseEvent(searchPopup, "popupshown");
  info("Opening search panel");
  EventUtils.synthesizeMouseAtCenter(searchIcon, {});
  yield promise;

  // 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");

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

  // Click the Search in New Tab menu item.
  promise = BrowserTestUtils.waitForNewTab(gBrowser);
  EventUtils.synthesizeMouseAtCenter(searchInNewTabMenuItem, {});
  let tab = yield promise;

  // By default the search will open in the background and the popup will stay open:
  promise = promiseEvent(searchPopup, "popuphidden");
  info("Closing search panel");
  EventUtils.synthesizeKey("VK_ESCAPE", {});
  yield promise;

  // Check the loaded tab.
  Assert.equal(tab.linkedBrowser.currentURI.spec,
               "http://mochi.test:8888/browser/browser/components/search/test/",
               "Expected search tab should have loaded");

  yield BrowserTestUtils.removeTab(tab);

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

function checkTelemetry(expectedEngineName) {
  let propertyPath = [
    "countableEvents",
    "__DEFAULT__",
    "search-oneoff",
    expectedEngineName + ".oneoff-context-searchbar",
    "unknown",
    "tab-background",
  ];
  let telem = BrowserUITelemetry.getToolbarMeasures();
  for (let prop of propertyPath) {
    Assert.ok(prop in telem, "Property " + prop + " should be in the telemetry");
    telem = telem[prop];
  }
  Assert.equal(telem, 1, "Click count");
}