File: single_client_plus_address_sync_test.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 (225 lines) | stat: -rw-r--r-- 9,419 bytes parent folder | download | duplicates (3)
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
// Copyright 2024 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 <string>
#include <vector>

#include "base/command_line.h"
#include "base/containers/span.h"
#include "base/scoped_observation.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "chrome/browser/plus_addresses/plus_address_service_factory.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/sync/test/integration/status_change_checker.h"
#include "chrome/browser/sync/test/integration/sync_service_impl_harness.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "components/plus_addresses/features.h"
#include "components/plus_addresses/plus_address_service.h"
#include "components/plus_addresses/plus_address_test_utils.h"
#include "components/plus_addresses/plus_address_types.h"
#include "components/plus_addresses/webdata/plus_address_sync_util.h"
#include "components/signin/public/identity_manager/account_info.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/identity_test_utils.h"
#include "components/sync/base/data_type.h"
#include "components/sync/base/features.h"
#include "components/sync/engine/loopback_server/persistent_tombstone_entity.h"
#include "components/sync/engine/loopback_server/persistent_unique_client_entity.h"
#include "components/sync/protocol/entity_specifics.pb.h"
#include "components/sync/test/fake_server.h"
#include "content/public/test/browser_test.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

using plus_addresses::EntityDataFromPlusProfile;
using plus_addresses::PlusAddressService;
using plus_addresses::PlusProfile;
using plus_addresses::test::CreatePlusProfile;

// Blocks until `service->GetPlusProfiles()` matches `matcher`.
// Since `PlusAddressService` lives on a different sequence than the bridge,
// waiting is necessary even after `SetupSync()`.
class PlusProfileChecker : public StatusChangeChecker,
                           public PlusAddressService::Observer {
 public:
  PlusProfileChecker(PlusAddressService* service,
                     testing::Matcher<base::span<const PlusProfile>> matcher)
      : service_(service), matcher_(std::move(matcher)) {
    scoped_observation_.Observe(service_);
  }

  // StatusChangeChecker:
  bool IsExitConditionSatisfied(std::ostream* os) override {
    testing::StringMatchResultListener listener;
    bool matches = testing::ExplainMatchResult(
        matcher_, service_->GetPlusProfiles(), &listener);
    *os << listener.str();
    return matches;
  }

  // PlusAddressService::Observer:
  void OnPlusAddressesChanged(
      const std::vector<plus_addresses::PlusAddressDataChange>& changes)
      override {
    CheckExitCondition();
  }
  void OnPlusAddressServiceShutdown() override {}

 private:
  const raw_ptr<PlusAddressService> service_;
  const testing::Matcher<base::span<const PlusProfile>> matcher_;
  base::ScopedObservation<PlusAddressService, PlusAddressService::Observer>
      scoped_observation_{this};
};

// PLUS_ADDRESS is supposed to behave the same in and outside of transport mode.
// These tests are parameterized by whether the test should run in transport
// mode (true) or not (false).
class SingleClientPlusAddressSyncTest
    : public SyncTest,
      public testing::WithParamInterface<bool> {
 public:
  SingleClientPlusAddressSyncTest() : SyncTest(SINGLE_CLIENT) {
    features_.InitWithFeaturesAndParameters(
        /*enabled_features=*/{{plus_addresses::features::kPlusAddressesEnabled,
                               {{plus_addresses::features::
                                     kEnterprisePlusAddressServerUrl.name,
                                 "https://not-used.com"}}}},
        /*disabled_features=*/{});
  }

  // Sets up the sync client in sync-the-feature or sync-the-transport mode,
  // depending on `GetParam()`. Returns true if setup succeeded.
  bool SetupSync(SyncTestAccount account = SyncTestAccount::kDefaultAccount) {
    const bool should_run_in_transport_mode = GetParam();
    if (should_run_in_transport_mode) {
      return SetupClients() && GetClient(0)->SignInPrimaryAccount(account) &&
             GetClient(0)->AwaitSyncTransportActive();
    }
    return SyncTest::SetupSync(account);
  }

  PlusAddressService* GetPlusAddressService() {
    return PlusAddressServiceFactory::GetForBrowserContext(GetProfile(0));
  }

  // Expects that `specifics` has `PlusAddressSpecifics`.
  void InjectEntityToServer(const sync_pb::EntitySpecifics& specifics) {
    GetFakeServer()->InjectEntity(
        syncer::PersistentUniqueClientEntity::CreateFromSpecificsForTesting(
            /*non_unique_name=*/"plus-profile",
            /*client_tag=*/
            specifics.plus_address().profile_id(), specifics,
            /*creation_time=*/0, /*last_modified_time=*/0));
  }

  // Injects a tombstone for the PLUS_ADDRESS entity with given `client_tag`.
  void InjectTombstoneToServer(const std::string& client_tag) {
    const std::string client_tag_hash =
        syncer::ClientTagHash::FromUnhashed(syncer::PLUS_ADDRESS, client_tag)
            .value();
    fake_server_->InjectEntity(
        syncer::PersistentTombstoneEntity::PersistentTombstoneEntity::CreateNew(
            syncer::LoopbackServerEntity::CreateId(syncer::PLUS_ADDRESS,
                                                   client_tag_hash),
            client_tag_hash));
  }

 private:
  base::test::ScopedFeatureList features_;
};

