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. 4 5 function test() { 6 if (window.webkitStorageInfo) { 7 window.jsTestIsAsync = true; 8 webkitStorageInfo.queryUsageAndQuota(webkitStorageInfo.TEMPORARY, 9 initUsageCallback, 10 unexpectedErrorCallback); 11 } else 12 debug("This test requires window.webkitStorageInfo."); 13 } 14 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)); 20 21 indexedDBTest(prepareDatabase, initQuotaEnforcing); 22 } 23 24 function prepareDatabase() 25 { 26 db = event.target.result; 27 objectStore = db.createObjectStore("test123"); 28 } 29 30 function displaySize(bytes) { 31 var k = bytes / 1024; 32 var m = k / 1024; 33 return bytes + " (" + k + "k) (" + m + "m)"; 34 } 35 36 function initQuotaEnforcing() { 37 var availableSpace = origReturnedQuota - origReturnedUsage; 38 var kMaxMbPerWrite = 5; 39 var kMinWrites = 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; 48 dataAdded = 0; 49 successfulWrites = 0; 50 startNewTransaction(); 51 } 52 53 function startNewTransaction() { 54 if (dataAdded > origReturnedQuota) { 55 fail("dataAdded > quota " + dataAdded + " > " + origReturnedQuota); 56 return; 57 } 58 debug(""); 59 debug("Starting new transaction."); 60 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; 67 } 68 69 function getQuotaAndUsage() { 70 successfulWrites++; 71 if (successfulWrites > maxExpectedWrites) { 72 debug("Weird: too many writes. There were " + successfulWrites + 73 " but we only expected " + maxExpectedWrites); 74 } 75 webkitStorageInfo.queryUsageAndQuota(webkitStorageInfo.TEMPORARY, 76 usageCallback, unexpectedErrorCallback); 77 } 78 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(); 88 } 89 90 function onAbort() { 91 shouldBeEqualToString("event.target.error.name", "QuotaExceededError"); 92 done("Transaction aborted. Data added: " + displaySize(dataAdded)); 93 debug("There were " + successfulWrites + " successful writes"); 94 } 95 96 function logError() { 97 debug("Error function called: (" + event.target.errorCode + ") " + 98 event.target.webkitErrorMessage); 99 event.preventDefault(); 100 } 101 102