1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140

content / test / data / indexeddb / index_test.js [blame]

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

function onCursor()
{
  var cursor = event.target.result;
  if (cursor === null) {
    debug('Reached end of object cursor.');
    if (!gotObjectThroughCursor) {
      fail('Did not get object through cursor.');
      return;
    }
    done();
    return;
  }

  debug('Got object through cursor.');
  shouldBe('event.target.result.key', '55');
  shouldBe('event.target.result.value.aValue', '"foo"');
  gotObjectThroughCursor = true;

  cursor.continue();
}

function onKeyCursor()
{
  var cursor = event.target.result;
  if (cursor === null) {
    debug('Reached end of key cursor.');
    if (!gotKeyThroughCursor) {
      fail('Did not get key through cursor.');
      return;
    }

    var request = index.openCursor(IDBKeyRange.only(55));
    request.onsuccess = onCursor;
    request.onerror = unexpectedErrorCallback;
    gotObjectThroughCursor = false;
    return;
  }

  debug('Got key through cursor.');
  shouldBe('event.target.result.key', '55');
  shouldBe('event.target.result.primaryKey', '1');
  gotKeyThroughCursor = true;

  cursor.continue();
}

function getSuccess()
{
  debug('Successfully got object through key in index.');

  shouldBe('event.target.result.aKey', '55');
  shouldBe('event.target.result.aValue', '"foo"');

  var request = index.openKeyCursor(IDBKeyRange.only(55));
  request.onsuccess = onKeyCursor;
  request.onerror = unexpectedErrorCallback;
  gotKeyThroughCursor = false;
}

function getKeySuccess()
{
  debug('Successfully got key.');
  shouldBe('event.target.result', '1');

  var request = index.get(55);
  request.onsuccess = getSuccess;
  request.onerror = unexpectedErrorCallback;
}

function moreDataAdded()
{
  debug('Successfully added more data.');

  var request = index.getKey(55);
  request.onsuccess = getKeySuccess;
  request.onerror = unexpectedErrorCallback;
}

function indexErrorExpected()
{
  debug('Existing index triggered on error as expected.');

  var request = objectStore.put({'aKey': 55, 'aValue': 'foo'}, 1);
  request.onsuccess = moreDataAdded;
  request.onerror = unexpectedErrorCallback;
}

function indexSuccess()
{
  debug('Index created successfully.');

  shouldBe("index.name", "'myIndex'");
  shouldBe("index.objectStore.name", "'test'");
  shouldBe("index.keyPath", "'aKey'");
  shouldBe("index.unique", "true");

  try {
    request = objectStore.createIndex('myIndex', 'aKey', {unique: true});
    fail('Re-creating an index must throw an exception');
  } catch (e) {
    indexErrorExpected();
  }
}

function createIndex(expect_error)
{
  debug('Creating an index.');
  try {
    window.index = objectStore.createIndex('myIndex', 'aKey', {unique: true});
    indexSuccess();
  } catch (e) {
    unexpectedErrorCallback();
  }
}

function dataAddedSuccess()
{
  debug('Data added');
  createIndex(false);
}

function populateObjectStore()
{
  debug('Populating object store');
  db = event.target.result;
  window.objectStore = db.createObjectStore('test');
  var myValue = {'aKey': 21, 'aValue': '!42'};
  var request = objectStore.add(myValue, 0);
  request.onsuccess = dataAddedSuccess;
  request.onerror = unexpectedErrorCallback;
}

function test() {
  indexedDBTest(populateObjectStore);
}