Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Side by Side Diff: components/offline_pages/core/prefetch/prefetch_dispatcher_impl_unittest.cc

Issue 2879013002: Create skeleton for the Prefetching store and initial pipeline step. (Closed)
Patch Set: Made construction params a cost&. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 "components/offline_pages/core/prefetch/prefetch_dispatcher_impl.h" 5 #include "components/offline_pages/core/prefetch/prefetch_dispatcher_impl.h"
6 6
7 #include "base/logging.h"
7 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/test/test_simple_task_runner.h"
10 #include "base/threading/thread_task_runner_handle.h"
8 #include "components/offline_pages/core/client_namespace_constants.h" 11 #include "components/offline_pages/core/client_namespace_constants.h"
9 #include "components/offline_pages/core/prefetch/prefetch_dispatcher.h" 12 #include "components/offline_pages/core/prefetch/prefetch_in_memory_store.h"
13 #include "components/offline_pages/core/prefetch/prefetch_service.h"
10 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
11 #include "url/gurl.h"
12 15
13 namespace offline_pages { 16 namespace offline_pages {
14 17
15 TEST(PrefetchDispatcherTest, DispatcherDoesNotCrash) { 18 class PrefetchDispatcherTest : public testing::Test, public PrefetchService {
16 PrefetchDispatcherImpl dispatcher; 19 public:
20 PrefetchDispatcherTest();
17 21
18 dispatcher.AddCandidatePrefetchURLs( 22 // Test implementation.
19 std::vector<PrefetchDispatcher::PrefetchURL>()); 23 void SetUp() override;
20 dispatcher.RemoveAllUnprocessedPrefetchURLs(kSuggestedArticlesNamespace); 24 void TearDown() override;
21 dispatcher.RemovePrefetchURLsByClientId({kSuggestedArticlesNamespace, "123"}); 25
26 // PrefetchService implementation:
27 OfflineMetricsCollector* GetOfflineMetricsCollector() override;
28 PrefetchDispatcher* GetPrefetchDispatcher() override;
29 PrefetchGCMHandler* GetPrefetchGCMHandler() override;
30 PrefetchStore* GetPrefetchStore() override;
31 void ObserveContentSuggestionsService(
32 ntp_snippets::ContentSuggestionsService* service) override;
33
34 // KeyedService implementation.
35 void Shutdown() override {}
36
37 void PumpLoop();
38
39 TaskQueue* dispatcher_task_queue() { return &dispatcher_impl_->task_queue_; }
40
41 private:
42 std::unique_ptr<PrefetchInMemoryStore> in_memory_store_;
43 std::unique_ptr<PrefetchDispatcherImpl> dispatcher_impl_;
44
45 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
46 base::ThreadTaskRunnerHandle task_runner_handle_;
47 };
48
49 PrefetchDispatcherTest::PrefetchDispatcherTest()
50 : task_runner_(new base::TestSimpleTaskRunner),
51 task_runner_handle_(task_runner_) {}
52
53 void PrefetchDispatcherTest::SetUp() {
54 ASSERT_EQ(base::ThreadTaskRunnerHandle::Get(), task_runner_);
55 ASSERT_FALSE(task_runner_->HasPendingTask());
56 in_memory_store_ = base::MakeUnique<PrefetchInMemoryStore>();
57 dispatcher_impl_ = base::MakeUnique<PrefetchDispatcherImpl>();
58 dispatcher_impl_->SetService(this);
59 }
60
61 void PrefetchDispatcherTest::TearDown() {
62 task_runner_->ClearPendingTasks();
63 }
64
65 OfflineMetricsCollector* PrefetchDispatcherTest::GetOfflineMetricsCollector() {
66 NOTREACHED();
67 return nullptr;
68 }
69
70 PrefetchDispatcher* PrefetchDispatcherTest::GetPrefetchDispatcher() {
71 return dispatcher_impl_.get();
72 }
73
74 PrefetchGCMHandler* PrefetchDispatcherTest::GetPrefetchGCMHandler() {
75 NOTREACHED();
76 return nullptr;
77 }
78
79 PrefetchStore* PrefetchDispatcherTest::GetPrefetchStore() {
80 return in_memory_store_.get();
81 }
82
83 void PrefetchDispatcherTest::ObserveContentSuggestionsService(
84 ntp_snippets::ContentSuggestionsService* service) {
85 NOTREACHED();
86 }
87
88 void PrefetchDispatcherTest::PumpLoop() {
89 task_runner_->RunUntilIdle();
90 }
91
92 TEST_F(PrefetchDispatcherTest, DispatcherDoesNotCrash) {
93 GetPrefetchDispatcher()->AddCandidatePrefetchURLs(std::vector<PrefetchURL>());
94 GetPrefetchDispatcher()->RemoveAllUnprocessedPrefetchURLs(
95 kSuggestedArticlesNamespace);
96 GetPrefetchDispatcher()->RemovePrefetchURLsByClientId(
97 {kSuggestedArticlesNamespace, "123"});
98 }
99
100 TEST_F(PrefetchDispatcherTest, AddCandidatePrefetchURLsTask) {
101 GetPrefetchDispatcher()->AddCandidatePrefetchURLs(std::vector<PrefetchURL>());
102 EXPECT_TRUE(dispatcher_task_queue()->HasPendingTasks());
103 EXPECT_TRUE(dispatcher_task_queue()->HasRunningTask());
104 PumpLoop();
105 EXPECT_FALSE(dispatcher_task_queue()->HasPendingTasks());
106 EXPECT_FALSE(dispatcher_task_queue()->HasRunningTask());
22 } 107 }
23 108
24 } // namespace offline_pages 109 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698