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

// This is a test for asyncExecuteLegacyQuery API.

add_task(async function test_history_query() {
  let uri = "http://test.visit.mozilla.com/";
  let title = "Test visit";
  await PlacesTestUtils.addVisits({ uri, title });

  let options = PlacesUtils.history.getNewQueryOptions();
  options.sortingMode = Ci.nsINavHistoryQueryOptions.SORT_BY_DATE_DESCENDING;
  let query = PlacesUtils.history.getNewQuery();

  return new Promise(resolve => {
    PlacesUtils.history.asyncExecuteLegacyQuery(query, options, {
      handleResult(aResultSet) {
        for (let row; (row = aResultSet.getNextRow()); ) {
          try {
            Assert.equal(row.getResultByIndex(1), uri);
            Assert.equal(row.getResultByIndex(2), title);
          } catch (e) {
            do_throw("Error while fetching page data.");
          }
        }
      },
      handleError(aError) {
        do_throw(
          "Async execution error (" + aError.result + "): " + aError.message
        );
      },
      handleCompletion(aReason) {
        cleanupTest().then(resolve);
      },
    });
  });
});

add_task(async function test_bookmarks_query() {
  let url = "http://test.bookmark.mozilla.com/";
  let title = "Test bookmark";
  await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    title,
    url,
  });

  let options = PlacesUtils.history.getNewQueryOptions();
  options.sortingMode =
    Ci.nsINavHistoryQueryOptions.SORT_BY_LASTMODIFIED_DESCENDING;
  options.queryType = options.QUERY_TYPE_BOOKMARKS;
  let query = PlacesUtils.history.getNewQuery();

  return new Promise(resolve => {
    PlacesUtils.history.asyncExecuteLegacyQuery(query, options, {
      handleResult(aResultSet) {
        for (let row; (row = aResultSet.getNextRow()); ) {
          try {
            Assert.equal(row.getResultByIndex(1), url);
            Assert.equal(row.getResultByIndex(2), title);
          } catch (e) {
            do_throw("Error while fetching page data.");
          }
        }
      },
      handleError(aError) {
        do_throw(
          "Async execution error (" + aError.result + "): " + aError.message
        );
      },
      handleCompletion(aReason) {
        cleanupTest().then(resolve);
      },
    });
  });
});

function cleanupTest() {
  return Promise.all([
    PlacesUtils.history.clear(),
    PlacesUtils.bookmarks.eraseEverything(),
  ]);
}