- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / indexed_db / indexed_db_database_unittest.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/browser/indexed_db/indexed_db_database.h"
6
7 #include "base/auto_reset.h"
8 #include "base/logging.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "content/browser/indexed_db/indexed_db.h"
12 #include "content/browser/indexed_db/indexed_db_backing_store.h"
13 #include "content/browser/indexed_db/indexed_db_callbacks.h"
14 #include "content/browser/indexed_db/indexed_db_connection.h"
15 #include "content/browser/indexed_db/indexed_db_cursor.h"
16 #include "content/browser/indexed_db/indexed_db_database.h"
17 #include "content/browser/indexed_db/indexed_db_factory.h"
18 #include "content/browser/indexed_db/indexed_db_fake_backing_store.h"
19 #include "content/browser/indexed_db/indexed_db_transaction.h"
20 #include "content/browser/indexed_db/mock_indexed_db_callbacks.h"
21 #include "content/browser/indexed_db/mock_indexed_db_database_callbacks.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 namespace content {
25
26 TEST(IndexedDBDatabaseTest, BackingStoreRetention) {
27   scoped_refptr<IndexedDBFakeBackingStore> backing_store =
28       new IndexedDBFakeBackingStore();
29   EXPECT_TRUE(backing_store->HasOneRef());
30
31   IndexedDBFactory* factory = 0;
32   scoped_refptr<IndexedDBDatabase> db = IndexedDBDatabase::Create(
33       ASCIIToUTF16("db"),
34       backing_store,
35       factory,
36       IndexedDBDatabase::Identifier());
37   EXPECT_FALSE(backing_store->HasOneRef());  // local and db
38   db = NULL;
39   EXPECT_TRUE(backing_store->HasOneRef());  // local
40 }
41
42 TEST(IndexedDBDatabaseTest, ConnectionLifecycle) {
43   scoped_refptr<IndexedDBFakeBackingStore> backing_store =
44       new IndexedDBFakeBackingStore();
45   EXPECT_TRUE(backing_store->HasOneRef());  // local
46
47   IndexedDBFactory* factory = 0;
48   scoped_refptr<IndexedDBDatabase> db =
49       IndexedDBDatabase::Create(ASCIIToUTF16("db"),
50                                 backing_store,
51                                 factory,
52                                 IndexedDBDatabase::Identifier());
53
54   EXPECT_FALSE(backing_store->HasOneRef());  // local and db
55
56   scoped_refptr<MockIndexedDBCallbacks> request1(new MockIndexedDBCallbacks());
57   scoped_refptr<MockIndexedDBDatabaseCallbacks> callbacks1(
58       new MockIndexedDBDatabaseCallbacks());
59   const int64 transaction_id1 = 1;
60   db->OpenConnection(request1,
61                      callbacks1,
62                      transaction_id1,
63                      IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION);
64
65   EXPECT_FALSE(backing_store->HasOneRef());  // db, connection count > 0
66
67   scoped_refptr<MockIndexedDBCallbacks> request2(new MockIndexedDBCallbacks());
68   scoped_refptr<MockIndexedDBDatabaseCallbacks> callbacks2(
69       new MockIndexedDBDatabaseCallbacks());
70   const int64 transaction_id2 = 2;
71   db->OpenConnection(request2,
72                      callbacks2,
73                      transaction_id2,
74                      IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION);
75
76   EXPECT_FALSE(backing_store->HasOneRef());  // local and connection
77
78   request1->connection()->ForceClose();
79   EXPECT_FALSE(request1->connection()->IsConnected());
80
81   EXPECT_FALSE(backing_store->HasOneRef());  // local and connection
82
83   request2->connection()->ForceClose();
84   EXPECT_FALSE(request2->connection()->IsConnected());
85
86   EXPECT_TRUE(backing_store->HasOneRef());
87   EXPECT_FALSE(db->backing_store());
88
89   db = NULL;
90 }
91
92 TEST(IndexedDBDatabaseTest, ForcedClose) {
93   scoped_refptr<IndexedDBFakeBackingStore> backing_store =
94       new IndexedDBFakeBackingStore();
95   EXPECT_TRUE(backing_store->HasOneRef());
96
97   IndexedDBFactory* factory = 0;
98   scoped_refptr<IndexedDBDatabase> database =
99       IndexedDBDatabase::Create(ASCIIToUTF16("db"),
100                                 backing_store,
101                                 factory,
102                                 IndexedDBDatabase::Identifier());
103
104   EXPECT_FALSE(backing_store->HasOneRef());  // local and db
105
106   scoped_refptr<MockIndexedDBDatabaseCallbacks> callbacks(
107       new MockIndexedDBDatabaseCallbacks());
108   scoped_refptr<MockIndexedDBCallbacks> request(new MockIndexedDBCallbacks());
109   const int64 upgrade_transaction_id = 3;
110   database->OpenConnection(request,
111                            callbacks,
112                            upgrade_transaction_id,
113                            IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION);
114   EXPECT_EQ(database, request->connection()->database());
115
116   const int64 transaction_id = 123;
117   const std::vector<int64> scope;
118   database->CreateTransaction(transaction_id,
119                               request->connection(),
120                               scope,
121                               indexed_db::TRANSACTION_READ_ONLY);
122
123   request->connection()->ForceClose();
124
125   EXPECT_TRUE(backing_store->HasOneRef());  // local
126   EXPECT_TRUE(callbacks->abort_called());
127 }
128
129 class MockDeleteCallbacks : public IndexedDBCallbacks {
130  public:
131   MockDeleteCallbacks()
132       : IndexedDBCallbacks(NULL, 0, 0),
133         blocked_called_(false),
134         success_void_called_(false) {}
135
136   virtual void OnBlocked(int64 existing_version) OVERRIDE {
137     blocked_called_ = true;
138   }
139   virtual void OnSuccess() OVERRIDE { success_void_called_ = true; }
140
141   bool blocked_called() const { return blocked_called_; }
142
143  private:
144   virtual ~MockDeleteCallbacks() { EXPECT_TRUE(success_void_called_); }
145
146   bool blocked_called_;
147   bool success_void_called_;
148 };
149
150 TEST(IndexedDBDatabaseTest, PendingDelete) {
151   scoped_refptr<IndexedDBFakeBackingStore> backing_store =
152       new IndexedDBFakeBackingStore();
153   EXPECT_TRUE(backing_store->HasOneRef());  // local
154
155   IndexedDBFactory* factory = 0;
156   scoped_refptr<IndexedDBDatabase> db =
157       IndexedDBDatabase::Create(ASCIIToUTF16("db"),
158                                 backing_store,
159                                 factory,
160                                 IndexedDBDatabase::Identifier());
161
162   EXPECT_FALSE(backing_store->HasOneRef());  // local and db
163
164   scoped_refptr<MockIndexedDBCallbacks> request1(new MockIndexedDBCallbacks());
165   scoped_refptr<MockIndexedDBDatabaseCallbacks> callbacks1(
166       new MockIndexedDBDatabaseCallbacks());
167   const int64 transaction_id1 = 1;
168   db->OpenConnection(request1,
169                      callbacks1,
170                      transaction_id1,
171                      IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION);
172
173   EXPECT_FALSE(backing_store->HasOneRef());  // local and db
174
175   scoped_refptr<MockDeleteCallbacks> request2(new MockDeleteCallbacks());
176   db->DeleteDatabase(request2);
177
178   EXPECT_TRUE(request2->blocked_called());
179   EXPECT_FALSE(backing_store->HasOneRef());  // local and db
180
181   db->Close(request1->connection(), true /* forced */);
182
183   EXPECT_FALSE(db->backing_store());
184   EXPECT_TRUE(backing_store->HasOneRef());  // local
185 }
186
187 }  // namespace content