summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/queries/test_queryMultipleFolder.js
blob: 7c24bef74e047e2b3fb3be67680195a720822389 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

var folderGuids = [];
var bookmarkGuids = [];

add_task(async function setup() {
  // adding bookmarks in the folders
  for (let i = 0; i < 3; ++i) {
    let folder = await PlacesUtils.bookmarks.insert({
      parentGuid: PlacesUtils.bookmarks.menuGuid,
      type: PlacesUtils.bookmarks.TYPE_FOLDER,
      title: `Folder${i}`,
    });
    folderGuids.push(folder.guid);

    for (let j = 0; j < 7; ++j) {
      let bm = await PlacesUtils.bookmarks.insert({
        parentGuid: folderGuids[i],
        url: `http://Bookmark${i}_${j}.com`,
        title: "",
      });
      bookmarkGuids.push(bm.guid);
    }
  }
});

add_task(async function test_queryMultipleFolders_ids() {
  // using queryStringToQuery
  let query = {},
    options = {};
  let maxResults = 20;
  let queryString = `place:${folderGuids
    .map(guid => "parent=" + guid)
    .join("&")}&sort=5&maxResults=${maxResults}`;
  PlacesUtils.history.queryStringToQuery(queryString, query, options);
  let rootNode = PlacesUtils.history.executeQuery(
    query.value,
    options.value
  ).root;
  rootNode.containerOpen = true;
  let resultLength = rootNode.childCount;
  Assert.equal(resultLength, maxResults);
  for (let i = 0; i < resultLength; ++i) {
    let node = rootNode.getChild(i);
    Assert.equal(bookmarkGuids[i], node.bookmarkGuid, node.uri);
  }
  rootNode.containerOpen = false;

  // using getNewQuery and getNewQueryOptions
  query = PlacesUtils.history.getNewQuery();
  options = PlacesUtils.history.getNewQueryOptions();
  query.setParents(folderGuids);
  options.sortingMode = options.SORT_BY_URI_ASCENDING;
  options.maxResults = maxResults;
  rootNode = PlacesUtils.history.executeQuery(query, options).root;
  rootNode.containerOpen = true;
  resultLength = rootNode.childCount;
  Assert.equal(resultLength, maxResults);
  for (let i = 0; i < resultLength; ++i) {
    let node = rootNode.getChild(i);
    Assert.equal(bookmarkGuids[i], node.bookmarkGuid, node.uri);
  }
  rootNode.containerOpen = false;
});

add_task(async function test_queryMultipleFolders_guids() {
  // using queryStringToQuery
  let query = {},
    options = {};
  let maxResults = 20;
  let queryString = `place:${folderGuids
    .map(guid => "parent=" + guid)
    .join("&")}&sort=5&maxResults=${maxResults}`;
  PlacesUtils.history.queryStringToQuery(queryString, query, options);
  let rootNode = PlacesUtils.history.executeQuery(
    query.value,
    options.value
  ).root;
  rootNode.containerOpen = true;
  let resultLength = rootNode.childCount;
  Assert.equal(resultLength, maxResults);
  for (let i = 0; i < resultLength; ++i) {
    let node = rootNode.getChild(i);
    Assert.equal(bookmarkGuids[i], node.bookmarkGuid, node.uri);
  }
  rootNode.containerOpen = false;

  // using getNewQuery and getNewQueryOptions
  query = PlacesUtils.history.getNewQuery();
  options = PlacesUtils.history.getNewQueryOptions();
  query.setParents(folderGuids);
  options.sortingMode = options.SORT_BY_URI_ASCENDING;
  options.maxResults = maxResults;
  rootNode = PlacesUtils.history.executeQuery(query, options).root;
  rootNode.containerOpen = true;
  resultLength = rootNode.childCount;
  Assert.equal(resultLength, maxResults);
  for (let i = 0; i < resultLength; ++i) {
    let node = rootNode.getChild(i);
    Assert.equal(bookmarkGuids[i], node.bookmarkGuid, node.uri);
  }
  rootNode.containerOpen = false;
});