summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/test/unit/test_blob_file_backed.js
blob: ba973d63d5756fc7d59a8577dc37bc5006f93c59 (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
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

/* exported testGenerator, disableWorkerTest */
var disableWorkerTest = "This test uses SpecialPowers";

var testGenerator = testSteps();

function* testSteps() {
  const fileData = "abcdefghijklmnopqrstuvwxyz";
  const fileType = "text/plain";

  const databaseName = "window" in this ? window.location.pathname : "Test";
  const objectStoreName = "foo";
  const objectStoreKey = "10";

  info("Creating temp file");

  SpecialPowers.createFiles(
    [{ data: fileData, options: { type: fileType } }],
    function (files) {
      testGenerator.next(files[0]);
    }
  );

  let file = yield undefined;

  ok(file instanceof File, "Got a File object");
  is(file.size, fileData.length, "Correct size");
  is(file.type, fileType, "Correct type");

  let fileReader = new FileReader();
  fileReader.onload = grabEventAndContinueHandler;
  fileReader.readAsText(file);

  let event = yield undefined;

  is(fileReader.result, fileData, "Correct data");

  let request = indexedDB.open(databaseName, 1);
  request.onerror = errorHandler;
  request.onupgradeneeded = grabEventAndContinueHandler;
  request.onsuccess = grabEventAndContinueHandler;

  event = yield undefined;

  let db = event.target.result;
  let objectStore = db.createObjectStore(objectStoreName);
  objectStore.put(file, objectStoreKey);

  event = yield undefined;

  db = event.target.result;

  file = null;

  objectStore = db.transaction(objectStoreName).objectStore(objectStoreName);
  objectStore.get(objectStoreKey).onsuccess = grabEventAndContinueHandler;

  event = yield undefined;

  file = event.target.result;

  ok(file instanceof File, "Got a File object");
  is(file.size, fileData.length, "Correct size");
  is(file.type, fileType, "Correct type");

  fileReader = new FileReader();
  fileReader.onload = grabEventAndContinueHandler;
  fileReader.readAsText(file);

  event = yield undefined;

  is(fileReader.result, fileData, "Correct data");

  finishTest();
}