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

function promiseGetPlacesInfo(aPlacesIdentifiers) {
  let deferred = Promise.defer();
  PlacesUtils.asyncHistory.getPlacesInfo(aPlacesIdentifiers, {
    _results: [],
    _errors: [],

    handleResult: function handleResult(aPlaceInfo) {
      this._results.push(aPlaceInfo);
    },
    handleError: function handleError(aResultCode, aPlaceInfo) {
      this._errors.push({ resultCode: aResultCode, info: aPlaceInfo });
    },
    handleCompletion: function handleCompletion() {
      deferred.resolve({ errors: this._errors, results: this._results });
    }
  });

  return deferred.promise;
}

function ensurePlacesInfoObjectsAreEqual(a, b) {
  do_check_true(a.uri.equals(b.uri));
  do_check_eq(a.title, b.title);
  do_check_eq(a.guid, b.guid);
  do_check_eq(a.placeId, b.placeId);
}

function* test_getPlacesInfoExistentPlace() {
  let testURI = NetUtil.newURI("http://www.example.tld");
  yield PlacesTestUtils.addVisits(testURI);

  let getPlacesInfoResult = yield promiseGetPlacesInfo([testURI]);
  do_check_eq(getPlacesInfoResult.results.length, 1);
  do_check_eq(getPlacesInfoResult.errors.length, 0);

  let placeInfo = getPlacesInfoResult.results[0];
  do_check_true(placeInfo instanceof Ci.mozIPlaceInfo);

  do_check_true(placeInfo.uri.equals(testURI));
  do_check_eq(placeInfo.title, "test visit for " + testURI.spec);
  do_check_true(placeInfo.guid.length > 0);
  do_check_eq(placeInfo.visits, null);
}
add_task(test_getPlacesInfoExistentPlace);

function* test_getPlacesInfoNonExistentPlace() {
  let testURI = NetUtil.newURI("http://www.example_non_existent.tld");
  let getPlacesInfoResult = yield promiseGetPlacesInfo(testURI);
  do_check_eq(getPlacesInfoResult.results.length, 0);
  do_check_eq(getPlacesInfoResult.errors.length, 1);
}
add_task(test_getPlacesInfoNonExistentPlace);

function* test_promisedHelper() {
  let uri = NetUtil.newURI("http://www.helper_existent_example.tld");
  yield PlacesTestUtils.addVisits(uri);
  let placeInfo = yield PlacesUtils.promisePlaceInfo(uri);
  do_check_true(placeInfo instanceof Ci.mozIPlaceInfo);

  uri = NetUtil.newURI("http://www.helper_non_existent_example.tld");
  try {
    yield PlacesUtils.promisePlaceInfo(uri);
    do_throw("PlacesUtils.promisePlaceInfo should have rejected the promise");
  }
  catch (ex) { }
}
add_task(test_promisedHelper);

function* test_infoByGUID() {
  let testURI = NetUtil.newURI("http://www.guid_example.tld");
  yield PlacesTestUtils.addVisits(testURI);

  let placeInfoByURI = yield PlacesUtils.promisePlaceInfo(testURI);
  let placeInfoByGUID = yield PlacesUtils.promisePlaceInfo(placeInfoByURI.guid);
  ensurePlacesInfoObjectsAreEqual(placeInfoByURI, placeInfoByGUID);
}
add_task(test_infoByGUID);

function* test_invalid_guid() {
  try {
    yield PlacesUtils.promisePlaceInfo("###");
    do_throw("getPlacesInfo should fail for invalid guids")
  }
  catch (ex) { }
}
add_task(test_invalid_guid);

function* test_mixed_selection() {
  let placeInfo1, placeInfo2;
  let uri = NetUtil.newURI("http://www.mixed_selection_test_1.tld");
  yield PlacesTestUtils.addVisits(uri);
  placeInfo1 = yield PlacesUtils.promisePlaceInfo(uri);

  uri = NetUtil.newURI("http://www.mixed_selection_test_2.tld");
  yield PlacesTestUtils.addVisits(uri);
  placeInfo2 = yield PlacesUtils.promisePlaceInfo(uri);

  let getPlacesInfoResult = yield promiseGetPlacesInfo([placeInfo1.uri, placeInfo2.guid]);
  do_check_eq(getPlacesInfoResult.results.length, 2);
  do_check_eq(getPlacesInfoResult.errors.length, 0);

  do_check_eq(getPlacesInfoResult.results[0].uri.spec, placeInfo1.uri.spec);
  do_check_eq(getPlacesInfoResult.results[1].guid, placeInfo2.guid);
}
add_task(test_mixed_selection);

function run_test() {
  run_next_test();
}