summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/queries/test_results-as-roots.js
blob: 2f082d3e0b97e767aeffda2d4fa1661654124792 (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
"use strict";

const MOBILE_BOOKMARKS_PREF = "browser.bookmarks.showMobileBookmarks";

const expectedRoots = [
  {
    title: "BookmarksToolbarFolderTitle",
    uri: `place:parent=${PlacesUtils.bookmarks.toolbarGuid}`,
    guid: PlacesUtils.bookmarks.virtualToolbarGuid,
  },
  {
    title: "BookmarksMenuFolderTitle",
    uri: `place:parent=${PlacesUtils.bookmarks.menuGuid}`,
    guid: PlacesUtils.bookmarks.virtualMenuGuid,
  },
  {
    title: "OtherBookmarksFolderTitle",
    uri: `place:parent=${PlacesUtils.bookmarks.unfiledGuid}`,
    guid: PlacesUtils.bookmarks.virtualUnfiledGuid,
  },
];

const expectedRootsWithMobile = [
  ...expectedRoots,
  {
    title: "MobileBookmarksFolderTitle",
    uri: `place:parent=${PlacesUtils.bookmarks.mobileGuid}`,
    guid: PlacesUtils.bookmarks.virtualMobileGuid,
  },
];

const placesStrings = Services.strings.createBundle(
  "chrome://places/locale/places.properties"
);

function getAllBookmarksQuery() {
  var query = PlacesUtils.history.getNewQuery();

  // Options
  var options = PlacesUtils.history.getNewQueryOptions();
  options.sortingMode = options.SORT_BY_VISITCOUNT_ASCENDING;
  options.resultType = options.RESULTS_AS_ROOTS_QUERY;

  // Results
  var result = PlacesUtils.history.executeQuery(query, options);
  return result.root;
}

function assertExpectedChildren(root, expectedChildren) {
  Assert.equal(
    root.childCount,
    expectedChildren.length,
    "Should have the expected number of children."
  );

  for (let i = 0; i < root.childCount; i++) {
    Assert.equal(
      root.getChild(i).uri,
      expectedChildren[i].uri,
      "Should have the correct uri for root ${i}"
    );
    Assert.equal(
      root.getChild(i).title,
      placesStrings.GetStringFromName(expectedChildren[i].title),
      "Should have the correct title for root ${i}"
    );
    Assert.equal(root.getChild(i).bookmarkGuid, expectedChildren[i].guid);
  }
}

/**
 * This test will test the basic RESULTS_AS_ROOTS_QUERY, that simply returns,
 * the existing bookmark roots.
 */
add_task(async function test_results_as_root() {
  let root = getAllBookmarksQuery();
  root.containerOpen = true;

  Assert.equal(
    PlacesUtils.asQuery(root).queryOptions.queryType,
    Ci.nsINavHistoryQueryOptions.QUERY_TYPE_BOOKMARKS,
    "Should have a query type of QUERY_TYPE_BOOKMARKS"
  );

  assertExpectedChildren(root, expectedRoots);

  root.containerOpen = false;
});

add_task(async function test_results_as_root_with_mobile() {
  Services.prefs.setBoolPref(MOBILE_BOOKMARKS_PREF, true);

  let root = getAllBookmarksQuery();
  root.containerOpen = true;

  assertExpectedChildren(root, expectedRootsWithMobile);

  root.containerOpen = false;
  Services.prefs.clearUserPref(MOBILE_BOOKMARKS_PREF);
});

add_task(async function test_results_as_root_remove_mobile_dynamic() {
  Services.prefs.setBoolPref(MOBILE_BOOKMARKS_PREF, true);

  let root = getAllBookmarksQuery();
  root.containerOpen = true;

  // Now un-set the pref, and poke the database to update the query.
  Services.prefs.clearUserPref(MOBILE_BOOKMARKS_PREF);

  assertExpectedChildren(root, expectedRoots);

  root.containerOpen = false;
});