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
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161

content / test / data / indexeddb / key_types_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 test() {
  indexedDBTest(prepareDatabase, testValidKeys);
}

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

var valid_keys = [
  "-Infinity",
  "-Number.MAX_VALUE",
  "-1",
  "-Number.MIN_VALUE",
  "0",
  "Number.MIN_VALUE",
  "1",
  "Number.MAX_VALUE",
  "Infinity",

  "new Date(0)",
  "new Date(1000)",
  "new Date(1317399931023)",

  "''",

  "'\x00'",
  "'a'",
  "'aa'",
  "'b'",
  "'ba'",

  "'\xA2'", // U+00A2 CENT SIGN
  "'\u6C34'", // U+6C34 CJK UNIFIED IDEOGRAPH (water)
  "'\uD834\uDD1E'", // U+1D11E MUSICAL SYMBOL G-CLEF (UTF-16 surrogate pair)
  "'\uFFFD'", // U+FFFD REPLACEMENT CHARACTER

  "[]",

  "[-Infinity]",
  "[-Number.MAX_VALUE]",
  "[-1]",
  "[-Number.MIN_VALUE]",
  "[0]",
  "[Number.MIN_VALUE]",
  "[1]",
  "[Number.MAX_VALUE]",
  "[Infinity]",

  "[new Date(0)]",
  "[new Date(1000)]",
  "[new Date(1317399931023)]",

  "['']",
  "['\x00']",
  "['a']",
  "['aa']",
  "['b']",
  "['ba']",

  "['\xA2']", // U+00A2 CENT SIGN
  "['\u6C34']", // U+6C34 CJK UNIFIED IDEOGRAPH (water)
  "['\uD834\uDD1E']", // U+1D11E MUSICAL SYMBOL G-CLEF (UTF-16 surrogate pair)
  "['\uFFFD']", // U+FFFD REPLACEMENT CHARACTER

  "[[]]",

  "[[], []]",
  "[[], [], []]",

  "[[[]]]",
  "[[[[]]]]"
];


var invalid_keys = [
  "void 0", // undefined
  "true",
  "false",
  "NaN",
  "null",
  "{}",
  "function () {}",
  "/regex/",
  "window",
  "window.document",
  "window.document.body",
  "(function() { var cyclic = []; cyclic.push(cyclic); return cyclic; }())"
];


function testValidKeys() {
  var test_keys = valid_keys.slice(); // make a copy
  var count = 0, when_complete = testInvalidKeys;
  testNextKey();

  function testNextKey() {
    var key = test_keys.shift();
    if (!key) {
      when_complete();
      return;
    }

    key = eval("(" + key + ")");
    var value = 'value' + (count++);
    var trans = db.transaction('store', 'readwrite');
    var store = trans.objectStore('store');
    var putreq = store.put(value, key);
    putreq.onerror = unexpectedErrorCallback;
    putreq.onsuccess = function() {
      getreq = store.get(key);
      getreq.onerror = unexpectedErrorCallback;
      getreq.onsuccess = function() {
        shouldBeEqualToString('getreq.result', value);
      };
    };
    trans.oncomplete = testNextKey;
  }
}

function testInvalidKeys() {

  var trans = db.transaction('store', 'readwrite');
  var store = trans.objectStore('store');

  invalid_keys.forEach(
    function(key) {
      try {
        key = eval("(" + key + ")");
        var putreq = store.put('value', key);
        putreq.onerror = unexpectedErrorCallback;
        putreq.onsuccess = unexpectedSuccessCallback;
        return;
      } catch (e) {
        window.ex = e;
        shouldBe("ex.code", "0");
        shouldBe("ex.name", "'DataError'");
      }
    });
  testKeyOrdering();
}

function testKeyOrdering() {

  for (var i = 0; i < valid_keys.length - 1; ++i) {
    var key1 = valid_keys[i];
    var key2 = valid_keys[i + 1];

    shouldBe("indexedDB.cmp(" + key1 + "," +  key2 + ")", "-1");
    shouldBe("indexedDB.cmp(" + key2 + "," +  key1 + ")", "1");
    shouldBe("indexedDB.cmp(" + key1 + "," +  key1 + ")", "0");
    shouldBe("indexedDB.cmp(" + key2 + "," +  key2 + ")", "0");
  }

  done();
}