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

"use strict";

// Tests that results with a suggestedIndex property end up in the expected
// position.

add_task(async function suggestedIndex() {
  let result1 = new UrlbarResult(
    UrlbarUtils.RESULT_TYPE.URL,
    UrlbarUtils.RESULT_SOURCE.HISTORY,
    { url: "http://mozilla.org/1" }
  );
  result1.suggestedIndex = 2;
  let result2 = new UrlbarResult(
    UrlbarUtils.RESULT_TYPE.URL,
    UrlbarUtils.RESULT_SOURCE.HISTORY,
    { url: "http://mozilla.org/2" }
  );
  result2.suggestedIndex = 6;

  let provider = new UrlbarTestUtils.TestProvider({
    results: [result1, result2],
  });
  UrlbarProvidersManager.registerProvider(provider);
  async function clean() {
    UrlbarProvidersManager.unregisterProvider(provider);
    await PlacesUtils.history.clear();
  }
  registerCleanupFunction(clean);

  let urls = [];
  let maxResults = UrlbarPrefs.get("maxRichResults");
  // Add more results, so that the sum of these results plus the above ones,
  // will be greater than maxResults.
  for (let i = 0; i < maxResults; ++i) {
    urls.push("http://example.com/foo" + i);
  }
  await PlacesTestUtils.addVisits(urls);

  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "foo",
  });

  Assert.equal(
    UrlbarTestUtils.getResultCount(window),
    maxResults,
    `There should be ${maxResults} results in the view.`
  );

  urls.reverse();
  urls.unshift(
    (await Services.search.getDefault()).getSubmission("foo").uri.spec
  );
  urls.splice(result1.suggestedIndex, 0, result1.payload.url);
  urls.splice(result2.suggestedIndex, 0, result2.payload.url);
  urls = urls.slice(0, maxResults);

  let expected = [];
  for (let i = 0; i < maxResults; ++i) {
    let url = (await UrlbarTestUtils.getDetailsOfResultAt(window, i)).url;
    expected.push(url);
  }
  // Check all the results.
  Assert.deepEqual(expected, urls);

  await clean();
  await UrlbarTestUtils.promisePopupClose(window);
});

add_task(async function suggestedIndex_append() {
  // When suggestedIndex is greater than the number of results the result is
  // appended.
  let result = new UrlbarResult(
    UrlbarUtils.RESULT_TYPE.URL,
    UrlbarUtils.RESULT_SOURCE.HISTORY,
    { url: "http://mozilla.org/append/" }
  );
  result.suggestedIndex = 4;

  let provider = new UrlbarTestUtils.TestProvider({ results: [result] });
  UrlbarProvidersManager.registerProvider(provider);
  async function clean() {
    UrlbarProvidersManager.unregisterProvider(provider);
    await PlacesUtils.history.clear();
  }
  registerCleanupFunction(clean);

  await PlacesTestUtils.addVisits("http://example.com/bar");

  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "bar",
  });

  Assert.equal(
    UrlbarTestUtils.getResultCount(window),
    3,
    `There should be 3 results in the view.`
  );

  let urls = [
    (await Services.search.getDefault()).getSubmission("bar").uri.spec,
    "http://example.com/bar",
    "http://mozilla.org/append/",
  ];

  let expected = [];
  for (let i = 0; i < 3; ++i) {
    let url = (await UrlbarTestUtils.getDetailsOfResultAt(window, i)).url;
    expected.push(url);
  }
  // Check all the results.
  Assert.deepEqual(expected, urls);

  await clean();
  await UrlbarTestUtils.promisePopupClose(window);
});