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 <set> | |
| 8 | 9 |
| 9 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" | |
| 10 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "chrome/browser/history/web_history_service_factory.h" | |
| 14 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" | |
| 15 #include "chrome/browser/signin/fake_signin_manager_builder.h" | |
| 16 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 17 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 18 #include "chrome/browser/sync/profile_sync_service_factory.h" | |
| 19 #include "chrome/browser/sync/profile_sync_test_util.h" | |
| 20 #include "chrome/test/base/testing_profile.h" | |
| 21 #include "components/history/core/test/fake_web_history_service.h" | |
| 22 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | |
| 23 #include "components/signin/core/browser/fake_signin_manager.h" | |
| 24 #include "components/signin/core/browser/test_signin_client.h" | |
| 25 #include "components/sync/base/model_type.h" | |
| 26 #include "components/sync/driver/fake_sync_service.h" | |
| 27 #include "content/public/browser/browser_thread.h" | |
| 28 #include "content/public/browser/web_contents.h" | |
| 29 #include "content/public/test/test_browser_thread_bundle.h" | |
| 30 #include "content/public/test/test_web_ui.h" | |
| 31 #include "net/http/http_status_code.h" | |
| 32 #include "net/url_request/url_request_test_util.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| 34 #include "url/gurl.h" | |
| 12 | 35 |
| 13 namespace { | 36 namespace { |
| 14 | 37 |
| 15 struct TestResult { | 38 struct TestResult { |
| 16 std::string url; | 39 std::string url; |
| 17 int64_t hour_offset; // Visit time in hours past the baseline time. | 40 int64_t hour_offset; // Visit time in hours past the baseline time. |
| 18 }; | 41 }; |
| 19 | 42 |
| 20 // Duplicates on the same day in the local timezone are removed, so set a | 43 // Duplicates on the same day in the local timezone are removed, so set a |
| 21 // baseline time in local time. | 44 // baseline time in local time. |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 41 // otherwise returns false. | 64 // otherwise returns false. |
| 42 bool ResultEquals( | 65 bool ResultEquals( |
| 43 const BrowsingHistoryHandler::HistoryEntry& result, | 66 const BrowsingHistoryHandler::HistoryEntry& result, |
| 44 const TestResult& correct_result) { | 67 const TestResult& correct_result) { |
| 45 base::Time correct_time = | 68 base::Time correct_time = |
| 46 baseline_time + base::TimeDelta::FromHours(correct_result.hour_offset); | 69 baseline_time + base::TimeDelta::FromHours(correct_result.hour_offset); |
| 47 | 70 |
| 48 return result.time == correct_time && result.url == GURL(correct_result.url); | 71 return result.time == correct_time && result.url == GURL(correct_result.url); |
| 49 } | 72 } |
| 50 | 73 |
| 74 void IgnoreBoolAndDoNothing(bool ignored_argument) {} | |
| 75 | |
| 76 class TestSyncService : public ProfileSyncService { | |
| 77 public: | |
| 78 explicit TestSyncService(Profile* profile) | |
| 79 : ProfileSyncService(CreateProfileSyncServiceParamsForTest(profile)), | |
| 80 sync_active_(false) {} | |
| 81 | |
| 82 bool IsSyncActive() const override { return true; } | |
| 83 | |
| 84 syncer::ModelTypeSet GetActiveDataTypes() const override { | |
| 85 return syncer::ModelTypeSet::All(); | |
| 86 } | |
| 87 | |
| 88 void set_sync_active(bool active) { sync_active_ = active; } | |
| 89 | |
| 90 private: | |
| 91 bool sync_active_; | |
|
tsergeant
2016/08/24 08:47:04
sync_active_ doesn't appear to be actually used an
msramek
2016/08/24 12:16:07
Thanks for spotting - I forgot the hardcoded "true
| |
| 92 }; | |
| 93 | |
| 94 class BrowsingHistoryHandlerWithWebUIForTesting | |
| 95 : public BrowsingHistoryHandler { | |
| 96 public: | |
| 97 explicit BrowsingHistoryHandlerWithWebUIForTesting(content::WebUI* web_ui) { | |
| 98 set_web_ui(web_ui); | |
| 99 } | |
| 100 }; | |
| 101 | |
| 51 } // namespace | 102 } // namespace |
| 52 | 103 |
| 104 class BrowsingHistoryHandlerTest : public ::testing::Test { | |
| 105 public: | |
| 106 void SetUp() override { | |
| 107 TestingProfile::Builder builder; | |
| 108 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), | |
| 109 &BuildFakeProfileOAuth2TokenService); | |
| 110 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), | |
| 111 &BuildFakeSigninManagerBase); | |
| 112 builder.AddTestingFactory(ProfileSyncServiceFactory::GetInstance(), | |
| 113 &BuildFakeSyncService); | |
| 114 builder.AddTestingFactory(WebHistoryServiceFactory::GetInstance(), | |
| 115 &BuildFakeWebHistoryService); | |
| 116 profile_ = builder.Build(); | |
| 117 | |
| 118 sync_service_ = static_cast<TestSyncService*>( | |
| 119 ProfileSyncServiceFactory::GetForProfile(profile_.get())); | |
| 120 web_history_service_ = static_cast<history::FakeWebHistoryService*>( | |
| 121 WebHistoryServiceFactory::GetForProfile(profile_.get())); | |
| 122 web_history_service_->SetupFakeResponse(true /* success */, net::HTTP_OK); | |
| 123 | |
| 124 web_contents_.reset(content::WebContents::Create( | |
| 125 content::WebContents::CreateParams(profile_.get()))); | |
| 126 web_ui_.reset(new content::TestWebUI); | |
| 127 web_ui_->set_web_contents(web_contents_.get()); | |
| 128 } | |
| 129 | |
| 130 void TearDown() override { | |
| 131 web_contents_.reset(); | |
| 132 web_ui_.reset(); | |
| 133 profile_.reset(); | |
| 134 } | |
| 135 | |
| 136 Profile* profile() { return profile_.get(); } | |
| 137 TestSyncService* sync_service() { return sync_service_; } | |
| 138 history::WebHistoryService* web_history_service() { | |
| 139 return web_history_service_; | |
| 140 } | |
| 141 content::TestWebUI* web_ui() { return web_ui_.get(); } | |
| 142 | |
| 143 private: | |
| 144 static std::unique_ptr<KeyedService> BuildFakeSyncService( | |
| 145 content::BrowserContext* context) { | |
| 146 return base::MakeUnique<TestSyncService>( | |
| 147 static_cast<TestingProfile*>(context)); | |
| 148 } | |
| 149 | |
| 150 static std::unique_ptr<KeyedService> BuildFakeWebHistoryService( | |
| 151 content::BrowserContext* context) { | |
| 152 Profile* profile = static_cast<TestingProfile*>(context); | |
| 153 | |
| 154 return base::MakeUnique<history::FakeWebHistoryService>( | |
| 155 ProfileOAuth2TokenServiceFactory::GetForProfile(profile), | |
| 156 SigninManagerFactory::GetForProfile(profile), | |
| 157 profile->GetRequestContext()); | |
| 158 } | |
| 159 | |
| 160 content::TestBrowserThreadBundle thread_bundle_; | |
| 161 std::unique_ptr<TestingProfile> profile_; | |
| 162 TestSyncService* sync_service_; | |
| 163 history::FakeWebHistoryService* web_history_service_; | |
| 164 std::unique_ptr<content::TestWebUI> web_ui_; | |
| 165 std::unique_ptr<content::WebContents> web_contents_; | |
| 166 }; | |
| 167 | |
| 53 // Tests that the MergeDuplicateResults method correctly removes duplicate | 168 // Tests that the MergeDuplicateResults method correctly removes duplicate |
| 54 // visits to the same URL on the same day. | 169 // visits to the same URL on the same day. |
| 55 // Fails on Android. http://crbug.com/2345 | 170 // Fails on Android. http://crbug.com/2345 |
| 56 #if defined(OS_ANDROID) | 171 #if defined(OS_ANDROID) |
| 57 #define MAYBE_MergeDuplicateResults DISABLED_MergeDuplicateResults | 172 #define MAYBE_MergeDuplicateResults DISABLED_MergeDuplicateResults |
| 58 #else | 173 #else |
| 59 #define MAYBE_MergeDuplicateResults MergeDuplicateResults | 174 #define MAYBE_MergeDuplicateResults MergeDuplicateResults |
| 60 #endif | 175 #endif |
| 61 TEST(BrowsingHistoryHandlerTest, MAYBE_MergeDuplicateResults) { | 176 TEST_F(BrowsingHistoryHandlerTest, MAYBE_MergeDuplicateResults) { |
| 62 { | 177 { |
| 63 // Basic test that duplicates on the same day are removed. | 178 // Basic test that duplicates on the same day are removed. |
| 64 TestResult test_data[] = { | 179 TestResult test_data[] = { |
| 65 { "http://google.com", 0 }, | 180 { "http://google.com", 0 }, |
| 66 { "http://google.de", 1 }, | 181 { "http://google.de", 1 }, |
| 67 { "http://google.com", 2 }, | 182 { "http://google.com", 2 }, |
| 68 { "http://google.com", 3 } // Most recent. | 183 { "http://google.com", 3 } // Most recent. |
| 69 }; | 184 }; |
| 70 std::vector<BrowsingHistoryHandler::HistoryEntry> results; | 185 std::vector<BrowsingHistoryHandler::HistoryEntry> results; |
| 71 AddQueryResults(test_data, arraysize(test_data), &results); | 186 AddQueryResults(test_data, arraysize(test_data), &results); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 AddQueryResults(test_data, arraysize(test_data), &results); | 245 AddQueryResults(test_data, arraysize(test_data), &results); |
| 131 BrowsingHistoryHandler::MergeDuplicateResults(&results); | 246 BrowsingHistoryHandler::MergeDuplicateResults(&results); |
| 132 | 247 |
| 133 ASSERT_EQ(2U, results.size()); | 248 ASSERT_EQ(2U, results.size()); |
| 134 EXPECT_TRUE(ResultEquals(results[0], test_data[3])); | 249 EXPECT_TRUE(ResultEquals(results[0], test_data[3])); |
| 135 EXPECT_TRUE(ResultEquals(results[1], test_data[1])); | 250 EXPECT_TRUE(ResultEquals(results[1], test_data[1])); |
| 136 EXPECT_EQ(3u, results[0].all_timestamps.size()); | 251 EXPECT_EQ(3u, results[0].all_timestamps.size()); |
| 137 EXPECT_EQ(1u, results[1].all_timestamps.size()); | 252 EXPECT_EQ(1u, results[1].all_timestamps.size()); |
| 138 } | 253 } |
| 139 } | 254 } |
| 255 | |
| 256 // Tests that BrowsingHistoryHandler observes WebHistoryService deletions. | |
| 257 TEST_F(BrowsingHistoryHandlerTest, ObservingWebHistoryDeletions) { | |
| 258 base::Callback<void(bool)> callback = base::Bind(&IgnoreBoolAndDoNothing); | |
| 259 | |
| 260 // BrowsingHistoryHandler listens to WebHistoryService history deletions. | |
| 261 { | |
| 262 sync_service()->set_sync_active(true); | |
| 263 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui()); | |
| 264 handler.RegisterMessages(); | |
| 265 | |
| 266 web_history_service()->ExpireHistoryBetween(std::set<GURL>(), base::Time(), | |
| 267 base::Time::Max(), callback); | |
| 268 | |
| 269 EXPECT_EQ(1U, web_ui()->call_data().size()); | |
| 270 EXPECT_EQ("historyDeleted", web_ui()->call_data().back()->function_name()); | |
| 271 } | |
| 272 | |
| 273 // BrowsingHistoryHandler will listen to WebHistoryService deletions even if | |
| 274 // history sync is activated later. | |
| 275 { | |
| 276 sync_service()->set_sync_active(false); | |
| 277 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui()); | |
| 278 handler.RegisterMessages(); | |
| 279 sync_service()->set_sync_active(true); | |
| 280 | |
| 281 web_history_service()->ExpireHistoryBetween(std::set<GURL>(), base::Time(), | |
| 282 base::Time::Max(), callback); | |
| 283 | |
| 284 EXPECT_EQ(2U, web_ui()->call_data().size()); | |
| 285 EXPECT_EQ("historyDeleted", web_ui()->call_data().back()->function_name()); | |
| 286 } | |
| 287 } | |
| OLD | NEW |