test_blob_archive.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 BLOB_DATA =
  14. "504B03040A00000000002E6BF14000000000000000000000000005001C00746573742F" +
  15. "555409000337CA055039CA055075780B000104E803000004E8030000504B0304140000" +
  16. "0008002D6BF1401780E15015000000580200000A001C00746573742F612E7478745554" +
  17. "09000336CA05503ACA055075780B000104E803000004E8030000CB48CDC9C95728CF2F" +
  18. "CA49E1CA18658FB2A9C40600504B03040A00000000002F88EC40662E84701000000010" +
  19. "0000000A001C00746573742F622E74787455540900035A65FF4F42C5055075780B0001" +
  20. "04E803000004E803000068656C6C6F20776F726C642C2032210A504B01021E030A0000" +
  21. "0000002E6BF140000000000000000000000000050018000000000000001000FD410000" +
  22. "0000746573742F555405000337CA055075780B000104E803000004E8030000504B0102" +
  23. "1E031400000008002D6BF1401780E15015000000580200000A00180000000000010000" +
  24. "00B4813F000000746573742F612E747874555405000336CA055075780B000104E80300" +
  25. "0004E8030000504B01021E030A00000000002F88EC40662E847010000000100000000A" +
  26. "0018000000000001000000B48198000000746573742F622E74787455540500035A65FF" +
  27. "4F75780B000104E803000004E8030000504B05060000000003000300EB000000EC0000" +
  28. "000000";
  29. const TEST_FILE_1 = "test/a.txt";
  30. const TEST_FILE_2 = "test/b.txt";
  31. let TEST_FILE_1_CONTENTS = "";
  32. for (let i = 0; i < 50; i++) {
  33. TEST_FILE_1_CONTENTS += "hello world\n";
  34. }
  35. const TEST_FILE_2_CONTENTS = "hello world, 2!\n";
  36. let binaryData = new Uint8Array(BLOB_DATA.length / 2);
  37. for (let i = 0, len = BLOB_DATA.length / 2; i < len; i++) {
  38. let hex = BLOB_DATA[i * 2] + BLOB_DATA[i * 2 + 1];
  39. binaryData[i] = parseInt(hex, 16);
  40. }
  41. let request = indexedDB.open(window.location.pathname, 1);
  42. request.onerror = errorHandler;
  43. request.onupgradeneeded = grabEventAndContinueHandler;
  44. request.onsuccess = unexpectedSuccessHandler;
  45. let event = yield undefined;
  46. let db = event.target.result;
  47. db.onerror = errorHandler;
  48. let objectStore = db.createObjectStore("foo", { autoIncrement: true });
  49. let index = objectStore.createIndex("foo", "index");
  50. request.onsuccess = grabEventAndContinueHandler;
  51. event = yield undefined;
  52. let data = new Blob([binaryData]);
  53. objectStore = db.transaction("foo", "readwrite").objectStore("foo");
  54. objectStore.add(data).onsuccess = grabEventAndContinueHandler;
  55. event = yield undefined;
  56. let key = event.target.result;
  57. objectStore = db.transaction("foo").objectStore("foo");
  58. objectStore.get(key).onsuccess = grabEventAndContinueHandler;
  59. event = yield undefined;
  60. let archiveReader = new ArchiveReader(event.target.result);
  61. ok(archiveReader, "Got an ArchiveReader");
  62. request = archiveReader.getFilenames();
  63. request.onsuccess = grabEventAndContinueHandler;
  64. request.onerror = errorHandler;
  65. event = yield undefined;
  66. is(event.target.result.length, 2, "Got 2 archive items");
  67. is(event.target.result[0], TEST_FILE_1,
  68. "First file is '" + TEST_FILE_1 + "'");
  69. is(event.target.result[1], TEST_FILE_2,
  70. "Second file is '" + TEST_FILE_2 + "'");
  71. request = archiveReader.getFile(TEST_FILE_1);
  72. request.onsuccess = grabEventAndContinueHandler;
  73. request.onerror = errorHandler;
  74. event = yield undefined;
  75. let fileReader = new FileReader();
  76. fileReader.readAsText(event.target.result);
  77. fileReader.onload = grabEventAndContinueHandler;
  78. fileReader.onerror = errorHandler;
  79. event = yield undefined;
  80. // Don't use is() because it prints out 100 lines of text...
  81. ok(event.target.result == TEST_FILE_1_CONTENTS, "Correct text");
  82. request = archiveReader.getFile(TEST_FILE_2);
  83. request.onsuccess = grabEventAndContinueHandler;
  84. request.onerror = errorHandler;
  85. event = yield undefined;
  86. fileReader = new FileReader();
  87. fileReader.readAsText(event.target.result);
  88. fileReader.onload = grabEventAndContinueHandler;
  89. fileReader.onerror = errorHandler;
  90. event = yield undefined;
  91. // Don't use is() because it prints out a newline...
  92. ok(event.target.result == TEST_FILE_2_CONTENTS, "Correct text");
  93. finishTest();
  94. yield undefined;
  95. }
  96. </script>
  97. <script type="text/javascript;version=1.7" src="helpers.js"></script>
  98. </head>
  99. <body onload="runTest();"></body>
  100. </html>