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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/unit/test_getPlacesInfo.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,116 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +function promiseGetPlacesInfo(aPlacesIdentifiers) {
     1.8 +  let deferred = Promise.defer();
     1.9 +  PlacesUtils.asyncHistory.getPlacesInfo(aPlacesIdentifiers, {
    1.10 +    _results: [],
    1.11 +    _errors: [],
    1.12 +
    1.13 +    handleResult: function handleResult(aPlaceInfo) {
    1.14 +      this._results.push(aPlaceInfo);
    1.15 +    },
    1.16 +    handleError: function handleError(aResultCode, aPlaceInfo) {
    1.17 +      this._errors.push({ resultCode: aResultCode, info: aPlaceInfo });
    1.18 +    },
    1.19 +    handleCompletion: function handleCompletion() {
    1.20 +      deferred.resolve({ errors: this._errors, results: this._results });
    1.21 +    }
    1.22 +  });
    1.23 +
    1.24 +  return deferred.promise;
    1.25 +}
    1.26 +
    1.27 +function ensurePlacesInfoObjectsAreEqual(a, b) {
    1.28 +  do_check_true(a.uri.equals(b.uri));
    1.29 +  do_check_eq(a.title, b.title);
    1.30 +  do_check_eq(a.guid, b.guid);
    1.31 +  do_check_eq(a.placeId, b.placeId);
    1.32 +}
    1.33 +
    1.34 +function test_getPlacesInfoExistentPlace() {
    1.35 +  let testURI = NetUtil.newURI("http://www.example.tld");
    1.36 +  yield promiseAddVisits(testURI);
    1.37 +
    1.38 +  let getPlacesInfoResult = yield promiseGetPlacesInfo([testURI]);
    1.39 +  do_check_eq(getPlacesInfoResult.results.length, 1);
    1.40 +  do_check_eq(getPlacesInfoResult.errors.length, 0);
    1.41 +
    1.42 +  let placeInfo = getPlacesInfoResult.results[0];
    1.43 +  do_check_true(placeInfo instanceof Ci.mozIPlaceInfo);
    1.44 +
    1.45 +  do_check_true(placeInfo.uri.equals(testURI));
    1.46 +  do_check_eq(placeInfo.title, "test visit for " + testURI.spec);
    1.47 +  do_check_true(placeInfo.guid.length > 0);
    1.48 +  do_check_eq(placeInfo.visits, null);
    1.49 +}
    1.50 +add_task(test_getPlacesInfoExistentPlace);
    1.51 +
    1.52 +function test_getPlacesInfoNonExistentPlace() {
    1.53 +  let testURI = NetUtil.newURI("http://www.example_non_existent.tld");
    1.54 +  let getPlacesInfoResult = yield promiseGetPlacesInfo(testURI);
    1.55 +  do_check_eq(getPlacesInfoResult.results.length, 0);
    1.56 +  do_check_eq(getPlacesInfoResult.errors.length, 1);
    1.57 +}
    1.58 +add_task(test_getPlacesInfoNonExistentPlace);
    1.59 +
    1.60 +function test_promisedHelper() {
    1.61 +  let (uri = NetUtil.newURI("http://www.helper_existent_example.tld")) {
    1.62 +    yield promiseAddVisits(uri);
    1.63 +    let placeInfo = yield PlacesUtils.promisePlaceInfo(uri);
    1.64 +    do_check_true(placeInfo instanceof Ci.mozIPlaceInfo);
    1.65 +  };
    1.66 +
    1.67 +  let (uri = NetUtil.newURI("http://www.helper_non_existent_example.tld")) {
    1.68 +    try {
    1.69 +      let placeInfo = yield PlacesUtils.promisePlaceInfo(uri);
    1.70 +      do_throw("PlacesUtils.promisePlaceInfo should have rejected the promise");
    1.71 +    }
    1.72 +    catch(ex) { }
    1.73 +  };
    1.74 +}
    1.75 +add_task(test_promisedHelper);
    1.76 +
    1.77 +function test_infoByGUID() {
    1.78 +  let testURI = NetUtil.newURI("http://www.guid_example.tld");
    1.79 +  yield promiseAddVisits(testURI);
    1.80 +
    1.81 +  let placeInfoByURI = yield PlacesUtils.promisePlaceInfo(testURI);
    1.82 +  let placeInfoByGUID = yield PlacesUtils.promisePlaceInfo(placeInfoByURI.guid);
    1.83 +  ensurePlacesInfoObjectsAreEqual(placeInfoByURI, placeInfoByGUID);
    1.84 +}
    1.85 +add_task(test_infoByGUID);
    1.86 +
    1.87 +function test_invalid_guid() {
    1.88 +  try {
    1.89 +    let placeInfoByGUID = yield PlacesUtils.promisePlaceInfo("###");
    1.90 +    do_throw("getPlacesInfo should fail for invalid guids")
    1.91 +  }
    1.92 +  catch(ex) { }
    1.93 +}
    1.94 +add_task(test_invalid_guid);
    1.95 +
    1.96 +function test_mixed_selection() {
    1.97 +  let placeInfo1, placeInfo2;
    1.98 +  let (uri = NetUtil.newURI("http://www.mixed_selection_test_1.tld")) {
    1.99 +    yield promiseAddVisits(uri);
   1.100 +    placeInfo1 = yield PlacesUtils.promisePlaceInfo(uri);
   1.101 +  };
   1.102 +
   1.103 +  let (uri = NetUtil.newURI("http://www.mixed_selection_test_2.tld")) {
   1.104 +    yield promiseAddVisits(uri);
   1.105 +    placeInfo2 = yield PlacesUtils.promisePlaceInfo(uri);
   1.106 +  };
   1.107 +
   1.108 +  let getPlacesInfoResult = yield promiseGetPlacesInfo([placeInfo1.uri, placeInfo2.guid]);
   1.109 +  do_check_eq(getPlacesInfoResult.results.length, 2);
   1.110 +  do_check_eq(getPlacesInfoResult.errors.length, 0);
   1.111 +
   1.112 +  do_check_eq(getPlacesInfoResult.results[0].uri.spec, placeInfo1.uri.spec);
   1.113 +  do_check_eq(getPlacesInfoResult.results[1].guid, placeInfo2.guid);
   1.114 +}
   1.115 +add_task(test_mixed_selection);
   1.116 +
   1.117 +function run_test() {
   1.118 +  run_next_test();
   1.119 +}

mercurial