test_message_manager_ipc.html 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Test for sending IndexedDB Blobs through MessageManager</title>
  5. <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js">
  6. </script>
  7. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  8. </head>
  9. <body onload="setup();">
  10. <script type="application/javascript;version=1.7">
  11. "use strict";
  12. function childFrameScript() {
  13. "use strict";
  14. const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
  15. const mmName = "test:idb-and-mm";
  16. const dbName = "test_message_manager_ipc.html - CHILD";
  17. const dbVersion = 1;
  18. const objStoreName = "bar";
  19. const key = 1;
  20. const blobData = ["So", " ", "many", " ", "blobs!"];
  21. const blobText = blobData.join("");
  22. const blobType = "text/plain";
  23. Cu.importGlobalProperties(["indexedDB"]);
  24. function info(msg) {
  25. sendAsyncMessage(mmName, { op: "info", msg: msg });
  26. }
  27. function ok(condition, name, diag) {
  28. sendAsyncMessage(mmName,
  29. { op: "ok",
  30. condition: condition,
  31. name: name,
  32. diag: diag });
  33. }
  34. function is(a, b, name) {
  35. let pass = a == b;
  36. let diag = pass ? "" : "got " + a + ", expected " + b;
  37. ok(pass, name, diag);
  38. }
  39. function finish(result) {
  40. sendAsyncMessage(mmName, { op: "done", result: result });
  41. }
  42. function grabAndContinue(arg) {
  43. testGenerator.send(arg);
  44. }
  45. function errorHandler(event) {
  46. ok(false,
  47. event.target + " received error event: '" + event.target.error.name +
  48. "'");
  49. finish();
  50. }
  51. function testSteps() {
  52. addMessageListener(mmName, grabAndContinue);
  53. let message = yield undefined;
  54. let blob = message.data;
  55. ok(blob instanceof Ci.nsIDOMBlob, "Message manager sent a blob");
  56. is(blob.size, blobText.length, "Blob has correct length");
  57. is(blob.type, blobType, "Blob has correct type");
  58. info("Reading blob");
  59. let reader = new FileReader();
  60. reader.addEventListener("load", grabAndContinue);
  61. reader.readAsText(blob);
  62. yield undefined;
  63. is(reader.result, blobText, "Blob has correct data");
  64. let slice = blob.slice(0, blobData[0].length, blobType);
  65. ok(slice instanceof Ci.nsIDOMBlob, "Slice returned a blob");
  66. is(slice.size, blobData[0].length, "Slice has correct length");
  67. is(slice.type, blobType, "Slice has correct type");
  68. info("Reading slice");
  69. reader = new FileReader();
  70. reader.addEventListener("load", grabAndContinue);
  71. reader.readAsText(slice);
  72. yield undefined;
  73. is(reader.result, blobData[0], "Slice has correct data");
  74. info("Deleting database");
  75. let req = indexedDB.deleteDatabase(dbName);
  76. req.onerror = errorHandler;
  77. req.onsuccess = grabAndContinue;
  78. let event = yield undefined;
  79. is(event.type, "success", "Got success event");
  80. info("Opening database");
  81. req = indexedDB.open(dbName, dbVersion);
  82. req.onerror = errorHandler;
  83. req.onupgradeneeded = grabAndContinue;
  84. req.onsuccess = grabAndContinue;
  85. event = yield undefined;
  86. is(event.type, "upgradeneeded", "Got upgradeneeded event");
  87. event.target.result.createObjectStore(objStoreName);
  88. event = yield undefined;
  89. is(event.type, "success", "Got success event");
  90. let db = event.target.result;
  91. info("Storing blob from message manager in database");
  92. let objectStore =
  93. db.transaction(objStoreName, "readwrite").objectStore(objStoreName);
  94. req = objectStore.add(blob, key);
  95. req.onerror = errorHandler;
  96. req.onsuccess = grabAndContinue;
  97. event = yield undefined;
  98. info("Getting blob from database");
  99. objectStore = db.transaction(objStoreName).objectStore(objStoreName);
  100. req = objectStore.get(key);
  101. req.onerror = errorHandler;
  102. req.onsuccess = grabAndContinue;
  103. event = yield undefined;
  104. blob = event.target.result;
  105. ok(blob instanceof Ci.nsIDOMBlob, "Database gave us a blob");
  106. is(blob.size, blobText.length, "Blob has correct length");
  107. is(blob.type, blobType, "Blob has correct type");
  108. info("Reading blob");
  109. reader = new FileReader();
  110. reader.addEventListener("load", grabAndContinue);
  111. reader.readAsText(blob);
  112. yield undefined;
  113. is(reader.result, blobText, "Blob has correct data");
  114. info("Storing slice from message manager in database");
  115. objectStore =
  116. db.transaction(objStoreName, "readwrite").objectStore(objStoreName);
  117. req = objectStore.put(slice, key);
  118. req.onerror = errorHandler;
  119. req.onsuccess = grabAndContinue;
  120. event = yield undefined;
  121. info("Getting slice from database");
  122. objectStore = db.transaction(objStoreName).objectStore(objStoreName);
  123. req = objectStore.get(key);
  124. req.onerror = errorHandler;
  125. req.onsuccess = grabAndContinue;
  126. event = yield undefined;
  127. slice = event.target.result;
  128. ok(slice instanceof Ci.nsIDOMBlob, "Database gave us a blob");
  129. is(slice.size, blobData[0].length, "Slice has correct length");
  130. is(slice.type, blobType, "Slice has correct type");
  131. info("Reading Slice");
  132. reader = new FileReader();
  133. reader.addEventListener("load", grabAndContinue);
  134. reader.readAsText(slice);
  135. yield undefined;
  136. is(reader.result, blobData[0], "Slice has correct data");
  137. info("Sending blob and slice from database to message manager");
  138. finish([blob, slice]);
  139. yield undefined;
  140. }
  141. let testGenerator = testSteps();
  142. testGenerator.next();
  143. }
  144. function parentFrameScript(mm) {
  145. const messageName = "test:idb-and-mm";
  146. const blobData = ["So", " ", "many", " ", "blobs!"];
  147. const blobText = blobData.join("");
  148. const blobType = "text/plain";
  149. const blob = new Blob(blobData, { type: blobType });
  150. function grabAndContinue(arg) {
  151. testGenerator.send(arg);
  152. }
  153. function testSteps() {
  154. let result = yield undefined;
  155. is(Array.isArray(result), true, "Child delivered an array of results");
  156. is(result.length, 2, "Child delivered two results");
  157. let blob = result[0];
  158. is(blob instanceof Blob, true, "Child delivered a blob");
  159. is(blob.size, blobText.length, "Blob has correct size");
  160. is(blob.type, blobType, "Blob has correct type");
  161. let slice = result[1];
  162. is(slice instanceof Blob, true, "Child delivered a slice");
  163. is(slice.size, blobData[0].length, "Slice has correct size");
  164. is(slice.type, blobType, "Slice has correct type");
  165. info("Reading blob");
  166. let reader = new FileReader();
  167. reader.onload = grabAndContinue;
  168. reader.readAsText(blob);
  169. yield undefined;
  170. is(reader.result, blobText, "Blob has correct data");
  171. info("Reading slice");
  172. reader = new FileReader();
  173. reader.onload = grabAndContinue;
  174. reader.readAsText(slice);
  175. yield undefined;
  176. is(reader.result, blobData[0], "Slice has correct data");
  177. slice = blob.slice(0, blobData[0].length, blobType);
  178. is(slice instanceof Blob, true, "Made a new slice from blob");
  179. is(slice.size, blobData[0].length, "Second slice has correct size");
  180. is(slice.type, blobType, "Second slice has correct type");
  181. info("Reading second slice");
  182. reader = new FileReader();
  183. reader.onload = grabAndContinue;
  184. reader.readAsText(slice);
  185. yield undefined;
  186. is(reader.result, blobData[0], "Second slice has correct data");
  187. SimpleTest.finish();
  188. yield undefined;
  189. }
  190. let testGenerator = testSteps();
  191. testGenerator.next();
  192. mm.addMessageListener(messageName, function(message) {
  193. let data = message.data;
  194. switch (data.op) {
  195. case "info": {
  196. info(data.msg);
  197. break;
  198. }
  199. case "ok": {
  200. ok(data.condition, data.name, data.diag);
  201. break;
  202. }
  203. case "done": {
  204. testGenerator.send(data.result);
  205. break;
  206. }
  207. default: {
  208. ok(false, "Unknown op: " + data.op);
  209. SimpleTest.finish();
  210. }
  211. }
  212. });
  213. mm.loadFrameScript("data:,(" + childFrameScript.toString() + ")();",
  214. false);
  215. mm.sendAsyncMessage(messageName, blob);
  216. }
  217. function setup() {
  218. info("Got load event");
  219. SpecialPowers.pushPrefEnv(
  220. { set: [ ["dom.ipc.browser_frames.oop_by_default", true],
  221. ["dom.mozBrowserFramesEnabled", true],
  222. ["network.disable.ipc.security", true],
  223. ["browser.pagethumbnails.capturing_disabled", true] ] },
  224. function() {
  225. info("Prefs set");
  226. SpecialPowers.pushPermissions(
  227. [ { type: "browser", allow: true, context: document } ],
  228. function() {
  229. info("Permissions set");
  230. let iframe = document.createElement("iframe");
  231. SpecialPowers.wrap(iframe).mozbrowser = true;
  232. iframe.id = "iframe";
  233. iframe.src =
  234. "data:text/html,<!DOCTYPE HTML><html><body></body></html>";
  235. iframe.addEventListener("mozbrowserloadend", function() {
  236. info("Starting tests");
  237. let mm = SpecialPowers.getBrowserFrameMessageManager(iframe)
  238. parentFrameScript(mm);
  239. });
  240. document.body.appendChild(iframe);
  241. }
  242. );
  243. }
  244. );
  245. }
  246. SimpleTest.waitForExplicitFinish();
  247. </script>
  248. </body>
  249. </html>