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() { | |
| 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(); | |
| 299 | |
| 300 handler.SetQueryTimeInWeeks(0, &options); | |
| 301 expected_time = expected_time.LocalMidnight() + base::TimeDelta::FromDays(1); | |
| 302 EXPECT_EQ(expected_time, options.end_time); | |
| 303 expected_time -= base::TimeDelta::FromDays(7); | |
| 304 EXPECT_EQ(expected_time, options.begin_time); | |
| 305 | |
| 306 handler.SetQueryTimeInWeeks(4, &options); | |
| 307 expected_time -= base::TimeDelta::FromDays(21); | |
| 308 EXPECT_EQ(expected_time, options.end_time); | |
| 309 expected_time -= base::TimeDelta::FromDays(7); | |
| 310 EXPECT_EQ(expected_time, options.begin_time); | |
|
Dan Beam
2016/11/10 03:31:34
i don't really understand what all this is testing
calamity
2016/11/10 07:16:27
Done.
| |
| 311 } | |
| 312 | |
| 313 TEST_F(BrowsingHistoryHandlerTest, SetQueryTimeInMonths) { | |
| 314 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui()); | |
| 315 handler.test_clock()->SetNow(GetReferenceTime()); | |
| 316 history::QueryOptions options; | |
| 317 | |
| 318 base::Time::Exploded exploded_expected_time; | |
| 319 GetReferenceTime().LocalExplode(&exploded_expected_time); | |
| 320 | |
| 321 base::Time expected_time; | |
| 322 | |
| 323 handler.SetQueryTimeInMonths(0, &options); | |
| 324 EXPECT_EQ(base::Time(), options.end_time); | |
| 325 | |
| 326 exploded_expected_time.day_of_month = 1; | |
| 327 exploded_expected_time.hour = 0; | |
| 328 EXPECT_TRUE( | |
| 329 base::Time::FromLocalExploded(exploded_expected_time, &expected_time)); | |
| 330 EXPECT_EQ(expected_time, options.begin_time); | |
| 331 | |
| 332 handler.SetQueryTimeInMonths(6, &options); | |
| 333 | |
| 334 exploded_expected_time.year = 2014; | |
| 335 exploded_expected_time.month = 8; | |
| 336 EXPECT_TRUE( | |
| 337 base::Time::FromLocalExploded(exploded_expected_time, &expected_time)); | |
| 338 EXPECT_EQ(expected_time, options.end_time); | |
| 339 exploded_expected_time.month = 7; | |
| 340 EXPECT_TRUE( | |
| 341 base::Time::FromLocalExploded(exploded_expected_time, &expected_time)); | |
| 342 EXPECT_EQ(expected_time, options.begin_time); | |
| 343 } | |
| 344 | |
| 268 // Tests that BrowsingHistoryHandler observes WebHistoryService deletions. | 345 // Tests that BrowsingHistoryHandler observes WebHistoryService deletions. |
| 269 TEST_F(BrowsingHistoryHandlerTest, ObservingWebHistoryDeletions) { | 346 TEST_F(BrowsingHistoryHandlerTest, ObservingWebHistoryDeletions) { |
| 270 base::Callback<void(bool)> callback = base::Bind(&IgnoreBoolAndDoNothing); | 347 base::Callback<void(bool)> callback = base::Bind(&IgnoreBoolAndDoNothing); |
| 271 | 348 |
| 272 // BrowsingHistoryHandler listens to WebHistoryService history deletions. | 349 // BrowsingHistoryHandler listens to WebHistoryService history deletions. |
| 273 { | 350 { |
| 274 sync_service()->SetSyncActive(true); | 351 sync_service()->SetSyncActive(true); |
| 275 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui()); | 352 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui()); |
| 276 handler.RegisterMessages(); | 353 handler.RegisterMessages(); |
| 277 | 354 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 342 const base::DictionaryValue* first_entry; | 419 const base::DictionaryValue* first_entry; |
| 343 ASSERT_TRUE(arg2->GetDictionary(0, &first_entry)); | 420 ASSERT_TRUE(arg2->GetDictionary(0, &first_entry)); |
| 344 | 421 |
| 345 base::string16 title; | 422 base::string16 title; |
| 346 ASSERT_TRUE(first_entry->GetString("title", &title)); | 423 ASSERT_TRUE(first_entry->GetString("title", &title)); |
| 347 | 424 |
| 348 ASSERT_EQ(0u, title.find(base::ASCIIToUTF16("http://loooo"))); | 425 ASSERT_EQ(0u, title.find(base::ASCIIToUTF16("http://loooo"))); |
| 349 EXPECT_EQ(300u, title.size()); | 426 EXPECT_EQ(300u, title.size()); |
| 350 } | 427 } |
| 351 #endif | 428 #endif |
| OLD | NEW |