toolkit/components/places/tests/unit/test_getPlacesInfo.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial