js/xpconnect/tests/unit/test_attributes.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
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 const Cc = Components.classes;
michael@0 6 const Ci = Components.interfaces;
michael@0 7
michael@0 8 function run_test() {
michael@0 9
michael@0 10 // Load the component manifests.
michael@0 11 Components.manager.autoRegister(do_get_file('../components/native/xpctest.manifest'));
michael@0 12 Components.manager.autoRegister(do_get_file('../components/js/xpctest.manifest'));
michael@0 13
michael@0 14 // Test for each component.
michael@0 15 test_component_readwrite("@mozilla.org/js/xpc/test/native/ObjectReadWrite;1");
michael@0 16 test_component_readwrite("@mozilla.org/js/xpc/test/js/ObjectReadWrite;1");
michael@0 17 test_component_readonly("@mozilla.org/js/xpc/test/native/ObjectReadOnly;1");
michael@0 18 test_component_readonly("@mozilla.org/js/xpc/test/js/ObjectReadOnly;1");
michael@0 19 }
michael@0 20
michael@0 21 function test_component_readwrite(contractid) {
michael@0 22
michael@0 23 // Instantiate the object.
michael@0 24 var o = Cc[contractid].createInstance(Ci["nsIXPCTestObjectReadWrite"]);
michael@0 25
michael@0 26 // Test the initial values.
michael@0 27 do_check_eq("XPConnect Read-Writable String", o.stringProperty);
michael@0 28 do_check_eq(true, o.booleanProperty);
michael@0 29 do_check_eq(32767, o.shortProperty);
michael@0 30 do_check_eq(2147483647, o.longProperty);
michael@0 31 do_check_true(5.25 < o.floatProperty && 5.75 > o.floatProperty);
michael@0 32 do_check_eq("X", o.charProperty);
michael@0 33 do_check_eq(-1, o.timeProperty);
michael@0 34
michael@0 35 // Write new values.
michael@0 36 o.stringProperty = "another string";
michael@0 37 o.booleanProperty = false;
michael@0 38 o.shortProperty = -12345;
michael@0 39 o.longProperty = 1234567890;
michael@0 40 o.floatProperty = 10.2;
michael@0 41 o.charProperty = "Z";
michael@0 42 o.timeProperty = 1;
michael@0 43
michael@0 44 // Test the new values.
michael@0 45 do_check_eq("another string", o.stringProperty);
michael@0 46 do_check_eq(false, o.booleanProperty);
michael@0 47 do_check_eq(-12345, o.shortProperty);
michael@0 48 do_check_eq(1234567890, o.longProperty);
michael@0 49 do_check_true(10.15 < o.floatProperty && 10.25 > o.floatProperty);
michael@0 50 do_check_eq("Z", o.charProperty);
michael@0 51 do_check_eq(1, o.timeProperty);
michael@0 52
michael@0 53 // Assign values that differ from the expected type to verify conversion.
michael@0 54
michael@0 55 function SetAndTestBooleanProperty(newValue, expectedValue) {
michael@0 56 o.booleanProperty = newValue;
michael@0 57 do_check_eq(expectedValue, o.booleanProperty);
michael@0 58 };
michael@0 59 SetAndTestBooleanProperty(false, false);
michael@0 60 SetAndTestBooleanProperty(1, true);
michael@0 61 SetAndTestBooleanProperty(null, false);
michael@0 62 SetAndTestBooleanProperty("A", true);
michael@0 63 SetAndTestBooleanProperty(undefined, false);
michael@0 64 SetAndTestBooleanProperty([], true);
michael@0 65 SetAndTestBooleanProperty({}, true);
michael@0 66 }
michael@0 67
michael@0 68 function test_component_readonly(contractid) {
michael@0 69
michael@0 70 // Instantiate the object.
michael@0 71 var o = Cc[contractid].createInstance(Ci["nsIXPCTestObjectReadOnly"]);
michael@0 72
michael@0 73 // Test the initial values.
michael@0 74 do_check_eq("XPConnect Read-Only String", o.strReadOnly);
michael@0 75 do_check_eq(true, o.boolReadOnly);
michael@0 76 do_check_eq(32767, o.shortReadOnly);
michael@0 77 do_check_eq(2147483647, o.longReadOnly);
michael@0 78 do_check_true(5.25 < o.floatReadOnly && 5.75 > o.floatReadOnly);
michael@0 79 do_check_eq("X", o.charReadOnly);
michael@0 80 do_check_eq(-1, o.timeReadOnly);
michael@0 81 }

mercurial