storage/test/unit/test_storage_progresshandler.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:a42b2c1b86b1
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 {
9 var msc = getOpenedDatabase();
10 msc.createTable("handler_tests", "id INTEGER PRIMARY KEY, num INTEGER");
11 msc.beginTransaction();
12
13 var stmt = createStatement("INSERT INTO handler_tests (id, num) VALUES(?1, ?2)");
14 for(var i = 0; i < 100; ++i) {
15 stmt.bindByIndex(0, i);
16 stmt.bindByIndex(1, Math.floor(Math.random()*1000));
17 stmt.execute();
18 }
19 stmt.reset();
20 msc.commitTransaction();
21 stmt.finalize();
22 }
23
24 var testProgressHandler = {
25 calls: 0,
26 abort: false,
27
28 onProgress: function(comm) {
29 ++this.calls;
30 return this.abort;
31 }
32 };
33
34 function test_handler_registration()
35 {
36 var msc = getOpenedDatabase();
37 msc.setProgressHandler(10, testProgressHandler);
38 }
39
40 function test_handler_return()
41 {
42 var msc = getOpenedDatabase();
43 var oldH = msc.setProgressHandler(5, testProgressHandler);
44 do_check_true(oldH instanceof Ci.mozIStorageProgressHandler);
45 }
46
47 function test_handler_removal()
48 {
49 var msc = getOpenedDatabase();
50 msc.removeProgressHandler();
51 var oldH = msc.removeProgressHandler();
52 do_check_eq(oldH, null);
53 }
54
55 function test_handler_call()
56 {
57 var msc = getOpenedDatabase();
58 msc.setProgressHandler(50, testProgressHandler);
59 // Some long-executing request
60 var stmt = createStatement(
61 "SELECT SUM(t1.num * t2.num) FROM handler_tests AS t1, handler_tests AS t2");
62 while(stmt.executeStep());
63 do_check_true(testProgressHandler.calls > 0);
64 stmt.finalize();
65 }
66
67 function test_handler_abort()
68 {
69 var msc = getOpenedDatabase();
70 testProgressHandler.abort = true;
71 msc.setProgressHandler(50, testProgressHandler);
72 // Some long-executing request
73 var stmt = createStatement(
74 "SELECT SUM(t1.num * t2.num) FROM handler_tests AS t1, handler_tests AS t2");
75
76 const SQLITE_INTERRUPT = 9;
77 try {
78 while(stmt.executeStep());
79 do_throw("We shouldn't get here!");
80 } catch (e) {
81 do_check_eq(Cr.NS_ERROR_ABORT, e.result);
82 do_check_eq(SQLITE_INTERRUPT, msc.lastError);
83 }
84 try {
85 stmt.finalize();
86 do_throw("We shouldn't get here!");
87 } catch (e) {
88 // finalize should return the error code since we encountered an error
89 do_check_eq(Cr.NS_ERROR_ABORT, e.result);
90 do_check_eq(SQLITE_INTERRUPT, msc.lastError);
91 }
92 }
93
94 var tests = [test_handler_registration, test_handler_return,
95 test_handler_removal, test_handler_call,
96 test_handler_abort];
97
98 function run_test()
99 {
100 setup();
101
102 for (var i = 0; i < tests.length; i++) {
103 tests[i]();
104 }
105
106 cleanup();
107 }

mercurial