2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "modules/indexeddb/IDBTransaction.h"
34 #include "core/dom/DOMError.h"
35 #include "core/dom/Document.h"
36 #include "modules/indexeddb/IDBDatabase.h"
37 #include "modules/indexeddb/IDBDatabaseCallbacks.h"
38 #include "modules/indexeddb/IDBPendingTransactionMonitor.h"
39 #include "platform/SharedBuffer.h"
40 #include "public/platform/WebIDBDatabase.h"
42 #include <gtest/gtest.h>
44 using namespace WebCore;
46 using blink::WebIDBDatabase;
50 class IDBTransactionTest : public testing::Test {
53 : m_scope(V8ExecutionScope::create(v8::Isolate::GetCurrent()))
54 , m_document(Document::create())
58 ExecutionContext* executionContext()
60 return m_document.get();
64 OwnPtr<V8ExecutionScope> m_scope;
65 RefPtr<Document> m_document;
68 class FakeWebIDBDatabase FINAL : public blink::WebIDBDatabase {
70 static PassOwnPtr<FakeWebIDBDatabase> create() { return adoptPtr(new FakeWebIDBDatabase()); }
72 virtual void commit(long long transactionId) OVERRIDE { }
73 virtual void abort(long long transactionId) OVERRIDE { }
74 virtual void close() OVERRIDE { }
77 FakeWebIDBDatabase() { }
80 class FakeIDBDatabaseCallbacks FINAL : public IDBDatabaseCallbacks {
82 static PassRefPtrWillBeRawPtr<FakeIDBDatabaseCallbacks> create() { return adoptRefWillBeNoop(new FakeIDBDatabaseCallbacks()); }
83 virtual void onVersionChange(int64_t oldVersion, int64_t newVersion) OVERRIDE { }
84 virtual void onForcedClose() OVERRIDE { }
85 virtual void onAbort(int64_t transactionId, PassRefPtrWillBeRawPtr<DOMError> error) OVERRIDE { }
86 virtual void onComplete(int64_t transactionId) OVERRIDE { }
88 FakeIDBDatabaseCallbacks() { }
91 TEST_F(IDBTransactionTest, EnsureLifetime)
93 OwnPtr<FakeWebIDBDatabase> backend = FakeWebIDBDatabase::create();
94 RefPtrWillBePersistent<IDBDatabase> db = IDBDatabase::create(executionContext(), backend.release(), FakeIDBDatabaseCallbacks::create());
96 const int64_t transactionId = 1234;
97 const Vector<String> transactionScope;
98 RefPtrWillBeRawPtr<IDBTransaction> transaction = IDBTransaction::create(executionContext(), transactionId, transactionScope, blink::WebIDBDatabase::TransactionReadOnly, db.get());
100 PersistentHeapHashSet<WeakMember<IDBTransaction> > set;
101 set.add(transaction);
103 Heap::collectAllGarbage();
104 EXPECT_EQ(1u, set.size());
106 // Local reference, IDBDatabase's reference and IDBPendingTransactionMonitor's reference:
107 EXPECT_EQ(3, transaction->refCount());
110 RefPtrWillBePersistent<IDBRequest> request = IDBRequest::create(executionContext(), IDBAny::createUndefined(), transaction.get());
111 IDBPendingTransactionMonitor::from(*executionContext()).deactivateNewTransactions();
114 Heap::collectAllGarbage();
115 EXPECT_EQ(1u, set.size());
117 // Local reference and IDBDatabase's reference., and the IDBRequest's reference
118 EXPECT_EQ(3, transaction->refCount());
121 // This will generate an abort() call to the back end which is dropped by the fake proxy,
122 // so an explicit onAbort call is made.
123 executionContext()->stopActiveDOMObjects();
124 transaction->onAbort(DOMError::create(AbortError, "Aborted"));
127 Heap::collectAllGarbage();
128 EXPECT_EQ(0u, set.size());
130 EXPECT_EQ(1, transaction->refCount());
135 TEST_F(IDBTransactionTest, TransactionFinish)
137 OwnPtr<FakeWebIDBDatabase> backend = FakeWebIDBDatabase::create();
138 RefPtrWillBePersistent<IDBDatabase> db = IDBDatabase::create(executionContext(), backend.release(), FakeIDBDatabaseCallbacks::create());
140 const int64_t transactionId = 1234;
141 const Vector<String> transactionScope;
142 RefPtrWillBeRawPtr<IDBTransaction> transaction = IDBTransaction::create(executionContext(), transactionId, transactionScope, blink::WebIDBDatabase::TransactionReadOnly, db.get());
144 PersistentHeapHashSet<WeakMember<IDBTransaction> > set;
145 set.add(transaction);
147 Heap::collectAllGarbage();
148 EXPECT_EQ(1u, set.size());
150 // Local reference, IDBDatabase's reference and IDBPendingTransactionMonitor's reference:
151 EXPECT_EQ(3, transaction->refCount());
154 IDBPendingTransactionMonitor::from(*executionContext()).deactivateNewTransactions();
157 Heap::collectAllGarbage();
158 EXPECT_EQ(1u, set.size());
160 // Local reference, IDBDatabase's reference
161 EXPECT_EQ(2, transaction->refCount());
165 IDBTransaction* transactionPtr = transaction.get();
170 Heap::collectAllGarbage();
171 EXPECT_EQ(1u, set.size());
173 // IDBDatabase's reference
174 EXPECT_EQ(1, transactionPtr->refCount());
177 // Stop the context, so events don't get queued (which would keep the transaction alive).
178 executionContext()->stopActiveDOMObjects();
180 // Fire an abort to make sure this doesn't free the transaction during use. The test
181 // will not fail if it is, but ASAN would notice the error.
182 db->onAbort(transactionId, DOMError::create(AbortError, "Aborted"));
184 // onAbort() should have cleared the transaction's reference to the database.
186 Heap::collectAllGarbage();
187 EXPECT_EQ(0u, set.size());
189 EXPECT_EQ(1, db->refCount());