test_filehandle_store_snapshot.html 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <!--
  2. Any copyright is dedicated to the Public Domain.
  3. http://creativecommons.org/publicdomain/zero/1.0/
  4. -->
  5. <html>
  6. <head>
  7. <title>Indexed Database Property Test</title>
  8. <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  9. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  10. <script type="text/javascript;version=1.7">
  11. function testSteps()
  12. {
  13. const READ_WRITE = "readwrite";
  14. const name = window.location.pathname;
  15. const objectStoreName = "Blobs";
  16. const testFile = getRandomFile("random.bin", 100000);
  17. let request = indexedDB.open(name, 1);
  18. request.onerror = errorHandler;
  19. request.onupgradeneeded = grabEventAndContinueHandler;
  20. request.onsuccess = grabEventAndContinueHandler;
  21. let event = yield undefined;
  22. is(event.type, "upgradeneeded", "Got correct event type");
  23. let db = event.target.result;
  24. db.onerror = errorHandler;
  25. let objectStore = db.createObjectStore(objectStoreName, { });
  26. event = yield undefined;
  27. is(event.type, "success", "Got correct event type");
  28. request = db.createMutableFile("random.bin", "binary/random");
  29. request.onsuccess = grabEventAndContinueHandler;
  30. event = yield undefined;
  31. let mutableFile = event.target.result;
  32. mutableFile.onerror = errorHandler;
  33. let fileHandle = mutableFile.open("readwrite");
  34. is(getFileId(mutableFile), 1, "Correct file id");
  35. request = fileHandle.write(testFile);
  36. request.onsuccess = grabEventAndContinueHandler;
  37. event = yield undefined;
  38. request = mutableFile.getFile();
  39. request.onsuccess = grabEventAndContinueHandler;
  40. event = yield undefined;
  41. let file = event.target.result;
  42. let trans = db.transaction([objectStoreName], READ_WRITE);
  43. objectStore = trans.objectStore(objectStoreName);
  44. request = objectStore.add(file, 42);
  45. request.onsuccess = grabEventAndContinueHandler;
  46. event = yield undefined;
  47. // At this moment, the file should not be readable anymore.
  48. let reader = new FileReader();
  49. try {
  50. reader.readAsArrayBuffer(file);
  51. ok(false, "Should have thrown!");
  52. }
  53. catch (e) {
  54. ok(e instanceof DOMException, "Got exception.");
  55. is(e.name, "FileHandleInactiveError", "Good error.");
  56. is(e.code, 0, "Good error code.");
  57. }
  58. request = objectStore.get(42);
  59. request.onsuccess = grabEventAndContinueHandler;
  60. event = yield undefined;
  61. verifyBlob(event.target.result, testFile, 2);
  62. yield undefined;
  63. finishTest();
  64. yield undefined;
  65. }
  66. </script>
  67. <script type="text/javascript;version=1.7" src="file.js"></script>
  68. <script type="text/javascript;version=1.7" src="helpers.js"></script>
  69. </head>
  70. <body onload="runTest();"></body>
  71. </html>