File: sync_aware_counter_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (337 lines) | stat: -rw-r--r-- 14,203 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>

#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/threading/platform_thread.h"
#include "build/build_config.h"
#include "chrome/browser/autofill/autofill_entity_data_manager_factory.h"
#include "chrome/browser/autofill/personal_data_manager_factory.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/history/web_history_service_factory.h"
#include "chrome/browser/password_manager/account_password_store_factory.h"
#include "chrome/browser/password_manager/profile_password_store_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/test/integration/sync_service_impl_harness.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/webdata_services/web_data_service_factory.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
#include "components/browsing_data/core/browsing_data_utils.h"
#include "components/browsing_data/core/counters/autofill_counter.h"
#include "components/browsing_data/core/counters/history_counter.h"
#include "components/browsing_data/core/counters/passwords_counter.h"
#include "components/browsing_data/core/pref_names.h"
#include "components/history/core/browser/web_history_service.h"
#include "components/history/core/test/fake_web_history_service.h"
#include "components/password_manager/core/browser/password_store/password_store_interface.h"
#include "components/prefs/pref_service.h"
#include "components/sync/service/sync_service_impl.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/browser_test.h"

using browsing_data::BrowsingDataCounter;

namespace {

static const int kFirstProfileIndex = 0;

// A test for the sync behavior of several BrowsingDataCounters.
class SyncAwareCounterTest : public SyncTest {
 public:
  SyncAwareCounterTest() : SyncTest(SINGLE_CLIENT) {}

  SyncAwareCounterTest(const SyncAwareCounterTest&) = delete;
  SyncAwareCounterTest& operator=(const SyncAwareCounterTest&) = delete;

  ~SyncAwareCounterTest() override = default;

  void SetUpOnMainThread() override {
    fake_web_history_service_ =
        std::make_unique<history::FakeWebHistoryService>();
    run_loop_ = std::make_unique<base::RunLoop>();
    SyncTest::SetUpOnMainThread();
  }

  history::WebHistoryService* GetFakeWebHistoryService(Profile* profile) {
    // Check if history sync is enabled.
    if (WebHistoryServiceFactory::GetForProfile(profile)) {
      return fake_web_history_service_.get();
    }
    return nullptr;
  }

  // Callback and result retrieval ---------------------------------------------

  void WaitForCounting() {
    run_loop_->Run();
    run_loop_ = std::make_unique<base::RunLoop>();
    finished_ = false;
  }

  // Returns true if the counter finished since the last call to
  // WaitForCounting() or CountingFinishedSinceLastAsked()
  bool CountingFinishedSinceLastAsked() {
    bool result = finished_;
    finished_ = false;
    return result;
  }

  bool IsSyncEnabled() { return sync_enabled_; }

  void OnCounterResult(std::unique_ptr<BrowsingDataCounter::Result> result) {
    finished_ = result->Finished();

    if (finished_) {
      auto* sync_result =
          static_cast<BrowsingDataCounter::SyncResult*>(result.get());
      sync_enabled_ = sync_result->is_sync_enabled();
      run_loop_->Quit();
    }
  }

 private:
  std::unique_ptr<base::RunLoop> run_loop_;
  std::unique_ptr<history::FakeWebHistoryService> fake_web_history_service_;

