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