summaryrefslogtreecommitdiffstats
path: root/content/browser/indexed_db/indexed_db_database_unittest.cc
blob: 93af23bbd85a14797df47b09a71b29ae4c41d32b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/indexed_db/indexed_db_database.h"

#include <gtest/gtest.h>

#include "base/auto_reset.h"
#include "base/logging.h"
#include "base/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "content/browser/indexed_db/indexed_db.h"
#include "content/browser/indexed_db/indexed_db_backing_store.h"
#include "content/browser/indexed_db/indexed_db_callbacks_wrapper.h"
#include "content/browser/indexed_db/indexed_db_cursor.h"
#include "content/browser/indexed_db/indexed_db_database_impl.h"
#include "content/browser/indexed_db/indexed_db_factory_impl.h"
#include "content/browser/indexed_db/indexed_db_fake_backing_store.h"
#include "content/browser/indexed_db/indexed_db_transaction.h"
#include "content/browser/indexed_db/webidbdatabase_impl.h"

using WebKit::WebIDBDatabase;
using WebKit::WebIDBDatabaseError;
using WebKit::WebIDBDatabaseCallbacks;

namespace content {

TEST(IndexedDBDatabaseTest, BackingStoreRetention) {
  scoped_refptr<IndexedDBFakeBackingStore> backing_store =
      new IndexedDBFakeBackingStore();
  EXPECT_TRUE(backing_store->HasOneRef());

  IndexedDBFactoryImpl* factory = 0;
  scoped_refptr<IndexedDBDatabaseImpl> db =
      IndexedDBDatabaseImpl::Create(ASCIIToUTF16("db"),
                                    backing_store.get(),
                                    factory,
                                    ASCIIToUTF16("uniqueid"));
  EXPECT_FALSE(backing_store->HasOneRef());  // local and db
  db = NULL;
  EXPECT_TRUE(backing_store->HasOneRef());  // local
}

class MockIDBCallbacks : public IndexedDBCallbacksWrapper {
 public:
  static scoped_refptr<MockIDBCallbacks> Create() {
    return make_scoped_refptr(new MockIDBCallbacks());
  }
  virtual void OnError(const IndexedDBDatabaseError& error) OVERRIDE {}
  virtual void OnSuccess(const std::vector<string16>& value) OVERRIDE {}
  virtual void OnSuccess(scoped_refptr<IndexedDBCursor> cursor,
                         const IndexedDBKey& key,
                         const IndexedDBKey& primary_key,
                         std::vector<char>* value) OVERRIDE {}
  virtual void OnSuccess(scoped_refptr<IndexedDBDatabase> db,
                         const IndexedDBDatabaseMetadata& metadata) OVERRIDE {
    was_success_db_called_ = true;
  }
  virtual void OnSuccess(const IndexedDBKey& key) OVERRIDE {}
  virtual void OnSuccess(std::vector<char>* value) OVERRIDE {}
  virtual void OnSuccess(std::vector<char>* value,
                         const IndexedDBKey& key,
                         const IndexedDBKeyPath& key_path) OVERRIDE {}
  virtual void OnSuccess(int64 value) OVERRIDE {}
  virtual void OnSuccess() OVERRIDE {}
  virtual void OnSuccess(const IndexedDBKey& key,
                         const IndexedDBKey& primary_key,
                         std::vector<char>* value) OVERRIDE {}
  virtual void OnSuccessWithPrefetch(
      const std::vector<IndexedDBKey>& keys,
      const std::vector<IndexedDBKey>& primary_keys,
      const std::vector<std::vector<char> >& values) OVERRIDE {}

 private:
  virtual ~MockIDBCallbacks() { EXPECT_TRUE(was_success_db_called_); }
  MockIDBCallbacks()
      : IndexedDBCallbacksWrapper(NULL), was_success_db_called_(false) {}
  bool was_success_db_called_;
};

class FakeIDBDatabaseCallbacks : public IndexedDBDatabaseCallbacksWrapper {
 public:
  static scoped_refptr<FakeIDBDatabaseCallbacks> Create() {
    return make_scoped_refptr(new FakeIDBDatabaseCallbacks());
  }
  virtual void OnVersionChange(int64 old_version, int64 new_version) OVERRIDE {}
  virtual void OnForcedClose() OVERRIDE {}
  virtual void OnAbort(int64 transaction_id,
                       const IndexedDBDatabaseError& error) OVERRIDE {}
  virtual void OnComplete(int64 transaction_id) OVERRIDE {}

