OLD | NEW |
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 |
| 33 class HistoryCounterTest : public InProcessBrowserTest { |
32 public: | 34 public: |
33 HistoryCounterTest() : SyncTest(SINGLE_CLIENT) { | 35 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 {}; | 36 ~HistoryCounterTest() override {}; |
41 | 37 |
42 void SetUpOnMainThread() override { | 38 void SetUpOnMainThread() override { |
43 time_ = base::Time::Now(); | 39 time_ = base::Time::Now(); |
44 history_service_ = HistoryServiceFactory::GetForProfileWithoutCreating( | 40 history_service_ = HistoryServiceFactory::GetForProfileWithoutCreating( |
45 browser()->profile()); | 41 browser()->profile()); |
46 fake_web_history_service_.reset(new history::FakeWebHistoryService( | 42 fake_web_history_service_.reset(new history::FakeWebHistoryService( |
47 ProfileOAuth2TokenServiceFactory::GetForProfile(browser()->profile()), | 43 ProfileOAuth2TokenServiceFactory::GetForProfile(browser()->profile()), |
48 SigninManagerFactory::GetForProfile(browser()->profile()), | 44 SigninManagerFactory::GetForProfile(browser()->profile()), |
49 browser()->profile()->GetRequestContext())); | 45 browser()->profile()->GetRequestContext())); |
(...skipping 22 matching lines...) Expand all Loading... |
72 void SetDeletionPeriodPref(browsing_data::TimePeriod period) { | 68 void SetDeletionPeriodPref(browsing_data::TimePeriod period) { |
73 browser()->profile()->GetPrefs()->SetInteger( | 69 browser()->profile()->GetPrefs()->SetInteger( |
74 browsing_data::prefs::kDeleteTimePeriod, static_cast<int>(period)); | 70 browsing_data::prefs::kDeleteTimePeriod, static_cast<int>(period)); |
75 } | 71 } |
76 | 72 |
77 void WaitForCounting() { | 73 void WaitForCounting() { |
78 run_loop_.reset(new base::RunLoop()); | 74 run_loop_.reset(new base::RunLoop()); |
79 run_loop_->Run(); | 75 run_loop_->Run(); |
80 } | 76 } |
81 | 77 |
82 browsing_data::BrowsingDataCounter::ResultInt GetLocalResult() { | 78 BrowsingDataCounter::ResultInt GetLocalResult() { |
83 DCHECK(finished_); | 79 DCHECK(finished_); |
84 return local_result_; | 80 return local_result_; |
85 } | 81 } |
86 | 82 |
87 bool HasSyncedVisits() { | 83 bool HasSyncedVisits() { |
88 DCHECK(finished_); | 84 DCHECK(finished_); |
89 return has_synced_visits_; | 85 return has_synced_visits_; |
90 } | 86 } |
91 | 87 |
92 void Callback( | 88 void Callback(std::unique_ptr<BrowsingDataCounter::Result> result) { |
93 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { | |
94 finished_ = result->Finished(); | 89 finished_ = result->Finished(); |
95 | 90 |
96 if (finished_) { | 91 if (finished_) { |
97 browsing_data::HistoryCounter::HistoryResult* history_result = | 92 auto* history_result = |
98 static_cast<browsing_data::HistoryCounter::HistoryResult*>( | 93 static_cast<BrowsingDataCounter::SyncResult*>(result.get()); |
99 result.get()); | |
100 | 94 |
101 local_result_ = history_result->Value(); | 95 local_result_ = history_result->Value(); |
102 has_synced_visits_ = history_result->has_synced_visits(); | 96 has_synced_visits_ = history_result->IsSyncEnabled(); |
103 } | 97 } |
104 | 98 |
105 if (run_loop_ && finished_) | 99 if (run_loop_ && finished_) |
106 run_loop_->Quit(); | 100 run_loop_->Quit(); |
107 } | 101 } |
108 | 102 |
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, | 103 history::WebHistoryService* GetFakeWebHistoryService(Profile* profile, |
124 bool check_sync_status) { | 104 bool check_sync_status) { |
125 // |check_sync_status| is true when the factory should check if | 105 // |check_sync_status| is true when the factory should check if |
126 // history sync is enabled. | 106 // history sync is enabled. |
127 if (!check_sync_status || | 107 if (!check_sync_status || |
128 WebHistoryServiceFactory::GetForProfile(profile)) { | 108 WebHistoryServiceFactory::GetForProfile(profile)) { |
129 return fake_web_history_service_.get(); | 109 return fake_web_history_service_.get(); |
130 } | 110 } |
131 return nullptr; | 111 return nullptr; |
132 } | 112 } |
133 | 113 |
134 history::WebHistoryService* GetRealWebHistoryService(Profile* profile) { | 114 history::WebHistoryService* GetRealWebHistoryService(Profile* profile) { |
135 return WebHistoryServiceFactory::GetForProfile(profile); | 115 return WebHistoryServiceFactory::GetForProfile(profile); |
136 } | 116 } |
137 | 117 |
138 history::HistoryService* GetHistoryService() { return history_service_; } | 118 history::HistoryService* GetHistoryService() { return history_service_; } |
139 | 119 |
140 private: | 120 private: |
141 std::unique_ptr<base::RunLoop> run_loop_; | 121 std::unique_ptr<base::RunLoop> run_loop_; |
142 history::HistoryService* history_service_; | 122 history::HistoryService* history_service_; |
143 std::unique_ptr<history::FakeWebHistoryService> fake_web_history_service_; | 123 std::unique_ptr<history::FakeWebHistoryService> fake_web_history_service_; |
144 base::Time time_; | 124 base::Time time_; |
145 | 125 |
146 bool finished_; | 126 bool finished_; |
147 browsing_data::BrowsingDataCounter::ResultInt local_result_; | 127 BrowsingDataCounter::ResultInt local_result_; |
148 bool has_synced_visits_; | 128 bool has_synced_visits_; |
149 }; | 129 }; |
150 | 130 |
151 // Tests that the counter considers duplicate visits from the same day | 131 // Tests that the counter considers duplicate visits from the same day |
152 // to be a single item. | 132 // to be a single item. |
153 IN_PROC_BROWSER_TEST_F(HistoryCounterTest, DuplicateVisits) { | 133 IN_PROC_BROWSER_TEST_F(HistoryCounterTest, DuplicateVisits) { |
154 AddVisit("https://www.google.com"); // 1 item | 134 AddVisit("https://www.google.com"); // 1 item |
155 AddVisit("https://www.google.com"); | 135 AddVisit("https://www.google.com"); |
156 AddVisit("https://www.chrome.com"); // 2 items | 136 AddVisit("https://www.chrome.com"); // 2 items |
157 AddVisit("https://www.chrome.com"); | 137 AddVisit("https://www.chrome.com"); |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 | 331 |
352 // Nonzero local count, empty sync. | 332 // Nonzero local count, empty sync. |
353 service->ClearSyncedVisits(); | 333 service->ClearSyncedVisits(); |
354 service->SetupFakeResponse(true /* success */, net::HTTP_OK); | 334 service->SetupFakeResponse(true /* success */, net::HTTP_OK); |
355 counter.Restart(); | 335 counter.Restart(); |
356 WaitForCounting(); | 336 WaitForCounting(); |
357 EXPECT_EQ(2u, GetLocalResult()); | 337 EXPECT_EQ(2u, GetLocalResult()); |
358 EXPECT_FALSE(HasSyncedVisits()); | 338 EXPECT_FALSE(HasSyncedVisits()); |
359 } | 339 } |
360 | 340 |
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 | 341 } // namespace |
OLD | NEW |