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" |
(...skipping 12 matching lines...) Expand all Loading... |
23 namespace content { | 23 namespace content { |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 class MockDispatcher : public IndexedDBDispatcher { | 27 class MockDispatcher : public IndexedDBDispatcher { |
28 public: | 28 public: |
29 MockDispatcher(ThreadSafeSender* thread_safe_sender) | 29 MockDispatcher(ThreadSafeSender* thread_safe_sender) |
30 : IndexedDBDispatcher(thread_safe_sender), | 30 : IndexedDBDispatcher(thread_safe_sender), |
31 prefetch_calls_(0), | 31 prefetch_calls_(0), |
32 last_prefetch_count_(0), | 32 last_prefetch_count_(0), |
| 33 reset_calls_(0), |
| 34 last_used_count_(0), |
33 advance_calls_(0), | 35 advance_calls_(0), |
34 continue_calls_(0), | 36 continue_calls_(0), |
35 destroyed_cursor_id_(0) {} | 37 destroyed_cursor_id_(0) {} |
36 | 38 |
37 virtual void RequestIDBCursorPrefetch(int n, | 39 virtual void RequestIDBCursorPrefetch(int n, |
38 WebIDBCallbacks* callbacks, | 40 WebIDBCallbacks* callbacks, |
39 int32 ipc_cursor_id) OVERRIDE { | 41 int32 ipc_cursor_id) OVERRIDE { |
40 ++prefetch_calls_; | 42 ++prefetch_calls_; |
41 last_prefetch_count_ = n; | 43 last_prefetch_count_ = n; |
42 callbacks_.reset(callbacks); | 44 callbacks_.reset(callbacks); |
43 } | 45 } |
44 | 46 |
| 47 virtual void RequestIDBCursorPrefetchReset(int used_prefetches, |
| 48 int unused_prefetches, |
| 49 int32 ipc_cursor_id) OVERRIDE { |
| 50 ++reset_calls_; |
| 51 last_used_count_ = used_prefetches; |
| 52 } |
| 53 |
45 virtual void RequestIDBCursorAdvance(unsigned long count, | 54 virtual void RequestIDBCursorAdvance(unsigned long count, |
46 WebIDBCallbacks* callbacks, | 55 WebIDBCallbacks* callbacks, |
47 int32 ipc_cursor_id) OVERRIDE { | 56 int32 ipc_cursor_id) OVERRIDE { |
48 ++advance_calls_; | 57 ++advance_calls_; |
49 callbacks_.reset(callbacks); | 58 callbacks_.reset(callbacks); |
50 } | 59 } |
51 | 60 |
52 virtual void RequestIDBCursorContinue(const IndexedDBKey& key, | 61 virtual void RequestIDBCursorContinue(const IndexedDBKey& key, |
53 const IndexedDBKey& primary_key, | 62 const IndexedDBKey& primary_key, |
54 WebIDBCallbacks* callbacks, | 63 WebIDBCallbacks* callbacks, |
55 int32 ipc_cursor_id) OVERRIDE { | 64 int32 ipc_cursor_id) OVERRIDE { |
56 ++continue_calls_; | 65 ++continue_calls_; |
57 callbacks_.reset(callbacks); | 66 callbacks_.reset(callbacks); |
58 } | 67 } |
59 | 68 |
60 virtual void CursorDestroyed(int32 ipc_cursor_id) OVERRIDE { | 69 virtual void CursorDestroyed(int32 ipc_cursor_id) OVERRIDE { |
61 destroyed_cursor_id_ = ipc_cursor_id; | 70 destroyed_cursor_id_ = ipc_cursor_id; |
62 } | 71 } |
63 | 72 |
64 int prefetch_calls() { return prefetch_calls_; } | 73 int prefetch_calls() { return prefetch_calls_; } |
| 74 int last_prefetch_count() { return last_prefetch_count_; } |
| 75 int reset_calls() { return reset_calls_; } |
| 76 int last_used_count() { return last_used_count_; } |
65 int advance_calls() { return advance_calls_; } | 77 int advance_calls() { return advance_calls_; } |
66 int continue_calls() { return continue_calls_; } | 78 int continue_calls() { return continue_calls_; } |
67 int last_prefetch_count() { return last_prefetch_count_; } | |
68 int32 destroyed_cursor_id() { return destroyed_cursor_id_; } | 79 int32 destroyed_cursor_id() { return destroyed_cursor_id_; } |
69 | 80 |
70 private: | 81 private: |
71 int prefetch_calls_; | 82 int prefetch_calls_; |
72 int last_prefetch_count_; | 83 int last_prefetch_count_; |
| 84 int reset_calls_; |
| 85 int last_used_count_; |
73 int advance_calls_; | 86 int advance_calls_; |
74 int continue_calls_; | 87 int continue_calls_; |
75 int32 destroyed_cursor_id_; | 88 int32 destroyed_cursor_id_; |
76 scoped_ptr<WebIDBCallbacks> callbacks_; | 89 scoped_ptr<WebIDBCallbacks> callbacks_; |
77 }; | 90 }; |
78 | 91 |
79 class MockContinueCallbacks : public WebIDBCallbacks { | 92 class MockContinueCallbacks : public WebIDBCallbacks { |
80 public: | 93 public: |
81 MockContinueCallbacks(IndexedDBKey* key = 0) : key_(key) {} | 94 MockContinueCallbacks(IndexedDBKey* key = 0) : key_(key) {} |
82 | 95 |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 | 253 |
241 // IDBCursor.advance(lots) - beyond the fetched amount | 254 // IDBCursor.advance(lots) - beyond the fetched amount |
242 cursor.advance(WebIDBCursorImpl::kMaxPrefetchAmount, | 255 cursor.advance(WebIDBCursorImpl::kMaxPrefetchAmount, |
243 new MockContinueCallbacks(&key)); | 256 new MockContinueCallbacks(&key)); |
244 EXPECT_EQ(1, dispatcher_->advance_calls()); | 257 EXPECT_EQ(1, dispatcher_->advance_calls()); |
245 EXPECT_EQ(1, dispatcher_->prefetch_calls()); | 258 EXPECT_EQ(1, dispatcher_->prefetch_calls()); |
246 EXPECT_EQ(static_cast<int>(WebIDBCursorImpl::kPrefetchContinueThreshold), | 259 EXPECT_EQ(static_cast<int>(WebIDBCursorImpl::kPrefetchContinueThreshold), |
247 dispatcher_->continue_calls()); | 260 dispatcher_->continue_calls()); |
248 } | 261 } |
249 | 262 |
| 263 TEST_F(WebIDBCursorImplTest, PrefetchReset) { |
| 264 WebIDBCursorImpl cursor(WebIDBCursorImpl::kInvalidCursorId, |
| 265 thread_safe_sender_.get()); |
| 266 |
| 267 // Call continue() until prefetching should kick in. |
| 268 int continue_calls = 0; |
| 269 EXPECT_EQ(dispatcher_->continue_calls(), 0); |
| 270 for (int i = 0; i < WebIDBCursorImpl::kPrefetchContinueThreshold; ++i) { |
| 271 cursor.continueFunction(null_key_, new MockContinueCallbacks()); |
| 272 EXPECT_EQ(++continue_calls, dispatcher_->continue_calls()); |
| 273 EXPECT_EQ(0, dispatcher_->prefetch_calls()); |
| 274 } |
| 275 |
| 276 // Initiate the prefetch |
| 277 cursor.continueFunction(null_key_, new MockContinueCallbacks()); |
| 278 EXPECT_EQ(continue_calls, dispatcher_->continue_calls()); |
| 279 EXPECT_EQ(1, dispatcher_->prefetch_calls()); |
| 280 EXPECT_EQ(0, dispatcher_->reset_calls()); |
| 281 |
| 282 // Now invalidate it |
| 283 cursor.ResetPrefetchCache(); |
| 284 |
| 285 // No reset should have been sent since nothing has been received yet. |
| 286 EXPECT_EQ(0, dispatcher_->reset_calls()); |
| 287 |
| 288 // Fill the prefetch cache as requested. |
| 289 int prefetch_count = dispatcher_->last_prefetch_count(); |
| 290 std::vector<IndexedDBKey> keys(prefetch_count); |
| 291 std::vector<IndexedDBKey> primary_keys(prefetch_count); |
| 292 std::vector<WebData> values(prefetch_count); |
| 293 cursor.SetPrefetchData(keys, primary_keys, values); |
| 294 |
| 295 // No reset should have been sent since prefetch data hasn't been used. |
| 296 EXPECT_EQ(0, dispatcher_->reset_calls()); |
| 297 |
| 298 // The real dispatcher would call cursor->CachedContinue(), so do that: |
| 299 scoped_ptr<WebIDBCallbacks> callbacks(new MockContinueCallbacks()); |
| 300 cursor.CachedContinue(callbacks.get()); |
| 301 |
| 302 // Now the cursor should have reset the rest of the cache. |
| 303 EXPECT_EQ(1, dispatcher_->reset_calls()); |
| 304 EXPECT_EQ(1, dispatcher_->last_used_count()); |
| 305 } |
| 306 |
250 } // namespace content | 307 } // namespace content |
OLD | NEW |