Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | // This file tests the custom progress handlers |
michael@0 | 6 | |
michael@0 | 7 | function setup() |
michael@0 | 8 | { |
michael@0 | 9 | var msc = getOpenedDatabase(); |
michael@0 | 10 | msc.createTable("handler_tests", "id INTEGER PRIMARY KEY, num INTEGER"); |
michael@0 | 11 | msc.beginTransaction(); |
michael@0 | 12 | |
michael@0 | 13 | var stmt = createStatement("INSERT INTO handler_tests (id, num) VALUES(?1, ?2)"); |
michael@0 | 14 | for(var i = 0; i < 100; ++i) { |
michael@0 | 15 | stmt.bindByIndex(0, i); |
michael@0 | 16 | stmt.bindByIndex(1, Math.floor(Math.random()*1000)); |
michael@0 | 17 | stmt.execute(); |
michael@0 | 18 | } |
michael@0 | 19 | stmt.reset(); |
michael@0 | 20 | msc.commitTransaction(); |
michael@0 | 21 | stmt.finalize(); |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | var testProgressHandler = { |
michael@0 | 25 | calls: 0, |
michael@0 | 26 | abort: false, |
michael@0 | 27 | |
michael@0 | 28 | onProgress: function(comm) { |
michael@0 | 29 | ++this.calls; |
michael@0 | 30 | return this.abort; |
michael@0 | 31 | } |
michael@0 | 32 | }; |
michael@0 | 33 | |
michael@0 | 34 | function test_handler_registration() |
michael@0 | 35 | { |
michael@0 | 36 | var msc = getOpenedDatabase(); |
michael@0 | 37 | msc.setProgressHandler(10, testProgressHandler); |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | function test_handler_return() |
michael@0 | 41 | { |
michael@0 | 42 | var msc = getOpenedDatabase(); |
michael@0 | 43 | var oldH = msc.setProgressHandler(5, testProgressHandler); |
michael@0 | 44 | do_check_true(oldH instanceof Ci.mozIStorageProgressHandler); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | function test_handler_removal() |
michael@0 | 48 | { |
michael@0 | 49 | var msc = getOpenedDatabase(); |
michael@0 | 50 | msc.removeProgressHandler(); |
michael@0 | 51 | var oldH = msc.removeProgressHandler(); |
michael@0 | 52 | do_check_eq(oldH, null); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | function test_handler_call() |
michael@0 | 56 | { |
michael@0 | 57 | var msc = getOpenedDatabase(); |
michael@0 | 58 | msc.setProgressHandler(50, testProgressHandler); |
michael@0 | 59 | // Some long-executing request |
michael@0 | 60 | var stmt = createStatement( |
michael@0 | 61 | "SELECT SUM(t1.num * t2.num) FROM handler_tests AS t1, handler_tests AS t2"); |
michael@0 | 62 | while(stmt.executeStep()); |
michael@0 | 63 | do_check_true(testProgressHandler.calls > 0); |
michael@0 | 64 | stmt.finalize(); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | function test_handler_abort() |
michael@0 | 68 | { |
michael@0 | 69 | var msc = getOpenedDatabase(); |
michael@0 | 70 | testProgressHandler.abort = true; |
michael@0 | 71 | msc.setProgressHandler(50, testProgressHandler); |
michael@0 | 72 | // Some long-executing request |
michael@0 | 73 | var stmt = createStatement( |
michael@0 | 74 | "SELECT SUM(t1.num * t2.num) FROM handler_tests AS t1, handler_tests AS t2"); |
michael@0 | 75 | |
michael@0 | 76 | const SQLITE_INTERRUPT = 9; |
michael@0 | 77 | try { |
michael@0 | 78 | while(stmt.executeStep()); |
michael@0 | 79 | do_throw("We shouldn't get here!"); |
michael@0 | 80 | } catch (e) { |
michael@0 | 81 | do_check_eq(Cr.NS_ERROR_ABORT, e.result); |
michael@0 | 82 | do_check_eq(SQLITE_INTERRUPT, msc.lastError); |
michael@0 | 83 | } |
michael@0 | 84 | try { |
michael@0 | 85 | stmt.finalize(); |
michael@0 | 86 | do_throw("We shouldn't get here!"); |
michael@0 | 87 | } catch (e) { |
michael@0 | 88 | // finalize should return the error code since we encountered an error |
michael@0 | 89 | do_check_eq(Cr.NS_ERROR_ABORT, e.result); |
michael@0 | 90 | do_check_eq(SQLITE_INTERRUPT, msc.lastError); |
michael@0 | 91 | } |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | var tests = [test_handler_registration, test_handler_return, |
michael@0 | 95 | test_handler_removal, test_handler_call, |
michael@0 | 96 | test_handler_abort]; |
michael@0 | 97 | |
michael@0 | 98 | function run_test() |
michael@0 | 99 | { |
michael@0 | 100 | setup(); |
michael@0 | 101 | |
michael@0 | 102 | for (var i = 0; i < tests.length; i++) { |
michael@0 | 103 | tests[i](); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | cleanup(); |
michael@0 | 107 | } |