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