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