| 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 PretendNow() { |
| 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); |
| 127 test_clock_->SetNow(PretendNow()); |
| 128 |
| 107 } | 129 } |
| 108 using BrowsingHistoryHandler::QueryComplete; | 130 using BrowsingHistoryHandler::QueryComplete; |
| 131 |
| 132 base::SimpleTestClock* test_clock() { return test_clock_; } |
| 133 |
| 134 private: |
| 135 base::SimpleTestClock* test_clock_; |
| 109 }; | 136 }; |
| 110 | 137 |
| 111 } // namespace | 138 } // namespace |
| 112 | 139 |
| 113 class BrowsingHistoryHandlerTest : public ::testing::Test { | 140 class BrowsingHistoryHandlerTest : public ::testing::Test { |
| 114 public: | 141 public: |
| 115 void SetUp() override { | 142 void SetUp() override { |
| 116 TestingProfile::Builder builder; | 143 TestingProfile::Builder builder; |
| 117 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), | 144 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), |
| 118 &BuildFakeProfileOAuth2TokenService); | 145 &BuildFakeProfileOAuth2TokenService); |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 BrowsingHistoryHandler::MergeDuplicateResults(&results); | 285 BrowsingHistoryHandler::MergeDuplicateResults(&results); |
| 259 | 286 |
| 260 ASSERT_EQ(2U, results.size()); | 287 ASSERT_EQ(2U, results.size()); |
| 261 EXPECT_TRUE(ResultEquals(results[0], test_data[3])); | 288 EXPECT_TRUE(ResultEquals(results[0], test_data[3])); |
| 262 EXPECT_TRUE(ResultEquals(results[1], test_data[1])); | 289 EXPECT_TRUE(ResultEquals(results[1], test_data[1])); |
| 263 EXPECT_EQ(3u, results[0].all_timestamps.size()); | 290 EXPECT_EQ(3u, results[0].all_timestamps.size()); |
| 264 EXPECT_EQ(1u, results[1].all_timestamps.size()); | 291 EXPECT_EQ(1u, results[1].all_timestamps.size()); |
| 265 } | 292 } |
| 266 } | 293 } |
| 267 | 294 |
| 295 TEST_F(BrowsingHistoryHandlerTest, SetQueryTimeInWeeks) { |
| 296 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui()); |
| 297 history::QueryOptions options; |
| 298 |
| 299 // Querying this week should result in end time being midnight tonight and |
| 300 // begin time being midnight a week ago. |
| 301 handler.SetQueryTimeInWeeks(0, &options); |
| 302 base::Time midnight_tonight = |
| 303 PretendNow().LocalMidnight() + base::TimeDelta::FromDays(1); |
| 304 EXPECT_EQ(midnight_tonight, options.end_time); |
| 305 base::Time midnight_a_week_ago = |
| 306 midnight_tonight - base::TimeDelta::FromDays(7); |
| 307 EXPECT_EQ(midnight_a_week_ago, options.begin_time); |
| 308 |
| 309 // Querying 4 weeks ago should result in end time being midnight 4 weeks ago |
| 310 // and begin time being midnight 5 weeks ago. |
| 311 handler.SetQueryTimeInWeeks(4, &options); |
| 312 base::Time midnight_4_weeks_ago = |
| 313 PretendNow().LocalMidnight() - base::TimeDelta::FromDays(27); |
| 314 EXPECT_EQ(midnight_4_weeks_ago, options.end_time); |
| 315 base::Time midnight_5_weeks_ago = |
| 316 midnight_4_weeks_ago - base::TimeDelta::FromDays(7); |
| 317 EXPECT_EQ(midnight_5_weeks_ago, options.begin_time); |
| 318 } |
| 319 |
| 320 TEST_F(BrowsingHistoryHandlerTest, SetQueryTimeInMonths) { |
| 321 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui()); |
| 322 history::QueryOptions options; |
| 323 |
| 324 base::Time::Exploded exploded_expected_time; |
| 325 PretendNow().LocalExplode(&exploded_expected_time); |
| 326 |
| 327 // Querying this month should result in end time being unset and begin time |
| 328 // being midnight of the first. |
| 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 base::Time first_jan_2015_midnight; |
| 335 EXPECT_TRUE(base::Time::FromLocalExploded(exploded_expected_time, |
| 336 &first_jan_2015_midnight)); |
| 337 EXPECT_EQ(first_jan_2015_midnight, options.begin_time); |
| 338 |
| 339 // Querying 6 months ago should result in end time being 2014-08-01 and begin |
| 340 // time being 2014-07-01. |
| 341 handler.SetQueryTimeInMonths(6, &options); |
| 342 |
| 343 exploded_expected_time.year = 2014; |
| 344 exploded_expected_time.month = 8; |
| 345 base::Time first_aug_2014_midnight; |
| 346 EXPECT_TRUE(base::Time::FromLocalExploded(exploded_expected_time, |
| 347 &first_aug_2014_midnight)); |
| 348 EXPECT_EQ(first_aug_2014_midnight, options.end_time); |
| 349 exploded_expected_time.month = 7; |
| 350 base::Time first_jul_2014_midnight; |
| 351 EXPECT_TRUE(base::Time::FromLocalExploded(exploded_expected_time, |
| 352 &first_jul_2014_midnight)); |
| 353 EXPECT_EQ(first_jul_2014_midnight, options.begin_time); |
| 354 } |
| 355 |
| 268 // Tests that BrowsingHistoryHandler observes WebHistoryService deletions. | 356 // Tests that BrowsingHistoryHandler observes WebHistoryService deletions. |
| 269 TEST_F(BrowsingHistoryHandlerTest, ObservingWebHistoryDeletions) { | 357 TEST_F(BrowsingHistoryHandlerTest, ObservingWebHistoryDeletions) { |
| 270 base::Callback<void(bool)> callback = base::Bind(&IgnoreBoolAndDoNothing); | 358 base::Callback<void(bool)> callback = base::Bind(&IgnoreBoolAndDoNothing); |
| 271 | 359 |
| 272 // BrowsingHistoryHandler listens to WebHistoryService history deletions. | 360 // BrowsingHistoryHandler listens to WebHistoryService history deletions. |
| 273 { | 361 { |
| 274 sync_service()->SetSyncActive(true); | 362 sync_service()->SetSyncActive(true); |
| 275 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui()); | 363 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui()); |
| 276 handler.RegisterMessages(); | 364 handler.RegisterMessages(); |
| 277 | 365 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 const base::DictionaryValue* first_entry; | 449 const base::DictionaryValue* first_entry; |
| 362 ASSERT_TRUE(arg2->GetDictionary(0, &first_entry)); | 450 ASSERT_TRUE(arg2->GetDictionary(0, &first_entry)); |
| 363 | 451 |
| 364 base::string16 title; | 452 base::string16 title; |
| 365 ASSERT_TRUE(first_entry->GetString("title", &title)); | 453 ASSERT_TRUE(first_entry->GetString("title", &title)); |
| 366 | 454 |
| 367 ASSERT_EQ(0u, title.find(base::ASCIIToUTF16("http://loooo"))); | 455 ASSERT_EQ(0u, title.find(base::ASCIIToUTF16("http://loooo"))); |
| 368 EXPECT_EQ(300u, title.size()); | 456 EXPECT_EQ(300u, title.size()); |
| 369 } | 457 } |
| 370 #endif | 458 #endif |
| OLD | NEW |