MockRegistry.jsm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. "use strict";
  2. this.EXPORTED_SYMBOLS = ["MockRegistry"];
  3. const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
  4. Cu.import("resource://testing-common/MockRegistrar.jsm");
  5. Cu.import("resource://gre/modules/XPCOMUtils.jsm");
  6. class MockRegistry {
  7. constructor() {
  8. // Three level structure of Maps pointing to Maps pointing to Maps
  9. // this.roots is the top of the structure and has ROOT_KEY_* values
  10. // as keys. Maps at the second level are the values of the first
  11. // level Map, they have registry keys (also called paths) as keys.
  12. // Third level maps are the values in second level maps, they have
  13. // map registry names to corresponding values (which in this implementation
  14. // are always strings).
  15. this.roots = new Map([
  16. [Ci.nsIWindowsRegKey.ROOT_KEY_LOCAL_MACHINE, new Map()],
  17. [Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER, new Map()],
  18. [Ci.nsIWindowsRegKey.ROOT_KEY_CLASSES_ROOT, new Map()],
  19. ]);
  20. let registry = this;
  21. /**
  22. * This is a mock nsIWindowsRegistry implementation. It only implements a
  23. * subset of the interface used in tests. In particular, only values
  24. * of type string are supported.
  25. */
  26. function MockWindowsRegKey() { }
  27. MockWindowsRegKey.prototype = {
  28. values: null,
  29. // --- Overridden nsISupports interface functions ---
  30. QueryInterface: XPCOMUtils.generateQI([Ci.nsIWindowsRegKey]),
  31. // --- Overridden nsIWindowsRegKey interface functions ---
  32. open: function(root, path, mode) {
  33. let rootKey = registry.getRoot(root);
  34. if (!rootKey.has(path)) {
  35. rootKey.set(path, new Map());
  36. }
  37. this.values = rootKey.get(path);
  38. },
  39. close: function() {
  40. this.values = null;
  41. },
  42. get valueCount() {
  43. if (!this.values)
  44. throw Components.results.NS_ERROR_FAILURE;
  45. return this.values.size;
  46. },
  47. hasValue: function(name) {
  48. if (!this.values) {
  49. return false;
  50. }
  51. return this.values.has(name);
  52. },
  53. getValueType: function(name) {
  54. return Ci.nsIWindowsRegKey.TYPE_STRING;
  55. },
  56. getValueName: function(index) {
  57. if (!this.values || index >= this.values.size)
  58. throw Components.results.NS_ERROR_FAILURE;
  59. let names = Array.from(this.values.keys());
  60. return names[index];
  61. },
  62. readStringValue: function(name) {
  63. if (!this.values) {
  64. throw new Error("invalid registry path");
  65. }
  66. return this.values.get(name);
  67. }
  68. };
  69. this.cid = MockRegistrar.register("@mozilla.org/windows-registry-key;1", MockWindowsRegKey);
  70. }
  71. shutdown() {
  72. MockRegistrar.unregister(this.cid);
  73. this.cid = null;
  74. }
  75. getRoot(root) {
  76. if (!this.roots.has(root)) {
  77. throw new Error(`No such root ${root}`);
  78. }
  79. return this.roots.get(root);
  80. }
  81. setValue(root, path, name, value) {
  82. let rootKey = this.getRoot(root);
  83. if (!rootKey.has(path)) {
  84. rootKey.set(path, new Map());
  85. }
  86. let pathmap = rootKey.get(path);
  87. if (value == null) {
  88. pathmap.delete(name);
  89. } else {
  90. pathmap.set(name, value);
  91. }
  92. }
  93. };