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

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

Issue 2855623005: Move browsing_data counter sync tests to separate file (Closed)
Patch Set: remove HistoryResult and PasswordResult 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
(Empty)
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
msramek 2017/05/11 15:49:14 nit: We don't use (c) anymore.
dullweber 2017/05/12 13:37:31 Done.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/macros.h"
6 #include "base/run_loop.h"
7 #include "base/threading/platform_thread.h"
8 #include "chrome/browser/history/history_service_factory.h"
9 #include "chrome/browser/history/web_history_service_factory.h"
10 #include "chrome/browser/password_manager/password_store_factory.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
13 #include "chrome/browser/signin/signin_manager_factory.h"
14 #include "chrome/browser/sync/test/integration/sync_test.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/web_data_service_factory.h"
17 #include "chrome/test/base/in_process_browser_test.h"
18 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
19 #include "components/browser_sync/profile_sync_service.h"
20 #include "components/browsing_data/core/browsing_data_utils.h"
21 #include "components/browsing_data/core/counters/autofill_counter.h"
22 #include "components/browsing_data/core/counters/history_counter.h"
23 #include "components/browsing_data/core/counters/passwords_counter.h"
24 #include "components/browsing_data/core/pref_names.h"
25 #include "components/history/core/browser/web_history_service.h"
26 #include "components/history/core/test/fake_web_history_service.h"
27 #include "components/prefs/pref_service.h"
28 #include "components/signin/core/browser/profile_oauth2_token_service.h"
29 #include "components/signin/core/browser/signin_manager.h"
30 #include "content/public/browser/browser_thread.h"
31
32 namespace {
33
34 // A test for the sync behavior of several BrowsingDataCounters.
35 class SyncAwareCounterTest : public SyncTest {
36 public:
37 SyncAwareCounterTest() : SyncTest(SINGLE_CLIENT) {}
38 ~SyncAwareCounterTest() override {}
39
40 void SetUpOnMainThread() override {
41 fake_web_history_service_.reset(new history::FakeWebHistoryService(
42 ProfileOAuth2TokenServiceFactory::GetForProfile(browser()->profile()),
43 SigninManagerFactory::GetForProfile(browser()->profile()),
44 browser()->profile()->GetRequestContext()));
45 }
46
47 history::WebHistoryService* GetFakeWebHistoryService(Profile* profile,
48 bool check_sync_status) {
49 // |check_sync_status| is true when the factory should check if
50 // history sync is enabled.
51 if (!check_sync_status ||
52 WebHistoryServiceFactory::GetForProfile(profile)) {
53 return fake_web_history_service_.get();
54 }
55 return nullptr;
56 }
57
58 // Callback and result retrieval ---------------------------------------------
59
60 void WaitForCounting() {
61 run_loop_.reset(new base::RunLoop());
62 run_loop_->Run();
63 }
64
65 bool CountingFinishedSinceLastAsked() {
66 bool result = finished_;
67 finished_ = false;
68 return result;
69 }
70
71 void WaitForCountingOrConfirmFinished() {
72 if (CountingFinishedSinceLastAsked())
73 return;
74
75 WaitForCounting();
76 CountingFinishedSinceLastAsked();
77 }
78
79 bool IsSyncEnabled() { return sync_enabled_; }
80
81 void Callback(
82 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) {
83 finished_ = result->Finished();
84
85 if (finished_) {
86 auto* sync_result =
87 static_cast<browsing_data::BrowsingDataCounter::SyncResult*>(
88 result.get());
89 sync_enabled_ = sync_result->IsSyncEnabled();
90 }
91
92 if (run_loop_ && finished_)
93 run_loop_->Quit();
94 }
95
96 private:
97 std::unique_ptr<base::RunLoop> run_loop_;
98 std::unique_ptr<history::FakeWebHistoryService> fake_web_history_service_;
99
100 bool finished_;
101 bool sync_enabled_;
102
103 DISALLOW_COPY_AND_ASSIGN(SyncAwareCounterTest);
104 };
105
106 // Test that the counting restarts when autofill sync state changes.
107 // TODO(crbug.com/553421): Move this to the sync/test/integration directory?
108 IN_PROC_BROWSER_TEST_F(SyncAwareCounterTest, AutofillCounter) {
109 // Set up the Sync client.
110 ASSERT_TRUE(SetupClients());
111 static const int kFirstProfileIndex = 0;
112 browser_sync::ProfileSyncService* sync_service =
113 GetSyncService(kFirstProfileIndex);
114 Profile* profile = GetProfile(kFirstProfileIndex);
115 // Set up the counter.
116 browsing_data::AutofillCounter counter(
117 WebDataServiceFactory::GetAutofillWebDataForProfile(
118 profile, ServiceAccessType::IMPLICIT_ACCESS),
119 sync_service);
120
121 counter.Init(
122 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
123 base::Bind(&SyncAwareCounterTest::Callback, base::Unretained(this)));
124
125 // Note that some Sync operations notify observers immediately (and thus there
126 // is no need to call |WaitForCounting()|; in fact, it would block the test),
127 // while other operations only post the task on UI thread's message loop
128 // (which requires calling |WaitForCounting()| for them to run). Therefore,
129 // this test always checks if the callback has already run and only waits
130 // if it has not.
131
132 // We sync all datatypes by default, so starting Sync means that we start
133 // syncing autofill, and this should restart the counter.
134 ASSERT_TRUE(SetupSync());
135 ASSERT_TRUE(sync_service->IsSyncActive());
136 ASSERT_TRUE(sync_service->GetActiveDataTypes().Has(syncer::AUTOFILL));
137 WaitForCountingOrConfirmFinished();
138 EXPECT_TRUE(IsSyncEnabled());
139
140 // We stop syncing autofill in particular. This restarts the counter.
141 syncer::ModelTypeSet everything_except_autofill =
142 syncer::UserSelectableTypes();
143 everything_except_autofill.Remove(syncer::AUTOFILL);
144 auto sync_blocker = sync_service->GetSetupInProgressHandle();
145 sync_service->OnUserChoseDatatypes(/*sync_everything=*/false,
146 everything_except_autofill);
147 ASSERT_FALSE(sync_service->GetPreferredDataTypes().Has(syncer::AUTOFILL));
148 sync_blocker.reset();
149 WaitForCountingOrConfirmFinished();
150 ASSERT_FALSE(sync_service->GetActiveDataTypes().Has(syncer::AUTOFILL));
151 EXPECT_FALSE(IsSyncEnabled());
152
153 // If autofill sync is not affected, the counter is not restarted.
154 syncer::ModelTypeSet only_history(syncer::HISTORY_DELETE_DIRECTIVES);
155 sync_blocker = sync_service->GetSetupInProgressHandle();
156 sync_service->ChangePreferredDataTypes(only_history);
157 sync_blocker.reset();
158 EXPECT_FALSE(CountingFinishedSinceLastAsked());
159
160 // We start syncing autofill again. This restarts the counter.
161 sync_blocker = sync_service->GetSetupInProgressHandle();
162 sync_service->ChangePreferredDataTypes(syncer::ModelTypeSet::All());
163 sync_blocker.reset();
164 WaitForCountingOrConfirmFinished();
165 EXPECT_TRUE(IsSyncEnabled());
166
167 // Stopping the Sync service triggers a restart.
168 sync_service->RequestStop(syncer::SyncService::CLEAR_DATA);
169 WaitForCountingOrConfirmFinished();
170 EXPECT_FALSE(IsSyncEnabled());
171 }
172
173 // Test that the counting restarts when password sync state changes.
174 // TODO(crbug.com/553421): Move this to the sync/test/integration directory?
175 IN_PROC_BROWSER_TEST_F(SyncAwareCounterTest, PasswordCounter) {
176 // Set up the Sync client.
177 ASSERT_TRUE(SetupClients());
178 static const int kFirstProfileIndex = 0;
179 browser_sync::ProfileSyncService* sync_service =
180 GetSyncService(kFirstProfileIndex);
181 Profile* profile = GetProfile(kFirstProfileIndex);
182 // Set up the counter.
183 browsing_data::PasswordsCounter counter(
184 PasswordStoreFactory::GetForProfile(profile,
185 ServiceAccessType::EXPLICIT_ACCESS),
186 sync_service);
187
188 counter.Init(
189 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
190 base::Bind(&SyncAwareCounterTest::Callback, base::Unretained(this)));
191
192 // Note that some Sync operations notify observers immediately (and thus there
193 // is no need to call |WaitForCounting()|; in fact, it would block the test),
194 // while other operations only post the task on UI thread's message loop
195 // (which requires calling |WaitForCounting()| for them to run). Therefore,
196 // this test always checks if the callback has already run and only waits
197 // if it has not.
198
199 // We sync all datatypes by default, so starting Sync means that we start
200 // syncing passwords, and this should restart the counter.
201 ASSERT_TRUE(SetupSync());
202 ASSERT_TRUE(sync_service->IsSyncActive());
203 ASSERT_TRUE(sync_service->GetPreferredDataTypes().Has(syncer::PASSWORDS));
204 WaitForCountingOrConfirmFinished();
205 EXPECT_TRUE(IsSyncEnabled());
206
207 // We stop syncing passwords in particular. This restarts the counter.
208 syncer::ModelTypeSet everything_except_passwords =
209 syncer::UserSelectableTypes();
210 everything_except_passwords.Remove(syncer::PASSWORDS);
211 auto sync_blocker = sync_service->GetSetupInProgressHandle();
212 sync_service->OnUserChoseDatatypes(/*sync_everything=*/false,
213 everything_except_passwords);
214 ASSERT_FALSE(sync_service->GetPreferredDataTypes().Has(syncer::PASSWORDS));
215 sync_blocker.reset();
216 WaitForCountingOrConfirmFinished();
217 ASSERT_FALSE(sync_service->GetPreferredDataTypes().Has(syncer::PASSWORDS));
218 EXPECT_FALSE(IsSyncEnabled());
219
220 // If password sync is not affected, the counter is not restarted.
221 syncer::ModelTypeSet only_history(syncer::HISTORY_DELETE_DIRECTIVES);
222 sync_service->ChangePreferredDataTypes(only_history);
223 sync_blocker = sync_service->GetSetupInProgressHandle();
224 sync_service->ChangePreferredDataTypes(only_history);
225 sync_blocker.reset();
226 EXPECT_FALSE(CountingFinishedSinceLastAsked());
227
228 // We start syncing passwords again. This restarts the counter.
229 sync_blocker = sync_service->GetSetupInProgressHandle();
230 sync_service->ChangePreferredDataTypes(syncer::ModelTypeSet::All());
231 sync_blocker.reset();
232 WaitForCountingOrConfirmFinished();
233 EXPECT_TRUE(IsSyncEnabled());
234
235 // Stopping the Sync service triggers a restart.
236 sync_service->RequestStop(syncer::SyncService::CLEAR_DATA);
237 WaitForCountingOrConfirmFinished();
238 EXPECT_FALSE(IsSyncEnabled());
239 }
240
241 // Test that the counting restarts when history sync state changes.
242 // TODO(crbug.com/553421): Move this to the sync/test/integration directory?
243 IN_PROC_BROWSER_TEST_F(SyncAwareCounterTest, HistoryCounter) {
244 // Set up the Sync client.
245 ASSERT_TRUE(SetupClients());
246 static const int kFirstProfileIndex = 0;
247 browser_sync::ProfileSyncService* sync_service =
248 GetSyncService(kFirstProfileIndex);
249 Profile* profile = GetProfile(kFirstProfileIndex);
250
251 // Set up the fake web history service and the counter.
252
253 browsing_data::HistoryCounter counter(
254 HistoryServiceFactory::GetForProfileWithoutCreating(browser()->profile()),
255 base::Bind(&SyncAwareCounterTest::GetFakeWebHistoryService,
256 base::Unretained(this), base::Unretained(profile), true),
257 sync_service);
258
259 counter.Init(
260 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
261 base::Bind(&SyncAwareCounterTest::Callback, base::Unretained(this)));
262
263 // Note that some Sync operations notify observers immediately (and thus there
264 // is no need to call |WaitForCounting()|; in fact, it would block the test),
265 // while other operations only post the task on UI thread's message loop
266 // (which requires calling |WaitForCounting()| for them to run). Therefore,
267 // this test always checks if the callback has already run and only waits
268 // if it has not.
269
270 // We sync all datatypes by default, so starting Sync means that we start
271 // syncing history deletion, and this should restart the counter.
272 ASSERT_TRUE(SetupSync());
273 ASSERT_TRUE(sync_service->IsSyncActive());
274 ASSERT_TRUE(sync_service->GetPreferredDataTypes().Has(
275 syncer::HISTORY_DELETE_DIRECTIVES));
276 WaitForCountingOrConfirmFinished();
277
278 // We stop syncing history deletion in particular. This restarts the counter.
279 syncer::ModelTypeSet everything_except_history =
280 syncer::UserSelectableTypes();
281 everything_except_history.Remove(syncer::HISTORY_DELETE_DIRECTIVES);
282 auto sync_blocker = sync_service->GetSetupInProgressHandle();
283 sync_service->OnUserChoseDatatypes(/*sync_everything=*/false,
284 everything_except_history);
285 sync_blocker.reset();
286 WaitForCountingOrConfirmFinished();
287
288 // If the history deletion sync is not affected, the counter is not restarted.
289 syncer::ModelTypeSet only_passwords(syncer::PASSWORDS);
290 sync_service->ChangePreferredDataTypes(only_passwords);
291 sync_blocker = sync_service->GetSetupInProgressHandle();
292 sync_service->ChangePreferredDataTypes(only_passwords);
293 sync_blocker.reset();
294 EXPECT_FALSE(counter.HasTrackedTasks());
295 EXPECT_FALSE(CountingFinishedSinceLastAsked());
296
297 // Same in this case.
298 syncer::ModelTypeSet autofill_and_passwords(syncer::AUTOFILL,
299 syncer::PASSWORDS);
300 sync_blocker = sync_service->GetSetupInProgressHandle();
301 sync_service->ChangePreferredDataTypes(autofill_and_passwords);
302 sync_blocker.reset();
303 EXPECT_FALSE(counter.HasTrackedTasks());
304 EXPECT_FALSE(CountingFinishedSinceLastAsked());
305
306 // We start syncing history deletion again. This restarts the counter.
307 sync_blocker = sync_service->GetSetupInProgressHandle();
308 sync_service->ChangePreferredDataTypes(syncer::ModelTypeSet::All());
309 sync_blocker.reset();
310 WaitForCountingOrConfirmFinished();
311
312 // Changing the syncing datatypes to another set that still includes history
313 // deletion should technically not trigger a restart, because the state of
314 // history deletion did not change. However, in reality we can get two
315 // notifications, one that history sync has stopped and another that it is
316 // active again.
317
318 // Stopping the Sync service triggers a restart.
319 sync_service->RequestStop(syncer::SyncService::CLEAR_DATA);
320 WaitForCountingOrConfirmFinished();
321 }
322
msramek 2017/05/11 15:49:14 Could you add test coverage for the fact that Sync
dullweber 2017/05/12 13:37:31 The Autofill and Password test already had a check
323 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698