1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is Storage Test Code.
16 * The Initial Developer of the Original Code is
17 * Mozilla Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2008
19 * the Initial Developer. All Rights Reserved.
22 * Shawn Wilsher <me@shawnwilsher.com> (Original Author)
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 // This file tests the functionality of mozIStorageStatement::executeAsync
41 const TEXT
= "this is test text";
45 function test_create_table()
47 dump("test_create_table()\n");
49 // Ensure our table doesn't exists
50 do_check_false(getOpenedDatabase().tableExists("test"));
52 var stmt
= getOpenedDatabase().createStatement(
53 "CREATE TABLE test (" +
54 "id INTEGER PRIMARY KEY, " +
64 handleResult: function(aResultSet
)
66 dump("handleResult("+aResultSet
+");\n");
67 do_throw("unexpected results obtained!");
69 handleError: function(aError
)
71 dump("handleError("+aError
+");\n");
72 do_throw("unexpected error!");
74 handleCompletion: function(aReason
)
76 dump("handleCompletion("+aReason
+");\n");
77 do_check_eq(Ci
.mozIStorageStatementCallback
.REASON_FINISHED
, aReason
);
79 // Check that the table has been created
80 do_check_true(getOpenedDatabase().tableExists("test"));
82 // Verify that it's created correctly (this will throw if it wasn't)
83 var stmt
= getOpenedDatabase().createStatement(
84 "SELECT id, string, number, nuller, blober FROM test"
88 // Now we run the rest of the tests
89 for (var i
= 0; i
< tests
.length
; i
++)
98 function test_add_data()
100 dump("test_add_data()\n");
102 var stmt
= getOpenedDatabase().createStatement(
103 "INSERT INTO test (id, string, number, nuller, blober) VALUES (?, ?, ?, ?, ?)"
105 stmt
.bindInt32Parameter(0, INTEGER
);
106 stmt
.bindStringParameter(1, TEXT
);
107 stmt
.bindDoubleParameter(2, REAL
);
108 stmt
.bindNullParameter(3);
109 stmt
.bindBlobParameter(4, BLOB
, BLOB
.length
);
113 handleResult: function(aResultSet
)
115 dump("handleResult("+aResultSet
+");\n");
116 do_throw("unexpected results obtained!");
118 handleError: function(aError
)
120 dump("handleError("+aError
+");\n");
121 do_throw("unexpected error!");
123 handleCompletion: function(aReason
)
125 dump("handleCompletion("+aReason
+");\n");
126 do_check_eq(Ci
.mozIStorageStatementCallback
.REASON_FINISHED
, aReason
);
128 // Check that the result is in the table
129 var stmt
= getOpenedDatabase().createStatement(
130 "SELECT string, number, nuller, blober FROM test WHERE id = ?"
132 stmt
.bindInt32Parameter(0, INTEGER
);
134 do_check_true(stmt
.executeStep());
135 do_check_eq(TEXT
, stmt
.getString(0));
136 do_check_eq(REAL
, stmt
.getDouble(1));
137 do_check_true(stmt
.getIsNull(2));
138 var count
= { value
: 0 };
139 var blob
= { value
: null };
140 stmt
.getBlob(3, count
, blob
);
141 do_check_eq(BLOB
.length
, count
.value
);
142 for (var i
= 0; i
< BLOB
.length
; i
++)
143 do_check_eq(BLOB
[i
], blob
.value
[i
]);
156 function test_get_data()
158 dump("test_get_data()\n");
160 var stmt
= getOpenedDatabase().createStatement(
161 "SELECT string, number, nuller, blober, id FROM test WHERE id = ?"
163 stmt
.bindInt32Parameter(0, 1);
167 resultObtained
: false,
168 handleResult: function(aResultSet
)
170 dump("handleResult("+aResultSet
+");\n");
171 do_check_false(this.resultObtained
);
172 this.resultObtained
= true;
174 // Check that we have a result
175 var tuple
= aResultSet
.getNextRow();
176 do_check_neq(null, tuple
);
178 // Check that it's what we expect
179 do_check_eq(tuple
.getResultByName("string"), tuple
.getResultByIndex(0));
180 do_check_eq(TEXT
, tuple
.getResultByName("string"));
181 do_check_eq(Ci
.mozIStorageValueArray
.VALUE_TYPE_TEXT
,
182 tuple
.getTypeOfIndex(0));
184 do_check_eq(tuple
.getResultByName("number"), tuple
.getResultByIndex(1));
185 do_check_eq(REAL
, tuple
.getResultByName("number"));
186 do_check_eq(Ci
.mozIStorageValueArray
.VALUE_TYPE_FLOAT
,
187 tuple
.getTypeOfIndex(1));
189 do_check_eq(tuple
.getResultByName("nuller"), tuple
.getResultByIndex(2));
190 do_check_eq(null, tuple
.getResultByName("nuller"));
191 do_check_eq(Ci
.mozIStorageValueArray
.VALUE_TYPE_NULL
,
192 tuple
.getTypeOfIndex(2));
194 var blobByName
= tuple
.getResultByName("blober");
195 do_check_eq(BLOB
.length
, blobByName
.length
);
196 var blobByIndex
= tuple
.getResultByIndex(3);
197 do_check_eq(BLOB
.length
, blobByIndex
.length
);
198 for (var i
= 0; i
< BLOB
.length
; i
++) {
199 do_check_eq(BLOB
[i
], blobByName
[i
]);
200 do_check_eq(BLOB
[i
], blobByIndex
[i
]);
202 var count
= { value
: 0 };
203 var blob
= { value
: null };
204 tuple
.getBlob(3, count
, blob
);
205 do_check_eq(BLOB
.length
, count
.value
);
206 for (var i
= 0; i
< BLOB
.length
; i
++)
207 do_check_eq(BLOB
[i
], blob
.value
[i
]);
208 do_check_eq(Ci
.mozIStorageValueArray
.VALUE_TYPE_BLOB
,
209 tuple
.getTypeOfIndex(3));
211 do_check_eq(tuple
.getResultByName("id"), tuple
.getResultByIndex(4));
212 do_check_eq(INTEGER
, tuple
.getResultByName("id"));
213 do_check_eq(Ci
.mozIStorageValueArray
.VALUE_TYPE_INTEGER
,
214 tuple
.getTypeOfIndex(4));
216 // check that we have no more results
217 tuple
= aResultSet
.getNextRow();
218 do_check_eq(null, tuple
);
220 handleError: function(aError
)
222 dump("handleError("+aError
+");\n");
223 do_throw("unexpected error!");
225 handleCompletion: function(aReason
)
227 dump("handleCompletion("+aReason
+");\n");
228 do_check_eq(Ci
.mozIStorageStatementCallback
.REASON_FINISHED
, aReason
);
229 do_check_true(this.resultObtained
);
236 function test_tuple_out_of_bounds()
238 dump("test_tuple_out_of_bounds()\n");
240 var stmt
= getOpenedDatabase().createStatement(
241 "SELECT string FROM test"
246 resultObtained
: false,
247 handleResult: function(aResultSet
)
249 dump("handleResult("+aResultSet
+");\n");
250 do_check_false(this.resultObtained
);
251 this.resultObtained
= true;
253 // Check that we have a result
254 var tuple
= aResultSet
.getNextRow();
255 do_check_neq(null, tuple
);
257 // Check all out of bounds - should throw
267 for (var i
in methods
) {
269 tuple
[methods
[i
]](tuple
.numEntries
);
270 do_throw("did not throw :(");
273 do_check_eq(Cr
.NS_ERROR_ILLEGAL_VALUE
, e
.result
);
277 // getBlob requires more args...
279 var blob
= { value
: null };
280 var size
= { value
: 0 };
281 tuple
.getBlob(tuple
.numEntries
, blob
, size
);
282 do_throw("did not throw :(");
285 do_check_eq(Cr
.NS_ERROR_ILLEGAL_VALUE
, e
.result
);
288 handleError: function(aError
)
290 dump("handleError("+aError
+");\n");
291 do_throw("unexpected error!");
293 handleCompletion: function(aReason
)
295 dump("handleCompletion("+aReason
+");\n");
296 do_check_eq(Ci
.mozIStorageStatementCallback
.REASON_FINISHED
, aReason
);
297 do_check_true(this.resultObtained
);
304 function test_no_listener_works()
306 dump("test_no_listener_works()\n");
308 var stmt
= getOpenedDatabase().createStatement(
309 "DELETE FROM test WHERE id = ?"
311 stmt
.bindInt32Parameter(0, 0);
316 function test_partial_listener_works()
318 dump("test_partial_listener_works()\n");
320 var stmt
= getOpenedDatabase().createStatement(
321 "DELETE FROM test WHERE id = ?"
323 stmt
.bindInt32Parameter(0, 0);
325 handleResult: function(aResultSet
)
330 handleError: function(aError
)
335 handleCompletion: function(aReason
)
342 function test_immediate_cancellation()
344 dump("test_immediate_cancelation()\n");
346 var stmt
= getOpenedDatabase().createStatement(
347 "DELETE FROM test WHERE id = ?"
349 stmt
.bindInt32Parameter(0, 0);
350 var pendingStatement
= stmt
.executeAsync({
351 handleResult: function(aResultSet
)
353 dump("handleResult("+aResultSet
+");\n");
354 do_throw("unexpected result!");
356 handleError: function(aError
)
358 dump("handleError("+aError
+");\n");
359 do_throw("unexpected error!");
361 handleCompletion: function(aReason
)
363 dump("handleCompletion("+aReason
+");\n");
364 do_check_eq(Ci
.mozIStorageStatementCallback
.REASON_CANCELED
, aReason
);
370 // Cancel immediately
371 pendingStatement
.cancel();
376 function test_double_cancellation()
378 dump("test_double_cancelation()\n");
380 var stmt
= getOpenedDatabase().createStatement(
381 "DELETE FROM test WHERE id = ?"
383 stmt
.bindInt32Parameter(0, 0);
384 var pendingStatement
= stmt
.executeAsync({
385 handleResult: function(aResultSet
)
387 dump("handleResult("+aResultSet
+");\n");
388 do_throw("unexpected result!");
390 handleError: function(aError
)
392 dump("handleError("+aError
+");\n");
393 do_throw("unexpected error!");
395 handleCompletion: function(aReason
)
397 dump("handleCompletion("+aReason
+");\n");
398 do_check_eq(Ci
.mozIStorageStatementCallback
.REASON_CANCELED
, aReason
);
404 // Cancel immediately
405 pendingStatement
.cancel();
407 // And cancel again - expect an exception
409 pendingStatement
.cancel();
410 do_throw("function call should have thrown!");
413 do_check_eq(Cr
.NS_ERROR_UNEXPECTED
, e
.result
);
419 function test_double_execute()
421 dump("test_double_execute()\n");
423 var stmt
= getOpenedDatabase().createStatement(
428 handleResult: function(aResultSet
)
430 dump("handleResult("+aResultSet
+");\n");
432 handleError: function(aError
)
434 dump("handleError("+aError
+");\n");
435 do_throw("unexpected error!");
437 handleCompletion: function(aReason
)
439 dump("handleCompletion("+aReason
+");\n");
440 do_check_eq(Ci
.mozIStorageStatementCallback
.REASON_FINISHED
, aReason
);
445 stmt
.executeAsync(listener
);
447 stmt
.executeAsync(listener
);
455 test_tuple_out_of_bounds
,
456 test_no_listener_works
,
457 test_partial_listener_works
,
458 test_immediate_cancellation
,
459 test_double_cancellation
,
467 // This test has to run first and run to completion. When it is done, it will
468 // run the rest of the tests.