Html程序  |  63行  |  1.41 KB

<!DOCTYPE html>
<html>
<head>
<!--
  Copyright 2013 The Chromium Authors. All rights reserved.
  Use of this source code is governed by a BSD-style license that can be
  found in the LICENSE file.
-->
<title>IDB test that database deletion triggers a compaction</title>
<script type="text/javascript" src="common.js"></script>
<script>

var dbname = 'delete_compact';

// Follow navigation requests from the browser test.
window.onhashchange = test;

function test()
{
  if (location.hash === '#fill')
    fill();
  else if (location.hash === '#purge')
    purge();
  else if (location.hash !== '#pass' && location.hash !== '#fail')
    fail('unexpected hash');
}

function fill()
{
  var bytes = 0;
  var request = indexedDB.open(dbname);
  request.onupgradeneeded = function() {
    var db = request.result;
    var store = db.createObjectStore('store');
    var kilobyte = Array(512+1).join('\u0100'); // 2 bytes in UTF-8 or UTF-16.
    var megabyte = Array(1024+1).join(kilobyte);
    for (var i = 0; i < 5; ++i) {
      store.put(megabyte, i);
      bytes += 1024 * 1024;
    }
  };
  request.onsuccess = function() {
    var db = request.result;
    db.close();
    done('filled with ' + bytes + ' bytes');
  };
}

function purge()
{
  var request = indexedDB.deleteDatabase(dbname);
  request.onsuccess = function() {
    done('purged');
  };
}

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