Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / indexeddb / IDBTransactionTest.cpp
1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #include "config.h"
32 #include "modules/indexeddb/IDBTransaction.h"
33
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"
41
42 #include <gtest/gtest.h>
43
44 using namespace WebCore;
45
46 using blink::WebIDBDatabase;
47
48 namespace {
49
50 class IDBTransactionTest : public testing::Test {
51 public:
52     IDBTransactionTest()
53         : m_scope(V8ExecutionScope::create(v8::Isolate::GetCurrent()))
54         , m_document(Document::create())
55     {
56     }
57
58     ExecutionContext* executionContext()
59     {
60         return m_document.get();
61     }
62
63 private:
64     OwnPtr<V8ExecutionScope> m_scope;
65     RefPtr<Document> m_document;
66 };
67
68 class FakeWebIDBDatabase FINAL : public blink::WebIDBDatabase {
69 public:
70     static PassOwnPtr<FakeWebIDBDatabase> create() { return adoptPtr(new FakeWebIDBDatabase()); }
71
72     virtual void commit(long long transactionId) OVERRIDE { }
73     virtual void abort(long long transactionId) OVERRIDE { }
74     virtual void close() OVERRIDE { }
75
76 private:
77     FakeWebIDBDatabase() { }
78 };
79
80 class FakeIDBDatabaseCallbacks FINAL : public IDBDatabaseCallbacks {
81 public:
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 { }
87 private:
88     FakeIDBDatabaseCallbacks() { }
89 };
90
91 TEST_F(IDBTransactionTest, EnsureLifetime)
92 {
93     OwnPtr<FakeWebIDBDatabase> backend = FakeWebIDBDatabase::create();
94     RefPtrWillBePersistent<IDBDatabase> db = IDBDatabase::create(executionContext(), backend.release(), FakeIDBDatabaseCallbacks::create());
95
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());
99 #if ENABLE(OILPAN)
100     PersistentHeapHashSet<WeakMember<IDBTransaction> > set;
101     set.add(transaction);
102
103     Heap::collectAllGarbage();
104     EXPECT_EQ(1u, set.size());
105 #else
106     // Local reference, IDBDatabase's reference and IDBPendingTransactionMonitor's reference:
107     EXPECT_EQ(3, transaction->refCount());
108 #endif
109
110     RefPtrWillBePersistent<IDBRequest> request = IDBRequest::create(executionContext(), IDBAny::createUndefined(), transaction.get());
111     IDBPendingTransactionMonitor::from(*executionContext()).deactivateNewTransactions();
112
113 #if ENABLE(OILPAN)
114     Heap::collectAllGarbage();
115     EXPECT_EQ(1u, set.size());
116 #else
117     // Local reference and  IDBDatabase's reference., and the IDBRequest's reference
118     EXPECT_EQ(3, transaction->refCount());
119 #endif
120
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"));
125
126 #if ENABLE(OILPAN)
127     Heap::collectAllGarbage();
128     EXPECT_EQ(0u, set.size());
129 #else
130     EXPECT_EQ(1, transaction->refCount());
131 #endif
132     request.clear();
133 }
134
135 TEST_F(IDBTransactionTest, TransactionFinish)
136 {
137     OwnPtr<FakeWebIDBDatabase> backend = FakeWebIDBDatabase::create();
138     RefPtrWillBePersistent<IDBDatabase> db = IDBDatabase::create(executionContext(), backend.release(), FakeIDBDatabaseCallbacks::create());
139
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());
143 #if ENABLE(OILPAN)
144     PersistentHeapHashSet<WeakMember<IDBTransaction> > set;
145     set.add(transaction);
146
147     Heap::collectAllGarbage();
148     EXPECT_EQ(1u, set.size());
149 #else
150     // Local reference, IDBDatabase's reference and IDBPendingTransactionMonitor's reference:
151     EXPECT_EQ(3, transaction->refCount());
152 #endif
153
154     IDBPendingTransactionMonitor::from(*executionContext()).deactivateNewTransactions();
155
156 #if ENABLE(OILPAN)
157     Heap::collectAllGarbage();
158     EXPECT_EQ(1u, set.size());
159 #else
160     // Local reference, IDBDatabase's reference
161     EXPECT_EQ(2, transaction->refCount());
162 #endif
163
164 #if !ENABLE(OILPAN)
165     IDBTransaction* transactionPtr = transaction.get();
166 #endif
167     transaction.clear();
168
169 #if ENABLE(OILPAN)
170     Heap::collectAllGarbage();
171     EXPECT_EQ(1u, set.size());
172 #else
173     // IDBDatabase's reference
174     EXPECT_EQ(1, transactionPtr->refCount());
175 #endif
176
177     // Stop the context, so events don't get queued (which would keep the transaction alive).
178     executionContext()->stopActiveDOMObjects();
179
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"));
183
184     // onAbort() should have cleared the transaction's reference to the database.
185 #if ENABLE(OILPAN)
186     Heap::collectAllGarbage();
187     EXPECT_EQ(0u, set.size());
188 #else
189     EXPECT_EQ(1, db->refCount());
190 #endif
191 }
192
193 } // namespace