storage/test/unit/test_storage_progresshandler.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/storage/test/unit/test_storage_progresshandler.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,107 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +// This file tests the custom progress handlers
     1.9 +
    1.10 +function setup()
    1.11 +{
    1.12 +  var msc = getOpenedDatabase();
    1.13 +  msc.createTable("handler_tests", "id INTEGER PRIMARY KEY, num INTEGER");
    1.14 +  msc.beginTransaction();
    1.15 +
    1.16 +  var stmt = createStatement("INSERT INTO handler_tests (id, num) VALUES(?1, ?2)");
    1.17 +  for(var i = 0; i < 100; ++i) {
    1.18 +    stmt.bindByIndex(0, i);
    1.19 +    stmt.bindByIndex(1, Math.floor(Math.random()*1000));
    1.20 +    stmt.execute();
    1.21 +  }
    1.22 +  stmt.reset();
    1.23 +  msc.commitTransaction();
    1.24 +  stmt.finalize();
    1.25 +}
    1.26 +
    1.27 +var testProgressHandler = {
    1.28 +  calls: 0,
    1.29 +  abort: false,
    1.30 +
    1.31 +  onProgress: function(comm) {
    1.32 +    ++this.calls;
    1.33 +    return this.abort;
    1.34 +  }
    1.35 +};
    1.36 +
    1.37 +function test_handler_registration()
    1.38 +{
    1.39 +  var msc = getOpenedDatabase();
    1.40 +  msc.setProgressHandler(10, testProgressHandler);
    1.41 +}
    1.42 +
    1.43 +function test_handler_return()
    1.44 +{
    1.45 +  var msc = getOpenedDatabase();
    1.46 +  var oldH = msc.setProgressHandler(5, testProgressHandler);
    1.47 +  do_check_true(oldH instanceof Ci.mozIStorageProgressHandler);
    1.48 +}
    1.49 +
    1.50 +function test_handler_removal()
    1.51 +{
    1.52 +  var msc = getOpenedDatabase();
    1.53 +  msc.removeProgressHandler();
    1.54 +  var oldH = msc.removeProgressHandler();
    1.55 +  do_check_eq(oldH, null);
    1.56 +}
    1.57 +
    1.58 +function test_handler_call()
    1.59 +{
    1.60 +  var msc = getOpenedDatabase();
    1.61 +  msc.setProgressHandler(50, testProgressHandler);
    1.62 +  // Some long-executing request
    1.63 +  var stmt = createStatement(
    1.64 +    "SELECT SUM(t1.num * t2.num) FROM handler_tests AS t1, handler_tests AS t2");
    1.65 +  while(stmt.executeStep());
    1.66 +  do_check_true(testProgressHandler.calls > 0);
    1.67 +  stmt.finalize();
    1.68 +}
    1.69 +
    1.70 +function test_handler_abort()
    1.71 +{
    1.72 +  var msc = getOpenedDatabase();
    1.73 +  testProgressHandler.abort = true;
    1.74 +  msc.setProgressHandler(50, testProgressHandler);
    1.75 +  // Some long-executing request
    1.76 +  var stmt = createStatement(
    1.77 +    "SELECT SUM(t1.num * t2.num) FROM handler_tests AS t1, handler_tests AS t2");
    1.78 +
    1.79 +  const SQLITE_INTERRUPT = 9;
    1.80 +  try {
    1.81 +    while(stmt.executeStep());
    1.82 +    do_throw("We shouldn't get here!");
    1.83 +  } catch (e) {
    1.84 +    do_check_eq(Cr.NS_ERROR_ABORT, e.result);
    1.85 +    do_check_eq(SQLITE_INTERRUPT, msc.lastError);
    1.86 +  }
    1.87 +  try {
    1.88 +    stmt.finalize();
    1.89 +    do_throw("We shouldn't get here!");
    1.90 +  } catch (e) {
    1.91 +    // finalize should return the error code since we encountered an error
    1.92 +    do_check_eq(Cr.NS_ERROR_ABORT, e.result);
    1.93 +    do_check_eq(SQLITE_INTERRUPT, msc.lastError);
    1.94 +  }
    1.95 +}
    1.96 +
    1.97 +var tests = [test_handler_registration, test_handler_return,
    1.98 +             test_handler_removal, test_handler_call,
    1.99 +             test_handler_abort];
   1.100 +
   1.101 +function run_test()
   1.102 +{
   1.103 +  setup();
   1.104 +
   1.105 +  for (var i = 0; i < tests.length; i++) {
   1.106 +    tests[i]();
   1.107 +  }
   1.108 +
   1.109 +  cleanup();
   1.110 +}

mercurial