test_blob_file_backed.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/
  4. */
  5. var disableWorkerTest = "This test uses SpecialPowers";
  6. var testGenerator = testSteps();
  7. function testSteps()
  8. {
  9. const fileData = "abcdefghijklmnopqrstuvwxyz";
  10. const fileType = "text/plain";
  11. const databaseName =
  12. ("window" in this) ? window.location.pathname : "Test";
  13. const objectStoreName = "foo";
  14. const objectStoreKey = "10";
  15. info("Creating temp file");
  16. SpecialPowers.createFiles([{data:fileData, options:{type:fileType}}], function (files) {
  17. testGenerator.next(files[0]);
  18. });
  19. let file = yield undefined;
  20. ok(file instanceof File, "Got a File object");
  21. is(file.size, fileData.length, "Correct size");
  22. is(file.type, fileType, "Correct type");
  23. let fileReader = new FileReader();
  24. fileReader.onload = grabEventAndContinueHandler;
  25. fileReader.readAsText(file);
  26. let event = yield undefined;
  27. is(fileReader.result, fileData, "Correct data");
  28. let request = indexedDB.open(databaseName, 1);
  29. request.onerror = errorHandler;
  30. request.onupgradeneeded = grabEventAndContinueHandler;
  31. request.onsuccess = grabEventAndContinueHandler;
  32. event = yield undefined;
  33. let db = event.target.result;
  34. let objectStore = db.createObjectStore(objectStoreName);
  35. objectStore.put(file, objectStoreKey);
  36. event = yield undefined;
  37. db = event.target.result;
  38. file = null;
  39. objectStore = db.transaction(objectStoreName).objectStore(objectStoreName);
  40. objectStore.get(objectStoreKey).onsuccess = grabEventAndContinueHandler;
  41. event = yield undefined;
  42. file = event.target.result;
  43. ok(file instanceof File, "Got a File object");
  44. is(file.size, fileData.length, "Correct size");
  45. is(file.type, fileType, "Correct type");
  46. fileReader = new FileReader();
  47. fileReader.onload = grabEventAndContinueHandler;
  48. fileReader.readAsText(file);
  49. event = yield undefined;
  50. is(fileReader.result, fileData, "Correct data");
  51. finishTest();
  52. yield undefined;
  53. }