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

// This is a test for asyncExecuteLegacyQueries API.

var tests = [

function test_history_query() {
  let uri = NetUtil.newURI("http://test.visit.mozilla.com/");
  let title = "Test visit";
  PlacesTestUtils.addVisits({ uri: uri, title: title }).then(function () {
    let options = PlacesUtils.history.getNewQueryOptions();
    options.sortingMode = Ci.nsINavHistoryQueryOptions.SORT_BY_DATE_DESCENDING;
    let query = PlacesUtils.history.getNewQuery();

    PlacesUtils.history.QueryInterface(Ci.nsPIPlacesDatabase)
                       .asyncExecuteLegacyQueries([query], 1, options, {
      handleResult: function (aResultSet) {
        for (let row; (row = aResultSet.getNextRow());) {
          try {
            do_check_eq(row.getResultByIndex(1), uri.spec);
            do_check_eq(row.getResultByIndex(2), title);
          } catch (e) {
            do_throw("Error while fetching page data.");
          }
        }
      },
      handleError: function (aError) {
        do_throw("Async execution error (" + aError.result + "): " + aError.message);
      },
      handleCompletion: function (aReason) {
        run_next_test();
      },
    });
  });
},

function test_bookmarks_query() {
  let uri = NetUtil.newURI("http://test.bookmark.mozilla.com/");
  let title = "Test bookmark";
  bookmark(uri, title);
  let options = PlacesUtils.history.getNewQueryOptions();
  options.sortingMode = Ci.nsINavHistoryQueryOptions.SORT_BY_LASMODIFIED_DESCENDING;
  options.queryType = options.QUERY_TYPE_BOOKMARKS;
  let query = PlacesUtils.history.getNewQuery();

  PlacesUtils.history.QueryInterface(Ci.nsPIPlacesDatabase)
                     .asyncExecuteLegacyQueries([query], 1, options, {
    handleResult: function (aResultSet) {
      for (let row; (row = aResultSet.getNextRow());) {
        try {
          do_check_eq(row.getResultByIndex(1), uri.spec);
          do_check_eq(row.getResultByIndex(2), title);
        } catch (e) {
          do_throw("Error while fetching page data.");
        }
      }
    },
    handleError: function (aError) {
      do_throw("Async execution error (" + aError.result + "): " + aError.message);
    },
    handleCompletion: function (aReason) {
      run_next_test();
    },
  });
},

];

function bookmark(aURI, aTitle)
{
  PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId,
                                       aURI,
                                       PlacesUtils.bookmarks.DEFAULT_INDEX,
                                       aTitle);
}

function run_test()
{
  do_test_pending();
  run_next_test();
}

function run_next_test() {
  if (tests.length == 0) {
    do_test_finished();
    return;
  }

  Promise.all([
    PlacesTestUtils.clearHistory(),
    PlacesUtils.bookmarks.eraseEverything()
  ]).then(tests.shift());
}