 private:
  friend class base::RefCounted<FakeIDBDatabaseCallbacks>;
  virtual ~FakeIDBDatabaseCallbacks() {}
  FakeIDBDatabaseCallbacks() : IndexedDBDatabaseCallbacksWrapper(NULL) {}
};

TEST(IndexedDBDatabaseTest, ConnectionLifecycle) {
  scoped_refptr<IndexedDBFakeBackingStore> backing_store =
      new IndexedDBFakeBackingStore();
  EXPECT_TRUE(backing_store->HasOneRef());  // local

  IndexedDBFactoryImpl* factory = 0;
  scoped_refptr<IndexedDBDatabaseImpl> db =
      IndexedDBDatabaseImpl::Create(ASCIIToUTF16("db"),
                                    backing_store.get(),
                                    factory,
                                    ASCIIToUTF16("uniqueid"));

  EXPECT_FALSE(backing_store->HasOneRef());  // local and db

  scoped_refptr<MockIDBCallbacks> request1 = MockIDBCallbacks::Create();
  scoped_refptr<FakeIDBDatabaseCallbacks> connection1 =
      FakeIDBDatabaseCallbacks::Create();
  const int64 transaction_id1 = 1;
  db->OpenConnection(request1,
                     connection1,
                     transaction_id1,
                     IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION);

  EXPECT_FALSE(backing_store->HasOneRef());  // db, connection count > 0

  scoped_refptr<MockIDBCallbacks> request2 = MockIDBCallbacks::Create();
  scoped_refptr<FakeIDBDatabaseCallbacks> connection2 =
      FakeIDBDatabaseCallbacks::Create();
  const int64 transaction_id2 = 2;
  db->OpenConnection(request2,
                     connection2,
                     transaction_id2,
                     IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION);

  EXPECT_FALSE(backing_store->HasOneRef());  // local and connection

  db->Close(connection1);

  EXPECT_FALSE(backing_store->HasOneRef());  // local and connection

  db->Close(connection2);
  EXPECT_TRUE(backing_store->HasOneRef());
  EXPECT_FALSE(db->BackingStore());

  db = NULL;
}

class MockIDBDatabaseCallbacks : public IndexedDBDatabaseCallbacksWrapper {
 public:
  static scoped_refptr<MockIDBDatabaseCallbacks> Create() {
    return make_scoped_refptr(new MockIDBDatabaseCallbacks());
  }
  virtual void OnVersionChange(int64 old_version, int64 new_version) OVERRIDE {}
  virtual void OnForcedClose() OVERRIDE {}
  virtual void OnAbort(int64 transaction_id,
                       const IndexedDBDatabaseError& error) OVERRIDE {
    was_abort_called_ = true;
  }
  virtual void OnComplete(int64 transaction_id) OVERRIDE {}

 private:
  MockIDBDatabaseCallbacks()
      : IndexedDBDatabaseCallbacksWrapper(NULL), was_abort_called_(false) {}
  virtual ~MockIDBDatabaseCallbacks() { EXPECT_TRUE(was_abort_called_); }
  bool was_abort_called_;
};

class WebIDBDatabaseCallbacksImpl : public WebIDBDatabaseCallbacks {
 public:
  explicit WebIDBDatabaseCallbacksImpl(
      scoped_refptr<IndexedDBDatabaseCallbacksWrapper> callbacks)
      : callbacks_(callbacks) {}
  virtual ~WebIDBDatabaseCallbacksImpl() {}

  virtual void onForcedClose() { callbacks_->OnForcedClose(); }
  virtual void onVersionChange(long long old_version, long long new_version) {
    callbacks_->OnVersionChange(old_version, new_version);
  }
  virtual void onAbort(long long transaction_id,
                       const WebIDBDatabaseError& error) {
    callbacks_->OnAbort(transaction_id, IndexedDBDatabaseError(error));
  }
  virtual void onComplete(long long transaction_id) {
    callbacks_->OnComplete(transaction_id);
  }

 private:
  scoped_refptr<IndexedDBDatabaseCallbacksWrapper> callbacks_;
};

TEST(IndexedDBDatabaseTest, ForcedClose) {
  scoped_refptr<IndexedDBFakeBackingStore> backing_store =
      new IndexedDBFakeBackingStore();
  EXPECT_TRUE(backing_store->HasOneRef());

  IndexedDBFactoryImpl* factory = 0;
  scoped_refptr<IndexedDBDatabaseImpl> backend =
      IndexedDBDatabaseImpl::Create(ASCIIToUTF16("db"),
                                    backing_store.get(),
                                    factory,
                                    ASCIIToUTF16("uniqueid"));

  EXPECT_FALSE(backing_store->HasOneRef());  // local and db

  scoped_refptr<MockIDBDatabaseCallbacks> connection =
      MockIDBDatabaseCallbacks::Create();
  scoped_refptr<IndexedDBDatabaseCallbacksWrapper> connection_proxy =
      IndexedDBDatabaseCallbacksWrapper::Create(
          new WebIDBDatabaseCallbacksImpl(connection));
  WebIDBDatabaseImpl web_database(backend, connection_proxy);

  scoped_refptr<MockIDBCallbacks> request = MockIDBCallbacks::Create();
  const int64 upgrade_transaction_id = 3;
  backend->OpenConnection(request,
                          connection_proxy,
                          upgrade_transaction_id,
                          IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION);

  const int64 transaction_id = 123;
  const std::vector<int64> scope;
  web_database.createTransaction(
      transaction_id, 0, scope, indexed_db::TRANSACTION_READ_ONLY);

  web_database.forceClose();

  EXPECT_TRUE(backing_store->HasOneRef());  // local
}

}  // namespace content