test_windows_registry.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* vim:set ts=2 sw=2 sts=2 et: */
  3. /* This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. const Cr = Components.results;
  7. const Ci = Components.interfaces;
  8. const Cc = Components.classes;
  9. const Cu = Components.utils;
  10. const CC = Components.Constructor;
  11. const nsIWindowsRegKey = Ci.nsIWindowsRegKey;
  12. let regKeyComponent = Cc["@mozilla.org/windows-registry-key;1"];
  13. function run_test()
  14. {
  15. //* create a key structure in a spot that's normally writable (somewhere under HKCU).
  16. let testKey = regKeyComponent.createInstance(nsIWindowsRegKey);
  17. // If it's already present because a previous test crashed or didn't clean up properly, clean it up first.
  18. let keyName = BASE_PATH + "\\" + TESTDATA_KEYNAME;
  19. setup_test_run(testKey, keyName);
  20. //* test that the write* functions write stuff
  21. test_writing_functions(testKey);
  22. //* check that the valueCount/getValueName functions work for the values we just wrote
  23. test_value_functions(testKey);
  24. //* check that the get* functions work for the values we just wrote.
  25. test_reading_functions(testKey);
  26. //* check that the get* functions fail with the right exception codes if we ask for the wrong type or if the value name doesn't exist at all
  27. test_invalidread_functions(testKey);
  28. //* check that creating/enumerating/deleting child keys works
  29. test_childkey_functions(testKey);
  30. test_watching_functions(testKey);
  31. //* clean up
  32. cleanup_test_run(testKey, keyName);
  33. }
  34. function setup_test_run(testKey, keyName)
  35. {
  36. do_print("Setup test run");
  37. try {
  38. testKey.open(nsIWindowsRegKey.ROOT_KEY_CURRENT_USER, keyName, nsIWindowsRegKey.ACCESS_READ);
  39. do_print("Test key exists. Needs cleanup.");
  40. cleanup_test_run(testKey, keyName);
  41. }
  42. catch (e if (e instanceof Ci.nsIException && e.result == Cr.NS_ERROR_FAILURE))
  43. {
  44. }
  45. testKey.create(nsIWindowsRegKey.ROOT_KEY_CURRENT_USER, keyName, nsIWindowsRegKey.ACCESS_ALL);
  46. }
  47. function test_writing_functions(testKey)
  48. {
  49. strictEqual(testKey.valueCount, 0);
  50. strictEqual(testKey.hasValue(TESTDATA_STRNAME), false);
  51. testKey.writeStringValue(TESTDATA_STRNAME, TESTDATA_STRVALUE);
  52. strictEqual(testKey.hasValue(TESTDATA_STRNAME), true);
  53. strictEqual(testKey.hasValue(TESTDATA_INTNAME), false);
  54. testKey.writeIntValue(TESTDATA_INTNAME, TESTDATA_INTVALUE);
  55. strictEqual(testKey.hasValue(TESTDATA_INT64NAME), false);
  56. testKey.writeInt64Value(TESTDATA_INT64NAME, TESTDATA_INT64VALUE);
  57. strictEqual(testKey.hasValue(TESTDATA_BINARYNAME), false);
  58. testKey.writeBinaryValue(TESTDATA_BINARYNAME, TESTDATA_BINARYVALUE);
  59. }
  60. function test_value_functions(testKey)
  61. {
  62. strictEqual(testKey.valueCount, 4);
  63. strictEqual(testKey.getValueName(0), TESTDATA_STRNAME);
  64. strictEqual(testKey.getValueName(1), TESTDATA_INTNAME);
  65. strictEqual(testKey.getValueName(2), TESTDATA_INT64NAME);
  66. strictEqual(testKey.getValueName(3), TESTDATA_BINARYNAME);
  67. }
  68. function test_reading_functions(testKey)
  69. {
  70. strictEqual(testKey.getValueType(TESTDATA_STRNAME), nsIWindowsRegKey.TYPE_STRING);
  71. strictEqual(testKey.readStringValue(TESTDATA_STRNAME), TESTDATA_STRVALUE);
  72. strictEqual(testKey.getValueType(TESTDATA_INTNAME), nsIWindowsRegKey.TYPE_INT);
  73. strictEqual(testKey.readIntValue(TESTDATA_INTNAME), TESTDATA_INTVALUE);
  74. strictEqual(testKey.getValueType(TESTDATA_INT64NAME), nsIWindowsRegKey.TYPE_INT64);
  75. strictEqual( testKey.readInt64Value(TESTDATA_INT64NAME), TESTDATA_INT64VALUE);
  76. strictEqual(testKey.getValueType(TESTDATA_BINARYNAME), nsIWindowsRegKey.TYPE_BINARY);
  77. strictEqual( testKey.readBinaryValue(TESTDATA_BINARYNAME), TESTDATA_BINARYVALUE);
  78. }
  79. function test_invalidread_functions(testKey)
  80. {
  81. try {
  82. testKey.readIntValue(TESTDATA_STRNAME);
  83. do_throw("Reading an integer from a string registry value should throw.");
  84. } catch (e if (e instanceof Ci.nsIException && e.result == Cr.NS_ERROR_FAILURE)) {
  85. }
  86. try {
  87. let val = testKey.readStringValue(TESTDATA_INTNAME);
  88. do_throw("Reading an string from an Int registry value should throw." + val);
  89. } catch (e if (e instanceof Ci.nsIException && e.result == Cr.NS_ERROR_FAILURE)) {
  90. }
  91. try {
  92. testKey.readStringValue(TESTDATA_INT64NAME);
  93. do_throw("Reading an string from an Int64 registry value should throw.");
  94. } catch (e if (e instanceof Ci.nsIException && e.result == Cr.NS_ERROR_FAILURE)) {
  95. }
  96. try {
  97. testKey.readStringValue(TESTDATA_BINARYNAME);
  98. do_throw("Reading a string from an Binary registry value should throw.");
  99. } catch (e if (e instanceof Ci.nsIException && e.result == Cr.NS_ERROR_FAILURE)) {
  100. }
  101. }
  102. function test_childkey_functions(testKey)
  103. {
  104. strictEqual(testKey.childCount, 0);
  105. strictEqual(testKey.hasChild(TESTDATA_CHILD_KEY), false);
  106. let childKey = testKey.createChild(TESTDATA_CHILD_KEY, nsIWindowsRegKey.ACCESS_ALL);
  107. childKey.close();
  108. strictEqual(testKey.childCount, 1);
  109. strictEqual(testKey.hasChild(TESTDATA_CHILD_KEY), true);
  110. strictEqual(testKey.getChildName(0), TESTDATA_CHILD_KEY);
  111. childKey = testKey.openChild(TESTDATA_CHILD_KEY, nsIWindowsRegKey.ACCESS_ALL);
  112. testKey.removeChild(TESTDATA_CHILD_KEY);
  113. strictEqual(testKey.childCount, 0);
  114. strictEqual(testKey.hasChild(TESTDATA_CHILD_KEY), false);
  115. }
  116. function test_watching_functions(testKey)
  117. {
  118. strictEqual(testKey.isWatching(), false);
  119. strictEqual(testKey.hasChanged(), false);
  120. testKey.startWatching(true);
  121. strictEqual(testKey.isWatching(), true);
  122. testKey.stopWatching();
  123. strictEqual(testKey.isWatching(), false);
  124. // Create a child key, and update a value
  125. let childKey = testKey.createChild(TESTDATA_CHILD_KEY, nsIWindowsRegKey.ACCESS_ALL);
  126. childKey.writeIntValue(TESTDATA_INTNAME, TESTDATA_INTVALUE);
  127. // Start a recursive watch, and update the child's value
  128. testKey.startWatching(true);
  129. strictEqual(testKey.isWatching(), true);
  130. childKey.writeIntValue(TESTDATA_INTNAME, 0);
  131. strictEqual(testKey.hasChanged(), true);
  132. testKey.stopWatching();
  133. strictEqual(testKey.isWatching(), false);
  134. childKey.removeValue(TESTDATA_INTNAME);
  135. childKey.close();
  136. testKey.removeChild(TESTDATA_CHILD_KEY);
  137. }
  138. function cleanup_test_run(testKey, keyName)
  139. {
  140. do_print("Cleaning up test.");
  141. for (var i = 0; i < testKey.childCount; i++) {
  142. testKey.removeChild(testKey.getChildName(i));
  143. }
  144. testKey.close();
  145. let baseKey = regKeyComponent.createInstance(nsIWindowsRegKey);
  146. baseKey.open(nsIWindowsRegKey.ROOT_KEY_CURRENT_USER, BASE_PATH, nsIWindowsRegKey.ACCESS_ALL);
  147. baseKey.removeChild(TESTDATA_KEYNAME);
  148. baseKey.close();
  149. }
  150. // Test data used above.
  151. const BASE_PATH = "SOFTWARE";
  152. const TESTDATA_KEYNAME = "TestRegXPC";
  153. const TESTDATA_STRNAME = "AString";
  154. const TESTDATA_STRVALUE = "The quick brown fox jumps over the lazy dog.";
  155. const TESTDATA_INTNAME = "AnInteger";
  156. const TESTDATA_INTVALUE = 65536;
  157. const TESTDATA_INT64NAME = "AnInt64";
  158. const TESTDATA_INT64VALUE = 9223372036854775807;
  159. const TESTDATA_BINARYNAME = "ABinary";
  160. const TESTDATA_BINARYVALUE = "She sells seashells by the seashore";
  161. const TESTDATA_CHILD_KEY = "TestChildKey";