michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: Components.utils.import("resource://gre/modules/PropertyListUtils.jsm"); michael@0: michael@0: function checkValue(aPropertyListObject, aType, aValue) { michael@0: do_check_eq(PropertyListUtils.getObjectType(aPropertyListObject), aType); michael@0: if (aValue !== undefined) { michael@0: // Perform strict equality checks until Bug 714467 is fixed. michael@0: let strictEqualityCheck = function(a, b) { michael@0: do_check_eq(typeof(a), typeof(b)); michael@0: do_check_eq(a, b); michael@0: }; michael@0: michael@0: if (typeof(aPropertyListObject) == "object") michael@0: strictEqualityCheck(aPropertyListObject.valueOf(), aValue.valueOf()); michael@0: else michael@0: strictEqualityCheck(aPropertyListObject, aValue); michael@0: } michael@0: } michael@0: michael@0: function checkLazyGetterValue(aObject, aPropertyName, aType, aValue) { michael@0: let descriptor = Object.getOwnPropertyDescriptor(aObject, aPropertyName); michael@0: do_check_eq(typeof(descriptor.get), "function"); michael@0: do_check_eq(typeof(descriptor.value), "undefined"); michael@0: checkValue(aObject[aPropertyName], aType, aValue); michael@0: descriptor = Object.getOwnPropertyDescriptor(aObject, aPropertyName); michael@0: do_check_eq(typeof(descriptor.get), "undefined"); michael@0: do_check_neq(typeof(descriptor.value), "undefined"); michael@0: } michael@0: michael@0: function checkMainPropertyList(aPropertyListRoot) { michael@0: const PRIMITIVE = PropertyListUtils.TYPE_PRIMITIVE; michael@0: michael@0: checkValue(aPropertyListRoot, PropertyListUtils.TYPE_DICTIONARY); michael@0: checkValue(aPropertyListRoot.get("Boolean"), PRIMITIVE, false); michael@0: let (array = aPropertyListRoot.get("Array")) { michael@0: checkValue(array, PropertyListUtils.TYPE_ARRAY); michael@0: do_check_eq(array.length, 8); michael@0: michael@0: // Test both long and short values, since binary property lists store michael@0: // long values a little bit differently (see readDataLengthAndOffset). michael@0: michael@0: // Short ASCII string michael@0: checkLazyGetterValue(array, 0, PRIMITIVE, "abc"); michael@0: // Long ASCII string michael@0: checkLazyGetterValue(array, 1, PRIMITIVE, new Array(1001).join("a")); michael@0: // Short unicode string michael@0: checkLazyGetterValue(array, 2, PRIMITIVE, "\u05D0\u05D0\u05D0"); michael@0: // Long unicode string michael@0: checkLazyGetterValue(array, 3, PRIMITIVE, new Array(1001).join("\u05D0")); michael@0: // Unicode surrogate pair michael@0: checkLazyGetterValue(array, 4, PRIMITIVE, michael@0: "\uD800\uDC00\uD800\uDC00\uD800\uDC00"); michael@0: michael@0: // Date michael@0: checkLazyGetterValue(array, 5, PropertyListUtils.TYPE_DATE, michael@0: new Date("2011-12-31T11:15:23Z")); michael@0: michael@0: // Data michael@0: checkLazyGetterValue(array, 6, PropertyListUtils.TYPE_UINT8_ARRAY); michael@0: let dataAsString = [String.fromCharCode(b) for each (b in array[6])].join(""); michael@0: do_check_eq(dataAsString, "2011-12-31T11:15:33Z"); michael@0: michael@0: // Dict michael@0: let (dict = array[7]) { michael@0: checkValue(dict, PropertyListUtils.TYPE_DICTIONARY); michael@0: checkValue(dict.get("Negative Number"), PRIMITIVE, -400); michael@0: checkValue(dict.get("Real Number"), PRIMITIVE, 2.71828183); michael@0: checkValue(dict.get("Big Int"), michael@0: PropertyListUtils.TYPE_INT64, michael@0: "9007199254740993"); michael@0: checkValue(dict.get("Negative Big Int"), michael@0: PropertyListUtils.TYPE_INT64, michael@0: "-9007199254740993"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: function readPropertyList(aFile, aCallback) { michael@0: PropertyListUtils.read(aFile, function(aPropertyListRoot) { michael@0: // Null root indicates failure to read property list. michael@0: // Note: It is important not to run do_check_n/eq directly on Dict and array michael@0: // objects, because it cases their toString to get invoked, doing away with michael@0: // all the lazy getter we'd like to test later. michael@0: do_check_true(aPropertyListRoot !== null); michael@0: aCallback(aPropertyListRoot); michael@0: run_next_test(); michael@0: }); michael@0: } michael@0: michael@0: function run_test() { michael@0: add_test(readPropertyList.bind(this, michael@0: do_get_file("propertyLists/bug710259_propertyListBinary.plist", false), michael@0: checkMainPropertyList)); michael@0: add_test(readPropertyList.bind(this, michael@0: do_get_file("propertyLists/bug710259_propertyListXML.plist", false), michael@0: checkMainPropertyList)); michael@0: run_next_test(); michael@0: }