| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
| 6 #include "base/values.h" | 6 #include "base/values.h" |
| 7 #include "content/child/indexed_db/indexed_db_dispatcher.h" | 7 #include "content/child/indexed_db/indexed_db_dispatcher.h" |
| 8 #include "content/child/indexed_db/indexed_db_key_builders.h" | 8 #include "content/child/indexed_db/indexed_db_key_builders.h" |
| 9 #include "content/child/indexed_db/webidbcursor_impl.h" | 9 #include "content/child/indexed_db/webidbcursor_impl.h" |
| 10 #include "content/child/thread_safe_sender.h" | 10 #include "content/child/thread_safe_sender.h" |
| 11 #include "content/common/indexed_db/indexed_db_key.h" | 11 #include "content/common/indexed_db/indexed_db_key.h" |
| 12 #include "ipc/ipc_sync_message_filter.h" | 12 #include "ipc/ipc_sync_message_filter.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "third_party/WebKit/public/platform/WebData.h" | 14 #include "third_party/WebKit/public/platform/WebData.h" |
| 15 #include "third_party/WebKit/public/platform/WebIDBCallbacks.h" | 15 #include "third_party/WebKit/public/platform/WebIDBCallbacks.h" |
| 16 | 16 |
| 17 using blink::WebBlobInfo; |
| 17 using blink::WebData; | 18 using blink::WebData; |
| 18 using blink::WebIDBCallbacks; | 19 using blink::WebIDBCallbacks; |
| 19 using blink::WebIDBDatabase; | 20 using blink::WebIDBDatabase; |
| 20 using blink::WebIDBKey; | 21 using blink::WebIDBKey; |
| 21 using blink::WebIDBKeyTypeNumber; | 22 using blink::WebIDBKeyTypeNumber; |
| 23 using blink::WebVector; |
| 22 | 24 |
| 23 namespace content { | 25 namespace content { |
| 24 | 26 |
| 25 namespace { | 27 namespace { |
| 26 | 28 |
| 27 class MockDispatcher : public IndexedDBDispatcher { | 29 class MockDispatcher : public IndexedDBDispatcher { |
| 28 public: | 30 public: |
| 29 MockDispatcher(ThreadSafeSender* thread_safe_sender) | 31 MockDispatcher(ThreadSafeSender* thread_safe_sender) |
| 30 : IndexedDBDispatcher(thread_safe_sender), | 32 : IndexedDBDispatcher(thread_safe_sender), |
| 31 prefetch_calls_(0), | 33 prefetch_calls_(0), |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 int reset_calls_; | 88 int reset_calls_; |
| 87 int last_used_count_; | 89 int last_used_count_; |
| 88 int advance_calls_; | 90 int advance_calls_; |
| 89 int continue_calls_; | 91 int continue_calls_; |
| 90 int32 destroyed_cursor_id_; | 92 int32 destroyed_cursor_id_; |
| 91 scoped_ptr<WebIDBCallbacks> callbacks_; | 93 scoped_ptr<WebIDBCallbacks> callbacks_; |
| 92 }; | 94 }; |
| 93 | 95 |
| 94 class MockContinueCallbacks : public WebIDBCallbacks { | 96 class MockContinueCallbacks : public WebIDBCallbacks { |
| 95 public: | 97 public: |
| 96 MockContinueCallbacks(IndexedDBKey* key = 0) : key_(key) {} | 98 MockContinueCallbacks(IndexedDBKey* key = 0, |
| 99 WebVector<WebBlobInfo>* webBlobInfo = 0) |
| 100 : key_(key), webBlobInfo_(webBlobInfo) {} |
| 97 | 101 |
| 98 virtual void onSuccess(const WebIDBKey& key, | 102 virtual void onSuccess(const WebIDBKey& key, |
| 99 const WebIDBKey& primaryKey, | 103 const WebIDBKey& primaryKey, |
| 100 const WebData& value) { | 104 const WebData& value, |
| 105 const WebVector<WebBlobInfo>& webBlobInfo) OVERRIDE { |
| 101 | 106 |
| 102 if (key_) | 107 if (key_) |
| 103 *key_ = IndexedDBKeyBuilder::Build(key); | 108 *key_ = IndexedDBKeyBuilder::Build(key); |
| 109 if (webBlobInfo_) |
| 110 *webBlobInfo_ = webBlobInfo; |
| 104 } | 111 } |
| 105 | 112 |
| 106 private: | 113 private: |
| 107 IndexedDBKey* key_; | 114 IndexedDBKey* key_; |
| 115 WebVector<WebBlobInfo>* webBlobInfo_; |
| 108 }; | 116 }; |
| 109 | 117 |
| 110 } // namespace | 118 } // namespace |
| 111 | 119 |
| 112 class WebIDBCursorImplTest : public testing::Test { | 120 class WebIDBCursorImplTest : public testing::Test { |
| 113 public: | 121 public: |
| 114 WebIDBCursorImplTest() { | 122 WebIDBCursorImplTest() { |
| 115 null_key_.assignNull(); | 123 null_key_.assignNull(); |
| 116 sync_message_filter_ = new IPC::SyncMessageFilter(NULL); | 124 sync_message_filter_ = new IPC::SyncMessageFilter(NULL); |
| 117 thread_safe_sender_ = new ThreadSafeSender( | 125 thread_safe_sender_ = new ThreadSafeSender( |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 | 172 |
| 165 // Verify that the requested count has increased since last time. | 173 // Verify that the requested count has increased since last time. |
| 166 int prefetch_count = dispatcher_->last_prefetch_count(); | 174 int prefetch_count = dispatcher_->last_prefetch_count(); |
| 167 EXPECT_GT(prefetch_count, last_prefetch_count); | 175 EXPECT_GT(prefetch_count, last_prefetch_count); |
| 168 last_prefetch_count = prefetch_count; | 176 last_prefetch_count = prefetch_count; |
| 169 | 177 |
| 170 // Fill the prefetch cache as requested. | 178 // Fill the prefetch cache as requested. |
| 171 std::vector<IndexedDBKey> keys; | 179 std::vector<IndexedDBKey> keys; |
| 172 std::vector<IndexedDBKey> primary_keys(prefetch_count); | 180 std::vector<IndexedDBKey> primary_keys(prefetch_count); |
| 173 std::vector<WebData> values(prefetch_count); | 181 std::vector<WebData> values(prefetch_count); |
| 182 std::vector<WebVector<WebBlobInfo> > blob_info; |
| 174 for (int i = 0; i < prefetch_count; ++i) { | 183 for (int i = 0; i < prefetch_count; ++i) { |
| 175 keys.push_back(IndexedDBKey(expected_key + i, WebIDBKeyTypeNumber)); | 184 keys.push_back(IndexedDBKey(expected_key + i, WebIDBKeyTypeNumber)); |
| 185 blob_info.push_back( |
| 186 WebVector<WebBlobInfo>(static_cast<size_t>(expected_key + i))); |
| 176 } | 187 } |
| 177 cursor.SetPrefetchData(keys, primary_keys, values); | 188 cursor.SetPrefetchData(keys, primary_keys, values, blob_info); |
| 178 | 189 |
| 179 // Note that the real dispatcher would call cursor->CachedContinue() | 190 // Note that the real dispatcher would call cursor->CachedContinue() |
| 180 // immediately after cursor->SetPrefetchData() to service the request | 191 // immediately after cursor->SetPrefetchData() to service the request |
| 181 // that initiated the prefetch. | 192 // that initiated the prefetch. |
| 182 | 193 |
| 183 // Verify that the cache is used for subsequent continue() calls. | 194 // Verify that the cache is used for subsequent continue() calls. |
| 184 for (int i = 0; i < prefetch_count; ++i) { | 195 for (int i = 0; i < prefetch_count; ++i) { |
| 185 IndexedDBKey key; | 196 IndexedDBKey key; |
| 186 cursor.continueFunction(null_key_, new MockContinueCallbacks(&key)); | 197 WebVector<WebBlobInfo> web_blob_info; |
| 198 cursor.continueFunction( |
| 199 null_key_, new MockContinueCallbacks(&key, &web_blob_info)); |
| 187 EXPECT_EQ(continue_calls, dispatcher_->continue_calls()); | 200 EXPECT_EQ(continue_calls, dispatcher_->continue_calls()); |
| 188 EXPECT_EQ(repetitions + 1, dispatcher_->prefetch_calls()); | 201 EXPECT_EQ(repetitions + 1, dispatcher_->prefetch_calls()); |
| 189 | 202 |
| 190 EXPECT_EQ(WebIDBKeyTypeNumber, key.type()); | 203 EXPECT_EQ(WebIDBKeyTypeNumber, key.type()); |
| 204 EXPECT_EQ(expected_key, static_cast<int>(web_blob_info.size())); |
| 191 EXPECT_EQ(expected_key++, key.number()); | 205 EXPECT_EQ(expected_key++, key.number()); |
| 192 } | 206 } |
| 193 } | 207 } |
| 194 } | 208 } |
| 195 | 209 |
| 196 EXPECT_EQ(dispatcher_->destroyed_cursor_id(), | 210 EXPECT_EQ(dispatcher_->destroyed_cursor_id(), |
| 197 WebIDBCursorImpl::kInvalidCursorId); | 211 WebIDBCursorImpl::kInvalidCursorId); |
| 198 } | 212 } |
| 199 | 213 |
| 200 TEST_F(WebIDBCursorImplTest, AdvancePrefetchTest) { | 214 TEST_F(WebIDBCursorImplTest, AdvancePrefetchTest) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 219 dispatcher_->continue_calls()); | 233 dispatcher_->continue_calls()); |
| 220 EXPECT_EQ(0, dispatcher_->advance_calls()); | 234 EXPECT_EQ(0, dispatcher_->advance_calls()); |
| 221 | 235 |
| 222 const int prefetch_count = dispatcher_->last_prefetch_count(); | 236 const int prefetch_count = dispatcher_->last_prefetch_count(); |
| 223 | 237 |
| 224 // Fill the prefetch cache as requested. | 238 // Fill the prefetch cache as requested. |
| 225 int expected_key = 0; | 239 int expected_key = 0; |
| 226 std::vector<IndexedDBKey> keys; | 240 std::vector<IndexedDBKey> keys; |
| 227 std::vector<IndexedDBKey> primary_keys(prefetch_count); | 241 std::vector<IndexedDBKey> primary_keys(prefetch_count); |
| 228 std::vector<WebData> values(prefetch_count); | 242 std::vector<WebData> values(prefetch_count); |
| 243 std::vector<WebVector<WebBlobInfo> > blob_info; |
| 229 for (int i = 0; i < prefetch_count; ++i) { | 244 for (int i = 0; i < prefetch_count; ++i) { |
| 230 keys.push_back(IndexedDBKey(expected_key + i, WebIDBKeyTypeNumber)); | 245 keys.push_back(IndexedDBKey(expected_key + i, WebIDBKeyTypeNumber)); |
| 246 blob_info.push_back( |
| 247 WebVector<WebBlobInfo>(static_cast<size_t>(expected_key + i))); |
| 231 } | 248 } |
| 232 cursor.SetPrefetchData(keys, primary_keys, values); | 249 cursor.SetPrefetchData(keys, primary_keys, values, blob_info); |
| 233 | 250 |
| 234 // Note that the real dispatcher would call cursor->CachedContinue() | 251 // Note that the real dispatcher would call cursor->CachedContinue() |
| 235 // immediately after cursor->SetPrefetchData() to service the request | 252 // immediately after cursor->SetPrefetchData() to service the request |
| 236 // that initiated the prefetch. | 253 // that initiated the prefetch. |
| 237 | 254 |
| 238 // Need at least this many in the cache for the test steps. | 255 // Need at least this many in the cache for the test steps. |
| 239 ASSERT_GE(prefetch_count, 5); | 256 ASSERT_GE(prefetch_count, 5); |
| 240 | 257 |
| 241 // IDBCursor.continue() | 258 // IDBCursor.continue() |
| 242 IndexedDBKey key; | 259 IndexedDBKey key; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 cursor.ResetPrefetchCache(); | 308 cursor.ResetPrefetchCache(); |
| 292 | 309 |
| 293 // No reset should have been sent since nothing has been received yet. | 310 // No reset should have been sent since nothing has been received yet. |
| 294 EXPECT_EQ(0, dispatcher_->reset_calls()); | 311 EXPECT_EQ(0, dispatcher_->reset_calls()); |
| 295 | 312 |
| 296 // Fill the prefetch cache as requested. | 313 // Fill the prefetch cache as requested. |
| 297 int prefetch_count = dispatcher_->last_prefetch_count(); | 314 int prefetch_count = dispatcher_->last_prefetch_count(); |
| 298 std::vector<IndexedDBKey> keys(prefetch_count); | 315 std::vector<IndexedDBKey> keys(prefetch_count); |
| 299 std::vector<IndexedDBKey> primary_keys(prefetch_count); | 316 std::vector<IndexedDBKey> primary_keys(prefetch_count); |
| 300 std::vector<WebData> values(prefetch_count); | 317 std::vector<WebData> values(prefetch_count); |
| 301 cursor.SetPrefetchData(keys, primary_keys, values); | 318 std::vector<WebVector<WebBlobInfo> > blob_info(prefetch_count); |
| 319 cursor.SetPrefetchData(keys, primary_keys, values, blob_info); |
| 302 | 320 |
| 303 // No reset should have been sent since prefetch data hasn't been used. | 321 // No reset should have been sent since prefetch data hasn't been used. |
| 304 EXPECT_EQ(0, dispatcher_->reset_calls()); | 322 EXPECT_EQ(0, dispatcher_->reset_calls()); |
| 305 | 323 |
| 306 // The real dispatcher would call cursor->CachedContinue(), so do that: | 324 // The real dispatcher would call cursor->CachedContinue(), so do that: |
| 307 scoped_ptr<WebIDBCallbacks> callbacks(new MockContinueCallbacks()); | 325 scoped_ptr<WebIDBCallbacks> callbacks(new MockContinueCallbacks()); |
| 308 cursor.CachedContinue(callbacks.get()); | 326 cursor.CachedContinue(callbacks.get()); |
| 309 | 327 |
| 310 // Now the cursor should have reset the rest of the cache. | 328 // Now the cursor should have reset the rest of the cache. |
| 311 EXPECT_EQ(1, dispatcher_->reset_calls()); | 329 EXPECT_EQ(1, dispatcher_->reset_calls()); |
| 312 EXPECT_EQ(1, dispatcher_->last_used_count()); | 330 EXPECT_EQ(1, dispatcher_->last_used_count()); |
| 313 } | 331 } |
| 314 | 332 |
| 315 } // namespace content | 333 } // namespace content |
| OLD | NEW |