Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/test/simple_test_clock.h" | |
| 14 #include "base/values.h" | 15 #include "base/values.h" |
| 15 #include "chrome/browser/history/web_history_service_factory.h" | 16 #include "chrome/browser/history/web_history_service_factory.h" |
| 16 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" | 17 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" |
| 17 #include "chrome/browser/signin/fake_signin_manager_builder.h" | 18 #include "chrome/browser/signin/fake_signin_manager_builder.h" |
| 18 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 19 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 19 #include "chrome/browser/signin/signin_manager_factory.h" | 20 #include "chrome/browser/signin/signin_manager_factory.h" |
| 20 #include "chrome/browser/sync/profile_sync_service_factory.h" | 21 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 21 #include "chrome/browser/sync/profile_sync_test_util.h" | 22 #include "chrome/browser/sync/profile_sync_test_util.h" |
| 22 #include "chrome/test/base/testing_profile.h" | 23 #include "chrome/test/base/testing_profile.h" |
| 23 #include "components/browser_sync/test_profile_sync_service.h" | 24 #include "components/browser_sync/test_profile_sync_service.h" |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 41 | 42 |
| 42 struct TestResult { | 43 struct TestResult { |
| 43 std::string url; | 44 std::string url; |
| 44 int64_t hour_offset; // Visit time in hours past the baseline time. | 45 int64_t hour_offset; // Visit time in hours past the baseline time. |
| 45 }; | 46 }; |
| 46 | 47 |
| 47 // Duplicates on the same day in the local timezone are removed, so set a | 48 // Duplicates on the same day in the local timezone are removed, so set a |
| 48 // baseline time in local time. | 49 // baseline time in local time. |
| 49 const base::Time baseline_time = base::Time::UnixEpoch().LocalMidnight(); | 50 const base::Time baseline_time = base::Time::UnixEpoch().LocalMidnight(); |
| 50 | 51 |
| 52 base::Time GetReferenceTime() { | |
|
Dan Beam
2016/11/10 19:06:37
nit: PretendNow?
calamity
2016/11/17 02:24:34
Done.
| |
| 53 base::Time::Exploded exploded_reference_time; | |
| 54 exploded_reference_time.year = 2015; | |
| 55 exploded_reference_time.month = 1; | |
| 56 exploded_reference_time.day_of_month = 2; | |
| 57 exploded_reference_time.day_of_week = 5; | |
| 58 exploded_reference_time.hour = 11; | |
| 59 exploded_reference_time.minute = 0; | |
| 60 exploded_reference_time.second = 0; | |
| 61 exploded_reference_time.millisecond = 0; | |
| 62 | |
| 63 base::Time out_time; | |
| 64 EXPECT_TRUE( | |
| 65 base::Time::FromLocalExploded(exploded_reference_time, &out_time)); | |
| 66 return out_time; | |
| 67 } | |
| 68 | |
| 51 // For each item in |results|, create a new Value representing the visit, and | 69 // For each item in |results|, create a new Value representing the visit, and |
| 52 // insert it into |list_value|. | 70 // insert it into |list_value|. |
| 53 void AddQueryResults( | 71 void AddQueryResults( |
| 54 TestResult* test_results, | 72 TestResult* test_results, |
| 55 int test_results_size, | 73 int test_results_size, |
| 56 std::vector<BrowsingHistoryHandler::HistoryEntry>* results) { | 74 std::vector<BrowsingHistoryHandler::HistoryEntry>* results) { |
| 57 for (int i = 0; i < test_results_size; ++i) { | 75 for (int i = 0; i < test_results_size; ++i) { |
| 58 BrowsingHistoryHandler::HistoryEntry entry; | 76 BrowsingHistoryHandler::HistoryEntry entry; |
| 59 entry.time = baseline_time + | 77 entry.time = baseline_time + |
| 60 base::TimeDelta::FromHours(test_results[i].hour_offset); | 78 base::TimeDelta::FromHours(test_results[i].hour_offset); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 NotifyObservers(); | 113 NotifyObservers(); |
| 96 } | 114 } |
| 97 | 115 |
| 98 private: | 116 private: |
| 99 bool sync_active_; | 117 bool sync_active_; |
| 100 }; | 118 }; |
| 101 | 119 |
| 102 class BrowsingHistoryHandlerWithWebUIForTesting | 120 class BrowsingHistoryHandlerWithWebUIForTesting |
| 103 : public BrowsingHistoryHandler { | 121 : public BrowsingHistoryHandler { |
| 104 public: | 122 public: |
| 105 explicit BrowsingHistoryHandlerWithWebUIForTesting(content::WebUI* web_ui) { | 123 explicit BrowsingHistoryHandlerWithWebUIForTesting(content::WebUI* web_ui) |
| 124 : test_clock_(new base::SimpleTestClock()) { | |
| 125 set_clock(base::WrapUnique(test_clock_)); | |
| 106 set_web_ui(web_ui); | 126 set_web_ui(web_ui); |
| 107 } | 127 } |
| 108 using BrowsingHistoryHandler::QueryComplete; | 128 using BrowsingHistoryHandler::QueryComplete; |
| 129 | |
| 130 base::SimpleTestClock* test_clock() { return test_clock_; } | |
| 131 | |
| 132 private: | |
| 133 base::SimpleTestClock* test_clock_; | |
| 109 }; | 134 }; |
| 110 | 135 |
| 111 } // namespace | 136 } // namespace |
| 112 | 137 |
| 113 class BrowsingHistoryHandlerTest : public ::testing::Test { | 138 class BrowsingHistoryHandlerTest : public ::testing::Test { |
| 114 public: | 139 public: |
| 115 void SetUp() override { | 140 void SetUp() override { |
| 116 TestingProfile::Builder builder; | 141 TestingProfile::Builder builder; |
| 117 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), | 142 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), |
| 118 &BuildFakeProfileOAuth2TokenService); | 143 &BuildFakeProfileOAuth2TokenService); |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 258 BrowsingHistoryHandler::MergeDuplicateResults(&results); | 283 BrowsingHistoryHandler::MergeDuplicateResults(&results); |
| 259 | 284 |
| 260 ASSERT_EQ(2U, results.size()); | 285 ASSERT_EQ(2U, results.size()); |
| 261 EXPECT_TRUE(ResultEquals(results[0], test_data[3])); | 286 EXPECT_TRUE(ResultEquals(results[0], test_data[3])); |
| 262 EXPECT_TRUE(ResultEquals(results[1], test_data[1])); | 287 EXPECT_TRUE(ResultEquals(results[1], test_data[1])); |
| 263 EXPECT_EQ(3u, results[0].all_timestamps.size()); | 288 EXPECT_EQ(3u, results[0].all_timestamps.size()); |
| 264 EXPECT_EQ(1u, results[1].all_timestamps.size()); | 289 EXPECT_EQ(1u, results[1].all_timestamps.size()); |
| 265 } | 290 } |
| 266 } | 291 } |
| 267 | 292 |
| 293 TEST_F(BrowsingHistoryHandlerTest, SetQueryTimeInWeeks) { | |
| 294 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui()); | |
| 295 handler.test_clock()->SetNow(GetReferenceTime()); | |
| 296 | |
| 297 history::QueryOptions options; | |
| 298 base::Time expected_time = GetReferenceTime(); | |
|
Dan Beam
2016/11/10 19:06:36
I think the reader loses a bit of information / ha
calamity
2016/11/17 02:24:34
Done.
| |
| 299 | |
| 300 // Querying this week should result in end time being midnight tonight and | |
| 301 // begin time being midnight a week ago. | |
| 302 handler.SetQueryTimeInWeeks(0, &options); | |
| 303 expected_time = expected_time.LocalMidnight() + base::TimeDelta::FromDays(1); | |
| 304 EXPECT_EQ(expected_time, options.end_time); | |
| 305 expected_time -= base::TimeDelta::FromDays(7); | |
| 306 EXPECT_EQ(expected_time, options.begin_time); | |
| 307 | |
| 308 // Querying 4 weeks ago should result in end time being midnight 4 weeks ago | |
| 309 // and begin time being midnight 5 weeks ago. | |
| 310 handler.SetQueryTimeInWeeks(4, &options); | |
| 311 expected_time = GetReferenceTime() - base::TimeDelta::FromDays(28); | |
| 312 EXPECT_EQ(expected_time, options.end_time); | |
| 313 expected_time -= base::TimeDelta::FromDays(7); | |
| 314 EXPECT_EQ(expected_time, options.begin_time); | |
| 315 } | |
| 316 | |
| 317 TEST_F(BrowsingHistoryHandlerTest, SetQueryTimeInMonths) { | |
| 318 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui()); | |
| 319 handler.test_clock()->SetNow(GetReferenceTime()); | |
| 320 history::QueryOptions options; | |
| 321 | |
| 322 base::Time::Exploded exploded_expected_time; | |
| 323 GetReferenceTime().LocalExplode(&exploded_expected_time); | |
| 324 | |
| 325 base::Time expected_time; | |
| 326 | |
| 327 // Querying this month should result in end time being unset and begin time | |
| 328 // being midnight of the 1st. | |
| 329 handler.SetQueryTimeInMonths(0, &options); | |
| 330 EXPECT_EQ(base::Time(), options.end_time); | |
| 331 | |
| 332 exploded_expected_time.day_of_month = 1; | |
| 333 exploded_expected_time.hour = 0; | |
| 334 EXPECT_TRUE( | |
| 335 base::Time::FromLocalExploded(exploded_expected_time, &expected_time)); | |
| 336 EXPECT_EQ(expected_time, options.begin_time); | |
| 337 | |
| 338 // Querying 6 months ago should result in end time being 2014-08-01 and begin | |
| 339 // time being 2014-07-01. | |
| 340 handler.SetQueryTimeInMonths(6, &options); | |
| 341 | |
| 342 exploded_expected_time.year = 2014; | |
| 343 exploded_expected_time.month = 8; | |
| 344 EXPECT_TRUE( | |
| 345 base::Time::FromLocalExploded(exploded_expected_time, &expected_time)); | |
| 346 EXPECT_EQ(expected_time, options.end_time); | |
| 347 exploded_expected_time.month = 7; | |
| 348 EXPECT_TRUE( | |
| 349 base::Time::FromLocalExploded(exploded_expected_time, &expected_time)); | |
| 350 EXPECT_EQ(expected_time, options.begin_time); | |
| 351 } | |
| 352 | |
| 268 // Tests that BrowsingHistoryHandler observes WebHistoryService deletions. | 353 // Tests that BrowsingHistoryHandler observes WebHistoryService deletions. |
| 269 TEST_F(BrowsingHistoryHandlerTest, ObservingWebHistoryDeletions) { | 354 TEST_F(BrowsingHistoryHandlerTest, ObservingWebHistoryDeletions) { |
| 270 base::Callback<void(bool)> callback = base::Bind(&IgnoreBoolAndDoNothing); | 355 base::Callback<void(bool)> callback = base::Bind(&IgnoreBoolAndDoNothing); |
| 271 | 356 |
| 272 // BrowsingHistoryHandler listens to WebHistoryService history deletions. | 357 // BrowsingHistoryHandler listens to WebHistoryService history deletions. |
| 273 { | 358 { |
| 274 sync_service()->SetSyncActive(true); | 359 sync_service()->SetSyncActive(true); |
| 275 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui()); | 360 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui()); |
| 276 handler.RegisterMessages(); | 361 handler.RegisterMessages(); |
| 277 | 362 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 361 const base::DictionaryValue* first_entry; | 446 const base::DictionaryValue* first_entry; |
| 362 ASSERT_TRUE(arg2->GetDictionary(0, &first_entry)); | 447 ASSERT_TRUE(arg2->GetDictionary(0, &first_entry)); |
| 363 | 448 |
| 364 base::string16 title; | 449 base::string16 title; |
| 365 ASSERT_TRUE(first_entry->GetString("title", &title)); | 450 ASSERT_TRUE(first_entry->GetString("title", &title)); |
| 366 | 451 |
| 367 ASSERT_EQ(0u, title.find(base::ASCIIToUTF16("http://loooo"))); | 452 ASSERT_EQ(0u, title.find(base::ASCIIToUTF16("http://loooo"))); |
| 368 EXPECT_EQ(300u, title.size()); | 453 EXPECT_EQ(300u, title.size()); |
| 369 } | 454 } |
| 370 #endif | 455 #endif |
| OLD | NEW |