  bool finished_;
  bool sync_enabled_;
};

// Test that the counting restarts when autofill sync state changes.
// TODO(crbug.com/40443942): Move this to the sync/test/integration directory?
IN_PROC_BROWSER_TEST_F(SyncAwareCounterTest, AutofillCounter) {
  // Set up the Sync client.
  ASSERT_TRUE(SetupClients());
  syncer::SyncService* sync_service = GetSyncService(kFirstProfileIndex);
  Profile* profile = GetProfile(kFirstProfileIndex);
  // Set up the counter.
  browsing_data::AutofillCounter counter(
      autofill::PersonalDataManagerFactory::GetForBrowserContext(profile),
      WebDataServiceFactory::GetAutofillWebDataForProfile(
          profile, ServiceAccessType::IMPLICIT_ACCESS),
      autofill::AutofillEntityDataManagerFactory::GetForProfile(profile),
      sync_service);

  counter.Init(profile->GetPrefs(),
               browsing_data::ClearBrowsingDataTab::ADVANCED,
               base::BindRepeating(&SyncAwareCounterTest::OnCounterResult,
                                   base::Unretained(this)));

  // We sync all datatypes by default, so starting Sync means that we start
  // syncing autofill, and this should restart the counter.
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(sync_service->IsSyncFeatureActive());
  ASSERT_TRUE(sync_service->GetActiveDataTypes().Has(syncer::AUTOFILL));
  WaitForCounting();
  EXPECT_TRUE(IsSyncEnabled());

  // We stop syncing autofill in particular. This restarts the counter.
  syncer::UserSelectableTypeSet everything_except_autofill =
      GetRegisteredSelectableTypes(kFirstProfileIndex);
  everything_except_autofill.Remove(syncer::UserSelectableType::kAutofill);
  auto sync_blocker = sync_service->GetSetupInProgressHandle();
  sync_service->GetUserSettings()->SetSelectedTypes(
      /*sync_everything=*/false, everything_except_autofill);
  ASSERT_FALSE(sync_service->GetUserSettings()->GetSelectedTypes().Has(
      syncer::UserSelectableType::kAutofill));
  sync_blocker.reset();
  WaitForCounting();
  ASSERT_FALSE(sync_service->GetActiveDataTypes().Has(syncer::AUTOFILL));
  EXPECT_FALSE(IsSyncEnabled());

  // If autofill sync is not affected, the counter is not restarted.
  sync_blocker = sync_service->GetSetupInProgressHandle();
  sync_service->GetUserSettings()->SetSelectedTypes(
      /*sync_everything=*/false,
      /*types=*/{syncer::UserSelectableType::kHistory});
  sync_blocker.reset();
  EXPECT_FALSE(CountingFinishedSinceLastAsked());

  // We start syncing autofill again. This restarts the counter.
  sync_blocker = sync_service->GetSetupInProgressHandle();
  sync_service->GetUserSettings()->SetSelectedTypes(
      /*sync_everything=*/false,
      GetRegisteredSelectableTypes(kFirstProfileIndex));
  sync_blocker.reset();
  WaitForCounting();
  EXPECT_TRUE(IsSyncEnabled());

  // Signout isn't possible on ChromeOS.
#if !BUILDFLAG(IS_CHROMEOS)
  // Stopping the Sync service triggers a restart.
  GetClient(0)->SignOutPrimaryAccount();
  WaitForCounting();
  EXPECT_FALSE(IsSyncEnabled());
#endif  // !BUILDFLAG(IS_CHROMEOS)
}

// Test that the counting restarts when password sync state changes.
// TODO(crbug.com/40443942): Move this to the sync/test/integration directory?
IN_PROC_BROWSER_TEST_F(SyncAwareCounterTest, PasswordCounter) {
  // Set up the Sync client.
  ASSERT_TRUE(SetupClients());
  syncer::SyncService* sync_service = GetSyncService(kFirstProfileIndex);
  Profile* profile = GetProfile(kFirstProfileIndex);
  // Set up the counter.
  browsing_data::PasswordsCounter counter(
      ProfilePasswordStoreFactory::GetForProfile(
          profile, ServiceAccessType::EXPLICIT_ACCESS),
      AccountPasswordStoreFactory::GetForProfile(
          profile, ServiceAccessType::EXPLICIT_ACCESS),
      profile->GetPrefs(), sync_service);

  counter.Init(profile->GetPrefs(),
               browsing_data::ClearBrowsingDataTab::ADVANCED,
               base::BindRepeating(&SyncAwareCounterTest::OnCounterResult,
                                   base::Unretained(this)));

  // We sync all datatypes by default, so starting Sync means that we start
  // syncing passwords, and this should restart the counter.
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(sync_service->IsSyncFeatureActive());
  ASSERT_TRUE(sync_service->GetUserSettings()->GetSelectedTypes().Has(
      syncer::UserSelectableType::kPasswords));
  WaitForCounting();
  EXPECT_TRUE(IsSyncEnabled());

  // We stop syncing passwords in particular. This restarts the counter.
  syncer::UserSelectableTypeSet everything_except_passwords =
      GetRegisteredSelectableTypes(kFirstProfileIndex);
  everything_except_passwords.Remove(syncer::UserSelectableType::kPasswords);
  auto sync_blocker = sync_service->GetSetupInProgressHandle();
  sync_service->GetUserSettings()->SetSelectedTypes(
      /*sync_everything=*/false, everything_except_passwords);
  ASSERT_FALSE(sync_service->GetUserSettings()->GetSelectedTypes().Has(
      syncer::UserSelectableType::kPasswords));
  sync_blocker.reset();
  WaitForCounting();
  ASSERT_FALSE(sync_service->GetUserSettings()->GetSelectedTypes().Has(
      syncer::UserSelectableType::kPasswords));
  EXPECT_FALSE(IsSyncEnabled());

  // If password sync is not affected, the counter is not restarted.
  syncer::UserSelectableTypeSet only_history = {
      syncer::UserSelectableType::kHistory};
  sync_service->GetUserSettings()->SetSelectedTypes(/*sync_everything=*/false,
                                                    only_history);
  sync_blocker = sync_service->GetSetupInProgressHandle();
  sync_service->GetUserSettings()->SetSelectedTypes(/*sync_everything=*/false,
                                                    only_history);
  sync_blocker.reset();
  EXPECT_FALSE(CountingFinishedSinceLastAsked());

  // We start syncing passwords again. This restarts the counter.
  sync_blocker = sync_service->GetSetupInProgressHandle();
  sync_service->GetUserSettings()->SetSelectedTypes(
      /*sync_everything=*/false,
      GetRegisteredSelectableTypes(kFirstProfileIndex));
  sync_blocker.reset();
  WaitForCounting();
  EXPECT_TRUE(IsSyncEnabled());

  // Signout isn't possible on ChromeOS.
#if !BUILDFLAG(IS_CHROMEOS)
  // Stopping the Sync service triggers a restart.
  GetClient(0)->SignOutPrimaryAccount();
  WaitForCounting();
  EXPECT_FALSE(IsSyncEnabled());
#endif  // !BUILDFLAG(IS_CHROMEOS)
}

// Test that the counting restarts when history sync state changes.
// TODO(crbug.com/40443942): Move this to the sync/test/integration directory?
IN_PROC_BROWSER_TEST_F(SyncAwareCounterTest, HistoryCounter) {
  // Set up the Sync client.
  ASSERT_TRUE(SetupClients());
  syncer::SyncService* sync_service = GetSyncService(kFirstProfileIndex);
  Profile* profile = GetProfile(kFirstProfileIndex);

  // Set up the fake web history service and the counter.

  browsing_data::HistoryCounter counter(
      HistoryServiceFactory::GetForProfileWithoutCreating(browser()->profile()),
      base::BindRepeating(&SyncAwareCounterTest::GetFakeWebHistoryService,
                          base::Unretained(this), base::Unretained(profile)),
      sync_service);

  counter.Init(profile->GetPrefs(),
               browsing_data::ClearBrowsingDataTab::ADVANCED,
               base::BindRepeating(&SyncAwareCounterTest::OnCounterResult,
                                   base::Unretained(this)));

  // We sync all datatypes by default, so starting Sync means that we start
  // syncing history deletion, and this should restart the counter.
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(sync_service->IsSyncFeatureActive());
  ASSERT_TRUE(sync_service->GetUserSettings()->GetSelectedTypes().Has(
      syncer::UserSelectableType::kHistory));
  ASSERT_TRUE(sync_service->GetActiveDataTypes().Has(
      syncer::HISTORY_DELETE_DIRECTIVES));

  WaitForCounting();
  EXPECT_TRUE(IsSyncEnabled());

  // We stop syncing history deletion in particular. This restarts the counter.
  syncer::UserSelectableTypeSet everything_except_history =
      GetRegisteredSelectableTypes(kFirstProfileIndex);
  everything_except_history.Remove(syncer::UserSelectableType::kHistory);
  auto sync_blocker = sync_service->GetSetupInProgressHandle();
  sync_service->GetUserSettings()->SetSelectedTypes(
      /*sync_everything=*/false, everything_except_history);
  sync_blocker.reset();
  WaitForCounting();
  EXPECT_FALSE(IsSyncEnabled());

  // If the history deletion sync is not affected, the counter is not restarted.
  syncer::UserSelectableTypeSet only_passwords = {
      syncer::UserSelectableType::kPasswords};
  sync_service->GetUserSettings()->SetSelectedTypes(/*sync_everything=*/false,
                                                    only_passwords);
  sync_blocker = sync_service->GetSetupInProgressHandle();
  sync_service->GetUserSettings()->SetSelectedTypes(/*sync_everything=*/false,
                                                    only_passwords);
  sync_blocker.reset();
  EXPECT_FALSE(counter.HasTrackedTasksForTesting());
  EXPECT_FALSE(CountingFinishedSinceLastAsked());

  // Same in this case.
  syncer::UserSelectableTypeSet autofill_and_passwords = {
      syncer::UserSelectableType::kAutofill,
      syncer::UserSelectableType::kPasswords};
  sync_blocker = sync_service->GetSetupInProgressHandle();
  sync_service->GetUserSettings()->SetSelectedTypes(
      /*sync_everything=*/false, autofill_and_passwords);
  sync_blocker.reset();
  EXPECT_FALSE(counter.HasTrackedTasksForTesting());
  EXPECT_FALSE(CountingFinishedSinceLastAsked());

  // We start syncing history deletion again. This restarts the counter.
  sync_blocker = sync_service->GetSetupInProgressHandle();
  sync_service->GetUserSettings()->SetSelectedTypes(
      /*sync_everything=*/false,
      GetRegisteredSelectableTypes(kFirstProfileIndex));
  sync_blocker.reset();
  WaitForCounting();
  EXPECT_TRUE(IsSyncEnabled());

  // Changing the syncing datatypes to another set that still includes history
  // deletion should technically not trigger a restart, because the state of
  // history deletion did not change. However, in reality we can get two
  // notifications, one that history sync has stopped and another that it is
  // active again.

  // Signout isn't possible on ChromeOS.
#if !BUILDFLAG(IS_CHROMEOS)
  // Stopping the Sync service triggers a restart.
  GetClient(0)->SignOutPrimaryAccount();
  WaitForCounting();
  EXPECT_FALSE(IsSyncEnabled());
#endif  // !BUILDFLAG(IS_CHROMEOS)
}

}  // namespace