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

/* global browser */

// This tests dynamic results using the WebExtension Experiment API.

"use strict";

add_task(async function test() {
  let ext = await loadExtension({
    extraFiles: {
      "dynamicResult.css": await (
        await fetch("file://" + getTestFilePath("dynamicResult.css"))
      ).text(),
    },
    background: async () => {
      browser.experiments.urlbar.addDynamicResultType("testDynamicType");
      browser.experiments.urlbar.addDynamicViewTemplate("testDynamicType", {
        stylesheet: "dynamicResult.css",
        children: [
          {
            name: "text",
            tag: "span",
          },
          {
            name: "button",
            tag: "span",
            attributes: {
              role: "button",
            },
          },
        ],
      });
      browser.urlbar.onBehaviorRequested.addListener(query => {
        return "restricting";
      }, "test");
      browser.urlbar.onResultsRequested.addListener(query => {
        return [
          {
            type: "dynamic",
            source: "local",
            heuristic: true,
            payload: {
              dynamicType: "testDynamicType",
            },
          },
        ];
      }, "test");
      browser.experiments.urlbar.onViewUpdateRequested.addListener(payload => {
        return {
          text: {
            textContent: "This is a dynamic result.",
          },
          button: {
            textContent: "Click Me",
          },
        };
      }, "test");
      browser.urlbar.onResultPicked.addListener((payload, elementName) => {
        browser.test.sendMessage("onResultPicked", [payload, elementName]);
      }, "test");
    },
  });

  // Wait for the provider and dynamic type to be registered before continuing.
  await TestUtils.waitForCondition(
    () =>
      UrlbarProvidersManager.getProvider("test") &&
      UrlbarResult.getDynamicResultType("testDynamicType"),
    "Waiting for provider and dynamic type to be registered"
  );
  Assert.ok(
    UrlbarProvidersManager.getProvider("test"),
    "Provider should be registered"
  );
  Assert.ok(
    UrlbarResult.getDynamicResultType("testDynamicType"),
    "Dynamic type should be registered"
  );

  // Do a search.
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "test",
    waitForFocus: SimpleTest.waitForFocus,
  });

  // Get the row.
  let row = await UrlbarTestUtils.waitForAutocompleteResultAt(window, 0);
  Assert.equal(
    row.result.type,
    UrlbarUtils.RESULT_TYPE.DYNAMIC,
    "row.result.type"
  );
  Assert.equal(
    row.getAttribute("dynamicType"),
    "testDynamicType",
    "row[dynamicType]"
  );

  let text = row.querySelector(".urlbarView-dynamic-testDynamicType-text");

  // The view's call to provider.getViewUpdate is async, so we need to make sure
  // the update has been applied before continuing to avoid intermittent
  // failures.
  await TestUtils.waitForCondition(
    () => text.textContent == "This is a dynamic result."
  );

  // Check the elements.
  Assert.equal(
    text.textContent,
    "This is a dynamic result.",
    "text.textContent"
  );
  let button = row.querySelector(".urlbarView-dynamic-testDynamicType-button");
  Assert.equal(button.textContent, "Click Me", "button.textContent");

  // The result's button should be selected since the result is the heuristic.
  Assert.equal(
    UrlbarTestUtils.getSelectedElement(window),
    button,
    "Button should be selected"
  );

  // Pick the button.
  let pickPromise = ext.awaitMessage("onResultPicked");
  await UrlbarTestUtils.promisePopupClose(window, () =>
    EventUtils.synthesizeKey("KEY_Enter")
  );
  let [payload, elementName] = await pickPromise;
  Assert.equal(payload.dynamicType, "testDynamicType", "Picked payload");
  Assert.equal(elementName, "button", "Picked element name");

  await ext.unload();
});