toolkit/modules/tests/xpcshell/test_propertyListsUtils.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 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 "use strict";
michael@0 5
michael@0 6 Components.utils.import("resource://gre/modules/PropertyListUtils.jsm");
michael@0 7
michael@0 8 function checkValue(aPropertyListObject, aType, aValue) {
michael@0 9 do_check_eq(PropertyListUtils.getObjectType(aPropertyListObject), aType);
michael@0 10 if (aValue !== undefined) {
michael@0 11 // Perform strict equality checks until Bug 714467 is fixed.
michael@0 12 let strictEqualityCheck = function(a, b) {
michael@0 13 do_check_eq(typeof(a), typeof(b));
michael@0 14 do_check_eq(a, b);
michael@0 15 };
michael@0 16
michael@0 17 if (typeof(aPropertyListObject) == "object")
michael@0 18 strictEqualityCheck(aPropertyListObject.valueOf(), aValue.valueOf());
michael@0 19 else
michael@0 20 strictEqualityCheck(aPropertyListObject, aValue);
michael@0 21 }
michael@0 22 }
michael@0 23
michael@0 24 function checkLazyGetterValue(aObject, aPropertyName, aType, aValue) {
michael@0 25 let descriptor = Object.getOwnPropertyDescriptor(aObject, aPropertyName);
michael@0 26 do_check_eq(typeof(descriptor.get), "function");
michael@0 27 do_check_eq(typeof(descriptor.value), "undefined");
michael@0 28 checkValue(aObject[aPropertyName], aType, aValue);
michael@0 29 descriptor = Object.getOwnPropertyDescriptor(aObject, aPropertyName);
michael@0 30 do_check_eq(typeof(descriptor.get), "undefined");
michael@0 31 do_check_neq(typeof(descriptor.value), "undefined");
michael@0 32 }
michael@0 33
michael@0 34 function checkMainPropertyList(aPropertyListRoot) {
michael@0 35 const PRIMITIVE = PropertyListUtils.TYPE_PRIMITIVE;
michael@0 36
michael@0 37 checkValue(aPropertyListRoot, PropertyListUtils.TYPE_DICTIONARY);
michael@0 38 checkValue(aPropertyListRoot.get("Boolean"), PRIMITIVE, false);
michael@0 39 let (array = aPropertyListRoot.get("Array")) {
michael@0 40 checkValue(array, PropertyListUtils.TYPE_ARRAY);
michael@0 41 do_check_eq(array.length, 8);
michael@0 42
michael@0 43 // Test both long and short values, since binary property lists store
michael@0 44 // long values a little bit differently (see readDataLengthAndOffset).
michael@0 45
michael@0 46 // Short ASCII string
michael@0 47 checkLazyGetterValue(array, 0, PRIMITIVE, "abc");
michael@0 48 // Long ASCII string
michael@0 49 checkLazyGetterValue(array, 1, PRIMITIVE, new Array(1001).join("a"));
michael@0 50 // Short unicode string
michael@0 51 checkLazyGetterValue(array, 2, PRIMITIVE, "\u05D0\u05D0\u05D0");
michael@0 52 // Long unicode string
michael@0 53 checkLazyGetterValue(array, 3, PRIMITIVE, new Array(1001).join("\u05D0"));
michael@0 54 // Unicode surrogate pair
michael@0 55 checkLazyGetterValue(array, 4, PRIMITIVE,
michael@0 56 "\uD800\uDC00\uD800\uDC00\uD800\uDC00");
michael@0 57
michael@0 58 // Date
michael@0 59 checkLazyGetterValue(array, 5, PropertyListUtils.TYPE_DATE,
michael@0 60 new Date("2011-12-31T11:15:23Z"));
michael@0 61
michael@0 62 // Data
michael@0 63 checkLazyGetterValue(array, 6, PropertyListUtils.TYPE_UINT8_ARRAY);
michael@0 64 let dataAsString = [String.fromCharCode(b) for each (b in array[6])].join("");
michael@0 65 do_check_eq(dataAsString, "2011-12-31T11:15:33Z");
michael@0 66
michael@0 67 // Dict
michael@0 68 let (dict = array[7]) {
michael@0 69 checkValue(dict, PropertyListUtils.TYPE_DICTIONARY);
michael@0 70 checkValue(dict.get("Negative Number"), PRIMITIVE, -400);
michael@0 71 checkValue(dict.get("Real Number"), PRIMITIVE, 2.71828183);
michael@0 72 checkValue(dict.get("Big Int"),
michael@0 73 PropertyListUtils.TYPE_INT64,
michael@0 74 "9007199254740993");
michael@0 75 checkValue(dict.get("Negative Big Int"),
michael@0 76 PropertyListUtils.TYPE_INT64,
michael@0 77 "-9007199254740993");
michael@0 78 }
michael@0 79 }
michael@0 80 }
michael@0 81
michael@0 82 function readPropertyList(aFile, aCallback) {
michael@0 83 PropertyListUtils.read(aFile, function(aPropertyListRoot) {
michael@0 84 // Null root indicates failure to read property list.
michael@0 85 // Note: It is important not to run do_check_n/eq directly on Dict and array
michael@0 86 // objects, because it cases their toString to get invoked, doing away with
michael@0 87 // all the lazy getter we'd like to test later.
michael@0 88 do_check_true(aPropertyListRoot !== null);
michael@0 89 aCallback(aPropertyListRoot);
michael@0 90 run_next_test();
michael@0 91 });
michael@0 92 }
michael@0 93
michael@0 94 function run_test() {
michael@0 95 add_test(readPropertyList.bind(this,
michael@0 96 do_get_file("propertyLists/bug710259_propertyListBinary.plist", false),
michael@0 97 checkMainPropertyList));
michael@0 98 add_test(readPropertyList.bind(this,
michael@0 99 do_get_file("propertyLists/bug710259_propertyListXML.plist", false),
michael@0 100 checkMainPropertyList));
michael@0 101 run_next_test();
michael@0 102 }

mercurial