Chromium Code Reviews| OLD | NEW |
|---|---|
| (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, not a stub. | |
|
dougarnett
2016/05/31 18:03:04
... use a real queue with memory store. ?
| |
| 49 std::unique_ptr<RequestQueue> queue_; | |
| 50 std::unique_ptr<RequestPicker> picker_; | |
| 51 std::unique_ptr<SavePageRequest> last_picked_; | |
| 52 bool request_queue_empty_called_; | |
| 53 | |
| 54 private: | |
| 55 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
| 56 base::ThreadTaskRunnerHandle task_runner_handle_; | |
| 57 }; | |
| 58 | |
| 59 RequestPickerTest::RequestPickerTest() | |
| 60 : task_runner_(new base::TestSimpleTaskRunner), | |
| 61 task_runner_handle_(task_runner_) {} | |
| 62 | |
| 63 RequestPickerTest::~RequestPickerTest() {} | |
| 64 | |
| 65 void RequestPickerTest::SetUp() { | |
| 66 std::unique_ptr<RequestQueueInMemoryStore> store( | |
| 67 new RequestQueueInMemoryStore()); | |
| 68 queue_.reset(new RequestQueue(std::move(store))); | |
| 69 picker_.reset(new RequestPicker( | |
| 70 queue_.get(), | |
| 71 base::Bind(&RequestPickerTest::RequestPicked, base::Unretained(this)), | |
| 72 base::Bind(&RequestPickerTest::RequestQueueEmpty, | |
| 73 base::Unretained(this)))); | |
| 74 request_queue_empty_called_ = false; | |
| 75 } | |
| 76 | |
| 77 void RequestPickerTest::PumpLoop() { | |
| 78 task_runner_->RunUntilIdle(); | |
| 79 } | |
| 80 | |
| 81 void RequestPickerTest::AddRequestDone(RequestQueue::AddRequestResult result, | |
| 82 const SavePageRequest& request) {} | |
| 83 | |
| 84 void RequestPickerTest::RequestPicked(const SavePageRequest& request) { | |
| 85 last_picked_.reset(new SavePageRequest(request)); | |
| 86 } | |
| 87 | |
| 88 void RequestPickerTest::RequestQueueEmpty() { | |
| 89 request_queue_empty_called_ = true; | |
| 90 } | |
| 91 | |
| 92 TEST_F(RequestPickerTest, ChooseNextRequest) { | |
| 93 base::Time creation_time = base::Time::Now(); | |
| 94 SavePageRequest request1(kRequestId1, kUrl1, kClientId1, creation_time); | |
| 95 SavePageRequest request2(kRequestId2, kUrl2, kClientId2, creation_time); | |
| 96 // Put some test requests on the Queue. | |
| 97 queue_->AddRequest(request1, base::Bind(&RequestPickerTest::AddRequestDone, | |
| 98 base::Unretained(this))); | |
| 99 queue_->AddRequest(request2, base::Bind(&RequestPickerTest::AddRequestDone, | |
| 100 base::Unretained(this))); | |
| 101 | |
| 102 // Pump the loop to give the async queue the opportunity to do the adds. | |
| 103 PumpLoop(); | |
| 104 | |
| 105 picker_->ChooseNextRequest(); | |
| 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 | |
| 119 // Pump the loop again to give the async queue the opportunity to return | |
| 120 // results from the Get operation, and for the picker to call the "QueueEmpty" | |
| 121 // callback. | |
| 122 PumpLoop(); | |
| 123 | |
| 124 EXPECT_TRUE(request_queue_empty_called_); | |
| 125 } | |
| 126 | |
| 127 } // namespace offline_pages | |
| OLD | NEW |