forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfamily_live_test.cc
324 lines (280 loc) · 11.7 KB
/
family_live_test.cc
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/test/supervised_user/family_live_test.h"
#include <ostream>
#include <string>
#include <string_view>
#include <vector>
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/notreached.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_test_util.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/supervised_user/supervised_user_service_factory.h"
#include "chrome/browser/sync/sync_service_factory.h"
#include "chrome/browser/sync/test/integration/invalidations/invalidations_status_checker.h"
#include "chrome/browser/sync/test/integration/single_client_status_change_checker.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/supervised_user/family_member.h"
#include "components/signin/public/identity_manager/test_accounts.h"
#include "components/supervised_user/test_support/browser_state_management.h"
#include "content/public/browser/storage_partition.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "net/dns/mock_host_resolver.h"
#include "ui/base/interaction/interactive_test_internal.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "url/gurl.h"
namespace supervised_user {
namespace {
// When enabled the tests explicitly wait for sync invalidation to be ready.
const char* kWaitForSyncInvalidationReadySwitch =
"supervised-tests-wait-for-sync-invalidation-ready";
// When enabled, the browser opens extra debugging tabs & the logging is more
// detailed.
const char* kDebugSwitch = "supervised-tests-debug-features";
bool IsSwitchEnabled(const char* flag) {
return base::CommandLine::ForCurrentProcess()->HasSwitch(flag);
}
// List of accounts specified in
// chrome/browser/internal/resources/signin/test_accounts.json.
static constexpr std::string_view kHeadOfHouseholdAccountIdSuffix{
"HEAD_OF_HOUSEHOLD"};
static constexpr std::string_view kChildAccountIdSuffix{"CHILD_1"};
Profile& CreateNewProfile() {
ProfileManager* profile_manager = g_browser_process->profile_manager();
base::FilePath profile_path =
profile_manager->GenerateNextProfileDirectoryPath();
return profiles::testing::CreateProfileSync(profile_manager, profile_path);
}
std::string GetFamilyIdentifier() {
const base::CommandLine* const cmd = base::CommandLine::ForCurrentProcess();
CHECK(cmd->HasSwitch(kFamilyIdentifierSwitch))
<< "Please specify " << kFamilyIdentifierSwitch << " switch";
return cmd->GetSwitchValueASCII(kFamilyIdentifierSwitch);
}
std::string GetFamilyMemberIdentifier(std::string_view member_identifier) {
return GetFamilyIdentifier() + "_" + std::string(member_identifier);
}
bool HasAuthError(syncer::SyncServiceImpl* service) {
return service->GetAuthError().state() ==
GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS ||
service->GetAuthError().state() ==
GoogleServiceAuthError::SERVICE_ERROR ||
service->GetAuthError().state() ==
GoogleServiceAuthError::REQUEST_CANCELED;
}
class SyncSetupChecker : public SingleClientStatusChangeChecker {
public:
explicit SyncSetupChecker(syncer::SyncServiceImpl* service)
: SingleClientStatusChangeChecker(service) {}
bool IsExitConditionSatisfied(std::ostream* os) override {
*os << "Waiting for sync setup to complete";
if (service()->GetTransportState() ==
syncer::SyncService::TransportState::ACTIVE &&
service()->IsSyncFeatureActive()) {
return true;
}
// Sync is blocked by an auth error.
if (HasAuthError(service())) {
return true;
}
// Still waiting on sync setup.
return false;
}
};
signin::TestAccountSigninCredentials CreateTestAccountFromCredentialsSwitch(
std::string_view credentials_switch) {
std::string credentials =
base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
credentials_switch);
std::string username, password;
if (RE2::FullMatch(credentials, "(.*):(.*)", &username, &password)) {
return {username, password};
}
NOTREACHED() << "Expected username:password format, but got: " << credentials;
}
} // namespace
FamilyLiveTest::FamilyLiveTest(RpcMode rpc_mode) : rpc_mode_(rpc_mode) {}
FamilyLiveTest::FamilyLiveTest(
RpcMode rpc_mode,
const std::vector<std::string>& extra_enabled_hosts)
: extra_enabled_hosts_(extra_enabled_hosts), rpc_mode_(rpc_mode) {}
FamilyLiveTest::~FamilyLiveTest() = default;
FamilyMember& FamilyLiveTest::head_of_household() const {
CHECK(head_of_household_)
<< "No head of household found for given family or credentials";
return *head_of_household_;
}
FamilyMember& FamilyLiveTest::child() const {
CHECK(child_) << "No child found for given family or credentials";
return *child_;
}
FamilyMember& FamilyLiveTest::rpc_issuer() const {
switch (rpc_mode_) {
case RpcMode::kProd:
return head_of_household();
case RpcMode::kTestImpersonation:
return child();
}
NOTREACHED();
}
void FamilyLiveTest::TurnOnSync() {
TurnOnSyncFor(*head_of_household_);
TurnOnSyncFor(*child_);
}
void FamilyLiveTest::TurnOnSyncFor(FamilyMember& member) {
member.TurnOnSync();
member.browser().tab_strip_model()->CloseWebContentsAt(
2, TabCloseTypes::CLOSE_CREATE_HISTORICAL_TAB);
member.browser().tab_strip_model()->CloseWebContentsAt(
1, TabCloseTypes::CLOSE_CREATE_HISTORICAL_TAB);
if (IsSwitchEnabled(kDebugSwitch)) {
CHECK(AddTabAtIndexToBrowser(&member.browser(), 1,
GURL("chrome://sync-internals"),
ui::PAGE_TRANSITION_AUTO_TOPLEVEL));
}
if (IsSwitchEnabled(kWaitForSyncInvalidationReadySwitch)) {
// After turning the sync on, wait until this is fully initialized.
LOG(INFO) << "Waiting for sync service to set up invalidations.";
syncer::SyncServiceImpl* service =
SyncServiceFactory::GetAsSyncServiceImplForProfileForTesting(
&member.profile());
service->SetInvalidationsForSessionsEnabled(true);
CHECK(SyncSetupChecker(service).Wait()) << "SyncSetupChecker timed out.";
CHECK(InvalidationsStatusChecker(service, /*expected_status=*/true).Wait())
<< "Invalidation checker timed out.";
LOG(INFO) << "Invalidations ready.";
}
}
void FamilyLiveTest::SetUp() {
signin::test::LiveTest::SetUp();
// Always disable animation for stability.
ui::ScopedAnimationDurationScaleMode disable_animation(
ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
}
void FamilyLiveTest::SetUpOnMainThread() {
signin::test::LiveTest::SetUpOnMainThread();
if (IsSwitchEnabled(kFamilyIdentifierSwitch)) {
// Family from static test_accounts file mode
CHECK(!IsSwitchEnabled(kHeadOfHouseholdCredentialsSwitch))
<< "Head of household credentials are ignored if "
<< kFamilyIdentifierSwitch << " is set";
CHECK(!IsSwitchEnabled(kChildCredentialsSwitch))
<< "Child credentials are ignored if " << kFamilyIdentifierSwitch
<< " is set";
SetHeadOfHousehold(GetAccountFromFile(kHeadOfHouseholdAccountIdSuffix));
SetChild(GetAccountFromFile(kChildAccountIdSuffix));
return;
}
if (IsSwitchEnabled(kHeadOfHouseholdCredentialsSwitch) &&
IsSwitchEnabled(kChildCredentialsSwitch)) {
SetHeadOfHousehold(CreateTestAccountFromCredentialsSwitch(
kHeadOfHouseholdCredentialsSwitch));
SetChild(CreateTestAccountFromCredentialsSwitch(kChildCredentialsSwitch));
return;
}
NOTREACHED() << "Either specify " << kFamilyIdentifierSwitch
<< " or configure credentials using "
<< kHeadOfHouseholdCredentialsSwitch << " and "
<< kChildCredentialsSwitch << ".";
}
void FamilyLiveTest::TearDownOnMainThread() {
head_of_household_.reset();
child_.reset();
signin::test::LiveTest::TearDownOnMainThread();
}
void FamilyLiveTest::SetHeadOfHousehold(
const signin::TestAccountSigninCredentials& account) {
head_of_household_ = MakeSignedInBrowser(account);
}
void FamilyLiveTest::SetChild(
const signin::TestAccountSigninCredentials& account) {
child_ = MakeSignedInBrowser(account);
}
void FamilyLiveTest::SetUpInProcessBrowserTestFixture() {
signin::test::LiveTest::SetUpInProcessBrowserTestFixture();
for (const auto& host : extra_enabled_hosts_) {
host_resolver()->AllowDirectLookup(host);
}
}
signin::TestAccountSigninCredentials FamilyLiveTest::GetAccountFromFile(
std::string_view account_name_suffix) const {
std::optional<signin::TestAccountSigninCredentials> account =
GetTestAccounts()->GetAccount(
GetFamilyMemberIdentifier(account_name_suffix));
CHECK(account.has_value());
return *account;
}
std::unique_ptr<FamilyMember> FamilyLiveTest::MakeSignedInBrowser(
const signin::TestAccountSigninCredentials& account) {
// Managed externally to the test fixture.
Profile& profile = CreateNewProfile();
Browser* browser = CreateBrowser(&profile);
CHECK(browser) << "Expected to create a browser.";
FamilyMember::NewTabCallback new_tab_callback = base::BindLambdaForTesting(
[this, browser](int index, const GURL& url,
ui::PageTransition transition) -> bool {
return this->AddTabAtIndexToBrowser(browser, index, url, transition);
});
return std::make_unique<FamilyMember>(
account,
profile.GetDefaultStoragePartition()
->GetURLLoaderFactoryForBrowserProcess(),
*IdentityManagerFactory::GetForProfile(&profile), *browser, profile,
new_tab_callback);
}
GURL FamilyLiveTest::GetRoutedUrl(std::string_view url_spec) const {
GURL url(url_spec);
for (std::string_view enabled_host : extra_enabled_hosts_) {
if (url.host() == enabled_host) {
return url;
}
}
NOTREACHED() << "Supplied url_spec is not routed in this test fixture.";
}
InteractiveFamilyLiveTest::InteractiveFamilyLiveTest(
FamilyLiveTest::RpcMode rpc_mode)
: InteractiveBrowserTestT<FamilyLiveTest>(rpc_mode) {}
InteractiveFamilyLiveTest::InteractiveFamilyLiveTest(
FamilyLiveTest::RpcMode rpc_mode,
const std::vector<std::string>& extra_enabled_hosts)
: InteractiveBrowserTestT<FamilyLiveTest>(rpc_mode, extra_enabled_hosts) {}
ui::test::internal::InteractiveTestPrivate::MultiStep
InteractiveFamilyLiveTest::WaitForStateSeeding(
ui::test::StateIdentifier<InIntendedStateObserver> id,
const FamilyMember& browser_user,
const BrowserState& state) {
return Steps(
Log(base::StrCat({"WaitForState[", state.ToString(), "]: start"})),
If([&]() { return !state.Check(browser_user.GetServices()); },
/*then_steps=*/
Steps(
Do([&]() {
state.Seed(rpc_issuer().identity_manager(),
rpc_issuer().url_loader_factory(),
browser_user.GetAccountId().ToString());
}),
PollState(
id, [&]() { return state.Check(browser_user.GetServices()); },
/*polling_interval=*/base::Seconds(2)),
WaitForState(id, true), StopObservingState(id)),
/*else_steps=*/
Steps(Log(base::StrCat(
{"WaitForState[", state.ToString(), "]: seeding skipped"})))),
Log(base::StrCat({"WaitForState[", state.ToString(), "]: completed"})));
}
std::string ToString(FamilyLiveTest::RpcMode rpc_mode) {
switch (rpc_mode) {
case FamilyLiveTest::RpcMode::kProd:
return "ProdRpcMode";
case FamilyLiveTest::RpcMode::kTestImpersonation:
return "TestImpersonationRpcMode";
}
NOTREACHED();
}
} // namespace supervised_user