test-simple-storage.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 file = require("sdk/io/file");
  5. const prefs = require("sdk/preferences/service");
  6. const QUOTA_PREF = "extensions.addon-sdk.simple-storage.quota";
  7. let {Cc,Ci} = require("chrome");
  8. const { Loader } = require("sdk/test/loader");
  9. const { id } = require("sdk/self");
  10. let storeFile = Cc["@mozilla.org/file/directory_service;1"].
  11. getService(Ci.nsIProperties).
  12. get("ProfD", Ci.nsIFile);
  13. storeFile.append("jetpack");
  14. storeFile.append(id);
  15. storeFile.append("simple-storage");
  16. storeFile.append("store.json");
  17. let storeFilename = storeFile.path;
  18. function manager(loader) loader.sandbox("sdk/simple-storage").manager;
  19. exports.testSetGet = function (assert, done) {
  20. // Load the module once, set a value.
  21. let loader = Loader(module);
  22. let ss = loader.require("sdk/simple-storage");
  23. manager(loader).jsonStore.onWrite = function (storage) {
  24. assert.ok(file.exists(storeFilename), "Store file should exist");
  25. // Load the module again and make sure the value stuck.
  26. loader = Loader(module);
  27. ss = loader.require("sdk/simple-storage");
  28. manager(loader).jsonStore.onWrite = function (storage) {
  29. file.remove(storeFilename);
  30. done();
  31. };
  32. assert.equal(ss.storage.foo, val, "Value should persist");
  33. loader.unload();
  34. };
  35. let val = "foo";
  36. ss.storage.foo = val;
  37. assert.equal(ss.storage.foo, val, "Value read should be value set");
  38. loader.unload();
  39. };
  40. exports.testSetGetRootArray = function (assert, done) {
  41. setGetRoot(assert, done, [1, 2, 3], function (arr1, arr2) {
  42. if (arr1.length !== arr2.length)
  43. return false;
  44. for (let i = 0; i < arr1.length; i++) {
  45. if (arr1[i] !== arr2[i])
  46. return false;
  47. }
  48. return true;
  49. });
  50. };
  51. exports.testSetGetRootBool = function (assert, done) {
  52. setGetRoot(assert, done, true);
  53. };
  54. exports.testSetGetRootFunction = function (assert, done) {
  55. setGetRootError(assert, done, function () {},
  56. "Setting storage to a function should fail");
  57. };
  58. exports.testSetGetRootNull = function (assert, done) {
  59. setGetRoot(assert, done, null);
  60. };
  61. exports.testSetGetRootNumber = function (assert, done) {
  62. setGetRoot(assert, done, 3.14);
  63. };
  64. exports.testSetGetRootObject = function (assert, done) {
  65. setGetRoot(assert, done, { foo: 1, bar: 2 }, function (obj1, obj2) {
  66. for (let prop in obj1) {
  67. if (!(prop in obj2) || obj2[prop] !== obj1[prop])
  68. return false;
  69. }
  70. for (let prop in obj2) {
  71. if (!(prop in obj1) || obj1[prop] !== obj2[prop])
  72. return false;
  73. }
  74. return true;
  75. });
  76. };
  77. exports.testSetGetRootString = function (assert, done) {
  78. setGetRoot(assert, done, "sho' 'nuff");
  79. };
  80. exports.testSetGetRootUndefined = function (assert, done) {
  81. setGetRootError(assert, done, undefined, "Setting storage to undefined should fail");
  82. };
  83. exports.testEmpty = function (assert) {
  84. let loader = Loader(module);
  85. let ss = loader.require("sdk/simple-storage");
  86. loader.unload();
  87. assert.ok(!file.exists(storeFilename), "Store file should not exist");
  88. };
  89. exports.testMalformed = function (assert) {
  90. let stream = file.open(storeFilename, "w");
  91. stream.write("i'm not json");
  92. stream.close();
  93. let loader = Loader(module);
  94. let ss = loader.require("sdk/simple-storage");
  95. let empty = true;
  96. for (let key in ss.storage) {
  97. empty = false;
  98. break;
  99. }
  100. assert.ok(empty, "Malformed storage should cause root to be empty");
  101. loader.unload();
  102. };
  103. // Go over quota and handle it by listener.
  104. exports.testQuotaExceededHandle = function (assert, done) {
  105. prefs.set(QUOTA_PREF, 18);
  106. let loader = Loader(module);
  107. let ss = loader.require("sdk/simple-storage");
  108. ss.on("OverQuota", function () {
  109. assert.pass("OverQuota was emitted as expected");
  110. assert.equal(this, ss, "`this` should be simple storage");
  111. ss.storage = { x: 4, y: 5 };
  112. manager(loader).jsonStore.onWrite = function () {
  113. loader = Loader(module);
  114. ss = loader.require("sdk/simple-storage");
  115. let numProps = 0;
  116. for (let prop in ss.storage)
  117. numProps++;
  118. assert.ok(numProps, 2,
  119. "Store should contain 2 values: " + ss.storage.toSource());
  120. assert.equal(ss.storage.x, 4, "x value should be correct");
  121. assert.equal(ss.storage.y, 5, "y value should be correct");
  122. manager(loader).jsonStore.onWrite = function (storage) {
  123. prefs.reset(QUOTA_PREF);
  124. done();
  125. };
  126. loader.unload();
  127. };
  128. loader.unload();
  129. });
  130. // This will be JSON.stringify()ed to: {"a":1,"b":2,"c":3} (19 bytes)
  131. ss.storage = { a: 1, b: 2, c: 3 };
  132. manager(loader).jsonStore.write();
  133. };
  134. // Go over quota but don't handle it. The last good state should still persist.
  135. exports.testQuotaExceededNoHandle = function (assert, done) {
  136. prefs.set(QUOTA_PREF, 5);
  137. let loader = Loader(module);
  138. let ss = loader.require("sdk/simple-storage");
  139. manager(loader).jsonStore.onWrite = function (storage) {
  140. loader = Loader(module);
  141. ss = loader.require("sdk/simple-storage");
  142. assert.equal(ss.storage, val,
  143. "Value should have persisted: " + ss.storage);
  144. ss.storage = "some very long string that is very long";
  145. ss.on("OverQuota", function () {
  146. assert.pass("OverQuota emitted as expected");
  147. manager(loader).jsonStore.onWrite = function () {
  148. assert.fail("Over-quota value should not have been written");
  149. };
  150. loader.unload();
  151. loader = Loader(module);
  152. ss = loader.require("sdk/simple-storage");
  153. assert.equal(ss.storage, val,
  154. "Over-quota value should not have been written, " +
  155. "old value should have persisted: " + ss.storage);
  156. loader.unload();
  157. prefs.reset(QUOTA_PREF);
  158. done();
  159. });
  160. manager(loader).jsonStore.write();
  161. };
  162. let val = "foo";
  163. ss.storage = val;
  164. loader.unload();
  165. };
  166. exports.testQuotaUsage = function (assert, done) {
  167. let quota = 21;
  168. prefs.set(QUOTA_PREF, quota);
  169. let loader = Loader(module);
  170. let ss = loader.require("sdk/simple-storage");
  171. // {"a":1} (7 bytes)
  172. ss.storage = { a: 1 };
  173. assert.equal(ss.quotaUsage, 7 / quota, "quotaUsage should be correct");
  174. // {"a":1,"bb":2} (14 bytes)
  175. ss.storage = { a: 1, bb: 2 };
  176. assert.equal(ss.quotaUsage, 14 / quota, "quotaUsage should be correct");
  177. // {"a":1,"bb":2,"cc":3} (21 bytes)
  178. ss.storage = { a: 1, bb: 2, cc: 3 };
  179. assert.equal(ss.quotaUsage, 21 / quota, "quotaUsage should be correct");
  180. manager(loader).jsonStore.onWrite = function () {
  181. prefs.reset(QUOTA_PREF);
  182. done();
  183. };
  184. loader.unload();
  185. };
  186. exports.testUninstall = function (assert, done) {
  187. let loader = Loader(module);
  188. let ss = loader.require("sdk/simple-storage");
  189. manager(loader).jsonStore.onWrite = function () {
  190. assert.ok(file.exists(storeFilename), "Store file should exist");
  191. loader = Loader(module);
  192. ss = loader.require("sdk/simple-storage");
  193. loader.unload("uninstall");
  194. assert.ok(!file.exists(storeFilename), "Store file should be removed");
  195. done();
  196. };
  197. ss.storage.foo = "foo";
  198. loader.unload();
  199. };
  200. exports.testSetNoSetRead = function (assert, done) {
  201. // Load the module, set a value.
  202. let loader = Loader(module);
  203. let ss = loader.require("sdk/simple-storage");
  204. manager(loader).jsonStore.onWrite = function (storage) {
  205. assert.ok(file.exists(storeFilename), "Store file should exist");
  206. // Load the module again but don't access ss.storage.
  207. loader = Loader(module);
  208. ss = loader.require("sdk/simple-storage");
  209. manager(loader).jsonStore.onWrite = function (storage) {
  210. assert.fail("Nothing should be written since `storage` was not accessed.");
  211. };
  212. loader.unload();
  213. // Load the module a third time and make sure the value stuck.
  214. loader = Loader(module);
  215. ss = loader.require("sdk/simple-storage");
  216. manager(loader).jsonStore.onWrite = function (storage) {
  217. file.remove(storeFilename);
  218. done();
  219. };
  220. assert.equal(ss.storage.foo, val, "Value should persist");
  221. loader.unload();
  222. };
  223. let val = "foo";
  224. ss.storage.foo = val;
  225. assert.equal(ss.storage.foo, val, "Value read should be value set");
  226. loader.unload();
  227. };
  228. function setGetRoot(assert, done, val, compare) {
  229. compare = compare || function (a, b) a === b;
  230. // Load the module once, set a value.
  231. let loader = Loader(module);
  232. let ss = loader.require("sdk/simple-storage");
  233. manager(loader).jsonStore.onWrite = function () {
  234. assert.ok(file.exists(storeFilename), "Store file should exist");
  235. // Load the module again and make sure the value stuck.
  236. loader = Loader(module);
  237. ss = loader.require("sdk/simple-storage");
  238. manager(loader).jsonStore.onWrite = function () {
  239. file.remove(storeFilename);
  240. done();
  241. };
  242. assert.ok(compare(ss.storage, val), "Value should persist");
  243. loader.unload();
  244. };
  245. ss.storage = val;
  246. assert.ok(compare(ss.storage, val), "Value read should be value set");
  247. loader.unload();
  248. }
  249. function setGetRootError(assert, done, val, msg) {
  250. let pred = new RegExp("storage must be one of the following types: " +
  251. "array, boolean, null, number, object, string");
  252. let loader = Loader(module);
  253. let ss = loader.require("sdk/simple-storage");
  254. assert.throws(function () ss.storage = val, pred, msg);
  255. done();
  256. loader.unload();
  257. }
  258. require('sdk/test').run(exports);