1 /*
2 * Copyright (C) 2012 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
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "modules/indexeddb/IDBRequest.h"
28
29 #include "core/dom/DOMError.h"
30 #include "core/dom/Document.h"
31 #include "core/events/EventQueue.h"
32 #include "modules/indexeddb/IDBDatabaseCallbacks.h"
33 #include "modules/indexeddb/IDBKeyRange.h"
34 #include "modules/indexeddb/IDBOpenDBRequest.h"
35 #include "platform/SharedBuffer.h"
36 #include "public/platform/WebIDBDatabase.h"
37 #include "wtf/PassOwnPtr.h"
38 #include <gtest/gtest.h>
39
40 using namespace WebCore;
41
42 namespace {
43
44 class NullEventQueue : public EventQueue {
45 public:
NullEventQueue()46 NullEventQueue() { }
~NullEventQueue()47 virtual ~NullEventQueue() { }
enqueueEvent(PassRefPtr<Event>)48 virtual bool enqueueEvent(PassRefPtr<Event>) OVERRIDE { return true; }
cancelEvent(Event *)49 virtual bool cancelEvent(Event*) OVERRIDE { return true; }
close()50 virtual void close() OVERRIDE { }
51 };
52
53 class NullExecutionContext : public ExecutionContext, public RefCounted<NullExecutionContext> {
54 public:
55 using RefCounted<NullExecutionContext>::ref;
56 using RefCounted<NullExecutionContext>::deref;
57
refExecutionContext()58 virtual void refExecutionContext() OVERRIDE { ref(); }
derefExecutionContext()59 virtual void derefExecutionContext() OVERRIDE { deref(); }
eventQueue() const60 virtual EventQueue* eventQueue() const OVERRIDE { return m_queue.get(); }
61
62 NullExecutionContext();
63 private:
64 OwnPtr<EventQueue> m_queue;
65 };
66
NullExecutionContext()67 NullExecutionContext::NullExecutionContext()
68 : m_queue(adoptPtr(new NullEventQueue()))
69 {
70 }
71
72 class IDBRequestTest : public testing::Test {
73 public:
IDBRequestTest()74 IDBRequestTest()
75 : m_handleScope(v8::Isolate::GetCurrent())
76 , m_scope(v8::Context::New(v8::Isolate::GetCurrent()))
77 , m_context(adoptRef(new NullExecutionContext()))
78 {
79 }
80
executionContext()81 ExecutionContext* executionContext()
82 {
83 return m_context.get();
84 }
85
86 private:
87 v8::HandleScope m_handleScope;
88 v8::Context::Scope m_scope;
89 RefPtr<ExecutionContext> m_context;
90 };
91
TEST_F(IDBRequestTest,EventsAfterStopping)92 TEST_F(IDBRequestTest, EventsAfterStopping)
93 {
94 IDBTransaction* transaction = 0;
95 RefPtr<IDBRequest> request = IDBRequest::create(executionContext(), IDBAny::createUndefined(), transaction);
96 EXPECT_EQ(request->readyState(), "pending");
97 executionContext()->stopActiveDOMObjects();
98
99 // Ensure none of the following raise assertions in stopped state:
100 request->onError(DOMError::create(AbortError, "Description goes here."));
101 request->onSuccess(Vector<String>());
102 request->onSuccess(nullptr, IDBKey::createInvalid(), IDBKey::createInvalid(), 0);
103 request->onSuccess(IDBKey::createInvalid());
104 request->onSuccess(PassRefPtr<SharedBuffer>(0));
105 request->onSuccess(PassRefPtr<SharedBuffer>(0), IDBKey::createInvalid(), IDBKeyPath());
106 request->onSuccess(0LL);
107 request->onSuccess();
108 request->onSuccess(IDBKey::createInvalid(), IDBKey::createInvalid(), 0);
109 }
110
TEST_F(IDBRequestTest,AbortErrorAfterAbort)111 TEST_F(IDBRequestTest, AbortErrorAfterAbort)
112 {
113 IDBTransaction* transaction = 0;
114 RefPtr<IDBRequest> request = IDBRequest::create(executionContext(), IDBAny::createUndefined(), transaction);
115 EXPECT_EQ(request->readyState(), "pending");
116
117 // Simulate the IDBTransaction having received onAbort from back end and aborting the request:
118 request->abort();
119
120 // Now simulate the back end having fired an abort error at the request to clear up any intermediaries.
121 // Ensure an assertion is not raised.
122 request->onError(DOMError::create(AbortError, "Description goes here."));
123 }
124
125 class MockWebIDBDatabase : public blink::WebIDBDatabase {
126 public:
create()127 static PassOwnPtr<MockWebIDBDatabase> create()
128 {
129 return adoptPtr(new MockWebIDBDatabase());
130 }
~MockWebIDBDatabase()131 virtual ~MockWebIDBDatabase()
132 {
133 EXPECT_TRUE(m_closeCalled);
134 }
135
close()136 virtual void close() OVERRIDE
137 {
138 m_closeCalled = true;
139 }
abort(long long transactionId)140 virtual void abort(long long transactionId) OVERRIDE { }
141
142 private:
MockWebIDBDatabase()143 MockWebIDBDatabase()
144 : m_closeCalled(false)
145 {
146 }
147
148 bool m_closeCalled;
149 };
150
TEST_F(IDBRequestTest,ConnectionsAfterStopping)151 TEST_F(IDBRequestTest, ConnectionsAfterStopping)
152 {
153 const int64_t transactionId = 1234;
154 const int64_t version = 1;
155 const int64_t oldVersion = 0;
156 const IDBDatabaseMetadata metadata;
157 RefPtr<IDBDatabaseCallbacks> callbacks = IDBDatabaseCallbacks::create();
158
159 {
160 OwnPtr<MockWebIDBDatabase> backend = MockWebIDBDatabase::create();
161 RefPtr<IDBOpenDBRequest> request = IDBOpenDBRequest::create(executionContext(), callbacks, transactionId, version);
162 EXPECT_EQ(request->readyState(), "pending");
163
164 executionContext()->stopActiveDOMObjects();
165 request->onUpgradeNeeded(oldVersion, backend.release(), metadata, blink::WebIDBDataLossNone, String());
166 }
167
168 {
169 OwnPtr<MockWebIDBDatabase> backend = MockWebIDBDatabase::create();
170 RefPtr<IDBOpenDBRequest> request = IDBOpenDBRequest::create(executionContext(), callbacks, transactionId, version);
171 EXPECT_EQ(request->readyState(), "pending");
172
173 executionContext()->stopActiveDOMObjects();
174 request->onSuccess(backend.release(), metadata);
175 }
176 }
177
178 } // namespace
179