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/remove_requests_task.h" | |
6 | |
7 #include <memory> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/test/test_simple_task_runner.h" | |
11 #include "base/threading/thread_task_runner_handle.h" | |
12 #include "components/offline_pages/background/request_queue_in_memory_store.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace offline_pages { | |
16 namespace { | |
17 const int64_t kRequestId1 = 42; | |
18 const int64_t kRequestId2 = 43; | |
19 const int64_t kRequestId3 = 44; | |
20 const GURL kUrl1("http://example.com"); | |
21 const GURL kUrl2("http://another-example.com"); | |
22 const ClientId kClientId1("bookmark", "1234"); | |
23 const ClientId kClientId2("async", "5678"); | |
24 } // namespace | |
25 | |
26 class RemoveRequestsTaskTest : public testing::Test { | |
27 public: | |
28 RemoveRequestsTaskTest(); | |
29 ~RemoveRequestsTaskTest() override; | |
30 | |
31 void PumpLoop(); | |
32 | |
33 void AddRequestsToStore(RequestQueueStore* store); | |
34 | |
35 void AddRequestDone(ItemActionStatus status); | |
36 | |
37 void RemoveRequestsCallback(std::unique_ptr<UpdateRequestsResult> result); | |
38 | |
39 UpdateRequestsResult* last_result() const { return result_.get(); } | |
40 | |
41 private: | |
42 std::unique_ptr<UpdateRequestsResult> result_; | |
43 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
44 base::ThreadTaskRunnerHandle task_runner_handle_; | |
45 }; | |
46 | |
47 RemoveRequestsTaskTest::RemoveRequestsTaskTest() | |
48 : task_runner_(new base::TestSimpleTaskRunner), | |
49 task_runner_handle_(task_runner_) {} | |
50 | |
51 RemoveRequestsTaskTest::~RemoveRequestsTaskTest() {} | |
52 | |
53 void RemoveRequestsTaskTest::PumpLoop() { | |
54 task_runner_->RunUntilIdle(); | |
55 } | |
56 | |
57 void RemoveRequestsTaskTest::AddRequestsToStore(RequestQueueStore* store) { | |
58 base::Time creation_time = base::Time::Now(); | |
59 SavePageRequest request_1(kRequestId1, kUrl1, kClientId1, creation_time, | |
60 true); | |
61 store->AddRequest(request_1, | |
62 base::Bind(&RemoveRequestsTaskTest::AddRequestDone, | |
63 base::Unretained(this))); | |
64 SavePageRequest request_2(kRequestId2, kUrl2, kClientId2, creation_time, | |
65 true); | |
66 store->AddRequest(request_2, | |
67 base::Bind(&RemoveRequestsTaskTest::AddRequestDone, | |
68 base::Unretained(this))); | |
69 PumpLoop(); | |
70 } | |
71 | |
72 void RemoveRequestsTaskTest::AddRequestDone(ItemActionStatus status) { | |
73 ASSERT_EQ(ItemActionStatus::SUCCESS, status); | |
74 } | |
75 | |
76 void RemoveRequestsTaskTest::RemoveRequestsCallback( | |
77 std::unique_ptr<UpdateRequestsResult> result) { | |
78 result_ = std::move(result); | |
79 } | |
80 | |
81 TEST_F(RemoveRequestsTaskTest, RemoveWhenStoreEmpty) { | |
82 RequestQueueInMemoryStore store; | |
83 std::vector<int64_t> request_ids{kRequestId1}; | |
84 RemoveRequestsTask task( | |
85 &store, request_ids, | |
86 base::Bind(&RemoveRequestsTaskTest::RemoveRequestsCallback, | |
87 base::Unretained(this))); | |
88 task.Run(); | |
89 PumpLoop(); | |
90 ASSERT_TRUE(last_result()); | |
91 EXPECT_EQ(1UL, last_result()->item_statuses.size()); | |
92 EXPECT_EQ(kRequestId1, last_result()->item_statuses.at(0).first); | |
93 EXPECT_EQ(ItemActionStatus::NOT_FOUND, | |
94 last_result()->item_statuses.at(0).second); | |
95 EXPECT_EQ(0UL, last_result()->updated_items.size()); | |
96 } | |
97 | |
98 TEST_F(RemoveRequestsTaskTest, RemoveSingleItem) { | |
99 RequestQueueInMemoryStore store; | |
100 AddRequestsToStore(&store); | |
101 std::vector<int64_t> request_ids{kRequestId1}; | |
102 RemoveRequestsTask task( | |
103 &store, request_ids, | |
104 base::Bind(&RemoveRequestsTaskTest::RemoveRequestsCallback, | |
105 base::Unretained(this))); | |
106 task.Run(); | |
107 PumpLoop(); | |
108 ASSERT_TRUE(last_result()); | |
109 EXPECT_EQ(1UL, last_result()->item_statuses.size()); | |
110 EXPECT_EQ(kRequestId1, last_result()->item_statuses.at(0).first); | |
111 EXPECT_EQ(ItemActionStatus::SUCCESS, | |
112 last_result()->item_statuses.at(0).second); | |
113 EXPECT_EQ(1UL, last_result()->updated_items.size()); | |
114 EXPECT_EQ(kRequestId1, last_result()->updated_items.at(0).request_id()); | |
115 } | |
116 | |
117 TEST_F(RemoveRequestsTaskTest, RemoveMultipleItems) { | |
118 RequestQueueInMemoryStore store; | |
119 AddRequestsToStore(&store); | |
120 std::vector<int64_t> request_ids{kRequestId1, kRequestId2}; | |
121 RemoveRequestsTask task( | |
122 &store, request_ids, | |
123 base::Bind(&RemoveRequestsTaskTest::RemoveRequestsCallback, | |
124 base::Unretained(this))); | |
125 task.Run(); | |
126 PumpLoop(); | |
127 ASSERT_TRUE(last_result()); | |
128 EXPECT_EQ(2UL, last_result()->item_statuses.size()); | |
129 EXPECT_EQ(kRequestId1, last_result()->item_statuses.at(0).first); | |
130 EXPECT_EQ(ItemActionStatus::SUCCESS, | |
131 last_result()->item_statuses.at(0).second); | |
132 EXPECT_EQ(kRequestId2, last_result()->item_statuses.at(1).first); | |
133 EXPECT_EQ(ItemActionStatus::SUCCESS, | |
134 last_result()->item_statuses.at(1).second); | |
135 EXPECT_EQ(2UL, last_result()->updated_items.size()); | |
136 EXPECT_EQ(kRequestId1, last_result()->updated_items.at(0).request_id()); | |
137 EXPECT_EQ(kRequestId2, last_result()->updated_items.at(1).request_id()); | |
138 } | |
139 | |
140 TEST_F(RemoveRequestsTaskTest, DeleteWithEmptyIdList) { | |
141 RequestQueueInMemoryStore store; | |
142 std::vector<int64_t> request_ids; | |
143 RemoveRequestsTask task( | |
144 &store, request_ids, | |
145 base::Bind(&RemoveRequestsTaskTest::RemoveRequestsCallback, | |
146 base::Unretained(this))); | |
147 task.Run(); | |
148 PumpLoop(); | |
149 ASSERT_TRUE(last_result()); | |
150 EXPECT_EQ(0UL, last_result()->item_statuses.size()); | |
151 EXPECT_EQ(0UL, last_result()->updated_items.size()); | |
152 } | |
153 | |
154 TEST_F(RemoveRequestsTaskTest, RemoveMissingItem) { | |
155 RequestQueueInMemoryStore store; | |
156 AddRequestsToStore(&store); | |
157 std::vector<int64_t> request_ids{kRequestId1, kRequestId3}; | |
158 RemoveRequestsTask task( | |
159 &store, request_ids, | |
160 base::Bind(&RemoveRequestsTaskTest::RemoveRequestsCallback, | |
161 base::Unretained(this))); | |
162 task.Run(); | |
163 PumpLoop(); | |
164 ASSERT_TRUE(last_result()); | |
165 EXPECT_EQ(2UL, last_result()->item_statuses.size()); | |
166 EXPECT_EQ(kRequestId1, last_result()->item_statuses.at(0).first); | |
167 EXPECT_EQ(ItemActionStatus::SUCCESS, | |
168 last_result()->item_statuses.at(0).second); | |
169 EXPECT_EQ(kRequestId3, last_result()->item_statuses.at(1).first); | |
170 EXPECT_EQ(ItemActionStatus::NOT_FOUND, | |
171 last_result()->item_statuses.at(1).second); | |
172 EXPECT_EQ(1UL, last_result()->updated_items.size()); | |
173 EXPECT_EQ(kRequestId1, last_result()->updated_items.at(0).request_id()); | |
174 } | |
175 | |
176 } // namespace offline_pages | |
OLD | NEW |