toolkit/modules/tests/xpcshell/test_propertyListsUtils.js

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

mercurial