c9316ca271f2e069c0a15461727f4c5e5bdf47c3
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / indexeddb / IDBRequestTest.cpp
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 FINAL : public EventQueue {
45 public:
46     NullEventQueue() { }
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 { }
51 };
52
53 class NullExecutionContext FINAL : public ExecutionContext, public RefCounted<NullExecutionContext> {
54 public:
55     using RefCounted<NullExecutionContext>::ref;
56     using RefCounted<NullExecutionContext>::deref;
57
58     virtual void refExecutionContext() OVERRIDE { ref(); }
59     virtual void derefExecutionContext() OVERRIDE { deref(); }
60     virtual EventQueue* eventQueue() const OVERRIDE { return m_queue.get(); }
61
62     NullExecutionContext();
63 private:
64     OwnPtr<EventQueue> m_queue;
65 };
66
67 NullExecutionContext::NullExecutionContext()
68     : m_queue(adoptPtr(new NullEventQueue()))
69 {
70 }
71
72 class IDBRequestTest : public testing::Test {
73 public:
74     IDBRequestTest()
75         : m_scope(V8ExecutionScope::create(v8::Isolate::GetCurrent()))
76         , m_context(adoptRef(new NullExecutionContext()))
77     {
78     }
79
80     ExecutionContext* executionContext()
81     {
82         return m_context.get();
83     }
84
85 private:
86     OwnPtr<V8ExecutionScope> m_scope;
87     RefPtr<ExecutionContext> m_context;
88 };
89
90 TEST_F(IDBRequestTest, EventsAfterStopping)
91 {
92     IDBTransaction* transaction = 0;
93     RefPtr<IDBRequest> request = IDBRequest::create(executionContext(), IDBAny::createUndefined(), transaction);
94     EXPECT_EQ(request->readyState(), "pending");
95     executionContext()->stopActiveDOMObjects();
96
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);
107 }
108
109 TEST_F(IDBRequestTest, AbortErrorAfterAbort)
110 {
111     IDBTransaction* transaction = 0;
112     RefPtr<IDBRequest> request = IDBRequest::create(executionContext(), IDBAny::createUndefined(), transaction);
113     EXPECT_EQ(request->readyState(), "pending");
114
115     // Simulate the IDBTransaction having received onAbort from back end and aborting the request:
116     request->abort();
117
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."));
121 }
122
123 class MockWebIDBDatabase : public blink::WebIDBDatabase {
124 public:
125     static PassOwnPtr<MockWebIDBDatabase> create()
126     {
127         return adoptPtr(new MockWebIDBDatabase());
128     }
129     virtual ~MockWebIDBDatabase()
130     {
131         EXPECT_TRUE(m_closeCalled);
132     }
133
134     virtual void close() OVERRIDE
135     {
136         m_closeCalled = true;
137     }
138     virtual void abort(long long transactionId) OVERRIDE { }
139
140 private:
141     MockWebIDBDatabase()
142         : m_closeCalled(false)
143     {
144     }
145
146     bool m_closeCalled;
147 };
148
149 TEST_F(IDBRequestTest, ConnectionsAfterStopping)
150 {
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();
156
157     {
158         OwnPtr<MockWebIDBDatabase> backend = MockWebIDBDatabase::create();
159         RefPtr<IDBOpenDBRequest> request = IDBOpenDBRequest::create(executionContext(), callbacks, transactionId, version);
160         EXPECT_EQ(request->readyState(), "pending");
161
162         executionContext()->stopActiveDOMObjects();
163         request->onUpgradeNeeded(oldVersion, backend.release(), metadata, blink::WebIDBDataLossNone, String());
164     }
165
166     {
167         OwnPtr<MockWebIDBDatabase> backend = MockWebIDBDatabase::create();
168         RefPtr<IDBOpenDBRequest> request = IDBOpenDBRequest::create(executionContext(), callbacks, transactionId, version);
169         EXPECT_EQ(request->readyState(), "pending");
170
171         executionContext()->stopActiveDOMObjects();
172         request->onSuccess(backend.release(), metadata);
173     }
174 }
175
176 } // namespace