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 test() {
      6   indexedDBTest(prepareDatabase, testValidKeys);
      7 }
      8 
      9 function prepareDatabase()
     10 {
     11   db = event.target.result;
     12   db.createObjectStore('store');
     13 }
     14 
     15 var valid_keys = [
     16   "-Infinity",
     17   "-Number.MAX_VALUE",
     18   "-1",
     19   "-Number.MIN_VALUE",
     20   "0",
     21   "Number.MIN_VALUE",
     22   "1",
     23   "Number.MAX_VALUE",
     24   "Infinity",
     25 
     26   "new Date(0)",
     27   "new Date(1000)",
     28   "new Date(1317399931023)",
     29 
     30   "''",
     31 
     32   "'\x00'",
     33   "'a'",
     34   "'aa'",
     35   "'b'",
     36   "'ba'",
     37 
     38   "'\xA2'", // U+00A2 CENT SIGN
     39   "'\u6C34'", // U+6C34 CJK UNIFIED IDEOGRAPH (water)
     40   "'\uD834\uDD1E'", // U+1D11E MUSICAL SYMBOL G-CLEF (UTF-16 surrogate pair)
     41   "'\uFFFD'", // U+FFFD REPLACEMENT CHARACTER
     42 
     43   "[]",
     44 
     45   "[-Infinity]",
     46   "[-Number.MAX_VALUE]",
     47   "[-1]",
     48   "[-Number.MIN_VALUE]",
     49   "[0]",
     50   "[Number.MIN_VALUE]",
     51   "[1]",
     52   "[Number.MAX_VALUE]",
     53   "[Infinity]",
     54 
     55   "[new Date(0)]",
     56   "[new Date(1000)]",
     57   "[new Date(1317399931023)]",
     58 
     59   "['']",
     60   "['\x00']",
     61   "['a']",
     62   "['aa']",
     63   "['b']",
     64   "['ba']",
     65 
     66   "['\xA2']", // U+00A2 CENT SIGN
     67   "['\u6C34']", // U+6C34 CJK UNIFIED IDEOGRAPH (water)
     68   "['\uD834\uDD1E']", // U+1D11E MUSICAL SYMBOL G-CLEF (UTF-16 surrogate pair)
     69   "['\uFFFD']", // U+FFFD REPLACEMENT CHARACTER
     70 
     71   "[[]]",
     72 
     73   "[[], []]",
     74   "[[], [], []]",
     75 
     76   "[[[]]]",
     77   "[[[[]]]]"
     78 ];
     79 
     80 
     81 var invalid_keys = [
     82   "void 0", // undefined
     83   "true",
     84   "false",
     85   "NaN",
     86   "null",
     87   "{}",
     88   "function () {}",
     89   "/regex/",
     90   "window",
     91   "window.document",
     92   "window.document.body",
     93   "(function() { var cyclic = []; cyclic.push(cyclic); return cyclic; }())"
     94 ];
     95 
     96 
     97 function testValidKeys() {
     98   var test_keys = valid_keys.slice(); // make a copy
     99   var count = 0, when_complete = testInvalidKeys;
    100   testNextKey();
    101 
    102   function testNextKey() {
    103     var key = test_keys.shift();
    104     if (!key) {
    105       when_complete();
    106       return;
    107     }
    108 
    109     key = eval("(" + key + ")");
    110     var value = 'value' + (count++);
    111     var trans = db.transaction('store', 'readwrite');
    112     var store = trans.objectStore('store');
    113     var putreq = store.put(value, key);
    114     putreq.onerror = unexpectedErrorCallback;
    115     putreq.onsuccess = function() {
    116       getreq = store.get(key);
    117       getreq.onerror = unexpectedErrorCallback;
    118       getreq.onsuccess = function() {
    119         shouldBeEqualToString('getreq.result', value);
    120       };
    121     };
    122     trans.oncomplete = testNextKey;
    123   }
    124 }
    125 
    126 function testInvalidKeys() {
    127 
    128   var trans = db.transaction('store', 'readwrite');
    129   var store = trans.objectStore('store');
    130 
    131   invalid_keys.forEach(
    132     function(key) {
    133       try {
    134         key = eval("(" + key + ")");
    135         var putreq = store.put('value', key);
    136         putreq.onerror = unexpectedErrorCallback;
    137         putreq.onsuccess = unexpectedSuccessCallback;
    138         return;
    139       } catch (e) {
    140         window.ex = e;
    141         shouldBe("ex.code", "0");
    142         shouldBe("ex.name", "'DataError'");
    143       }
    144     });
    145   testKeyOrdering();
    146 }
    147 
    148 function testKeyOrdering() {
    149 
    150   for (var i = 0; i < valid_keys.length - 1; ++i) {
    151     var key1 = valid_keys[i];
    152     var key2 = valid_keys[i + 1];
    153 
    154     shouldBe("indexedDB.cmp(" + key1 + "," +  key2 + ")", "-1");
    155     shouldBe("indexedDB.cmp(" + key2 + "," +  key1 + ")", "1");
    156     shouldBe("indexedDB.cmp(" + key1 + "," +  key1 + ")", "0");
    157     shouldBe("indexedDB.cmp(" + key2 + "," +  key2 + ")", "0");
    158   }
    159 
    160   done();
    161 }
    162