Chromium Code Reviews| Index: chrome/browser/ui/webui/browsing_history_handler_unittest.cc |
| diff --git a/chrome/browser/ui/webui/browsing_history_handler_unittest.cc b/chrome/browser/ui/webui/browsing_history_handler_unittest.cc |
| index 0d026cb40f5f189bf942fcc3426bed5bfca3ecf2..8a02b126d04280c5a4f21a267cda8cb7861f66a0 100644 |
| --- a/chrome/browser/ui/webui/browsing_history_handler_unittest.cc |
| +++ b/chrome/browser/ui/webui/browsing_history_handler_unittest.cc |
| @@ -11,6 +11,7 @@ |
| #include "base/macros.h" |
| #include "base/memory/ptr_util.h" |
| #include "base/strings/utf_string_conversions.h" |
| +#include "base/test/simple_test_clock.h" |
| #include "base/values.h" |
| #include "chrome/browser/history/web_history_service_factory.h" |
| #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" |
| @@ -48,6 +49,23 @@ struct TestResult { |
| // baseline time in local time. |
| const base::Time baseline_time = base::Time::UnixEpoch().LocalMidnight(); |
| +base::Time GetReferenceTime() { |
|
Dan Beam
2016/11/10 19:06:37
nit: PretendNow?
calamity
2016/11/17 02:24:34
Done.
|
| + base::Time::Exploded exploded_reference_time; |
| + exploded_reference_time.year = 2015; |
| + exploded_reference_time.month = 1; |
| + exploded_reference_time.day_of_month = 2; |
| + exploded_reference_time.day_of_week = 5; |
| + exploded_reference_time.hour = 11; |
| + exploded_reference_time.minute = 0; |
| + exploded_reference_time.second = 0; |
| + exploded_reference_time.millisecond = 0; |
| + |
| + base::Time out_time; |
| + EXPECT_TRUE( |
| + base::Time::FromLocalExploded(exploded_reference_time, &out_time)); |
| + return out_time; |
| +} |
| + |
| // For each item in |results|, create a new Value representing the visit, and |
| // insert it into |list_value|. |
| void AddQueryResults( |
| @@ -102,10 +120,17 @@ class TestSyncService : public browser_sync::TestProfileSyncService { |
| class BrowsingHistoryHandlerWithWebUIForTesting |
| : public BrowsingHistoryHandler { |
| public: |
| - explicit BrowsingHistoryHandlerWithWebUIForTesting(content::WebUI* web_ui) { |
| + explicit BrowsingHistoryHandlerWithWebUIForTesting(content::WebUI* web_ui) |
| + : test_clock_(new base::SimpleTestClock()) { |
| + set_clock(base::WrapUnique(test_clock_)); |
| set_web_ui(web_ui); |
| } |
| using BrowsingHistoryHandler::QueryComplete; |
| + |
| + base::SimpleTestClock* test_clock() { return test_clock_; } |
| + |
| + private: |
| + base::SimpleTestClock* test_clock_; |
| }; |
| } // namespace |
| @@ -265,6 +290,66 @@ TEST_F(BrowsingHistoryHandlerTest, MAYBE_MergeDuplicateResults) { |
| } |
| } |
| +TEST_F(BrowsingHistoryHandlerTest, SetQueryTimeInWeeks) { |
| + BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui()); |
| + handler.test_clock()->SetNow(GetReferenceTime()); |
| + |
| + history::QueryOptions options; |
| + 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.
|
| + |
| + // Querying this week should result in end time being midnight tonight and |
| + // begin time being midnight a week ago. |
| + handler.SetQueryTimeInWeeks(0, &options); |
| + expected_time = expected_time.LocalMidnight() + base::TimeDelta::FromDays(1); |
| + EXPECT_EQ(expected_time, options.end_time); |
| + expected_time -= base::TimeDelta::FromDays(7); |
| + EXPECT_EQ(expected_time, options.begin_time); |
| + |
| + // Querying 4 weeks ago should result in end time being midnight 4 weeks ago |
| + // and begin time being midnight 5 weeks ago. |
| + handler.SetQueryTimeInWeeks(4, &options); |
| + expected_time = GetReferenceTime() - base::TimeDelta::FromDays(28); |
| + EXPECT_EQ(expected_time, options.end_time); |
| + expected_time -= base::TimeDelta::FromDays(7); |
| + EXPECT_EQ(expected_time, options.begin_time); |
| +} |
| + |
| +TEST_F(BrowsingHistoryHandlerTest, SetQueryTimeInMonths) { |
| + BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui()); |
| + handler.test_clock()->SetNow(GetReferenceTime()); |
| + history::QueryOptions options; |
| + |
| + base::Time::Exploded exploded_expected_time; |
| + GetReferenceTime().LocalExplode(&exploded_expected_time); |
| + |
| + base::Time expected_time; |
| + |
| + // Querying this month should result in end time being unset and begin time |
| + // being midnight of the 1st. |
| + handler.SetQueryTimeInMonths(0, &options); |
| + EXPECT_EQ(base::Time(), options.end_time); |
| + |
| + exploded_expected_time.day_of_month = 1; |
| + exploded_expected_time.hour = 0; |
| + EXPECT_TRUE( |
| + base::Time::FromLocalExploded(exploded_expected_time, &expected_time)); |
| + EXPECT_EQ(expected_time, options.begin_time); |
| + |
| + // Querying 6 months ago should result in end time being 2014-08-01 and begin |
| + // time being 2014-07-01. |
| + handler.SetQueryTimeInMonths(6, &options); |
| + |
| + exploded_expected_time.year = 2014; |
| + exploded_expected_time.month = 8; |
| + EXPECT_TRUE( |
| + base::Time::FromLocalExploded(exploded_expected_time, &expected_time)); |
| + EXPECT_EQ(expected_time, options.end_time); |
| + exploded_expected_time.month = 7; |
| + EXPECT_TRUE( |
| + base::Time::FromLocalExploded(exploded_expected_time, &expected_time)); |
| + EXPECT_EQ(expected_time, options.begin_time); |
| +} |
| + |
| // Tests that BrowsingHistoryHandler observes WebHistoryService deletions. |
| TEST_F(BrowsingHistoryHandlerTest, ObservingWebHistoryDeletions) { |
| base::Callback<void(bool)> callback = base::Bind(&IgnoreBoolAndDoNothing); |