summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/test/test_blob_archive.html
blob: add1ad7ee42d337bed1a864e6ae9a6d55c944ba8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<html>
<head>
  <title>Indexed Database Property Test</title>

  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>

  <script type="text/javascript;version=1.7">

  function testSteps()
  {
    const BLOB_DATA =
      "504B03040A00000000002E6BF14000000000000000000000000005001C00746573742F" +
      "555409000337CA055039CA055075780B000104E803000004E8030000504B0304140000" +
      "0008002D6BF1401780E15015000000580200000A001C00746573742F612E7478745554" +
      "09000336CA05503ACA055075780B000104E803000004E8030000CB48CDC9C95728CF2F" +
      "CA49E1CA18658FB2A9C40600504B03040A00000000002F88EC40662E84701000000010" +
      "0000000A001C00746573742F622E74787455540900035A65FF4F42C5055075780B0001" +
      "04E803000004E803000068656C6C6F20776F726C642C2032210A504B01021E030A0000" +
      "0000002E6BF140000000000000000000000000050018000000000000001000FD410000" +
      "0000746573742F555405000337CA055075780B000104E803000004E8030000504B0102" +
      "1E031400000008002D6BF1401780E15015000000580200000A00180000000000010000" +
      "00B4813F000000746573742F612E747874555405000336CA055075780B000104E80300" +
      "0004E8030000504B01021E030A00000000002F88EC40662E847010000000100000000A" +
      "0018000000000001000000B48198000000746573742F622E74787455540500035A65FF" +
      "4F75780B000104E803000004E8030000504B05060000000003000300EB000000EC0000" +
      "000000";

    const TEST_FILE_1 = "test/a.txt";
    const TEST_FILE_2 = "test/b.txt";

    let TEST_FILE_1_CONTENTS = "";
    for (let i = 0; i < 50; i++) {
      TEST_FILE_1_CONTENTS += "hello world\n";
    }
    const TEST_FILE_2_CONTENTS = "hello world, 2!\n";

    let binaryData = new Uint8Array(BLOB_DATA.length / 2);
    for (let i = 0, len = BLOB_DATA.length / 2; i < len; i++) {
      let hex = BLOB_DATA[i * 2] + BLOB_DATA[i * 2 + 1];
      binaryData[i] = parseInt(hex, 16);
    }

    let request = indexedDB.open(window.location.pathname, 1);
    request.onerror = errorHandler;
    request.onupgradeneeded = grabEventAndContinueHandler;
    request.onsuccess = unexpectedSuccessHandler;
    let event = yield undefined;

    let db = event.target.result;
    db.onerror = errorHandler;

    let objectStore = db.createObjectStore("foo", { autoIncrement: true });
    let index = objectStore.createIndex("foo", "index");

    request.onsuccess = grabEventAndContinueHandler;
    event = yield undefined;

    let data = new Blob([binaryData]);

    objectStore = db.transaction("foo", "readwrite").objectStore("foo");
    objectStore.add(data).onsuccess = grabEventAndContinueHandler;
    event = yield undefined;

    let key = event.target.result;

    objectStore = db.transaction("foo").objectStore("foo");
    objectStore.get(key).onsuccess = grabEventAndContinueHandler;
    event = yield undefined;

    let archiveReader = new ArchiveReader(event.target.result);
    ok(archiveReader, "Got an ArchiveReader");

    request = archiveReader.getFilenames();
    request.onsuccess = grabEventAndContinueHandler;
    request.onerror = errorHandler;
    event = yield undefined;

    is(event.target.result.length, 2, "Got 2 archive items");
    is(event.target.result[0], TEST_FILE_1,
       "First file is '" + TEST_FILE_1 + "'");
    is(event.target.result[1], TEST_FILE_2,
       "Second file is '" + TEST_FILE_2 + "'");

    request = archiveReader.getFile(TEST_FILE_1);
    request.onsuccess = grabEventAndContinueHandler;
    request.onerror = errorHandler;
    event = yield undefined;

    let fileReader = new FileReader();
    fileReader.readAsText(event.target.result);
    fileReader.onload = grabEventAndContinueHandler;
    fileReader.onerror = errorHandler;
    event = yield undefined;

    // Don't use is() because it prints out 100 lines of text...
    ok(event.target.result == TEST_FILE_1_CONTENTS, "Correct text");

    request = archiveReader.getFile(TEST_FILE_2);
    request.onsuccess = grabEventAndContinueHandler;
    request.onerror = errorHandler;
    event = yield undefined;

    fileReader = new FileReader();
    fileReader.readAsText(event.target.result);
    fileReader.onload = grabEventAndContinueHandler;
    fileReader.onerror = errorHandler;
    event = yield undefined;

    // Don't use is() because it prints out a newline...
    ok(event.target.result == TEST_FILE_2_CONTENTS, "Correct text");

    finishTest();
    yield undefined;
  }
  </script>
  <script type="text/javascript;version=1.7" src="helpers.js"></script>

</head>

<body onload="runTest();"></body>

</html>