Home | History | Annotate | Download | only in indexeddb
      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 onCursor()
      6 {
      7   var cursor = event.target.result;
      8   if (cursor === null) {
      9     debug('Reached end of object cursor.');
     10     if (!gotObjectThroughCursor) {
     11       fail('Did not get object through cursor.');
     12       return;
     13     }
     14     done();
     15     return;
     16   }
     17 
     18   debug('Got object through cursor.');
     19   shouldBe('event.target.result.key', '55');
     20   shouldBe('event.target.result.value.aValue', '"foo"');
     21   gotObjectThroughCursor = true;
     22 
     23   cursor.continue();
     24 }
     25 
     26 function onKeyCursor()
     27 {
     28   var cursor = event.target.result;
     29   if (cursor === null) {
     30     debug('Reached end of key cursor.');
     31     if (!gotKeyThroughCursor) {
     32       fail('Did not get key through cursor.');
     33       return;
     34     }
     35 
     36     var request = index.openCursor(IDBKeyRange.only(55));
     37     request.onsuccess = onCursor;
     38     request.onerror = unexpectedErrorCallback;
     39     gotObjectThroughCursor = false;
     40     return;
     41   }
     42 
     43   debug('Got key through cursor.');
     44   shouldBe('event.target.result.key', '55');
     45   shouldBe('event.target.result.primaryKey', '1');
     46   gotKeyThroughCursor = true;
     47 
     48   cursor.continue();
     49 }
     50 
     51 function getSuccess()
     52 {
     53   debug('Successfully got object through key in index.');
     54 
     55   shouldBe('event.target.result.aKey', '55');
     56   shouldBe('event.target.result.aValue', '"foo"');
     57 
     58   var request = index.openKeyCursor(IDBKeyRange.only(55));
     59   request.onsuccess = onKeyCursor;
     60   request.onerror = unexpectedErrorCallback;
     61   gotKeyThroughCursor = false;
     62 }
     63 
     64 function getKeySuccess()
     65 {
     66   debug('Successfully got key.');
     67   shouldBe('event.target.result', '1');
     68 
     69   var request = index.get(55);
     70   request.onsuccess = getSuccess;
     71   request.onerror = unexpectedErrorCallback;
     72 }
     73 
     74 function moreDataAdded()
     75 {
     76   debug('Successfully added more data.');
     77 
     78   var request = index.getKey(55);
     79   request.onsuccess = getKeySuccess;
     80   request.onerror = unexpectedErrorCallback;
     81 }
     82 
     83 function indexErrorExpected()
     84 {
     85   debug('Existing index triggered on error as expected.');
     86 
     87   var request = objectStore.put({'aKey': 55, 'aValue': 'foo'}, 1);
     88   request.onsuccess = moreDataAdded;
     89   request.onerror = unexpectedErrorCallback;
     90 }
     91 
     92 function indexSuccess()
     93 {
     94   debug('Index created successfully.');
     95 
     96   shouldBe("index.name", "'myIndex'");
     97   shouldBe("index.objectStore.name", "'test'");
     98   shouldBe("index.keyPath", "'aKey'");
     99   shouldBe("index.unique", "true");
    100 
    101   try {
    102     request = objectStore.createIndex('myIndex', 'aKey', {unique: true});
    103     fail('Re-creating an index must throw an exception');
    104   } catch (e) {
    105     indexErrorExpected();
    106   }
    107 }
    108 
    109 function createIndex(expect_error)
    110 {
    111   debug('Creating an index.');
    112   try {
    113     window.index = objectStore.createIndex('myIndex', 'aKey', {unique: true});
    114     indexSuccess();
    115   } catch (e) {
    116     unexpectedErrorCallback();
    117   }
    118 }
    119 
    120 function dataAddedSuccess()
    121 {
    122   debug('Data added');
    123   createIndex(false);
    124 }
    125 
    126 function populateObjectStore()
    127 {
    128   debug('Populating object store');
    129   db = event.target.result;
    130   window.objectStore = db.createObjectStore('test');
    131   var myValue = {'aKey': 21, 'aValue': '!42'};
    132   var request = objectStore.add(myValue, 0);
    133   request.onsuccess = dataAddedSuccess;
    134   request.onerror = unexpectedErrorCallback;
    135 }
    136 
    137 function test() {
    138   indexedDBTest(populateObjectStore);
    139 }
    140 
    141