chromium/content/test/data/indexeddb/blobs_use_quota.html

<!DOCTYPE html>
<html>
<!--
Copyright 2014 The Chromium Authors
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<head>
<title>IDB Test that blobs use space when created and free up space when deleted</title>
<script src="common.js"></script>
<script>

// Constants.
var store_name = 'blobs_use_space';
var blob_key = 'blob_key';
var blob_size = 4 * 1024 * 1024;

// Shared variables.
var db;
var size_initial;
var size_after_write;

function test() {
  indexedDBTest(prepareDatabase, getInitialSize);
}

function prepareDatabase() {
  db = event.target.result;
  db.createObjectStore(store_name);
}

function getInitialSize() {
  getUsage(function(usage) {
    size_initial = usage;
    putBlob();
  });
}

function putBlob() {
  debug("Writing blob.");

  var trans = db.transaction(store_name, 'readwrite');
  trans.onabort = unexpectedAbortCallback;
  trans.oncomplete = checkSizeIncreased;

  var data = new Array(1 + blob_size).join("X");
  var blob = new Blob([data]);
  var request = trans.objectStore(store_name).put(blob, blob_key);
  request.onerror = unexpectedErrorCallback;
}

function checkSizeIncreased() {
  getUsage(function(usage) {
    if (usage < size_initial + blob_size)
      fail("Size increased by only " + (usage - size_initial) +
           " after writing 20MB blob");
    size_after_write = usage;
    deleteBlob();
  });
}

function deleteBlob() {
  debug("Deleting blob.");
  var trans = db.transaction(store_name, 'readwrite');
  trans.onabort = unexpectedAbortCallback;
  trans.oncomplete = checkSizeBackToInitial;

  var request = trans.objectStore(store_name).delete(blob_key);
  request.onerror = unexpectedErrorCallback;
}

function checkSizeBackToInitial() {
  getUsage(function(usage) {
    if (usage < size_after_write) {
      done();
    } else {
      fail("Size after deleting blob is " + (usage - size_after_write) +
           " higher than size after write");
    }
  });
}

function getUsage(cb) {
  navigator.webkitTemporaryStorage.queryUsageAndQuota(
      function(usage, quota) {
        debug("Current database size is " + usage + "B");
        cb(usage);
      },
      unexpectedErrorCallback);
}

function displaySize(bytes) {
  var k = bytes / 1024;
  var m = k / 1024;
  return bytes + " (" + k + "k) (" + m + "m)";
}

</script>
</head>
<body onLoad="test()">
  <div id="status">Starting...<br></div>
</body>
</html>