|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 function promiseGetPlacesInfo(aPlacesIdentifiers) { |
|
5 let deferred = Promise.defer(); |
|
6 PlacesUtils.asyncHistory.getPlacesInfo(aPlacesIdentifiers, { |
|
7 _results: [], |
|
8 _errors: [], |
|
9 |
|
10 handleResult: function handleResult(aPlaceInfo) { |
|
11 this._results.push(aPlaceInfo); |
|
12 }, |
|
13 handleError: function handleError(aResultCode, aPlaceInfo) { |
|
14 this._errors.push({ resultCode: aResultCode, info: aPlaceInfo }); |
|
15 }, |
|
16 handleCompletion: function handleCompletion() { |
|
17 deferred.resolve({ errors: this._errors, results: this._results }); |
|
18 } |
|
19 }); |
|
20 |
|
21 return deferred.promise; |
|
22 } |
|
23 |
|
24 function ensurePlacesInfoObjectsAreEqual(a, b) { |
|
25 do_check_true(a.uri.equals(b.uri)); |
|
26 do_check_eq(a.title, b.title); |
|
27 do_check_eq(a.guid, b.guid); |
|
28 do_check_eq(a.placeId, b.placeId); |
|
29 } |
|
30 |
|
31 function test_getPlacesInfoExistentPlace() { |
|
32 let testURI = NetUtil.newURI("http://www.example.tld"); |
|
33 yield promiseAddVisits(testURI); |
|
34 |
|
35 let getPlacesInfoResult = yield promiseGetPlacesInfo([testURI]); |
|
36 do_check_eq(getPlacesInfoResult.results.length, 1); |
|
37 do_check_eq(getPlacesInfoResult.errors.length, 0); |
|
38 |
|
39 let placeInfo = getPlacesInfoResult.results[0]; |
|
40 do_check_true(placeInfo instanceof Ci.mozIPlaceInfo); |
|
41 |
|
42 do_check_true(placeInfo.uri.equals(testURI)); |
|
43 do_check_eq(placeInfo.title, "test visit for " + testURI.spec); |
|
44 do_check_true(placeInfo.guid.length > 0); |
|
45 do_check_eq(placeInfo.visits, null); |
|
46 } |
|
47 add_task(test_getPlacesInfoExistentPlace); |
|
48 |
|
49 function test_getPlacesInfoNonExistentPlace() { |
|
50 let testURI = NetUtil.newURI("http://www.example_non_existent.tld"); |
|
51 let getPlacesInfoResult = yield promiseGetPlacesInfo(testURI); |
|
52 do_check_eq(getPlacesInfoResult.results.length, 0); |
|
53 do_check_eq(getPlacesInfoResult.errors.length, 1); |
|
54 } |
|
55 add_task(test_getPlacesInfoNonExistentPlace); |
|
56 |
|
57 function test_promisedHelper() { |
|
58 let (uri = NetUtil.newURI("http://www.helper_existent_example.tld")) { |
|
59 yield promiseAddVisits(uri); |
|
60 let placeInfo = yield PlacesUtils.promisePlaceInfo(uri); |
|
61 do_check_true(placeInfo instanceof Ci.mozIPlaceInfo); |
|
62 }; |
|
63 |
|
64 let (uri = NetUtil.newURI("http://www.helper_non_existent_example.tld")) { |
|
65 try { |
|
66 let placeInfo = yield PlacesUtils.promisePlaceInfo(uri); |
|
67 do_throw("PlacesUtils.promisePlaceInfo should have rejected the promise"); |
|
68 } |
|
69 catch(ex) { } |
|
70 }; |
|
71 } |
|
72 add_task(test_promisedHelper); |
|
73 |
|
74 function test_infoByGUID() { |
|
75 let testURI = NetUtil.newURI("http://www.guid_example.tld"); |
|
76 yield promiseAddVisits(testURI); |
|
77 |
|
78 let placeInfoByURI = yield PlacesUtils.promisePlaceInfo(testURI); |
|
79 let placeInfoByGUID = yield PlacesUtils.promisePlaceInfo(placeInfoByURI.guid); |
|
80 ensurePlacesInfoObjectsAreEqual(placeInfoByURI, placeInfoByGUID); |
|
81 } |
|
82 add_task(test_infoByGUID); |
|
83 |
|
84 function test_invalid_guid() { |
|
85 try { |
|
86 let placeInfoByGUID = yield PlacesUtils.promisePlaceInfo("###"); |
|
87 do_throw("getPlacesInfo should fail for invalid guids") |
|
88 } |
|
89 catch(ex) { } |
|
90 } |
|
91 add_task(test_invalid_guid); |
|
92 |
|
93 function test_mixed_selection() { |
|
94 let placeInfo1, placeInfo2; |
|
95 let (uri = NetUtil.newURI("http://www.mixed_selection_test_1.tld")) { |
|
96 yield promiseAddVisits(uri); |
|
97 placeInfo1 = yield PlacesUtils.promisePlaceInfo(uri); |
|
98 }; |
|
99 |
|
100 let (uri = NetUtil.newURI("http://www.mixed_selection_test_2.tld")) { |
|
101 yield promiseAddVisits(uri); |
|
102 placeInfo2 = yield PlacesUtils.promisePlaceInfo(uri); |
|
103 }; |
|
104 |
|
105 let getPlacesInfoResult = yield promiseGetPlacesInfo([placeInfo1.uri, placeInfo2.guid]); |
|
106 do_check_eq(getPlacesInfoResult.results.length, 2); |
|
107 do_check_eq(getPlacesInfoResult.errors.length, 0); |
|
108 |
|
109 do_check_eq(getPlacesInfoResult.results[0].uri.spec, placeInfo1.uri.spec); |
|
110 do_check_eq(getPlacesInfoResult.results[1].guid, placeInfo2.guid); |
|
111 } |
|
112 add_task(test_mixed_selection); |
|
113 |
|
114 function run_test() { |
|
115 run_next_test(); |
|
116 } |