summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/IndexedDB/file_support.sub.html
blob: fe4bdf13ed18098c9e35643f20249b5477a073dd (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
<!doctype html>
<meta charset=utf8>
<title>File support in IndexedDB</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="resources/support-promises.js"></script>
<form id="form">
  <input id="file_input" name="file_input" type="file">
</form>
<script>

function assert_file_metadata_equal(file1, file2) {
  assert_true(file1 instanceof File);
  assert_true(file2 instanceof File)
  assert_equals(file1.lastModified, file2.lastModified);
  assert_equals(file1.name, file2.name);
  assert_equals(file1.size, file2.size);
  assert_equals(file1.type, file2.type);
}

async function assert_file_contents_equals(file1, file2) {
  const file1_text = await file1.text();
  const file2_text = await file2.text();
  assert_equals(file1_text, file2_text);
}

promise_test(async (testCase) => {
  const input = document.getElementById("file_input");
  await test_driver.send_keys(input, String.raw`{{fs_path(resources/file_to_save.txt)}}`);
  assert_equals(input.files.length, 1);

  const file = input.files[0];

  const db = await createDatabase(testCase, db => {
    db.createObjectStore('objectStore');
  });

  const txn = db.transaction(['objectStore'], 'readwrite');
  txn.objectStore('objectStore').add(file, 'key1');
  txn.objectStore('objectStore').add({file: file, other: 'data'}, 'key2');
  await promiseForTransaction(testCase, txn);

  const readTxn = db.transaction(['objectStore'], 'readonly');
  const fileByItself = await promiseForRequest(
      testCase, readTxn.objectStore('objectStore').get('key1'));
  const fileInDict = await promiseForRequest(
      testCase, readTxn.objectStore('objectStore').get('key2'));

  assert_file_metadata_equal(fileByItself, file);
  assert_file_metadata_equal(fileInDict.file, file);
  assert_file_metadata_equal(fileInDict.file, fileByItself);

  await assert_file_contents_equals(fileByItself, file);
  await assert_file_contents_equals(fileInDict.file, file);

  db.close();
}, "Saves and loads back File objects from IndexedDB");

</script>