• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "bindings/v8/ScriptState.h"
30 #include "bindings/v8/V8Binding.h"
31 #include "core/dom/DOMError.h"
32 #include "core/dom/ExecutionContext.h"
33 #include "core/testing/NullExecutionContext.h"
34 #include "modules/indexeddb/IDBDatabaseCallbacks.h"
35 #include "modules/indexeddb/IDBKey.h"
36 #include "modules/indexeddb/IDBKeyRange.h"
37 #include "modules/indexeddb/IDBOpenDBRequest.h"
38 #include "platform/SharedBuffer.h"
39 #include "public/platform/WebBlobInfo.h"
40 #include "public/platform/WebIDBDatabase.h"
41 #include "wtf/OwnPtr.h"
42 #include "wtf/PassOwnPtr.h"
43 #include "wtf/PassRefPtr.h"
44 #include "wtf/Vector.h"
45 #include "wtf/dtoa/utils.h"
46 #include <gtest/gtest.h>
47 #include <v8.h>
48 
49 using blink::WebBlobInfo;
50 using namespace WebCore;
51 
52 namespace {
53 
54 class IDBRequestTest : public testing::Test {
55 public:
IDBRequestTest()56     IDBRequestTest()
57         : m_scope(v8::Isolate::GetCurrent())
58         , m_executionContext(adoptRefWillBeNoop(new NullExecutionContext()))
59     {
60         m_scope.scriptState()->setExecutionContext(m_executionContext.get());
61     }
62 
~IDBRequestTest()63     ~IDBRequestTest()
64     {
65         m_scope.scriptState()->setExecutionContext(0);
66     }
67 
isolate() const68     v8::Isolate* isolate() const { return m_scope.isolate(); }
scriptState() const69     ScriptState* scriptState() const { return m_scope.scriptState(); }
executionContext() const70     ExecutionContext* executionContext() const { return m_scope.scriptState()->executionContext(); }
71 
72 private:
73     V8TestingScope m_scope;
74     RefPtrWillBePersistent<ExecutionContext> m_executionContext;
75 };
76 
TEST_F(IDBRequestTest,EventsAfterStopping)77 TEST_F(IDBRequestTest, EventsAfterStopping)
78 {
79     IDBTransaction* transaction = 0;
80     IDBRequest* request = IDBRequest::create(scriptState(), IDBAny::createUndefined(), transaction);
81     EXPECT_EQ(request->readyState(), "pending");
82     executionContext()->stopActiveDOMObjects();
83 
84     // Ensure none of the following raise assertions in stopped state:
85     request->onError(DOMError::create(AbortError, "Description goes here."));
86     request->onSuccess(Vector<String>());
87     request->onSuccess(nullptr, IDBKey::createInvalid(), IDBKey::createInvalid(), nullptr, adoptPtr(new Vector<WebBlobInfo>()));
88     request->onSuccess(IDBKey::createInvalid());
89     request->onSuccess(PassRefPtr<SharedBuffer>(nullptr), adoptPtr(new Vector<WebBlobInfo>()));
90     request->onSuccess(PassRefPtr<SharedBuffer>(nullptr), adoptPtr(new Vector<WebBlobInfo>()), IDBKey::createInvalid(), IDBKeyPath());
91     request->onSuccess(static_cast<int64_t>(0));
92     request->onSuccess();
93     request->onSuccess(IDBKey::createInvalid(), IDBKey::createInvalid(), nullptr, adoptPtr(new Vector<WebBlobInfo>()));
94 }
95 
TEST_F(IDBRequestTest,AbortErrorAfterAbort)96 TEST_F(IDBRequestTest, AbortErrorAfterAbort)
97 {
98     IDBTransaction* transaction = 0;
99     IDBRequest* request = IDBRequest::create(scriptState(), IDBAny::createUndefined(), transaction);
100     EXPECT_EQ(request->readyState(), "pending");
101 
102     // Simulate the IDBTransaction having received onAbort from back end and aborting the request:
103     request->abort();
104 
105     // Now simulate the back end having fired an abort error at the request to clear up any intermediaries.
106     // Ensure an assertion is not raised.
107     request->onError(DOMError::create(AbortError, "Description goes here."));
108 }
109 
110 class MockWebIDBDatabase : public blink::WebIDBDatabase {
111 public:
create()112     static PassOwnPtr<MockWebIDBDatabase> create()
113     {
114         return adoptPtr(new MockWebIDBDatabase());
115     }
~MockWebIDBDatabase()116     virtual ~MockWebIDBDatabase()
117     {
118         EXPECT_TRUE(m_closeCalled);
119     }
120 
close()121     virtual void close() OVERRIDE
122     {
123         m_closeCalled = true;
124     }
abort(long long transactionId)125     virtual void abort(long long transactionId) OVERRIDE { }
126 
127 private:
MockWebIDBDatabase()128     MockWebIDBDatabase()
129         : m_closeCalled(false)
130     {
131     }
132 
133     bool m_closeCalled;
134 };
135 
TEST_F(IDBRequestTest,ConnectionsAfterStopping)136 TEST_F(IDBRequestTest, ConnectionsAfterStopping)
137 {
138     const int64_t transactionId = 1234;
139     const int64_t version = 1;
140     const int64_t oldVersion = 0;
141     const IDBDatabaseMetadata metadata;
142     Persistent<IDBDatabaseCallbacks> callbacks = IDBDatabaseCallbacks::create();
143 
144     {
145         OwnPtr<MockWebIDBDatabase> backend = MockWebIDBDatabase::create();
146         IDBOpenDBRequest* request = IDBOpenDBRequest::create(scriptState(), callbacks, transactionId, version);
147         EXPECT_EQ(request->readyState(), "pending");
148 
149         executionContext()->stopActiveDOMObjects();
150         request->onUpgradeNeeded(oldVersion, backend.release(), metadata, blink::WebIDBDataLossNone, String());
151     }
152 
153     {
154         OwnPtr<MockWebIDBDatabase> backend = MockWebIDBDatabase::create();
155         IDBOpenDBRequest* request = IDBOpenDBRequest::create(scriptState(), callbacks, transactionId, version);
156         EXPECT_EQ(request->readyState(), "pending");
157 
158         executionContext()->stopActiveDOMObjects();
159         request->onSuccess(backend.release(), metadata);
160     }
161 }
162 
163 } // namespace
164