Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(564)

Side by Side Diff: chrome/browser/browsing_data/history_counter_browsertest.cc

Issue 2855623005: Move browsing_data counter sync tests to separate file (Closed)
Patch Set: fixes Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2015 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 "components/browsing_data/core/counters/history_counter.h" 5 #include "components/browsing_data/core/counters/history_counter.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "chrome/browser/history/history_service_factory.h" 10 #include "chrome/browser/history/history_service_factory.h"
11 #include "chrome/browser/history/web_history_service_factory.h" 11 #include "chrome/browser/history/web_history_service_factory.h"
12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
13 #include "chrome/browser/signin/signin_manager_factory.h" 13 #include "chrome/browser/signin/signin_manager_factory.h"
14 #include "chrome/browser/sync/profile_sync_service_factory.h" 14 #include "chrome/browser/sync/profile_sync_service_factory.h"
15 #include "chrome/browser/sync/test/integration/sync_test.h"
16 #include "chrome/browser/ui/browser.h" 15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/test/base/in_process_browser_test.h"
17 #include "components/browser_sync/profile_sync_service.h" 17 #include "components/browser_sync/profile_sync_service.h"
18 #include "components/browsing_data/core/browsing_data_utils.h" 18 #include "components/browsing_data/core/browsing_data_utils.h"
19 #include "components/browsing_data/core/pref_names.h" 19 #include "components/browsing_data/core/pref_names.h"
20 #include "components/history/core/browser/history_service.h" 20 #include "components/history/core/browser/history_service.h"
21 #include "components/history/core/browser/web_history_service.h" 21 #include "components/history/core/browser/web_history_service.h"
22 #include "components/history/core/test/fake_web_history_service.h" 22 #include "components/history/core/test/fake_web_history_service.h"
23 #include "components/prefs/pref_service.h" 23 #include "components/prefs/pref_service.h"
24 #include "components/signin/core/browser/profile_oauth2_token_service.h" 24 #include "components/signin/core/browser/profile_oauth2_token_service.h"
25 #include "components/signin/core/browser/signin_manager.h" 25 #include "components/signin/core/browser/signin_manager.h"
26 #include "net/http/http_status_code.h" 26 #include "net/http/http_status_code.h"
27 #include "url/gurl.h" 27 #include "url/gurl.h"
28 28
29 namespace { 29 namespace {
30 30
31 class HistoryCounterTest : public SyncTest { 31 using browsing_data::BrowsingDataCounter;
32 using browsing_data::HistoryCounter;
33
34 class HistoryCounterTest : public InProcessBrowserTest {
32 public: 35 public:
33 HistoryCounterTest() : SyncTest(SINGLE_CLIENT) { 36 HistoryCounterTest() {}
34 // TODO(msramek): Only one of the test cases, RestartOnSyncChange, is a Sync
35 // integration test. Extract it and move it to the rest of integration tests
36 // in chrome/browser/sync/test/integration/. Change this class back to
37 // InProcessBrowserTest.
38 }
39
40 ~HistoryCounterTest() override {}; 37 ~HistoryCounterTest() override {};
41 38
42 void SetUpOnMainThread() override { 39 void SetUpOnMainThread() override {
43 time_ = base::Time::Now(); 40 time_ = base::Time::Now();
44 history_service_ = HistoryServiceFactory::GetForProfileWithoutCreating( 41 history_service_ = HistoryServiceFactory::GetForProfileWithoutCreating(
45 browser()->profile()); 42 browser()->profile());
46 fake_web_history_service_.reset(new history::FakeWebHistoryService( 43 fake_web_history_service_.reset(new history::FakeWebHistoryService(
47 ProfileOAuth2TokenServiceFactory::GetForProfile(browser()->profile()), 44 ProfileOAuth2TokenServiceFactory::GetForProfile(browser()->profile()),
48 SigninManagerFactory::GetForProfile(browser()->profile()), 45 SigninManagerFactory::GetForProfile(browser()->profile()),
49 browser()->profile()->GetRequestContext())); 46 browser()->profile()->GetRequestContext()));
(...skipping 22 matching lines...) Expand all
72 void SetDeletionPeriodPref(browsing_data::TimePeriod period) { 69 void SetDeletionPeriodPref(browsing_data::TimePeriod period) {
73 browser()->profile()->GetPrefs()->SetInteger( 70 browser()->profile()->GetPrefs()->SetInteger(
74 browsing_data::prefs::kDeleteTimePeriod, static_cast<int>(period)); 71 browsing_data::prefs::kDeleteTimePeriod, static_cast<int>(period));
75 } 72 }
76 73
77 void WaitForCounting() { 74 void WaitForCounting() {
78 run_loop_.reset(new base::RunLoop()); 75 run_loop_.reset(new base::RunLoop());
79 run_loop_->Run(); 76 run_loop_->Run();
80 } 77 }
81 78
82 browsing_data::BrowsingDataCounter::ResultInt GetLocalResult() { 79 BrowsingDataCounter::ResultInt GetLocalResult() {
83 DCHECK(finished_); 80 DCHECK(finished_);
84 return local_result_; 81 return local_result_;
85 } 82 }
86 83
87 bool HasSyncedVisits() { 84 bool HasSyncedVisits() {
88 DCHECK(finished_); 85 DCHECK(finished_);
89 return has_synced_visits_; 86 return has_synced_visits_;
90 } 87 }
91 88
92 void Callback( 89 void Callback(std::unique_ptr<BrowsingDataCounter::Result> result) {
93 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) {
94 finished_ = result->Finished(); 90 finished_ = result->Finished();
95 91
96 if (finished_) { 92 if (finished_) {
97 browsing_data::HistoryCounter::HistoryResult* history_result = 93 auto* history_result =
98 static_cast<browsing_data::HistoryCounter::HistoryResult*>( 94 static_cast<HistoryCounter::HistoryResult*>(result.get());
99 result.get());
100 95
101 local_result_ = history_result->Value(); 96 local_result_ = history_result->Value();
102 has_synced_visits_ = history_result->has_synced_visits(); 97 has_synced_visits_ = history_result->has_synced_visits();
103 } 98 }
104 99
105 if (run_loop_ && finished_) 100 if (run_loop_ && finished_)
106 run_loop_->Quit(); 101 run_loop_->Quit();
107 } 102 }
108 103
109 bool CountingFinishedSinceLastAsked() {
110 bool result = finished_;
111 finished_ = false;
112 return result;
113 }
114
115 void WaitForCountingOrConfirmFinished() {
116 if (CountingFinishedSinceLastAsked())
117 return;
118
119 WaitForCounting();
120 CountingFinishedSinceLastAsked();
121 }
122
123 history::WebHistoryService* GetFakeWebHistoryService(Profile* profile, 104 history::WebHistoryService* GetFakeWebHistoryService(Profile* profile,
124 bool check_sync_status) { 105 bool check_sync_status) {
125 // |check_sync_status| is true when the factory should check if 106 // |check_sync_status| is true when the factory should check if
126 // history sync is enabled. 107 // history sync is enabled.
127 if (!check_sync_status || 108 if (!check_sync_status ||
128 WebHistoryServiceFactory::GetForProfile(profile)) { 109 WebHistoryServiceFactory::GetForProfile(profile)) {
129 return fake_web_history_service_.get(); 110 return fake_web_history_service_.get();
130 } 111 }
131 return nullptr; 112 return nullptr;
132 } 113 }
133 114
134 history::WebHistoryService* GetRealWebHistoryService(Profile* profile) { 115 history::WebHistoryService* GetRealWebHistoryService(Profile* profile) {
135 return WebHistoryServiceFactory::GetForProfile(profile); 116 return WebHistoryServiceFactory::GetForProfile(profile);
136 } 117 }
137 118
138 history::HistoryService* GetHistoryService() { return history_service_; } 119 history::HistoryService* GetHistoryService() { return history_service_; }
139 120
140 private: 121 private:
141 std::unique_ptr<base::RunLoop> run_loop_; 122 std::unique_ptr<base::RunLoop> run_loop_;
142 history::HistoryService* history_service_; 123 history::HistoryService* history_service_;
143 std::unique_ptr<history::FakeWebHistoryService> fake_web_history_service_; 124 std::unique_ptr<history::FakeWebHistoryService> fake_web_history_service_;
144 base::Time time_; 125 base::Time time_;
145 126
146 bool finished_; 127 bool finished_;
147 browsing_data::BrowsingDataCounter::ResultInt local_result_; 128 BrowsingDataCounter::ResultInt local_result_;
148 bool has_synced_visits_; 129 bool has_synced_visits_;
149 }; 130 };
150 131
151 // Tests that the counter considers duplicate visits from the same day 132 // Tests that the counter considers duplicate visits from the same day
152 // to be a single item. 133 // to be a single item.
153 IN_PROC_BROWSER_TEST_F(HistoryCounterTest, DuplicateVisits) { 134 IN_PROC_BROWSER_TEST_F(HistoryCounterTest, DuplicateVisits) {
154 AddVisit("https://www.google.com"); // 1 item 135 AddVisit("https://www.google.com"); // 1 item
155 AddVisit("https://www.google.com"); 136 AddVisit("https://www.google.com");
156 AddVisit("https://www.chrome.com"); // 2 items 137 AddVisit("https://www.chrome.com"); // 2 items
157 AddVisit("https://www.chrome.com"); 138 AddVisit("https://www.chrome.com");
158 AddVisit("https://www.chrome.com"); 139 AddVisit("https://www.chrome.com");
159 AddVisit("https://www.example.com"); // 3 items 140 AddVisit("https://www.example.com"); // 3 items
160 141
161 RevertTimeInDays(1); 142 RevertTimeInDays(1);
162 AddVisit("https://www.google.com"); // 4 items 143 AddVisit("https://www.google.com"); // 4 items
163 AddVisit("https://www.example.com"); // 5 items 144 AddVisit("https://www.example.com"); // 5 items
164 AddVisit("https://www.example.com"); 145 AddVisit("https://www.example.com");
165 146
166 RevertTimeInDays(1); 147 RevertTimeInDays(1);
167 AddVisit("https://www.chrome.com"); // 6 items 148 AddVisit("https://www.chrome.com"); // 6 items
168 AddVisit("https://www.chrome.com"); 149 AddVisit("https://www.chrome.com");
169 AddVisit("https://www.google.com"); // 7 items 150 AddVisit("https://www.google.com"); // 7 items
170 AddVisit("https://www.chrome.com"); 151 AddVisit("https://www.chrome.com");
171 AddVisit("https://www.google.com"); 152 AddVisit("https://www.google.com");
172 AddVisit("https://www.google.com"); 153 AddVisit("https://www.google.com");
173 AddVisit("https://www.chrome.com"); 154 AddVisit("https://www.chrome.com");
174 155
175 Profile* profile = browser()->profile(); 156 Profile* profile = browser()->profile();
176 157
177 browsing_data::HistoryCounter counter( 158 HistoryCounter counter(
178 GetHistoryService(), 159 GetHistoryService(),
179 base::Bind(&HistoryCounterTest::GetRealWebHistoryService, 160 base::Bind(&HistoryCounterTest::GetRealWebHistoryService,
180 base::Unretained(this), 161 base::Unretained(this), base::Unretained(profile)),
181 base::Unretained(profile)),
182 ProfileSyncServiceFactory::GetForProfile(profile)); 162 ProfileSyncServiceFactory::GetForProfile(profile));
183 163
184 counter.Init( 164 counter.Init(
185 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, 165 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
186 base::Bind(&HistoryCounterTest::Callback, base::Unretained(this))); 166 base::Bind(&HistoryCounterTest::Callback, base::Unretained(this)));
187 counter.Restart(); 167 counter.Restart();
188 168
189 WaitForCounting(); 169 WaitForCounting();
190 EXPECT_EQ(7u, GetLocalResult()); 170 EXPECT_EQ(7u, GetLocalResult());
191 } 171 }
192 172
193 // Tests that the counter starts counting automatically when the deletion 173 // Tests that the counter starts counting automatically when the deletion
194 // pref changes to true. 174 // pref changes to true.
195 IN_PROC_BROWSER_TEST_F(HistoryCounterTest, PrefChanged) { 175 IN_PROC_BROWSER_TEST_F(HistoryCounterTest, PrefChanged) {
196 SetHistoryDeletionPref(false); 176 SetHistoryDeletionPref(false);
197 AddVisit("https://www.google.com"); 177 AddVisit("https://www.google.com");
198 AddVisit("https://www.chrome.com"); 178 AddVisit("https://www.chrome.com");
199 179
200 Profile* profile = browser()->profile(); 180 Profile* profile = browser()->profile();
201 181
202 browsing_data::HistoryCounter counter( 182 HistoryCounter counter(
203 GetHistoryService(), 183 GetHistoryService(),
204 base::Bind(&HistoryCounterTest::GetRealWebHistoryService, 184 base::Bind(&HistoryCounterTest::GetRealWebHistoryService,
205 base::Unretained(this), 185 base::Unretained(this), base::Unretained(profile)),
206 base::Unretained(profile)),
207 ProfileSyncServiceFactory::GetForProfile(profile)); 186 ProfileSyncServiceFactory::GetForProfile(profile));
208 187
209 counter.Init( 188 counter.Init(
210 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, 189 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
211 base::Bind(&HistoryCounterTest::Callback, base::Unretained(this))); 190 base::Bind(&HistoryCounterTest::Callback, base::Unretained(this)));
212 SetHistoryDeletionPref(true); 191 SetHistoryDeletionPref(true);
213 192
214 WaitForCounting(); 193 WaitForCounting();
215 EXPECT_EQ(2u, GetLocalResult()); 194 EXPECT_EQ(2u, GetLocalResult());
216 } 195 }
(...skipping 17 matching lines...) Expand all
234 AddVisit("https://www.chrome.com"); 213 AddVisit("https://www.chrome.com");
235 AddVisit("https://www.example.com"); 214 AddVisit("https://www.example.com");
236 215
237 RevertTimeInDays(10); 216 RevertTimeInDays(10);
238 AddVisit("https://www.example.com"); 217 AddVisit("https://www.example.com");
239 AddVisit("https://www.example.com"); 218 AddVisit("https://www.example.com");
240 AddVisit("https://www.example.com"); 219 AddVisit("https://www.example.com");
241 220
242 Profile* profile = browser()->profile(); 221 Profile* profile = browser()->profile();
243 222
244 browsing_data::HistoryCounter counter( 223 HistoryCounter counter(
245 GetHistoryService(), 224 GetHistoryService(),
246 base::Bind(&HistoryCounterTest::GetRealWebHistoryService, 225 base::Bind(&HistoryCounterTest::GetRealWebHistoryService,
247 base::Unretained(this), 226 base::Unretained(this), base::Unretained(profile)),
248 base::Unretained(profile)),
249 ProfileSyncServiceFactory::GetForProfile(profile)); 227 ProfileSyncServiceFactory::GetForProfile(profile));
250 228
251 counter.Init( 229 counter.Init(
252 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, 230 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
253 base::Bind(&HistoryCounterTest::Callback, base::Unretained(this))); 231 base::Bind(&HistoryCounterTest::Callback, base::Unretained(this)));
254 232
255 SetDeletionPeriodPref(browsing_data::TimePeriod::LAST_HOUR); 233 SetDeletionPeriodPref(browsing_data::TimePeriod::LAST_HOUR);
256 WaitForCounting(); 234 WaitForCounting();
257 EXPECT_EQ(1u, GetLocalResult()); 235 EXPECT_EQ(1u, GetLocalResult());
258 236
(...skipping 13 matching lines...) Expand all
272 WaitForCounting(); 250 WaitForCounting();
273 EXPECT_EQ(9u, GetLocalResult()); 251 EXPECT_EQ(9u, GetLocalResult());
274 } 252 }
275 253
276 // Test the behavior for a profile that syncs history. 254 // Test the behavior for a profile that syncs history.
277 IN_PROC_BROWSER_TEST_F(HistoryCounterTest, Synced) { 255 IN_PROC_BROWSER_TEST_F(HistoryCounterTest, Synced) {
278 // WebHistoryService makes network requests, so we need to use a fake one 256 // WebHistoryService makes network requests, so we need to use a fake one
279 // for testing. 257 // for testing.
280 Profile* profile = browser()->profile(); 258 Profile* profile = browser()->profile();
281 259
282 browsing_data::HistoryCounter counter( 260 HistoryCounter counter(
283 GetHistoryService(), 261 GetHistoryService(),
284 base::Bind(&HistoryCounterTest::GetFakeWebHistoryService, 262 base::Bind(&HistoryCounterTest::GetFakeWebHistoryService,
285 base::Unretained(this), 263 base::Unretained(this), base::Unretained(profile), false),
286 base::Unretained(profile),
287 false),
288 ProfileSyncServiceFactory::GetForProfile(profile)); 264 ProfileSyncServiceFactory::GetForProfile(profile));
289 265
290 counter.Init( 266 counter.Init(
291 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, 267 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
292 base::Bind(&HistoryCounterTest::Callback, base::Unretained(this))); 268 base::Bind(&HistoryCounterTest::Callback, base::Unretained(this)));
293 269
294 history::FakeWebHistoryService* service = 270 history::FakeWebHistoryService* service =
295 static_cast<history::FakeWebHistoryService*>(GetFakeWebHistoryService( 271 static_cast<history::FakeWebHistoryService*>(GetFakeWebHistoryService(
296 profile, false)); 272 profile, false));
297 273
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 327
352 // Nonzero local count, empty sync. 328 // Nonzero local count, empty sync.
353 service->ClearSyncedVisits(); 329 service->ClearSyncedVisits();
354 service->SetupFakeResponse(true /* success */, net::HTTP_OK); 330 service->SetupFakeResponse(true /* success */, net::HTTP_OK);
355 counter.Restart(); 331 counter.Restart();
356 WaitForCounting(); 332 WaitForCounting();
357 EXPECT_EQ(2u, GetLocalResult()); 333 EXPECT_EQ(2u, GetLocalResult());
358 EXPECT_FALSE(HasSyncedVisits()); 334 EXPECT_FALSE(HasSyncedVisits());
359 } 335 }
360 336
361 // Test that the counting restarts when history sync state changes.
362 // TODO(crbug.com/553421): Move this to the sync/test/integration directory?
363 IN_PROC_BROWSER_TEST_F(HistoryCounterTest, RestartOnSyncChange) {
364 // Set up the Sync client.
365 ASSERT_TRUE(SetupClients());
366 static const int kFirstProfileIndex = 0;
367 browser_sync::ProfileSyncService* sync_service =
368 GetSyncService(kFirstProfileIndex);
369 Profile* profile = GetProfile(kFirstProfileIndex);
370
371 // Set up the fake web history service and the counter.
372
373 browsing_data::HistoryCounter counter(
374 GetHistoryService(),
375 base::Bind(&HistoryCounterTest::GetFakeWebHistoryService,
376 base::Unretained(this),
377 base::Unretained(profile),
378 true),
379 sync_service);
380
381 counter.Init(
382 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
383 base::Bind(&HistoryCounterTest::Callback, base::Unretained(this)));
384
385 // Note that some Sync operations notify observers immediately (and thus there
386 // is no need to call |WaitForCounting()|; in fact, it would block the test),
387 // while other operations only post the task on UI thread's message loop
388 // (which requires calling |WaitForCounting()| for them to run). Therefore,
389 // this test always checks if the callback has already run and only waits
390 // if it has not.
391
392 // We sync all datatypes by default, so starting Sync means that we start
393 // syncing history deletion, and this should restart the counter.
394 ASSERT_TRUE(SetupSync());
395 ASSERT_TRUE(sync_service->IsSyncActive());
396 ASSERT_TRUE(sync_service->GetPreferredDataTypes().Has(
397 syncer::HISTORY_DELETE_DIRECTIVES));
398 WaitForCountingOrConfirmFinished();
399
400 // We stop syncing history deletion in particular. This restarts the counter.
401 syncer::ModelTypeSet everything_except_history =
402 syncer::UserSelectableTypes();
403 everything_except_history.Remove(syncer::HISTORY_DELETE_DIRECTIVES);
404 auto sync_blocker = sync_service->GetSetupInProgressHandle();
405 sync_service->OnUserChoseDatatypes(/*sync_everything=*/false,
406 everything_except_history);
407 sync_blocker.reset();
408 WaitForCountingOrConfirmFinished();
409
410 // If the history deletion sync is not affected, the counter is not restarted.
411 syncer::ModelTypeSet only_passwords(syncer::PASSWORDS);
412 sync_service->ChangePreferredDataTypes(only_passwords);
413 sync_blocker = sync_service->GetSetupInProgressHandle();
414 sync_service->ChangePreferredDataTypes(only_passwords);
415 sync_blocker.reset();
416 EXPECT_FALSE(counter.HasTrackedTasks());
417 EXPECT_FALSE(CountingFinishedSinceLastAsked());
418
419 // Same in this case.
420 syncer::ModelTypeSet autofill_and_passwords(
421 syncer::AUTOFILL, syncer::PASSWORDS);
422 sync_blocker = sync_service->GetSetupInProgressHandle();
423 sync_service->ChangePreferredDataTypes(autofill_and_passwords);
424 sync_blocker.reset();
425 EXPECT_FALSE(counter.HasTrackedTasks());
426 EXPECT_FALSE(CountingFinishedSinceLastAsked());
427
428 // We start syncing history deletion again. This restarts the counter.
429 sync_blocker = sync_service->GetSetupInProgressHandle();
430 sync_service->ChangePreferredDataTypes(syncer::ModelTypeSet::All());
431 sync_blocker.reset();
432 WaitForCountingOrConfirmFinished();
433
434 // Changing the syncing datatypes to another set that still includes history
435 // deletion should technically not trigger a restart, because the state of
436 // history deletion did not change. However, in reality we can get two
437 // notifications, one that history sync has stopped and another that it is
438 // active again.
439
440 // Stopping the Sync service triggers a restart.
441 sync_service->RequestStop(syncer::SyncService::CLEAR_DATA);
442 WaitForCountingOrConfirmFinished();
443 }
444
445 } // namespace 337 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698