2 * Copyright (C) 2012 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
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.
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.
27 #include "modules/indexeddb/IDBRequest.h"
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>
40 using namespace WebCore;
44 class NullEventQueue FINAL : public EventQueue {
47 virtual ~NullEventQueue() { }
48 virtual bool enqueueEvent(PassRefPtr<Event>) OVERRIDE { return true; }
49 virtual bool cancelEvent(Event*) OVERRIDE { return true; }
50 virtual void close() OVERRIDE { }
53 class NullExecutionContext FINAL : public ExecutionContext, public RefCounted<NullExecutionContext> {
55 using RefCounted<NullExecutionContext>::ref;
56 using RefCounted<NullExecutionContext>::deref;
58 virtual void refExecutionContext() OVERRIDE { ref(); }
59 virtual void derefExecutionContext() OVERRIDE { deref(); }
60 virtual EventQueue* eventQueue() const OVERRIDE { return m_queue.get(); }
62 NullExecutionContext();
64 OwnPtr<EventQueue> m_queue;
67 NullExecutionContext::NullExecutionContext()
68 : m_queue(adoptPtr(new NullEventQueue()))
72 class IDBRequestTest : public testing::Test {
75 : m_scope(V8ExecutionScope::create(v8::Isolate::GetCurrent()))
76 , m_context(adoptRef(new NullExecutionContext()))
80 ExecutionContext* executionContext()
82 return m_context.get();
86 OwnPtr<V8ExecutionScope> m_scope;
87 RefPtr<ExecutionContext> m_context;
90 TEST_F(IDBRequestTest, EventsAfterStopping)
92 IDBTransaction* transaction = 0;
93 RefPtr<IDBRequest> request = IDBRequest::create(executionContext(), IDBAny::createUndefined(), transaction);
94 EXPECT_EQ(request->readyState(), "pending");
95 executionContext()->stopActiveDOMObjects();
97 // Ensure none of the following raise assertions in stopped state:
98 request->onError(DOMError::create(AbortError, "Description goes here."));
99 request->onSuccess(Vector<String>());
100 request->onSuccess(nullptr, IDBKey::createInvalid(), IDBKey::createInvalid(), nullptr);
101 request->onSuccess(IDBKey::createInvalid());
102 request->onSuccess(PassRefPtr<SharedBuffer>(nullptr));
103 request->onSuccess(PassRefPtr<SharedBuffer>(nullptr), IDBKey::createInvalid(), IDBKeyPath());
104 request->onSuccess(0LL);
105 request->onSuccess();
106 request->onSuccess(IDBKey::createInvalid(), IDBKey::createInvalid(), nullptr);
109 TEST_F(IDBRequestTest, AbortErrorAfterAbort)
111 IDBTransaction* transaction = 0;
112 RefPtr<IDBRequest> request = IDBRequest::create(executionContext(), IDBAny::createUndefined(), transaction);
113 EXPECT_EQ(request->readyState(), "pending");
115 // Simulate the IDBTransaction having received onAbort from back end and aborting the request:
118 // Now simulate the back end having fired an abort error at the request to clear up any intermediaries.
119 // Ensure an assertion is not raised.
120 request->onError(DOMError::create(AbortError, "Description goes here."));
123 class MockWebIDBDatabase : public blink::WebIDBDatabase {
125 static PassOwnPtr<MockWebIDBDatabase> create()
127 return adoptPtr(new MockWebIDBDatabase());
129 virtual ~MockWebIDBDatabase()
131 EXPECT_TRUE(m_closeCalled);
134 virtual void close() OVERRIDE
136 m_closeCalled = true;
138 virtual void abort(long long transactionId) OVERRIDE { }
142 : m_closeCalled(false)
149 TEST_F(IDBRequestTest, ConnectionsAfterStopping)
151 const int64_t transactionId = 1234;
152 const int64_t version = 1;
153 const int64_t oldVersion = 0;
154 const IDBDatabaseMetadata metadata;
155 RefPtr<IDBDatabaseCallbacks> callbacks = IDBDatabaseCallbacks::create();
158 OwnPtr<MockWebIDBDatabase> backend = MockWebIDBDatabase::create();
159 RefPtr<IDBOpenDBRequest> request = IDBOpenDBRequest::create(executionContext(), callbacks, transactionId, version);
160 EXPECT_EQ(request->readyState(), "pending");
162 executionContext()->stopActiveDOMObjects();
163 request->onUpgradeNeeded(oldVersion, backend.release(), metadata, blink::WebIDBDataLossNone, String());
167 OwnPtr<MockWebIDBDatabase> backend = MockWebIDBDatabase::create();
168 RefPtr<IDBOpenDBRequest> request = IDBOpenDBRequest::create(executionContext(), callbacks, transactionId, version);
169 EXPECT_EQ(request->readyState(), "pending");
171 executionContext()->stopActiveDOMObjects();
172 request->onSuccess(backend.release(), metadata);