dom/indexedDB/test/unit/test_keys.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /**
     2  * Any copyright is dedicated to the Public Domain.
     3  * http://creativecommons.org/publicdomain/zero/1.0/
     4  */
     6 var testGenerator = testSteps();
     8 function testSteps()
     9 {
    10   const dbname = this.window ? window.location.pathname : "Splendid Test";
    11   const RW = "readwrite"
    12   let c1 = 1;
    13   let c2 = 1;
    15   let openRequest = indexedDB.open(dbname, 1);
    16   openRequest.onerror = errorHandler;
    17   openRequest.onupgradeneeded = grabEventAndContinueHandler;
    18   openRequest.onsuccess = unexpectedSuccessHandler;
    19   let event = yield undefined;
    20   let db = event.target.result;
    21   let trans = event.target.transaction;
    23   // Create test stores
    24   let store = db.createObjectStore("store");
    26     // Test simple inserts
    27   var keys = [
    28     -1/0,
    29     -1.7e308,
    30     -10000,
    31     -2,
    32     -1.5,
    33     -1,
    34     -1.00001e-200,
    35     -1e-200,
    36     0,
    37     1e-200,
    38     1.00001e-200,
    39     1,
    40     2,
    41     10000,
    42     1.7e308,
    43     1/0,
    44     new Date("1750-01-02"),
    45     new Date("1800-12-31T12:34:56.001"),
    46     new Date(-1000),
    47     new Date(-10),
    48     new Date(-1),
    49     new Date(0),
    50     new Date(1),
    51     new Date(2),
    52     new Date(1000),
    53     new Date("1971-01-01"),
    54     new Date("1971-01-01T01:01:01Z"),
    55     new Date("1971-01-01T01:01:01.001Z"),
    56     new Date("1971-01-01T01:01:01.01Z"),
    57     new Date("1971-01-01T01:01:01.1Z"),
    58     new Date("1980-02-02"),
    59     new Date("3333-03-19T03:33:33.333"),
    60     "",
    61     "\x00",
    62     "\x00\x00",
    63     "\x00\x01",
    64     "\x01",
    65     "\x02",
    66     "\x03",
    67     "\x04",
    68     "\x07",
    69     "\x08",
    70     "\x0F",
    71     "\x10",
    72     "\x1F",
    73     "\x20",
    74     "01234",
    75     "\x3F",
    76     "\x40",
    77     "A",
    78     "A\x00",
    79     "A1",
    80     "ZZZZ",
    81     "a",
    82     "a\x00",
    83     "aa",
    84     "azz",
    85     "}",
    86     "\x7E",
    87     "\x7F",
    88     "\x80",
    89     "\xFF",
    90     "\u0100",
    91     "\u01FF",
    92     "\u0200",
    93     "\u03FF",
    94     "\u0400",
    95     "\u07FF",
    96     "\u0800",
    97     "\u0FFF",
    98     "\u1000",
    99     "\u1FFF",
   100     "\u2000",
   101     "\u3FFF",
   102     "\u4000",
   103     "\u7FFF",
   104     "\u8000",
   105     "\uD800",
   106     "\uD800a",
   107     "\uD800\uDC01",
   108     "\uDBFF",
   109     "\uDC00",
   110     "\uDFFF\uD800",
   111     "\uFFFE",
   112     "\uFFFF",
   113      "\uFFFF\x00",
   114     "\uFFFFZZZ",
   115     [],
   116     [-1/0],
   117     [-1],
   118     [0],
   119     [1],
   120     [1, "a"],
   121     [1, []],
   122     [1, [""]],
   123     [2, 3],
   124     [2, 3.0000000000001],
   125     [12, [[]]],
   126     [12, [[[]]]],
   127     [12, [[[""]]]],
   128     [12, [[["foo"]]]],
   129     [12, [[[[[3]]]]]],
   130     [12, [[[[[[3]]]]]]],
   131     [new Date(-1)],
   132     [new Date(1)],
   133     [""],
   134     ["", [[]]],
   135     ["", [[[]]]],
   136     ["abc"],
   137     ["abc", "def"],
   138     ["abc\x00"],
   139     ["abc\x00", "\x00\x01"],
   140     ["abc\x00", "\x00def"],
   141     ["abc\x00\x00def"],
   142     ["x", [[]]],
   143     ["x", [[[]]]],
   144     [[]],
   145     [[],"foo"],
   146     [[],[]],
   147     [[[]]],
   148     [[[]], []],
   149     [[[]], [[]]],
   150     [[[]], [[1]]],
   151     [[[]], [[[]]]],
   152     [[[1]]],
   153     [[[[]], []]],
   154     ];
   156   for (var i = 0; i < keys.length; ++i) {
   157     let keyI = keys[i];
   158     is(indexedDB.cmp(keyI, keyI), 0, i + " compared to self");
   160     function doCompare(keyI) {
   161       for (var j = i-1; j >= i-10 && j >= 0; --j) {
   162         is(indexedDB.cmp(keyI, keys[j]), 1, i + " compared to " + j);
   163         is(indexedDB.cmp(keys[j], keyI), -1, j + " compared to " + i);
   164       }
   165     }
   167     doCompare(keyI);
   168     store.add(i, keyI).onsuccess = function(e) {
   169       is(indexedDB.cmp(e.target.result, keyI), 0,
   170          "Returned key should cmp as equal");
   171       ok(compareKeys(e.target.result, keyI),
   172          "Returned key should actually be equal");
   173     };
   175     // Test that -0 compares the same as 0
   176     if (keyI === 0) {
   177       doCompare(-0);
   178       let req = store.add(i, -0);
   179       req.addEventListener("error", new ExpectError("ConstraintError", true));
   180       req.onsuccess = unexpectedSuccessHandler;
   181       yield undefined;
   182     }
   183     else if (Array.isArray(keyI) && keyI.length === 1 && keyI[0] === 0) {
   184       doCompare([-0]);
   185       let req = store.add(i, [-0]);
   186       req.addEventListener("error", new ExpectError("ConstraintError", true));
   187       req.onsuccess = unexpectedSuccessHandler;
   188       yield undefined;
   189     }
   190   }
   192   store.openCursor().onsuccess = grabEventAndContinueHandler;
   193   for (i = 0; i < keys.length; ++i) {
   194     event = yield undefined;
   195     let cursor = event.target.result;
   196     is(indexedDB.cmp(cursor.key, keys[i]), 0,
   197        "Read back key should cmp as equal");
   198     ok(compareKeys(cursor.key, keys[i]),
   199        "Read back key should actually be equal");
   200     is(cursor.value, i, "Stored with right value");
   202     cursor.continue();
   203   }
   204   event = yield undefined;
   205   is(event.target.result, undefined, "no more results expected");
   207   var nan = 0/0;
   208   var invalidKeys = [
   209     nan,
   210     undefined,
   211     null,
   212     /x/,
   213     {},
   214     new Date(NaN),
   215     new Date("foopy"),
   216     [nan],
   217     [undefined],
   218     [null],
   219     [/x/],
   220     [{}],
   221     [new Date(NaN)],
   222     [1, nan],
   223     [1, undefined],
   224     [1, null],
   225     [1, /x/],
   226     [1, {}],
   227     [1, [nan]],
   228     [1, [undefined]],
   229     [1, [null]],
   230     [1, [/x/]],
   231     [1, [{}]],
   232     ];
   234   for (i = 0; i < invalidKeys.length; ++i) {
   235     try {
   236       indexedDB.cmp(invalidKeys[i], 1);
   237       ok(false, "didn't throw");
   238     }
   239     catch(ex) {
   240       ok(ex instanceof DOMException, "Threw DOMException");
   241       is(ex.name, "DataError", "Threw right DOMException");
   242       is(ex.code, 0, "Threw with right code");
   243     }
   244     try {
   245       indexedDB.cmp(1, invalidKeys[i]);
   246       ok(false, "didn't throw2");
   247     }
   248     catch(ex) {
   249       ok(ex instanceof DOMException, "Threw DOMException2");
   250       is(ex.name, "DataError", "Threw right DOMException2");
   251       is(ex.code, 0, "Threw with right code2");
   252     }
   253     try {
   254       store.put(1, invalidKeys[i]);
   255       ok(false, "didn't throw3");
   256     }
   257     catch(ex) {
   258       ok(ex instanceof DOMException, "Threw DOMException3");
   259       is(ex.name, "DataError", "Threw right DOMException3");
   260       is(ex.code, 0, "Threw with right code3");
   261     }
   262   }
   264   openRequest.onsuccess = grabEventAndContinueHandler;
   265   yield undefined;
   267   finishTest();
   268   yield undefined;
   269 }

mercurial