INSTANTIATE_TEST_SUITE_P(
    ,
    SingleClientPlusAddressSyncTest,
#if BUILDFLAG(IS_CHROMEOS)
    // On ChromeOS, sync-the-feature gets started automatically once a primary
    // account is signed in and transport mode is not a thing. As such, only run
    // the tests in sync-the-feature mode.
    testing::Values(false)
#else
    testing::Bool()
#endif
);

IN_PROC_BROWSER_TEST_P(SingleClientPlusAddressSyncTest, InitialSync) {
  // Start syncing with an existing `plus_profile` on the server.
  const PlusProfile plus_profile = CreatePlusProfile();
  InjectEntityToServer(EntityDataFromPlusProfile(plus_profile).specifics);
  ASSERT_TRUE(SetupSync());
  EXPECT_TRUE(PlusProfileChecker(GetPlusAddressService(),
                                 testing::UnorderedElementsAre(plus_profile))
                  .Wait());
}

IN_PROC_BROWSER_TEST_P(SingleClientPlusAddressSyncTest, IncrementalUpdate_Add) {
  ASSERT_TRUE(SetupSync());
  // Simulate creating a new `plus_profile` on the server after sync started.
  const PlusProfile plus_profile = CreatePlusProfile();
  InjectEntityToServer(EntityDataFromPlusProfile(plus_profile).specifics);
  EXPECT_TRUE(PlusProfileChecker(GetPlusAddressService(),
                                 testing::UnorderedElementsAre(plus_profile))
                  .Wait());
}

IN_PROC_BROWSER_TEST_P(SingleClientPlusAddressSyncTest,
                       IncrementalUpdate_Update) {
  PlusProfile plus_profile = CreatePlusProfile();
  InjectEntityToServer(EntityDataFromPlusProfile(plus_profile).specifics);
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(PlusProfileChecker(GetPlusAddressService(),
                                 testing::UnorderedElementsAre(plus_profile))
                  .Wait());
  // Simulate updating the `plus_profile` on the server.
  plus_profile.plus_address =
      plus_addresses::PlusAddress("new-" + *plus_profile.plus_address);
  InjectEntityToServer(EntityDataFromPlusProfile(plus_profile).specifics);
  EXPECT_TRUE(PlusProfileChecker(GetPlusAddressService(),
                                 testing::UnorderedElementsAre(plus_profile))
                  .Wait());
}

IN_PROC_BROWSER_TEST_P(SingleClientPlusAddressSyncTest,
                       IncrementalUpdate_Remove) {
  const PlusProfile plus_profile = CreatePlusProfile();
  InjectEntityToServer(EntityDataFromPlusProfile(plus_profile).specifics);
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(PlusProfileChecker(GetPlusAddressService(),
                                 testing::UnorderedElementsAre(plus_profile))
                  .Wait());
  // Simulate removing the `plus_profile` on the server.
  InjectTombstoneToServer(*plus_profile.profile_id);
  EXPECT_TRUE(
      PlusProfileChecker(GetPlusAddressService(), testing::IsEmpty()).Wait());
}

// ChromeOS does not support signing out of the primary account.
#if !BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_P(SingleClientPlusAddressSyncTest, Signout_DataCleared) {
  InjectEntityToServer(
      EntityDataFromPlusProfile(CreatePlusProfile()).specifics);
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(PlusProfileChecker(GetPlusAddressService(),
                                 testing::Not(testing::IsEmpty()))
                  .Wait());
  GetClient(0)->SignOutPrimaryAccount();
  EXPECT_TRUE(
      PlusProfileChecker(GetPlusAddressService(), testing::IsEmpty()).Wait());
}
#endif  // !BUILDFLAG(IS_CHROMEOS)

IN_PROC_BROWSER_TEST_P(SingleClientPlusAddressSyncTest,
                       DisabledForManagedAccounts) {
  // Sign in with a managed account.
  ASSERT_TRUE(SetupSync(SyncTestAccount::kEnterpriseAccount1));

  EXPECT_FALSE(GetSyncService(0)->GetActiveDataTypes().HasAny(
      {syncer::PLUS_ADDRESS, syncer::PLUS_ADDRESS_SETTING}));
}

}  // namespace