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

Side by Side Diff: components/offline_pages/background/request_coordinator_unittest.cc

Issue 1969463002: Add a save page request to the request queue (and test it). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More CR feedback from FGorski Created 4 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
« no previous file with comments | « components/offline_pages/background/request_coordinator.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/background/request_coordinator.h" 5 #include "components/offline_pages/background/request_coordinator.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/test/test_simple_task_runner.h"
12 #include "base/thread_task_runner_handle.h"
11 #include "components/offline_pages/background/offliner_factory.h" 13 #include "components/offline_pages/background/offliner_factory.h"
12 #include "components/offline_pages/background/offliner_policy.h" 14 #include "components/offline_pages/background/offliner_policy.h"
15 #include "components/offline_pages/background/request_queue.h"
16 #include "components/offline_pages/background/request_queue_in_memory_store.h"
13 #include "components/offline_pages/background/save_page_request.h" 17 #include "components/offline_pages/background/save_page_request.h"
14 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
15 19
16 namespace offline_pages { 20 namespace offline_pages {
17 21
18 namespace { 22 namespace {
19 // put test constants here 23 // put test constants here
20 const GURL kUrl("http://universe.com/everything"); 24 const GURL kUrl("http://universe.com/everything");
21 const ClientId kClientId("bookmark", "42"); 25 const ClientId kClientId("bookmark", "42");
22 } // namespace 26 } // namespace
23 27
24 class RequestCoordinatorTest 28 class RequestCoordinatorTest
25 : public testing::Test { 29 : public testing::Test {
26 public: 30 public:
27 RequestCoordinatorTest(); 31 RequestCoordinatorTest();
28 ~RequestCoordinatorTest() override; 32 ~RequestCoordinatorTest() override;
29 33
30 void CallbackFunction() { 34 void SetUp() override;
35
36 void PumpLoop();
37
38 RequestCoordinator* getCoordinator() {
39 return coordinator_.get();
31 } 40 }
41
42 // Empty callback function
43 void EmptyCallbackFunction() {
44 }
45
46 // Callback for getting requests.
47 void GetRequestsDone(RequestQueue::GetRequestsResult result,
48 const std::vector<SavePageRequest>& requests);
49
50 RequestQueue::GetRequestsResult last_get_requests_result() const {
51 return last_get_requests_result_;
52 }
53
54 const std::vector<SavePageRequest>& last_requests() const {
55 return last_requests_;
56 }
57
58 private:
59 RequestQueue::GetRequestsResult last_get_requests_result_;
60 std::vector<SavePageRequest> last_requests_;
61 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
62 base::ThreadTaskRunnerHandle task_runner_handle_;
63 std::unique_ptr<RequestCoordinator> coordinator_;
32 }; 64 };
33 65
34 RequestCoordinatorTest::RequestCoordinatorTest() {} 66 RequestCoordinatorTest::RequestCoordinatorTest()
67 : last_get_requests_result_(RequestQueue::GetRequestsResult::kStoreFailure),
68 task_runner_(new base::TestSimpleTaskRunner),
69 task_runner_handle_(task_runner_) {}
35 70
36 RequestCoordinatorTest::~RequestCoordinatorTest() {} 71 RequestCoordinatorTest::~RequestCoordinatorTest() {}
37 72
73 void RequestCoordinatorTest::SetUp() {
74 std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy());
75 std::unique_ptr<OfflinerFactory> factory;
76 std::unique_ptr<RequestQueueInMemoryStore>
77 store(new RequestQueueInMemoryStore());
78 std::unique_ptr<RequestQueue> queue(new RequestQueue(std::move(store)));
79 coordinator_.reset(new RequestCoordinator(
80 std::move(policy), std::move(factory), std::move(queue)));
81 }
82
83
84 void RequestCoordinatorTest::PumpLoop() {
85 task_runner_->RunUntilIdle();
86 }
87
88 void RequestCoordinatorTest::GetRequestsDone(
89 RequestQueue::GetRequestsResult result,
90 const std::vector<SavePageRequest>& requests) {
91 last_get_requests_result_ = result;
92 last_requests_ = requests;
93 }
94
38 TEST_F(RequestCoordinatorTest, StartProcessingWithNoRequests) { 95 TEST_F(RequestCoordinatorTest, StartProcessingWithNoRequests) {
39 RequestCoordinator::ProcessingDoneCallback callback = 96 RequestCoordinator::ProcessingDoneCallback callback =
40 base::Bind( 97 base::Bind(
41 &RequestCoordinatorTest::CallbackFunction, 98 &RequestCoordinatorTest::EmptyCallbackFunction,
42 base::Unretained(this)); 99 base::Unretained(this));
43 std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy()); 100 EXPECT_FALSE(getCoordinator()->StartProcessing(callback));
44 std::unique_ptr<OfflinerFactory> factory;
45 RequestCoordinator coordinator(std::move(policy), std::move(factory));
46 EXPECT_FALSE(coordinator.StartProcessing(callback));
47 } 101 }
48 102
49 TEST_F(RequestCoordinatorTest, SavePageLater) { 103 TEST_F(RequestCoordinatorTest, SavePageLater) {
50 std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy()); 104 EXPECT_TRUE(getCoordinator()->SavePageLater(kUrl, kClientId));
51 std::unique_ptr<OfflinerFactory> factory; 105
52 RequestCoordinator coordinator(std::move(policy), std::move(factory)); 106 // Expect that a request got placed on the queue.
53 EXPECT_TRUE(coordinator.SavePageLater(kUrl, kClientId)); 107 getCoordinator()->GetQueue()->GetRequests(
108 base::Bind(&RequestCoordinatorTest::GetRequestsDone,
109 base::Unretained(this)));
110
111 // Wait for callback to finish.
112 PumpLoop();
113
114 // Check the results are as expected.
115 EXPECT_EQ(1UL, last_requests().size());
116 EXPECT_EQ(kUrl, last_requests()[0].url());
117 EXPECT_EQ(kClientId, last_requests()[0].client_id());
118
119 // TODO(petewil): Expect that the scheduler got notified.
54 } 120 }
55 121
56 } // namespace offline_pages 122 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/background/request_coordinator.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698