test_attributes.js 2.9 KB

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