test_filehandle_workers.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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>File Handle 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 name = window.location.pathname;
  14. let testBuffer = getRandomBuffer(100000);
  15. let request = indexedDB.open(name, 1);
  16. request.onerror = errorHandler;
  17. request.onupgradeneeded = grabEventAndContinueHandler;
  18. request.onsuccess = errorHandler;
  19. let event = yield undefined;
  20. is(event.type, "upgradeneeded", "Got correct event type");
  21. let db = event.target.result;
  22. db.onerror = errorHandler;
  23. db.createObjectStore("Foo", { });
  24. request.onupgradeneeded = errorHandler;
  25. request.onsuccess = grabEventAndContinueHandler;
  26. event = yield undefined;
  27. is(event.type, "success", "Got correct event type");
  28. request = db.createMutableFile("test.txt");
  29. request.onerror = errorHandler;
  30. request.onsuccess = grabEventAndContinueHandler;
  31. event = yield undefined;
  32. let mutableFile = event.target.result;
  33. function dummyWorkerScript() {
  34. onmessage = function(event) {
  35. throw("Shouldn't be called!");
  36. }
  37. }
  38. let url =
  39. URL.createObjectURL(new Blob(["(", dummyWorkerScript.toSource(), ")()"]));
  40. let worker1 = new Worker(url);
  41. try {
  42. worker1.postMessage(mutableFile);
  43. ok(false, "Should have thrown!");
  44. }
  45. catch (e) {
  46. ok(e instanceof DOMException, "Got exception.");
  47. is(e.name, "DataCloneError", "Good error.");
  48. is(e.code, DOMException.DATA_CLONE_ERR, "Good error code.")
  49. }
  50. mutableFile.onerror = errorHandler;
  51. let fileHandle = mutableFile.open("readwrite");
  52. request = fileHandle.write(testBuffer);
  53. request.onsuccess = grabEventAndContinueHandler;
  54. event = yield undefined;
  55. request = mutableFile.getFile();
  56. request.onsuccess = grabEventAndContinueHandler;
  57. event = yield undefined;
  58. let file = event.target.result;
  59. let worker2 = new Worker(url);
  60. URL.revokeObjectURL(url);
  61. try {
  62. worker2.postMessage(file);
  63. ok(false, "Should have thrown!");
  64. }
  65. catch (e) {
  66. ok(e instanceof DOMException, "Got exception.");
  67. is(e.name, "DataCloneError", "Good error.");
  68. is(e.code, DOMException.DATA_CLONE_ERR, "Good error code.")
  69. }
  70. let objectStore =
  71. db.transaction("Foo", "readwrite").objectStore("Foo");
  72. request = objectStore.add(mutableFile, 42);
  73. request.onsuccess = grabEventAndContinueHandler;
  74. event = yield undefined;
  75. function workerScript() {
  76. onmessage = function(event) {
  77. var name = event.data;
  78. var request = indexedDB.open(name, 1);
  79. request.onsuccess = function(event) {
  80. var db = event.target.result;
  81. let objectStore = db.transaction("Foo").objectStore("Foo");
  82. request = objectStore.get(42);
  83. request.onsuccess = function(event) {
  84. try {
  85. let result = request.result;
  86. postMessage("error");
  87. }
  88. catch (e) {
  89. postMessage("success");
  90. }
  91. }
  92. request.onerror = function(event) {
  93. postMessage("error");
  94. }
  95. }
  96. request.onerror = function(event) {
  97. postMessage("error");
  98. }
  99. }
  100. }
  101. url = URL.createObjectURL(new Blob(["(", workerScript.toSource(), ")()"]));
  102. let worker3 = new Worker(url);
  103. URL.revokeObjectURL(url);
  104. worker3.postMessage(name);
  105. worker3.onmessage = grabEventAndContinueHandler;
  106. event = yield undefined;
  107. is(event.data, "success", "Good response.");
  108. todo(false, "Terminate all workers at the end of the test to work around bug 1340941.");
  109. worker1.terminate();
  110. worker2.terminate();
  111. worker3.terminate();
  112. finishTest();
  113. yield undefined;
  114. }
  115. </script>
  116. <script type="text/javascript;version=1.7" src="file.js"></script>
  117. <script type="text/javascript;version=1.7" src="helpers.js"></script>
  118. </head>
  119. <body onload="runTest();"></body>
  120. </html>