Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

main.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /* jshint jasmine: true */
  2. /* global NativeStorage */
  3. exports.defineAutoTests = function() {
  4. describe('Write/Read/Delete Tests', function() {
  5. it("checks if th plugin is available", function() {
  6. expect(NativeStorage).toEqual(jasmine.anything());
  7. });
  8. it('Booleans', function(done) {
  9. var dummyData = true;
  10. NativeStorage.set("dummy_ref_bool",
  11. dummyData,
  12. function(result) {
  13. NativeStorage.getBoolean("dummy_ref_bool",
  14. function(result) {
  15. expect(result).toEqual(dummyData);
  16. NativeStorage.remove("dummy_ref_bool", function() {
  17. done();
  18. }, function(e) {
  19. fail("Delete Boolean Failed");
  20. });
  21. },
  22. function(e) {
  23. fail("Read Boolean Failed");
  24. });
  25. },
  26. function(e) {
  27. fail("Write Boolean Failed");
  28. });
  29. });
  30. it('Ints', function(done) {
  31. var dummyData = 154243;
  32. NativeStorage.set("dummy_ref_int",
  33. dummyData,
  34. function(result) {
  35. NativeStorage.getInt("dummy_ref_int",
  36. function(result) {
  37. expect(result).toEqual(dummyData);
  38. NativeStorage.remove("dummy_ref_int", function() {
  39. done();
  40. }, function(e) {
  41. fail("Delete Boolean Failed");
  42. });
  43. },
  44. function(e) {
  45. fail("Read Int Failed");
  46. });
  47. },
  48. function(e) {
  49. fail("Write Int Failed");
  50. });
  51. });
  52. it('Doubles', function(done) {
  53. var dummyData = 12327.023;
  54. NativeStorage.set("dummy_ref_double",
  55. dummyData,
  56. function(result) {
  57. NativeStorage.getDouble("dummy_ref_double",
  58. function(result) {
  59. expect(result).toEqual(dummyData);
  60. NativeStorage.remove("dummy_ref_double", function() {
  61. done();
  62. }, function(e) {
  63. fail("Delete Boolean Failed");
  64. });
  65. },
  66. function(e) {
  67. fail("Read Double Failed");
  68. });
  69. },
  70. function(e) {
  71. fail("Write String Failed");
  72. });
  73. });
  74. it('Strings', function(done) {
  75. var dummyData = "sdadadfsjdhbfwehfnciu7834fybzx2lnqo8japf;ckamicoa.c.a/";
  76. NativeStorage.set("dummy_ref_str",
  77. dummyData,
  78. function(result) {
  79. NativeStorage.getString("dummy_ref_str",
  80. function(result) {
  81. expect(result).toEqual(dummyData);
  82. NativeStorage.remove("dummy_ref_str", function() {
  83. done();
  84. }, function(e) {
  85. fail("Delete Boolean Failed");
  86. });
  87. },
  88. function(e) {
  89. fail("Read String Failed");
  90. });
  91. },
  92. function(e) {
  93. fail("Write String Failed");
  94. });
  95. });
  96. it('Objects', function(done) {
  97. var dummyData = {
  98. data1: "",
  99. data2: 2,
  100. data3: 3.0
  101. };
  102. NativeStorage.set("dummy_ref_obj",
  103. dummyData,
  104. function(result) {
  105. NativeStorage.getObject("dummy_ref_obj",
  106. function(result) {
  107. expect(result).toEqual(dummyData);
  108. NativeStorage.remove("dummy_ref_obj", function() {
  109. done();
  110. }, function(e) {
  111. fail("Delete Boolean Failed");
  112. });
  113. },
  114. function(e) {
  115. fail("Read Object Failed");
  116. });
  117. },
  118. function(e) {
  119. fail("Write Object Failed");
  120. });
  121. });
  122. });
  123. /* NEW API test */
  124. describe('Write/Read/Delete Tests new API', function() {
  125. it("Plugin available", function() {
  126. expect(NativeStorage).toEqual(jasmine.anything());
  127. });
  128. it('Booleans', function(done) {
  129. var dummyData = true;
  130. NativeStorage.setItem("dummy_ref_bool_new",
  131. dummyData,
  132. function(result) {
  133. NativeStorage.getItem("dummy_ref_bool_new",
  134. function(result) {
  135. expect(result).toEqual(dummyData);
  136. NativeStorage.remove("dummy_ref_bool_new", function() {
  137. done();
  138. }, function(e) {
  139. fail("Delete Boolean Failed");
  140. });
  141. },
  142. function(e) {
  143. fail("Read Boolean Failed");
  144. });
  145. },
  146. function(e) {
  147. fail("Write Boolean Failed");
  148. });
  149. });
  150. it('Ints', function(done) {
  151. var dummyData = 154243;
  152. NativeStorage.setItem("dummy_ref_int_new",
  153. dummyData,
  154. function(result) {
  155. NativeStorage.getItem("dummy_ref_int_new",
  156. function(result) {
  157. expect(result).toEqual(dummyData);
  158. NativeStorage.remove("dummy_ref_int_new", function() {
  159. done();
  160. }, function(e) {
  161. fail("Delete Boolean Failed");
  162. });
  163. },
  164. function(e) {
  165. fail("Read Int Failed");
  166. });
  167. },
  168. function(e) {
  169. fail("Write Int Failed");
  170. });
  171. });
  172. it('Doubles', function(done) {
  173. var dummyData = 12327.023;
  174. NativeStorage.setItem("dummy_ref_double_new",
  175. dummyData,
  176. function(result) {
  177. NativeStorage.getItem("dummy_ref_double_new",
  178. function(result) {
  179. expect(result).toEqual(dummyData);
  180. NativeStorage.remove("dummy_ref_double_new", function() {
  181. done();
  182. }, function(e) {
  183. fail("Delete Boolean Failed");
  184. });
  185. },
  186. function(e) {
  187. fail("Read Double Failed");
  188. });
  189. },
  190. function(e) {
  191. fail("Write String Failed");
  192. });
  193. });
  194. it('Strings', function(done) {
  195. var dummyData = "sdadadfsjdhbfwehfnciu7834fybzx2lnqo8japf;ckamicoa.c.a/";
  196. NativeStorage.setItem("dummy_ref_str_new",
  197. dummyData,
  198. function(result) {
  199. NativeStorage.getItem("dummy_ref_str_new",
  200. function(result) {
  201. expect(result).toEqual(dummyData);
  202. NativeStorage.remove("dummy_ref_str_new", function() {
  203. done();
  204. }, function(e) {
  205. fail("Delete Boolean Failed");
  206. });
  207. },
  208. function(e) {
  209. fail("Read String Failed");
  210. });
  211. },
  212. function(e) {
  213. fail("Write String Failed");
  214. });
  215. });
  216. it('Objects', function(done) {
  217. var dummyData = {
  218. data1: "",
  219. data2: 2,
  220. data3: 3.0
  221. };
  222. NativeStorage.setItem("dummy_ref_obj_new",
  223. dummyData,
  224. function(result) {
  225. NativeStorage.getItem("dummy_ref_obj_new",
  226. function(result) {
  227. expect(result).toEqual(dummyData);
  228. NativeStorage.remove("dummy_ref_obj_new", function() {
  229. done();
  230. }, function(e) {
  231. fail("Delete Boolean Failed");
  232. });
  233. },
  234. function(e) {
  235. fail("Read Object Failed");
  236. });
  237. },
  238. function(e) {
  239. fail("Write Object Failed");
  240. });
  241. });
  242. });
  243. /* NEW API test */
  244. describe('Fail Tests new API', function() {
  245. it("Plugin available", function() {
  246. expect(NativeStorage).toEqual(jasmine.anything());
  247. });
  248. it('Null reference', function(done) {
  249. NativeStorage.setItem(null, "objbio",
  250. function(result) {
  251. fail("Item should not have been found, because ref is null " + result);
  252. },
  253. function(e) {
  254. expect(e.code).toEqual(3);
  255. done();
  256. });
  257. });
  258. it('Item Not Found', function(done) {
  259. NativeStorage.getItem("dummy_ref_fail",
  260. function(result) {
  261. fail("Item should not have been found " + result);
  262. },
  263. function(e) {
  264. expect(e.code).toEqual(2);
  265. done();
  266. });
  267. });
  268. it('bad JSON', function(done) {
  269. var a = {};
  270. a.a = a;
  271. NativeStorage.setItem("dummy_ref_bad_json", a, function(result) {
  272. fail("JSON stringify should give an error " + result);
  273. },
  274. function(e) {
  275. expect(e.code).toEqual(5);
  276. done();
  277. });
  278. });
  279. });
  280. /* NEW API test with Password encryption*/
  281. describe('Password encryption Tests new API', function() {
  282. it("Plugin available", function() {
  283. expect(NativeStorage).toEqual(jasmine.anything());
  284. });
  285. it('should store, retrieve and remove a secret item', function(done) {
  286. //reference, obj, encryptConfig, success, error
  287. NativeStorage.setSecretItem("ref_to_secret_value", "SomeVerySecretText", {
  288. mode: "password",
  289. value: "mySecretPassword"
  290. },
  291. function(result) {
  292. // Oke good we've set the secret
  293. expect(result).toEqual("SomeVerySecretText");
  294. // let's grab it back
  295. NativeStorage.getSecretItem("ref_to_secret_value", {
  296. mode: "password",
  297. value: "mySecretPassword"
  298. }, function(result) {
  299. // we've got it again, yes! :D
  300. // let's remove it
  301. NativeStorage.remove("ref_to_secret_value", function() {
  302. // We're done!
  303. done();
  304. }, function(e) {
  305. fail("Delete item Failed");
  306. });
  307. }, function(e) {
  308. fail("Get item Failed");
  309. });
  310. },
  311. function(e) {
  312. fail("Set item Failed");
  313. });
  314. });
  315. });
  316. /* NEW API test with Password encryption*/
  317. describe('Password encryption Error Tests new API', function() {
  318. it("Plugin available", function() {
  319. expect(NativeStorage).toEqual(jasmine.anything());
  320. });
  321. it('should invoke the error callback function for a secret item', function(done) {
  322. //reference, obj, encryptConfig, success, error
  323. NativeStorage.setSecretItem("ref_to_secret_value", "SomeVerySecretText", {
  324. mode: "password",
  325. value: "mySecretPassword"
  326. },
  327. function(result) {
  328. // Oke good we've set the secret
  329. expect(result).toEqual("SomeVerySecretText");
  330. // let's grab it back
  331. NativeStorage.getSecretItem("ref_to_secret_value", {
  332. mode: "password",
  333. value: "SomeOhterPassword"
  334. }, function(result) {
  335. // if we are in the browser
  336. if ((window.cordova && window.cordova.platformId === 'browser') || !(window.phonegap || window.cordova)) {
  337. done();
  338. }
  339. fail("We've giving the pasword so it shouldn't work");
  340. NativeStorage.remove("ref_to_secret_value", function() {
  341. fail("We shouldn't got here... maybe we are in a browser!");
  342. }, function(e) {
  343. fail("Delete item Failed");
  344. });
  345. },
  346. function(e) {
  347. done(e);
  348. });
  349. },
  350. function(e) {
  351. fail("Set item Failed");
  352. });
  353. });
  354. });
  355. /* NEW API test with clear() function*/
  356. describe('clear function', function() {
  357. it("Plugin available", function() {
  358. expect(NativeStorage).toEqual(jasmine.anything());
  359. });
  360. it('should invoke the error callback function with error code 2', function(done) {
  361. var a = {};
  362. NativeStorage.setItem("dummy_ref_clear", a, function(result) {
  363. expect(result).toEqual(a);
  364. NativeStorage.clear(function(result) {
  365. NativeStorage.getItem("dummy_ref_clear", function(result) {
  366. fail("Should not give a result");
  367. }, function(e) {
  368. expect(e.code).toEqual(2);
  369. done();
  370. });
  371. },
  372. function(e) {
  373. fail("Error when clearing native storage");
  374. });
  375. },
  376. function(e) {
  377. fail("Error when item is set");
  378. });
  379. });
  380. });
  381. describe('keys function', function() {
  382. it("Plugin available", function() {
  383. expect(NativeStorage).toEqual(jasmine.anything());
  384. });
  385. it('should return the keys', function(done) {
  386. var a = {};
  387. NativeStorage.setItem("dummy_ref_clear", a, function(result) {
  388. expect(result).toEqual(a);
  389. NativeStorage.keys(function(result) {
  390. expect(result).toContain('dummy_ref_clear');
  391. done();
  392. },
  393. function(e) {
  394. fail("Error when listing keys in native storage");
  395. });
  396. },
  397. function(e) {
  398. fail("Error when item is set");
  399. });
  400. });
  401. });
  402. };