1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "storage_test_harness.h"
8
9 #include "mozStorageHelper.h"
10
11 /**
12 * This file test our statement scoper in mozStorageHelper.h.
13 */
14
TEST(storage_statement_scoper,automatic_reset)15 TEST(storage_statement_scoper, automatic_reset)
16 {
17 nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
18
19 // Need to create a table to populate sqlite_master with an entry.
20 (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
21 "CREATE TABLE test (id INTEGER PRIMARY KEY)"
22 ));
23
24 nsCOMPtr<mozIStorageStatement> stmt;
25 (void)db->CreateStatement(NS_LITERAL_CSTRING(
26 "SELECT * FROM sqlite_master"
27 ), getter_AddRefs(stmt));
28
29 // Reality check - make sure we start off in the right state.
30 int32_t state = -1;
31 (void)stmt->GetState(&state);
32 do_check_true(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY);
33
34 // Start executing the statement, which will put it into an executing state.
35 {
36 mozStorageStatementScoper scoper(stmt);
37 bool hasMore;
38 do_check_true(NS_SUCCEEDED(stmt->ExecuteStep(&hasMore)));
39
40 // Reality check that we are executing.
41 state = -1;
42 (void)stmt->GetState(&state);
43 do_check_true(state ==
44 mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING);
45 }
46
47 // And we should be ready again.
48 state = -1;
49 (void)stmt->GetState(&state);
50 do_check_true(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY);
51 }
52
TEST(storage_statement_scoper,Abandon)53 TEST(storage_statement_scoper, Abandon)
54 {
55 nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
56
57 // Need to create a table to populate sqlite_master with an entry.
58 (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
59 "CREATE TABLE test (id INTEGER PRIMARY KEY)"
60 ));
61
62 nsCOMPtr<mozIStorageStatement> stmt;
63 (void)db->CreateStatement(NS_LITERAL_CSTRING(
64 "SELECT * FROM sqlite_master"
65 ), getter_AddRefs(stmt));
66
67 // Reality check - make sure we start off in the right state.
68 int32_t state = -1;
69 (void)stmt->GetState(&state);
70 do_check_true(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY);
71
72 // Start executing the statement, which will put it into an executing state.
73 {
74 mozStorageStatementScoper scoper(stmt);
75 bool hasMore;
76 do_check_true(NS_SUCCEEDED(stmt->ExecuteStep(&hasMore)));
77
78 // Reality check that we are executing.
79 state = -1;
80 (void)stmt->GetState(&state);
81 do_check_true(state ==
82 mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING);
83
84 // And call Abandon. We should not reset now when we fall out of scope.
85 scoper.Abandon();
86 }
87
88 // And we should still be executing.
89 state = -1;
90 (void)stmt->GetState(&state);
91 do_check_true(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING);
92 }
93