1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
4 #include "storage_test_harness.h"
6 #include "nsIEventTarget.h"
7 #include "nsIInterfaceRequestorUtils.h"
8 #include "mozilla/Attributes.h"
12 ////////////////////////////////////////////////////////////////////////////////
16 * Spins the events loop for current thread until aCondition is true.
19 spin_events_loop_until_true(const bool* const aCondition
)
21 nsCOMPtr
<nsIThread
> thread(::do_GetCurrentThread());
23 bool processed
= true;
24 while (!(*aCondition
) && NS_SUCCEEDED(rv
)) {
25 rv
= thread
->ProcessNextEvent(true, &processed
);
29 ////////////////////////////////////////////////////////////////////////////////
30 //// mozIStorageStatementCallback implementation
32 class UnownedCallback MOZ_FINAL
: public mozIStorageStatementCallback
37 // Whether the object has been destroyed.
39 // Whether the first result was received.
41 // Whether an error was received.
44 explicit UnownedCallback(mozIStorageConnection
* aDBConn
)
57 blocking_async_close(mDBConn
);
61 NS_IMETHOD
HandleResult(mozIStorageResultSet
* aResultSet
)
64 spin_events_loop_until_true(&mCompleted
);
66 NS_RUNTIMEABORT("The statement callback was destroyed prematurely.");
71 NS_IMETHOD
HandleError(mozIStorageError
* aError
)
74 spin_events_loop_until_true(&mCompleted
);
76 NS_RUNTIMEABORT("The statement callback was destroyed prematurely.");
81 NS_IMETHOD
HandleCompletion(uint16_t aReason
)
88 nsCOMPtr
<mozIStorageConnection
> mDBConn
;
92 NS_IMPL_ISUPPORTS(UnownedCallback
, mozIStorageStatementCallback
)
94 bool UnownedCallback::sAlive
= false;
95 bool UnownedCallback::sResult
= false;
96 bool UnownedCallback::sError
= false;
98 ////////////////////////////////////////////////////////////////////////////////
102 test_SpinEventsLoopInHandleResult()
104 nsCOMPtr
<mozIStorageConnection
> db(getMemoryDatabase());
106 // Create a test table and populate it.
107 nsCOMPtr
<mozIStorageStatement
> stmt
;
108 db
->CreateStatement(NS_LITERAL_CSTRING(
109 "CREATE TABLE test (id INTEGER PRIMARY KEY)"
110 ), getter_AddRefs(stmt
));
114 db
->CreateStatement(NS_LITERAL_CSTRING(
115 "INSERT INTO test (id) VALUES (?)"
116 ), getter_AddRefs(stmt
));
117 for (int32_t i
= 0; i
< 30; ++i
) {
118 stmt
->BindInt32ByIndex(0, i
);
124 db
->CreateStatement(NS_LITERAL_CSTRING(
126 ), getter_AddRefs(stmt
));
127 nsCOMPtr
<mozIStoragePendingStatement
> ps
;
128 do_check_success(stmt
->ExecuteAsync(new UnownedCallback(db
),
129 getter_AddRefs(ps
)));
132 spin_events_loop_until_true(&UnownedCallback::sResult
);
136 test_SpinEventsLoopInHandleError()
138 nsCOMPtr
<mozIStorageConnection
> db(getMemoryDatabase());
140 // Create a test table and populate it.
141 nsCOMPtr
<mozIStorageStatement
> stmt
;
142 db
->CreateStatement(NS_LITERAL_CSTRING(
143 "CREATE TABLE test (id INTEGER PRIMARY KEY)"
144 ), getter_AddRefs(stmt
));
148 db
->CreateStatement(NS_LITERAL_CSTRING(
149 "INSERT INTO test (id) VALUES (1)"
150 ), getter_AddRefs(stmt
));
154 // This will cause a constraint error.
155 db
->CreateStatement(NS_LITERAL_CSTRING(
156 "INSERT INTO test (id) VALUES (1)"
157 ), getter_AddRefs(stmt
));
158 nsCOMPtr
<mozIStoragePendingStatement
> ps
;
159 do_check_success(stmt
->ExecuteAsync(new UnownedCallback(db
),
160 getter_AddRefs(ps
)));
163 spin_events_loop_until_true(&UnownedCallback::sError
);
166 void (*gTests
[])(void) = {
167 test_SpinEventsLoopInHandleResult
,
168 test_SpinEventsLoopInHandleError
,
171 const char *file
= __FILE__
;
172 #define TEST_NAME "test async callbacks with spun event loops"
173 #define TEST_FILE file
174 #include "storage_test_harness_tail.h"