[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / test / data / indexeddb / blobs_use_quota.html
blobcf0d427dcd3ba0e9eed3371fec2196c71e17ffb8
1 <!DOCTYPE html>
2 <html>
3 <!--
4 Copyright 2014 The Chromium Authors. All rights reserved.
5 Use of this source code is governed by a BSD-style license that can be
6 found in the LICENSE file.
7 -->
8 <head>
9 <title>IDB Test that blobs use space when created and free up space when deleted</title>
10 <script src="common.js"></script>
11 <script>
13 // Constants.
14 var store_name = 'blobs_use_space';
15 var blob_key = 'blob_key';
16 var blob_size = 20 * 1024 * 1024;
18 // Shared variables.
19 var db;
20 var size_initial;
21 var size_after_write;
23 function test() {
24 indexedDBTest(prepareDatabase, getInitialSize);
27 function prepareDatabase() {
28 db = event.target.result;
29 db.createObjectStore(store_name);
32 function getInitialSize() {
33 getUsage(function(usage) {
34 size_initial = usage;
35 putBlob();
36 });
39 function putBlob() {
40 debug("Writing blob.");
42 var trans = db.transaction(store_name, 'readwrite');
43 trans.onabort = unexpectedAbortCallback;
44 trans.oncomplete = checkSizeIncreased;
46 var data = new Array(1 + blob_size).join("X");
47 var blob = new Blob([data]);
48 var request = trans.objectStore(store_name).put(blob, blob_key);
49 request.onerror = unexpectedErrorCallback;
52 function checkSizeIncreased() {
53 getUsage(function(usage) {
54 if (usage < size_initial + blob_size)
55 fail("Size increased by only " + (usage - size_initial) +
56 " after writing 20MB blob");
57 size_after_write = usage;
58 deleteBlob();
59 });
62 function deleteBlob() {
63 debug("Deleting blob.");
64 var trans = db.transaction(store_name, 'readwrite');
65 trans.onabort = unexpectedAbortCallback;
66 trans.oncomplete = checkSizeBackToInitial;
68 var request = trans.objectStore(store_name).delete(blob_key);
69 request.onerror = unexpectedErrorCallback;
72 function checkSizeBackToInitial() {
73 getUsage(function(usage) {
74 if (usage < size_after_write) {
75 done();
76 } else {
77 fail("Size after deleting blob is " + (usage - size_after_write) +
78 " higher than size after write");
80 });
83 function getUsage(cb) {
84 navigator.webkitTemporaryStorage.queryUsageAndQuota(
85 function(usage, quota) {
86 debug("Current database size is " + usage + "B");
87 cb(usage);
89 unexpectedErrorCallback);
92 function displaySize(bytes) {
93 var k = bytes / 1024;
94 var m = k / 1024;
95 return bytes + " (" + k + "k) (" + m + "m)";
98 </script>
99 </head>
100 <body onLoad="test()">
101 <div id="status">Starting...<br></div>
102 </body>
103 </html>