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

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

Issue 2020833002: Add the request picker and a unit test (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: CR feedback per Dimich and DougArnett Created 4 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
« no previous file with comments | « components/offline_pages/background/request_picker.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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/offline_pages/background/request_picker.h"
6
7 #include "base/bind.h"
8 #include "base/test/test_simple_task_runner.h"
9 #include "base/threading/thread_task_runner_handle.h"
10 #include "base/time/time.h"
11 #include "components/offline_pages/background/request_queue.h"
12 #include "components/offline_pages/background/request_queue_in_memory_store.h"
13 #include "components/offline_pages/background/save_page_request.h"
14 #include "components/offline_pages/offline_page_item.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace offline_pages {
18
19 namespace {
20 // Data for request 1.
21 const int64_t kRequestId1 = 17;
22 const GURL kUrl1("https://google.com");
23 const ClientId kClientId1("bookmark", "1234");
24 // Data for request 2.
25 const int64_t kRequestId2 = 42;
26 const GURL kUrl2("http://nytimes.com");
27 const ClientId kClientId2("bookmark", "5678");
28 } // namespace
29
30 class RequestPickerTest : public testing::Test {
31 public:
32 RequestPickerTest();
33
34 ~RequestPickerTest() override;
35
36 void SetUp() override;
37
38 void PumpLoop();
39
40 void AddRequestDone(RequestQueue::AddRequestResult result,
41 const SavePageRequest& request);
42
43 void RequestPicked(const SavePageRequest& request);
44
45 void RequestQueueEmpty();
46
47 protected:
48 // The request queue is simple enough we will use a real queue with a memory
49 // store instead of a stub.
50 std::unique_ptr<RequestQueue> queue_;
51 std::unique_ptr<RequestPicker> picker_;
52 std::unique_ptr<SavePageRequest> last_picked_;
53 bool request_queue_empty_called_;
54
55 private:
56 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
57 base::ThreadTaskRunnerHandle task_runner_handle_;
58 };
59
60 RequestPickerTest::RequestPickerTest()
61 : task_runner_(new base::TestSimpleTaskRunner),
62 task_runner_handle_(task_runner_) {}
63
64 RequestPickerTest::~RequestPickerTest() {}
65
66 void RequestPickerTest::SetUp() {
67 std::unique_ptr<RequestQueueInMemoryStore> store(
68 new RequestQueueInMemoryStore());
69 queue_.reset(new RequestQueue(std::move(store)));
70 picker_.reset(new RequestPicker(queue_.get()));
71 request_queue_empty_called_ = false;
72 }
73
74 void RequestPickerTest::PumpLoop() {
75 task_runner_->RunUntilIdle();
76 }
77
78 void RequestPickerTest::AddRequestDone(RequestQueue::AddRequestResult result,
79 const SavePageRequest& request) {}
80
81 void RequestPickerTest::RequestPicked(const SavePageRequest& request) {
82 last_picked_.reset(new SavePageRequest(request));
83 }
84
85 void RequestPickerTest::RequestQueueEmpty() {
86 request_queue_empty_called_ = true;
87 }
88
89 TEST_F(RequestPickerTest, ChooseNextRequest) {
90 base::Time creation_time = base::Time::Now();
91 SavePageRequest request1(kRequestId1, kUrl1, kClientId1, creation_time);
92 SavePageRequest request2(kRequestId2, kUrl2, kClientId2, creation_time);
93 // Put some test requests on the Queue.
94 queue_->AddRequest(request1, base::Bind(&RequestPickerTest::AddRequestDone,
95 base::Unretained(this)));
96 queue_->AddRequest(request2, base::Bind(&RequestPickerTest::AddRequestDone,
97 base::Unretained(this)));
98
99 // Pump the loop to give the async queue the opportunity to do the adds.
100 PumpLoop();
101
102 picker_->ChooseNextRequest(
103 base::Bind(&RequestPickerTest::RequestPicked, base::Unretained(this)),
104 base::Bind(&RequestPickerTest::RequestQueueEmpty,
105 base::Unretained(this)));
106
107 // Pump the loop again to give the async queue the opportunity to return
108 // results from the Get operation, and for the picker to call the "picked"
109 // callback.
110 PumpLoop();
111
112 EXPECT_EQ(kRequestId1, last_picked_->request_id());
113 EXPECT_FALSE(request_queue_empty_called_);
114 }
115
116 TEST_F(RequestPickerTest, PickFromEmptyQueue) {
117 picker_->ChooseNextRequest(
118 base::Bind(&RequestPickerTest::RequestPicked, base::Unretained(this)),
119 base::Bind(&RequestPickerTest::RequestQueueEmpty,
120 base::Unretained(this)));
121
122 // Pump the loop again to give the async queue the opportunity to return
123 // results from the Get operation, and for the picker to call the "QueueEmpty"
124 // callback.
125 PumpLoop();
126
127 EXPECT_TRUE(request_queue_empty_called_);
128 }
129
130 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/background/request_picker.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698