js/xpconnect/tests/unit/test_attributes.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/xpconnect/tests/unit/test_attributes.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,81 @@
     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
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const Cc = Components.classes;
     1.9 +const Ci = Components.interfaces;
    1.10 +
    1.11 +function run_test() {
    1.12 +
    1.13 +  // Load the component manifests.
    1.14 +  Components.manager.autoRegister(do_get_file('../components/native/xpctest.manifest'));
    1.15 +  Components.manager.autoRegister(do_get_file('../components/js/xpctest.manifest'));
    1.16 +
    1.17 +  // Test for each component.
    1.18 +  test_component_readwrite("@mozilla.org/js/xpc/test/native/ObjectReadWrite;1");
    1.19 +  test_component_readwrite("@mozilla.org/js/xpc/test/js/ObjectReadWrite;1");
    1.20 +  test_component_readonly("@mozilla.org/js/xpc/test/native/ObjectReadOnly;1");
    1.21 +  test_component_readonly("@mozilla.org/js/xpc/test/js/ObjectReadOnly;1");
    1.22 +}
    1.23 +
    1.24 +function test_component_readwrite(contractid) {
    1.25 +
    1.26 +  // Instantiate the object.
    1.27 +  var o = Cc[contractid].createInstance(Ci["nsIXPCTestObjectReadWrite"]);
    1.28 +
    1.29 +  // Test the initial values.
    1.30 +  do_check_eq("XPConnect Read-Writable String", o.stringProperty);
    1.31 +  do_check_eq(true, o.booleanProperty);
    1.32 +  do_check_eq(32767, o.shortProperty);
    1.33 +  do_check_eq(2147483647, o.longProperty);
    1.34 +  do_check_true(5.25 < o.floatProperty && 5.75 > o.floatProperty);
    1.35 +  do_check_eq("X", o.charProperty);
    1.36 +  do_check_eq(-1, o.timeProperty);
    1.37 +
    1.38 +  // Write new values.
    1.39 +  o.stringProperty = "another string";
    1.40 +  o.booleanProperty = false;
    1.41 +  o.shortProperty = -12345;
    1.42 +  o.longProperty = 1234567890;
    1.43 +  o.floatProperty = 10.2;
    1.44 +  o.charProperty = "Z";
    1.45 +  o.timeProperty = 1;
    1.46 +
    1.47 +  // Test the new values.
    1.48 +  do_check_eq("another string", o.stringProperty);
    1.49 +  do_check_eq(false, o.booleanProperty);
    1.50 +  do_check_eq(-12345, o.shortProperty);
    1.51 +  do_check_eq(1234567890, o.longProperty);
    1.52 +  do_check_true(10.15 < o.floatProperty && 10.25 > o.floatProperty);
    1.53 +  do_check_eq("Z", o.charProperty);
    1.54 +  do_check_eq(1, o.timeProperty);
    1.55 +
    1.56 +  // Assign values that differ from the expected type to verify conversion.
    1.57 +
    1.58 +  function SetAndTestBooleanProperty(newValue, expectedValue) {
    1.59 +    o.booleanProperty = newValue;
    1.60 +    do_check_eq(expectedValue, o.booleanProperty);
    1.61 +  };
    1.62 +  SetAndTestBooleanProperty(false, false);
    1.63 +  SetAndTestBooleanProperty(1, true);
    1.64 +  SetAndTestBooleanProperty(null, false);
    1.65 +  SetAndTestBooleanProperty("A", true);
    1.66 +  SetAndTestBooleanProperty(undefined, false);
    1.67 +  SetAndTestBooleanProperty([], true);
    1.68 +  SetAndTestBooleanProperty({}, true);
    1.69 +}
    1.70 +
    1.71 +function test_component_readonly(contractid) {
    1.72 +
    1.73 +  // Instantiate the object.
    1.74 +  var o = Cc[contractid].createInstance(Ci["nsIXPCTestObjectReadOnly"]);
    1.75 +
    1.76 +  // Test the initial values.
    1.77 +  do_check_eq("XPConnect Read-Only String", o.strReadOnly);
    1.78 +  do_check_eq(true, o.boolReadOnly);
    1.79 +  do_check_eq(32767, o.shortReadOnly);
    1.80 +  do_check_eq(2147483647, o.longReadOnly);
    1.81 +  do_check_true(5.25 < o.floatReadOnly && 5.75 > o.floatReadOnly);
    1.82 +  do_check_eq("X", o.charReadOnly);
    1.83 +  do_check_eq(-1, o.timeReadOnly);
    1.84 +}

mercurial