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

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

Powered by Google App Engine
This is Rietveld 408576698