michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // This file tests the custom progress handlers michael@0: michael@0: function setup() michael@0: { michael@0: var msc = getOpenedDatabase(); michael@0: msc.createTable("handler_tests", "id INTEGER PRIMARY KEY, num INTEGER"); michael@0: msc.beginTransaction(); michael@0: michael@0: var stmt = createStatement("INSERT INTO handler_tests (id, num) VALUES(?1, ?2)"); michael@0: for(var i = 0; i < 100; ++i) { michael@0: stmt.bindByIndex(0, i); michael@0: stmt.bindByIndex(1, Math.floor(Math.random()*1000)); michael@0: stmt.execute(); michael@0: } michael@0: stmt.reset(); michael@0: msc.commitTransaction(); michael@0: stmt.finalize(); michael@0: } michael@0: michael@0: var testProgressHandler = { michael@0: calls: 0, michael@0: abort: false, michael@0: michael@0: onProgress: function(comm) { michael@0: ++this.calls; michael@0: return this.abort; michael@0: } michael@0: }; michael@0: michael@0: function test_handler_registration() michael@0: { michael@0: var msc = getOpenedDatabase(); michael@0: msc.setProgressHandler(10, testProgressHandler); michael@0: } michael@0: michael@0: function test_handler_return() michael@0: { michael@0: var msc = getOpenedDatabase(); michael@0: var oldH = msc.setProgressHandler(5, testProgressHandler); michael@0: do_check_true(oldH instanceof Ci.mozIStorageProgressHandler); michael@0: } michael@0: michael@0: function test_handler_removal() michael@0: { michael@0: var msc = getOpenedDatabase(); michael@0: msc.removeProgressHandler(); michael@0: var oldH = msc.removeProgressHandler(); michael@0: do_check_eq(oldH, null); michael@0: } michael@0: michael@0: function test_handler_call() michael@0: { michael@0: var msc = getOpenedDatabase(); michael@0: msc.setProgressHandler(50, testProgressHandler); michael@0: // Some long-executing request michael@0: var stmt = createStatement( michael@0: "SELECT SUM(t1.num * t2.num) FROM handler_tests AS t1, handler_tests AS t2"); michael@0: while(stmt.executeStep()); michael@0: do_check_true(testProgressHandler.calls > 0); michael@0: stmt.finalize(); michael@0: } michael@0: michael@0: function test_handler_abort() michael@0: { michael@0: var msc = getOpenedDatabase(); michael@0: testProgressHandler.abort = true; michael@0: msc.setProgressHandler(50, testProgressHandler); michael@0: // Some long-executing request michael@0: var stmt = createStatement( michael@0: "SELECT SUM(t1.num * t2.num) FROM handler_tests AS t1, handler_tests AS t2"); michael@0: michael@0: const SQLITE_INTERRUPT = 9; michael@0: try { michael@0: while(stmt.executeStep()); michael@0: do_throw("We shouldn't get here!"); michael@0: } catch (e) { michael@0: do_check_eq(Cr.NS_ERROR_ABORT, e.result); michael@0: do_check_eq(SQLITE_INTERRUPT, msc.lastError); michael@0: } michael@0: try { michael@0: stmt.finalize(); michael@0: do_throw("We shouldn't get here!"); michael@0: } catch (e) { michael@0: // finalize should return the error code since we encountered an error michael@0: do_check_eq(Cr.NS_ERROR_ABORT, e.result); michael@0: do_check_eq(SQLITE_INTERRUPT, msc.lastError); michael@0: } michael@0: } michael@0: michael@0: var tests = [test_handler_registration, test_handler_return, michael@0: test_handler_removal, test_handler_call, michael@0: test_handler_abort]; michael@0: michael@0: function run_test() michael@0: { michael@0: setup(); michael@0: michael@0: for (var i = 0; i < tests.length; i++) { michael@0: tests[i](); michael@0: } michael@0: michael@0: cleanup(); michael@0: }