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

Side by Side Diff: chrome/browser/ui/webui/browsing_history_handler_unittest.cc

Issue 2450453002: Refactor BrowsingHistoryHandler, create BrowsingHistoryService (Closed)
Patch Set: Rename HistoryUiService to BrowsingHistoryService Created 4 years, 1 month 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
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 "chrome/browser/ui/webui/browsing_history_handler.h" 5 #include "chrome/browser/ui/webui/browsing_history_handler.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <memory> 8 #include <memory>
9 #include <set> 9 #include <set>
10 #include <utility>
10 11
11 #include "base/macros.h" 12 #include "base/macros.h"
12 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
13 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h" 15 #include "base/values.h"
16 #include "chrome/browser/history/browsing_history_service.h"
15 #include "chrome/browser/history/web_history_service_factory.h" 17 #include "chrome/browser/history/web_history_service_factory.h"
16 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" 18 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
17 #include "chrome/browser/signin/fake_signin_manager_builder.h" 19 #include "chrome/browser/signin/fake_signin_manager_builder.h"
18 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 20 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
19 #include "chrome/browser/signin/signin_manager_factory.h" 21 #include "chrome/browser/signin/signin_manager_factory.h"
20 #include "chrome/browser/sync/profile_sync_service_factory.h" 22 #include "chrome/browser/sync/profile_sync_service_factory.h"
21 #include "chrome/browser/sync/profile_sync_test_util.h" 23 #include "chrome/browser/sync/profile_sync_test_util.h"
22 #include "chrome/test/base/testing_profile.h" 24 #include "chrome/test/base/testing_profile.h"
23 #include "components/browser_sync/test_profile_sync_service.h" 25 #include "components/browser_sync/test_profile_sync_service.h"
24 #include "components/history/core/test/fake_web_history_service.h" 26 #include "components/history/core/test/fake_web_history_service.h"
25 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" 27 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
26 #include "components/signin/core/browser/fake_signin_manager.h" 28 #include "components/signin/core/browser/fake_signin_manager.h"
27 #include "components/sync/base/model_type.h" 29 #include "components/sync/base/model_type.h"
28 #include "content/public/browser/browser_thread.h" 30 #include "content/public/browser/browser_thread.h"
29 #include "content/public/browser/web_contents.h" 31 #include "content/public/browser/web_contents.h"
30 #include "content/public/test/test_browser_thread_bundle.h" 32 #include "content/public/test/test_browser_thread_bundle.h"
31 #include "content/public/test/test_web_ui.h" 33 #include "content/public/test/test_web_ui.h"
32 #include "net/http/http_status_code.h" 34 #include "net/http/http_status_code.h"
33 #include "testing/gtest/include/gtest/gtest.h" 35 #include "testing/gtest/include/gtest/gtest.h"
34 #include "url/gurl.h" 36 #include "url/gurl.h"
35 37
36 #if !defined(OS_ANDROID) 38 #if !defined(OS_ANDROID)
37 #include "chrome/browser/ui/webui/md_history_ui.h" 39 #include "chrome/browser/ui/webui/md_history_ui.h"
38 #endif 40 #endif
39 41
40 namespace { 42 namespace {
41 43
42 struct TestResult {
43 std::string url;
44 int64_t hour_offset; // Visit time in hours past the baseline time.
45 };
46
47 // Duplicates on the same day in the local timezone are removed, so set a
48 // baseline time in local time.
49 const base::Time baseline_time = base::Time::UnixEpoch().LocalMidnight();
50
51 // For each item in |results|, create a new Value representing the visit, and
52 // insert it into |list_value|.
53 void AddQueryResults(
54 TestResult* test_results,
55 int test_results_size,
56 std::vector<BrowsingHistoryHandler::HistoryEntry>* results) {
57 for (int i = 0; i < test_results_size; ++i) {
58 BrowsingHistoryHandler::HistoryEntry entry;
59 entry.time = baseline_time +
60 base::TimeDelta::FromHours(test_results[i].hour_offset);
61 entry.url = GURL(test_results[i].url);
62 entry.all_timestamps.insert(entry.time.ToInternalValue());
63 results->push_back(entry);
64 }
65 }
66
67 // Returns true if |result| matches the test data given by |correct_result|,
68 // otherwise returns false.
69 bool ResultEquals(
70 const BrowsingHistoryHandler::HistoryEntry& result,
71 const TestResult& correct_result) {
72 base::Time correct_time =
73 baseline_time + base::TimeDelta::FromHours(correct_result.hour_offset);
74
75 return result.time == correct_time && result.url == GURL(correct_result.url);
76 }
77
78 void IgnoreBoolAndDoNothing(bool ignored_argument) {} 44 void IgnoreBoolAndDoNothing(bool ignored_argument) {}
79 45
80 class TestSyncService : public browser_sync::TestProfileSyncService { 46 class TestSyncService : public browser_sync::TestProfileSyncService {
81 public: 47 public:
82 explicit TestSyncService(Profile* profile) 48 explicit TestSyncService(Profile* profile)
83 : browser_sync::TestProfileSyncService( 49 : browser_sync::TestProfileSyncService(
84 CreateProfileSyncServiceParamsForTest(profile)), 50 CreateProfileSyncServiceParamsForTest(profile)),
85 sync_active_(true) {} 51 sync_active_(true) {}
86 52
87 bool IsSyncActive() const override { return sync_active_; } 53 bool IsSyncActive() const override { return sync_active_; }
(...skipping 10 matching lines...) Expand all
98 private: 64 private:
99 bool sync_active_; 65 bool sync_active_;
100 }; 66 };
101 67
102 class BrowsingHistoryHandlerWithWebUIForTesting 68 class BrowsingHistoryHandlerWithWebUIForTesting
103 : public BrowsingHistoryHandler { 69 : public BrowsingHistoryHandler {
104 public: 70 public:
105 explicit BrowsingHistoryHandlerWithWebUIForTesting(content::WebUI* web_ui) { 71 explicit BrowsingHistoryHandlerWithWebUIForTesting(content::WebUI* web_ui) {
106 set_web_ui(web_ui); 72 set_web_ui(web_ui);
107 } 73 }
108 using BrowsingHistoryHandler::QueryComplete;
109 }; 74 };
110 75
111 } // namespace 76 } // namespace
112 77
113 class BrowsingHistoryHandlerTest : public ::testing::Test { 78 class BrowsingHistoryHandlerTest : public ::testing::Test {
114 public: 79 public:
115 void SetUp() override { 80 void SetUp() override {
116 TestingProfile::Builder builder; 81 TestingProfile::Builder builder;
117 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), 82 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
118 &BuildFakeProfileOAuth2TokenService); 83 &BuildFakeProfileOAuth2TokenService);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 } 135 }
171 136
172 content::TestBrowserThreadBundle thread_bundle_; 137 content::TestBrowserThreadBundle thread_bundle_;
173 std::unique_ptr<TestingProfile> profile_; 138 std::unique_ptr<TestingProfile> profile_;
174 TestSyncService* sync_service_; 139 TestSyncService* sync_service_;
175 history::FakeWebHistoryService* web_history_service_; 140 history::FakeWebHistoryService* web_history_service_;
176 std::unique_ptr<content::TestWebUI> web_ui_; 141 std::unique_ptr<content::TestWebUI> web_ui_;
177 std::unique_ptr<content::WebContents> web_contents_; 142 std::unique_ptr<content::WebContents> web_contents_;
178 }; 143 };
179 144
180 // Tests that the MergeDuplicateResults method correctly removes duplicate 145 // Tests that BrowsingHistoryHandler is informed about WebHistoryService
181 // visits to the same URL on the same day. 146 // deletions.
182 // Fails on Android. http://crbug.com/2345
183 #if defined(OS_ANDROID)
184 #define MAYBE_MergeDuplicateResults DISABLED_MergeDuplicateResults
185 #else
186 #define MAYBE_MergeDuplicateResults MergeDuplicateResults
187 #endif
188 TEST_F(BrowsingHistoryHandlerTest, MAYBE_MergeDuplicateResults) {
189 {
190 // Basic test that duplicates on the same day are removed.
191 TestResult test_data[] = {
192 { "http://google.com", 0 },
193 { "http://google.de", 1 },
194 { "http://google.com", 2 },
195 { "http://google.com", 3 } // Most recent.
196 };
197 std::vector<BrowsingHistoryHandler::HistoryEntry> results;
198 AddQueryResults(test_data, arraysize(test_data), &results);
199 BrowsingHistoryHandler::MergeDuplicateResults(&results);
200
201 ASSERT_EQ(2U, results.size());
202 EXPECT_TRUE(ResultEquals(results[0], test_data[3]));
203 EXPECT_TRUE(ResultEquals(results[1], test_data[1]));
204 }
205
206 {
207 // Test that a duplicate URL on the next day is not removed.
208 TestResult test_data[] = {
209 { "http://google.com", 0 },
210 { "http://google.com", 23 },
211 { "http://google.com", 24 }, // Most recent.
212 };
213 std::vector<BrowsingHistoryHandler::HistoryEntry> results;
214 AddQueryResults(test_data, arraysize(test_data), &results);
215 BrowsingHistoryHandler::MergeDuplicateResults(&results);
216
217 ASSERT_EQ(2U, results.size());
218 EXPECT_TRUE(ResultEquals(results[0], test_data[2]));
219 EXPECT_TRUE(ResultEquals(results[1], test_data[1]));
220 }
221
222 {
223 // Test multiple duplicates across multiple days.
224 TestResult test_data[] = {
225 // First day.
226 { "http://google.de", 0 },
227 { "http://google.com", 1 },
228 { "http://google.de", 2 },
229 { "http://google.com", 3 },
230
231 // Second day.
232 { "http://google.de", 24 },
233 { "http://google.com", 25 },
234 { "http://google.de", 26 },
235 { "http://google.com", 27 }, // Most recent.
236 };
237 std::vector<BrowsingHistoryHandler::HistoryEntry> results;
238 AddQueryResults(test_data, arraysize(test_data), &results);
239 BrowsingHistoryHandler::MergeDuplicateResults(&results);
240
241 ASSERT_EQ(4U, results.size());
242 EXPECT_TRUE(ResultEquals(results[0], test_data[7]));
243 EXPECT_TRUE(ResultEquals(results[1], test_data[6]));
244 EXPECT_TRUE(ResultEquals(results[2], test_data[3]));
245 EXPECT_TRUE(ResultEquals(results[3], test_data[2]));
246 }
247
248 {
249 // Test that timestamps for duplicates are properly saved.
250 TestResult test_data[] = {
251 { "http://google.com", 0 },
252 { "http://google.de", 1 },
253 { "http://google.com", 2 },
254 { "http://google.com", 3 } // Most recent.
255 };
256 std::vector<BrowsingHistoryHandler::HistoryEntry> results;
257 AddQueryResults(test_data, arraysize(test_data), &results);
258 BrowsingHistoryHandler::MergeDuplicateResults(&results);
259
260 ASSERT_EQ(2U, results.size());
261 EXPECT_TRUE(ResultEquals(results[0], test_data[3]));
262 EXPECT_TRUE(ResultEquals(results[1], test_data[1]));
263 EXPECT_EQ(3u, results[0].all_timestamps.size());
264 EXPECT_EQ(1u, results[1].all_timestamps.size());
265 }
266 }
267
268 // Tests that BrowsingHistoryHandler observes WebHistoryService deletions.
269 TEST_F(BrowsingHistoryHandlerTest, ObservingWebHistoryDeletions) { 147 TEST_F(BrowsingHistoryHandlerTest, ObservingWebHistoryDeletions) {
270 base::Callback<void(bool)> callback = base::Bind(&IgnoreBoolAndDoNothing); 148 base::Callback<void(bool)> callback = base::Bind(&IgnoreBoolAndDoNothing);
271 149
272 // BrowsingHistoryHandler listens to WebHistoryService history deletions. 150 // BrowsingHistoryHandler is informed about WebHistoryService history
151 // deletions.
273 { 152 {
274 sync_service()->SetSyncActive(true); 153 sync_service()->SetSyncActive(true);
275 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui()); 154 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui());
276 handler.RegisterMessages(); 155 handler.RegisterMessages();
277 156
278 web_history_service()->ExpireHistoryBetween(std::set<GURL>(), base::Time(), 157 web_history_service()->ExpireHistoryBetween(std::set<GURL>(), base::Time(),
279 base::Time::Max(), callback); 158 base::Time::Max(), callback);
280 159
281 EXPECT_EQ(1U, web_ui()->call_data().size()); 160 EXPECT_EQ(1U, web_ui()->call_data().size());
282 EXPECT_EQ("historyDeleted", web_ui()->call_data().back()->function_name()); 161 EXPECT_EQ("historyDeleted", web_ui()->call_data().back()->function_name());
283 } 162 }
284 163
285 // BrowsingHistoryHandler will listen to WebHistoryService deletions even if 164 // BrowsingHistoryHandler will be informed about WebHistoryService deletions
286 // history sync is activated later. 165 // even if history sync is activated later.
287 { 166 {
288 sync_service()->SetSyncActive(false); 167 sync_service()->SetSyncActive(false);
289 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui()); 168 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui());
290 handler.RegisterMessages(); 169 handler.RegisterMessages();
291 sync_service()->SetSyncActive(true); 170 sync_service()->SetSyncActive(true);
292 171
293 web_history_service()->ExpireHistoryBetween(std::set<GURL>(), base::Time(), 172 web_history_service()->ExpireHistoryBetween(std::set<GURL>(), base::Time(),
294 base::Time::Max(), callback); 173 base::Time::Max(), callback);
295 174
296 EXPECT_EQ(2U, web_ui()->call_data().size()); 175 EXPECT_EQ(2U, web_ui()->call_data().size());
297 EXPECT_EQ("historyDeleted", web_ui()->call_data().back()->function_name()); 176 EXPECT_EQ("historyDeleted", web_ui()->call_data().back()->function_name());
298 } 177 }
299 178
300 // BrowsingHistoryHandler does not fire historyDeleted while a web history 179 // BrowsingHistoryHandler does not fire historyDeleted while a web history
301 // delete request is happening. 180 // delete request is happening.
302 { 181 {
303 sync_service()->SetSyncActive(true); 182 sync_service()->SetSyncActive(true);
304 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui()); 183 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui());
305 handler.RegisterMessages(); 184 handler.RegisterMessages();
306 185
307 // Simulate an ongoing delete request. 186 // Simulate an ongoing delete request.
308 handler.has_pending_delete_request_ = true; 187 handler.browsing_history_service_->has_pending_delete_request_ = true;
309 188
310 web_history_service()->ExpireHistoryBetween( 189 web_history_service()->ExpireHistoryBetween(
311 std::set<GURL>(), base::Time(), base::Time::Max(), 190 std::set<GURL>(), base::Time(), base::Time::Max(),
312 base::Bind(&BrowsingHistoryHandler::RemoveWebHistoryComplete, 191 base::Bind(
313 handler.weak_factory_.GetWeakPtr())); 192 &BrowsingHistoryService::RemoveWebHistoryComplete,
193 handler.browsing_history_service_->weak_factory_.GetWeakPtr()));
314 194
315 EXPECT_EQ(3U, web_ui()->call_data().size()); 195 EXPECT_EQ(3U, web_ui()->call_data().size());
316 EXPECT_EQ("deleteComplete", web_ui()->call_data().back()->function_name()); 196 EXPECT_EQ("deleteComplete", web_ui()->call_data().back()->function_name());
317 } 197 }
318 198
319 // When history sync is not active, we don't listen to WebHistoryService 199 // When history sync is not active, we don't listen to WebHistoryService
320 // deletions. The WebHistoryService object still exists (because it's a 200 // deletions. The WebHistoryService object still exists (because it's a
321 // BrowserContextKeyedService), but is not visible to BrowsingHistoryHandler. 201 // BrowserContextKeyedService), but is not visible to BrowsingHistoryHandler.
322 { 202 {
323 sync_service()->SetSyncActive(false); 203 sync_service()->SetSyncActive(false);
(...skipping 19 matching lines...) Expand all
343 "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo" 223 "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
344 "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo" 224 "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
345 "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo" 225 "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
346 "ngurlislong.com"), base::Time()); 226 "ngurlislong.com"), base::Time());
347 ASSERT_GT(long_result.url().spec().size(), 300u); 227 ASSERT_GT(long_result.url().spec().size(), 300u);
348 228
349 history::QueryResults results; 229 history::QueryResults results;
350 results.AppendURLBySwapping(&long_result); 230 results.AppendURLBySwapping(&long_result);
351 231
352 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui()); 232 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui());
233 handler.RegisterMessages(); // Needed to create BrowsingHistoryService.
353 web_ui()->ClearTrackedCalls(); 234 web_ui()->ClearTrackedCalls();
354 235
355 handler.QueryComplete(base::string16(), history::QueryOptions(), &results); 236 handler.browsing_history_service_->QueryComplete(
237 base::string16(), history::QueryOptions(), &results);
356 ASSERT_FALSE(web_ui()->call_data().empty()); 238 ASSERT_FALSE(web_ui()->call_data().empty());
357 239
358 const base::ListValue* arg2; 240 const base::ListValue* arg2;
359 ASSERT_TRUE(web_ui()->call_data().front()->arg2()->GetAsList(&arg2)); 241 ASSERT_TRUE(web_ui()->call_data().front()->arg2()->GetAsList(&arg2));
360 242
361 const base::DictionaryValue* first_entry; 243 const base::DictionaryValue* first_entry;
362 ASSERT_TRUE(arg2->GetDictionary(0, &first_entry)); 244 ASSERT_TRUE(arg2->GetDictionary(0, &first_entry));
363 245
364 base::string16 title; 246 base::string16 title;
365 ASSERT_TRUE(first_entry->GetString("title", &title)); 247 ASSERT_TRUE(first_entry->GetString("title", &title));
366 248
367 ASSERT_EQ(0u, title.find(base::ASCIIToUTF16("http://loooo"))); 249 ASSERT_EQ(0u, title.find(base::ASCIIToUTF16("http://loooo")));
368 EXPECT_EQ(300u, title.size()); 250 EXPECT_EQ(300u, title.size());
369 } 251 }
370 #endif 252 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698