1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 if (window.webkitStorageInfo) {
7 window.jsTestIsAsync = true;
8 navigator.webkitTemporaryStorage.queryUsageAndQuota(
10 unexpectedErrorCallback);
12 debug("This test requires window.webkitStorageInfo.");
15 function initUsageCallback(usage, quota) {
16 origReturnedUsage = returnedUsage = usage;
17 origReturnedQuota = returnedQuota = quota;
18 debug("original quota is " + displaySize(origReturnedQuota));
19 debug("original usage is " + displaySize(origReturnedUsage));
21 indexedDBTest(prepareDatabase, initQuotaEnforcing);
24 function prepareDatabase()
26 db = event.target.result;
27 objectStore = db.createObjectStore("test123");
30 function displaySize(bytes) {
33 return bytes + " (" + k + "k) (" + m + "m)";
36 function initQuotaEnforcing() {
37 var availableSpace = origReturnedQuota - origReturnedUsage;
38 var kMaxMbPerWrite = 5;
40 var len = Math.min(kMaxMbPerWrite * 1024 * 1024,
41 Math.floor(availableSpace / kMinWrites));
42 maxExpectedWrites = Math.floor(availableSpace / len) + 1;
43 debug("Chunk size: " + displaySize(len));
44 debug("Expecting at most " + maxExpectedWrites + " writes, but we could " +
45 "have more if snappy is used or LevelDB is about to compact.");
46 data = Array(1+len).join("X");
47 dataLength = data.length;
50 startNewTransaction();
53 function startNewTransaction() {
54 if (dataAdded > origReturnedQuota) {
55 fail("dataAdded > quota " + dataAdded + " > " + origReturnedQuota);
59 debug("Starting new transaction.");
61 var trans = db.transaction(['test123'], 'readwrite');
62 trans.onabort = onAbort;
63 trans.oncomplete = getQuotaAndUsage;
64 var store = trans.objectStore('test123');
65 request = store.put({x: data}, dataAdded);
66 request.onerror = logError;
69 function getQuotaAndUsage() {
71 if (successfulWrites > maxExpectedWrites) {
72 debug("Weird: too many writes. There were " + successfulWrites +
73 " but we only expected " + maxExpectedWrites);
75 navigator.webkitTemporaryStorage.queryUsageAndQuota(
76 usageCallback, unexpectedErrorCallback);
79 function usageCallback(usage, quota) {
80 debug("Transaction finished.");
81 dataAdded += dataLength;
82 debug("We've added "+ displaySize(dataAdded));
83 returnedUsage = usage;
84 returnedQuota = quota;
85 debug("Allotted quota is " + displaySize(returnedQuota));
86 debug("LevelDB usage is " + displaySize(returnedUsage));
87 startNewTransaction();
91 shouldBeEqualToString("event.target.error.name", "QuotaExceededError");
92 done("Transaction aborted. Data added: " + displaySize(dataAdded));
93 debug("There were " + successfulWrites + " successful writes");
97 debug("Error function called: (" + event.target.error.name + ") " +
98 event.target.error.message);
99 event.preventDefault();