toolkit/modules/tests/xpcshell/test_propertyListsUtils.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/modules/tests/xpcshell/test_propertyListsUtils.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,102 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +"use strict";
     1.8 +
     1.9 +Components.utils.import("resource://gre/modules/PropertyListUtils.jsm");
    1.10 +
    1.11 +function checkValue(aPropertyListObject, aType, aValue) {
    1.12 +  do_check_eq(PropertyListUtils.getObjectType(aPropertyListObject), aType);
    1.13 +  if (aValue !== undefined) {
    1.14 +    // Perform strict equality checks until Bug 714467 is fixed.
    1.15 +    let strictEqualityCheck = function(a, b) {
    1.16 +      do_check_eq(typeof(a), typeof(b));
    1.17 +      do_check_eq(a, b);
    1.18 +    };
    1.19 +
    1.20 +    if (typeof(aPropertyListObject) == "object")
    1.21 +      strictEqualityCheck(aPropertyListObject.valueOf(), aValue.valueOf());
    1.22 +    else
    1.23 +      strictEqualityCheck(aPropertyListObject, aValue);
    1.24 +  }
    1.25 +}
    1.26 +
    1.27 +function checkLazyGetterValue(aObject, aPropertyName, aType, aValue) {
    1.28 +  let descriptor = Object.getOwnPropertyDescriptor(aObject, aPropertyName);
    1.29 +  do_check_eq(typeof(descriptor.get), "function");
    1.30 +  do_check_eq(typeof(descriptor.value), "undefined");
    1.31 +  checkValue(aObject[aPropertyName], aType, aValue);
    1.32 +  descriptor = Object.getOwnPropertyDescriptor(aObject, aPropertyName);
    1.33 +  do_check_eq(typeof(descriptor.get), "undefined");
    1.34 +  do_check_neq(typeof(descriptor.value), "undefined");
    1.35 +}
    1.36 +
    1.37 +function checkMainPropertyList(aPropertyListRoot) {
    1.38 +  const PRIMITIVE = PropertyListUtils.TYPE_PRIMITIVE;
    1.39 +
    1.40 +  checkValue(aPropertyListRoot, PropertyListUtils.TYPE_DICTIONARY);
    1.41 +  checkValue(aPropertyListRoot.get("Boolean"), PRIMITIVE, false);
    1.42 +  let (array = aPropertyListRoot.get("Array")) {
    1.43 +    checkValue(array, PropertyListUtils.TYPE_ARRAY);
    1.44 +    do_check_eq(array.length, 8);
    1.45 +
    1.46 +    // Test both long and short values, since binary property lists store
    1.47 +    // long values a little bit differently (see readDataLengthAndOffset).
    1.48 +
    1.49 +    // Short ASCII string
    1.50 +    checkLazyGetterValue(array, 0, PRIMITIVE, "abc");
    1.51 +    // Long ASCII string
    1.52 +    checkLazyGetterValue(array, 1, PRIMITIVE, new Array(1001).join("a"));
    1.53 +    // Short unicode string
    1.54 +    checkLazyGetterValue(array, 2, PRIMITIVE, "\u05D0\u05D0\u05D0");
    1.55 +    // Long unicode string
    1.56 +    checkLazyGetterValue(array, 3, PRIMITIVE, new Array(1001).join("\u05D0"));
    1.57 +    // Unicode surrogate pair
    1.58 +    checkLazyGetterValue(array, 4, PRIMITIVE,
    1.59 +                         "\uD800\uDC00\uD800\uDC00\uD800\uDC00");
    1.60 +
    1.61 +    // Date
    1.62 +    checkLazyGetterValue(array, 5, PropertyListUtils.TYPE_DATE,
    1.63 +                         new Date("2011-12-31T11:15:23Z"));
    1.64 +
    1.65 +    // Data
    1.66 +    checkLazyGetterValue(array, 6, PropertyListUtils.TYPE_UINT8_ARRAY);
    1.67 +    let dataAsString = [String.fromCharCode(b) for each (b in array[6])].join("");
    1.68 +    do_check_eq(dataAsString, "2011-12-31T11:15:33Z");
    1.69 +
    1.70 +    // Dict
    1.71 +    let (dict = array[7]) {
    1.72 +      checkValue(dict, PropertyListUtils.TYPE_DICTIONARY);
    1.73 +      checkValue(dict.get("Negative Number"), PRIMITIVE, -400);
    1.74 +      checkValue(dict.get("Real Number"), PRIMITIVE, 2.71828183);
    1.75 +      checkValue(dict.get("Big Int"),
    1.76 +                 PropertyListUtils.TYPE_INT64,
    1.77 +                 "9007199254740993");
    1.78 +      checkValue(dict.get("Negative Big Int"),
    1.79 +                 PropertyListUtils.TYPE_INT64,
    1.80 +                 "-9007199254740993");
    1.81 +    }
    1.82 +  }
    1.83 +}
    1.84 +
    1.85 +function readPropertyList(aFile, aCallback) {
    1.86 +  PropertyListUtils.read(aFile, function(aPropertyListRoot) {
    1.87 +    // Null root indicates failure to read property list.
    1.88 +    // Note: It is important not to run do_check_n/eq directly on Dict and array
    1.89 +    // objects, because it cases their toString to get invoked, doing away with
    1.90 +    // all the lazy getter we'd like to test later.
    1.91 +    do_check_true(aPropertyListRoot !== null);
    1.92 +    aCallback(aPropertyListRoot);
    1.93 +    run_next_test();
    1.94 +  });
    1.95 +}
    1.96 +
    1.97 +function run_test() {
    1.98 +  add_test(readPropertyList.bind(this,
    1.99 +    do_get_file("propertyLists/bug710259_propertyListBinary.plist", false),
   1.100 +    checkMainPropertyList));
   1.101 +  add_test(readPropertyList.bind(this,
   1.102 +    do_get_file("propertyLists/bug710259_propertyListXML.plist", false),
   1.103 +    checkMainPropertyList));
   1.104 +  run_next_test();
   1.105 +}

mercurial