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

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: Dispatcher instance is now injected into Service. Minor changes. Created 3 years, 7 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/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/test/test_simple_task_runner.h"
9 #include "base/threading/thread_task_runner_handle.h"
8 #include "components/offline_pages/core/client_namespace_constants.h" 10 #include "components/offline_pages/core/client_namespace_constants.h"
9 #include "components/offline_pages/core/prefetch/prefetch_dispatcher.h" 11 #include "components/offline_pages/core/prefetch/prefetch_service.h"
12 #include "components/offline_pages/core/prefetch/prefetch_test_store.h"
10 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
11 #include "url/gurl.h"
12 14
13 namespace offline_pages { 15 namespace offline_pages {
14 16
15 TEST(PrefetchDispatcherTest, DispatcherDoesNotCrash) { 17 class PrefetchDispatcherTest : public testing::Test, public PrefetchService {
16 PrefetchDispatcherImpl dispatcher; 18 public:
19 PrefetchDispatcherTest();
17 20
18 dispatcher.AddCandidatePrefetchURLs( 21 // Test implementation.
19 std::vector<PrefetchDispatcher::PrefetchURL>()); 22 void SetUp() override;
20 dispatcher.RemoveAllUnprocessedPrefetchURLs(kSuggestedArticlesNamespace); 23 void TearDown() override;
21 dispatcher.RemovePrefetchURLsByClientId({kSuggestedArticlesNamespace, "123"}); 24
25 // PrefetchService implementation.
26 PrefetchDispatcher* GetDispatcher() override {
27 return dispatcher_impl_.get();
28 }
29 PrefetchStore* GetStore() override { return test_store_.get(); }
30
31 // KeyedService implementation.
32 void Shutdown() override {}
33
34 void PumpLoop();
35
36 TaskQueue* dispatcher_task_queue() { return &dispatcher_impl_->task_queue_; }
37
38 private:
39 std::unique_ptr<PrefetchTestStore> test_store_;
40 std::unique_ptr<PrefetchDispatcherImpl> dispatcher_impl_;
41
42 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
43 base::ThreadTaskRunnerHandle task_runner_handle_;
44 };
45
46 PrefetchDispatcherTest::PrefetchDispatcherTest()
47 : task_runner_(new base::TestSimpleTaskRunner),
48 task_runner_handle_(task_runner_) {}
49
50 void PrefetchDispatcherTest::SetUp() {
51 ASSERT_EQ(base::ThreadTaskRunnerHandle::Get(), task_runner_);
52 ASSERT_FALSE(task_runner_->HasPendingTask());
53 test_store_ = base::MakeUnique<PrefetchTestStore>();
54 dispatcher_impl_ = base::MakeUnique<PrefetchDispatcherImpl>();
55 dispatcher_impl_->SetService(this);
56 }
57
58 void PrefetchDispatcherTest::TearDown() {
59 task_runner_->ClearPendingTasks();
60 }
61
62 void PrefetchDispatcherTest::PumpLoop() {
63 task_runner_->RunUntilIdle();
64 }
65
66 TEST_F(PrefetchDispatcherTest, DispatcherDoesNotCrash) {
67 GetDispatcher()->AddCandidatePrefetchURLs(kSuggestedArticlesNamespace,
68 std::vector<PrefetchURL>());
69 GetDispatcher()->RemoveAllUnprocessedPrefetchURLs(
70 kSuggestedArticlesNamespace);
71 GetDispatcher()->RemovePrefetchURLsByClientId(kSuggestedArticlesNamespace,
72 "123");
73 }
74
75 TEST_F(PrefetchDispatcherTest, AddCandidatePrefetchURLsTask) {
76 GetDispatcher()->AddCandidatePrefetchURLs(kSuggestedArticlesNamespace,
77 std::vector<PrefetchURL>());
78 EXPECT_TRUE(dispatcher_task_queue()->HasPendingTasks());
79 EXPECT_TRUE(dispatcher_task_queue()->HasRunningTask());
80 PumpLoop();
81 EXPECT_FALSE(dispatcher_task_queue()->HasPendingTasks());
82 EXPECT_FALSE(dispatcher_task_queue()->HasRunningTask());
22 } 83 }
23 84
24 } // namespace offline_pages 85 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698