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), | |
35 advance_calls_(0), | 33 advance_calls_(0), |
36 continue_calls_(0), | 34 continue_calls_(0), |
37 destroyed_cursor_id_(0) {} | 35 destroyed_cursor_id_(0) {} |
38 | 36 |
39 virtual void RequestIDBCursorPrefetch(int n, | 37 virtual void RequestIDBCursorPrefetch(int n, |
40 WebIDBCallbacks* callbacks, | 38 WebIDBCallbacks* callbacks, |
41 int32 ipc_cursor_id) OVERRIDE { | 39 int32 ipc_cursor_id) OVERRIDE { |
42 ++prefetch_calls_; | 40 ++prefetch_calls_; |
43 last_prefetch_count_ = n; | 41 last_prefetch_count_ = n; |
44 callbacks_.reset(callbacks); | 42 callbacks_.reset(callbacks); |
45 } | 43 } |
46 | 44 |
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 | |
54 virtual void RequestIDBCursorAdvance(unsigned long count, | 45 virtual void RequestIDBCursorAdvance(unsigned long count, |
55 WebIDBCallbacks* callbacks, | 46 WebIDBCallbacks* callbacks, |
56 int32 ipc_cursor_id) OVERRIDE { | 47 int32 ipc_cursor_id) OVERRIDE { |
57 ++advance_calls_; | 48 ++advance_calls_; |
58 callbacks_.reset(callbacks); | 49 callbacks_.reset(callbacks); |
59 } | 50 } |
60 | 51 |
61 virtual void RequestIDBCursorContinue(const IndexedDBKey& key, | 52 virtual void RequestIDBCursorContinue(const IndexedDBKey& key, |
62 const IndexedDBKey& primary_key, | 53 const IndexedDBKey& primary_key, |
63 WebIDBCallbacks* callbacks, | 54 WebIDBCallbacks* callbacks, |
64 int32 ipc_cursor_id) OVERRIDE { | 55 int32 ipc_cursor_id) OVERRIDE { |
65 ++continue_calls_; | 56 ++continue_calls_; |
66 callbacks_.reset(callbacks); | 57 callbacks_.reset(callbacks); |
67 } | 58 } |
68 | 59 |
69 virtual void CursorDestroyed(int32 ipc_cursor_id) OVERRIDE { | 60 virtual void CursorDestroyed(int32 ipc_cursor_id) OVERRIDE { |
70 destroyed_cursor_id_ = ipc_cursor_id; | 61 destroyed_cursor_id_ = ipc_cursor_id; |
71 } | 62 } |
72 | 63 |
73 int prefetch_calls() { return prefetch_calls_; } | 64 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_; } | |
77 int advance_calls() { return advance_calls_; } | 65 int advance_calls() { return advance_calls_; } |
78 int continue_calls() { return continue_calls_; } | 66 int continue_calls() { return continue_calls_; } |
| 67 int last_prefetch_count() { return last_prefetch_count_; } |
79 int32 destroyed_cursor_id() { return destroyed_cursor_id_; } | 68 int32 destroyed_cursor_id() { return destroyed_cursor_id_; } |
80 | 69 |
81 private: | 70 private: |
82 int prefetch_calls_; | 71 int prefetch_calls_; |
83 int last_prefetch_count_; | 72 int last_prefetch_count_; |
84 int reset_calls_; | |
85 int last_used_count_; | |
86 int advance_calls_; | 73 int advance_calls_; |
87 int continue_calls_; | 74 int continue_calls_; |
88 int32 destroyed_cursor_id_; | 75 int32 destroyed_cursor_id_; |
89 scoped_ptr<WebIDBCallbacks> callbacks_; | 76 scoped_ptr<WebIDBCallbacks> callbacks_; |
90 }; | 77 }; |
91 | 78 |
92 class MockContinueCallbacks : public WebIDBCallbacks { | 79 class MockContinueCallbacks : public WebIDBCallbacks { |
93 public: | 80 public: |
94 MockContinueCallbacks(IndexedDBKey* key = 0) : key_(key) {} | 81 MockContinueCallbacks(IndexedDBKey* key = 0) : key_(key) {} |
95 | 82 |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 | 240 |
254 // IDBCursor.advance(lots) - beyond the fetched amount | 241 // IDBCursor.advance(lots) - beyond the fetched amount |
255 cursor.advance(WebIDBCursorImpl::kMaxPrefetchAmount, | 242 cursor.advance(WebIDBCursorImpl::kMaxPrefetchAmount, |
256 new MockContinueCallbacks(&key)); | 243 new MockContinueCallbacks(&key)); |
257 EXPECT_EQ(1, dispatcher_->advance_calls()); | 244 EXPECT_EQ(1, dispatcher_->advance_calls()); |
258 EXPECT_EQ(1, dispatcher_->prefetch_calls()); | 245 EXPECT_EQ(1, dispatcher_->prefetch_calls()); |
259 EXPECT_EQ(static_cast<int>(WebIDBCursorImpl::kPrefetchContinueThreshold), | 246 EXPECT_EQ(static_cast<int>(WebIDBCursorImpl::kPrefetchContinueThreshold), |
260 dispatcher_->continue_calls()); | 247 dispatcher_->continue_calls()); |
261 } | 248 } |
262 | 249 |
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 cursor.CachedContinue(new MockContinueCallbacks()); | |
300 | |
301 // Now the cursor should have reset the rest of the cache. | |
302 EXPECT_EQ(1, dispatcher_->reset_calls()); | |
303 EXPECT_EQ(1, dispatcher_->last_used_count()); | |
304 } | |
305 | |
306 } // namespace content | 250 } // namespace content |
OLD | NEW |