summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/browser/browser_history_sidebar_search.js
blob: 44d51eec31eb5adc9386e3fa7ff4aa44c76a6eb3 (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
add_task(async function test() {
  let sidebar = document.getElementById("sidebar");

  // Visited pages listed by descending visit date.
  let pages = [
    "http://sidebar.mozilla.org/a",
    "http://sidebar.mozilla.org/b",
    "http://sidebar.mozilla.org/c",
    "http://www.mozilla.org/d",
  ];

  // Number of pages that will be filtered out by the search.
  const FILTERED_COUNT = 1;

  await PlacesUtils.history.clear();

  // Add some visited page.
  let time = Date.now();
  let places = [];
  for (let i = 0; i < pages.length; i++) {
    places.push({
      uri: NetUtil.newURI(pages[i]),
      visitDate: (time - i) * 1000,
      transition: PlacesUtils.history.TRANSITION_TYPED,
    });
  }
  await PlacesTestUtils.addVisits(places);

  await withSidebarTree("history", function () {
    info("Set 'by last visited' view");
    sidebar.contentDocument.getElementById("bylastvisited").doCommand();
    let tree = sidebar.contentDocument.getElementById("historyTree");
    check_tree_order(tree, pages);

    // Set a search value.
    let searchBox = sidebar.contentDocument.getElementById("search-box");
    ok(searchBox, "search box is in context");
    searchBox.value = "sidebar.mozilla";
    searchBox.doCommand();
    check_tree_order(tree, pages, -FILTERED_COUNT);

    info("Reset the search");
    searchBox.value = "";
    searchBox.doCommand();
    check_tree_order(tree, pages);
  });

  await PlacesUtils.history.clear();
});

function check_tree_order(tree, pages, aNumberOfRowsDelta = 0) {
  let treeView = tree.view;
  let columns = tree.columns;
  is(columns.count, 1, "There should be only 1 column in the sidebar");

  let found = 0;
  for (let i = 0; i < treeView.rowCount; i++) {
    let node = treeView.nodeForTreeIndex(i);
    // We could inherit delayed visits from previous tests, skip them.
    if (!pages.includes(node.uri)) {
      continue;
    }
    is(
      node.uri,
      pages[i],
      "Node is in correct position based on its visit date"
    );
    found++;
  }
  is(found, pages.length + aNumberOfRowsDelta, "Found all expected results");
}