-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
apc_client_impl_unittest.cc
602 lines (495 loc) · 23 KB
/
apc_client_impl_unittest.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/autofill_assistant/password_change/apc_client_impl.h"
#include <memory>
#include <string>
#include "base/callback.h"
#include "base/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/test/gmock_move_support.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/autofill_assistant/password_change/apc_external_action_delegate.h"
#include "chrome/browser/autofill_assistant/password_change/apc_onboarding_coordinator_impl.h"
#include "chrome/browser/autofill_assistant/password_change/mock_apc_onboarding_coordinator.h"
#include "chrome/browser/ui/autofill_assistant/password_change/mock_apc_scrim_manager.h"
#include "chrome/browser/ui/autofill_assistant/password_change/mock_assistant_side_panel_coordinator.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "components/autofill_assistant/browser/public/mock_headless_script_controller.h"
#include "components/autofill_assistant/browser/public/mock_runtime_manager.h"
#include "components/autofill_assistant/browser/public/password_change/mock_website_login_manager.h"
#include "components/password_manager/core/browser/password_manager_client.h"
#include "components/password_manager/core/browser/password_manager_client_helper.h"
#include "components/password_manager/core/browser/stub_password_manager_client.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/test_renderer_host.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "url/gurl.h"
namespace {
constexpr char kUrl1[] = "https://www.example.com";
constexpr char kUsername1[] = "Lori";
constexpr char kDebugBundleId[] = "testuser/123/password_change/example.com";
constexpr char kDebugSocketId[] = "testuser";
constexpr char kPasswordChangeSkipLoginParameter[] =
"PASSWORD_CHANGE_SKIP_LOGIN";
constexpr char kSourceParameter[] = "SOURCE";
constexpr char kDebugBundleIdParameter[] = "DEBUG_BUNDLE_ID";
constexpr char kDebugSocketIdParameter[] = "DEBUG_SOCKET_ID";
constexpr char kSourcePasswordChangeLeakWarning[] = "10";
constexpr char kSourcePasswordChangeSettings[] = "11";
constexpr int kDescriptionId1 = 3;
constexpr int kDescriptionId2 = 17;
class MockApcExternalActionDelegate : public ApcExternalActionDelegate {
public:
MockApcExternalActionDelegate(
AssistantDisplayDelegate* display_delegate,
ApcScrimManager* apc_scrim_manager,
autofill_assistant::WebsiteLoginManager* website_login_manager)
: ApcExternalActionDelegate(display_delegate,
apc_scrim_manager,
website_login_manager) {}
~MockApcExternalActionDelegate() override = default;
MOCK_METHOD(void, ShowStartingScreen, (const GURL&), (override));
MOCK_METHOD(void, ShowCompletionScreen, (base::RepeatingClosure), (override));
MOCK_METHOD(void, ShowErrorScreen, (), (override));
MOCK_METHOD(void, SetupDisplay, (), (override));
};
} // namespace
using ::testing::_;
using ::testing::DoAll;
using ::testing::SaveArg;
using ::testing::StrEq;
class TestApcClientImpl : public ApcClientImpl {
public:
static TestApcClientImpl* CreateForWebContents(
content::WebContents* web_contents);
explicit TestApcClientImpl(content::WebContents* web_contents)
: ApcClientImpl(web_contents) {}
std::unique_ptr<ApcOnboardingCoordinator> CreateOnboardingCoordinator()
override {
return std::move(coordinator_);
}
std::unique_ptr<AssistantSidePanelCoordinator> CreateSidePanel() override {
return std::move(side_panel_);
}
std::unique_ptr<autofill_assistant::HeadlessScriptController>
CreateHeadlessScriptController() override {
return std::move(external_script_controller_);
}
autofill_assistant::RuntimeManager* GetRuntimeManager() override {
return runtime_manager_;
}
std::unique_ptr<ApcScrimManager> CreateApcScrimManager() override {
return std::move(scrim_manager_);
}
std::unique_ptr<ApcExternalActionDelegate> CreateApcExternalActionDelegate()
override {
return std::move(apc_external_action_delegate_);
}
std::unique_ptr<autofill_assistant::WebsiteLoginManager>
CreateWebsiteLoginManager() override {
return std::move(website_login_manager_);
}
password_manager::PasswordManagerClient* GetPasswordManagerClient() override {
return password_manager_client_.get();
}
// Allows setting an onboarding coordinator that is returned by the factory
// function. Must be called at least once before every expected call to
// `CreateOnboardingCoordinator()`.
void InjectOnboardingCoordinatorForTesting(
std::unique_ptr<ApcOnboardingCoordinator> coordinator) {
coordinator_ = std::move(coordinator);
}
void InjectSidePanelForTesting(
std::unique_ptr<AssistantSidePanelCoordinator> side_panel) {
side_panel_ = std::move(side_panel);
}
// Allows setting an HeadlessScriptController. Must be called at least once
// before every expected call to `CreateHeadlessScriptController()`.
void InjectHeadlessScriptControllerForTesting(
std::unique_ptr<autofill_assistant::HeadlessScriptController>
external_script_controller) {
external_script_controller_ = std::move(external_script_controller);
}
// Allows setting an RunTimeManager.
void InjectRunTimeManagerForTesting(
autofill_assistant::RuntimeManager* runtime_manager) {
runtime_manager_ = runtime_manager;
}
// Allows setting an ApcScrimManager.
void InjectApcScrimManagerForTesting(
std::unique_ptr<ApcScrimManager> scrim_manager) {
scrim_manager_ = std::move(scrim_manager);
}
// Allows setting a ApcExternalActionDelegate.
void InjectApcExternalActionDelegateForTesting(
std::unique_ptr<ApcExternalActionDelegate> apc_external_action_delegate) {
apc_external_action_delegate_ = std::move(apc_external_action_delegate);
}
// Allows setting a WebsiteLoginManager.
void InjectWebsiteLoginManagerForTesting(
std::unique_ptr<autofill_assistant::WebsiteLoginManager>
website_login_manager) {
website_login_manager_ = std::move(website_login_manager);
}
// Allows setting an PasswordManagerClient.
void InjectPasswordManagerClientForTesting(
std::unique_ptr<password_manager::PasswordManagerClient>
password_manager_client) {
password_manager_client_ = std::move(password_manager_client);
}
private:
std::unique_ptr<ApcOnboardingCoordinator> coordinator_;
std::unique_ptr<AssistantSidePanelCoordinator> side_panel_;
std::unique_ptr<autofill_assistant::HeadlessScriptController>
external_script_controller_;
raw_ptr<autofill_assistant::RuntimeManager> runtime_manager_;
std::unique_ptr<ApcScrimManager> scrim_manager_;
std::unique_ptr<ApcExternalActionDelegate> apc_external_action_delegate_;
std::unique_ptr<autofill_assistant::WebsiteLoginManager>
website_login_manager_;
std::unique_ptr<password_manager::PasswordManagerClient>
password_manager_client_;
};
// static
TestApcClientImpl* TestApcClientImpl::CreateForWebContents(
content::WebContents* web_contents) {
const void* key = WebContentsUserData<ApcClientImpl>::UserDataKey();
web_contents->SetUserData(key,
std::make_unique<TestApcClientImpl>(web_contents));
return static_cast<TestApcClientImpl*>(web_contents->GetUserData(key));
}
class ApcClientImplTest : public ChromeRenderViewHostTestHarness {
public:
ApcClientImplTest() {
feature_list_.InitWithFeatures({features::kUnifiedSidePanel}, {});
}
void SetUp() override {
content::RenderViewHostTestHarness::SetUp();
test_apc_client_ = TestApcClientImpl::CreateForWebContents(web_contents());
// Prepare the coordinator.
auto coordinator = std::make_unique<MockApcOnboardingCoordinator>();
coordinator_ref_ = coordinator.get();
test_apc_client_->InjectOnboardingCoordinatorForTesting(
std::move(coordinator));
// Prepare the side panel.
auto side_panel = std::make_unique<MockAssistantSidePanelCoordinator>();
side_panel_ref_ = side_panel.get();
test_apc_client_->InjectSidePanelForTesting(std::move(side_panel));
// Register the observer of the side panel. During testing, we implicitly
// assume that there is only one.
ON_CALL(*side_panel_ref_, AddObserver)
.WillByDefault(SaveArg<0>(&side_panel_observer_));
// Prepare the HeadlessScriptController.
auto external_script_controller =
std::make_unique<autofill_assistant::MockHeadlessScriptController>();
external_script_controller_ref_ = external_script_controller.get();
test_apc_client_->InjectHeadlessScriptControllerForTesting(
std::move(external_script_controller));
// Prepare the RunTimeManager.
test_apc_client_->InjectRunTimeManagerForTesting(
mock_runtime_manager_.get());
// Prepare the ApcScrimManager.
auto scrim_manager = std::make_unique<MockApcScrimManager>();
scrim_manager_ref_ = scrim_manager.get();
test_apc_client_->InjectApcScrimManagerForTesting(std::move(scrim_manager));
// Prepare the PasswordManagerClient.
auto password_manager_client =
std::make_unique<password_manager::StubPasswordManagerClient>();
password_manager_client_ref_ = password_manager_client.get();
test_apc_client_->InjectPasswordManagerClientForTesting(
std::move(password_manager_client));
// Prepare the WebsiteLoginManager.
auto website_login_manager =
std::make_unique<autofill_assistant::MockWebsiteLoginManager>();
website_login_manager_ref_ = website_login_manager.get();
test_apc_client_->InjectWebsiteLoginManagerForTesting(
std::move(website_login_manager));
// Prepare the ApcExternalActionDelegate.
auto apc_external_action_delegate =
std::make_unique<MockApcExternalActionDelegate>(
side_panel_ref_, scrim_manager_ref_, website_login_manager_ref_);
apc_external_action_delegate_ref_ = apc_external_action_delegate.get();
test_apc_client_->InjectApcExternalActionDelegateForTesting(
std::move(apc_external_action_delegate));
}
TestApcClientImpl* apc_client() { return test_apc_client_; }
MockApcOnboardingCoordinator* coordinator() { return coordinator_ref_; }
MockAssistantSidePanelCoordinator* side_panel() { return side_panel_ref_; }
MockApcScrimManager* scrim_manager() { return scrim_manager_ref_; }
MockApcExternalActionDelegate* apc_external_action_delegate() {
return apc_external_action_delegate_ref_;
}
AssistantSidePanelCoordinator::Observer* side_panel_observer() {
return side_panel_observer_;
}
autofill_assistant::MockHeadlessScriptController*
external_script_controller() {
return external_script_controller_ref_;
}
autofill_assistant::MockRuntimeManager* runtime_manager() {
return mock_runtime_manager_.get();
}
private:
// Necessary to turn on the unified sidepanel.
base::test::ScopedFeatureList feature_list_;
// Pointers to mocked components that are injected into the `ApcClientImpl`.
raw_ptr<MockApcOnboardingCoordinator> coordinator_ref_ = nullptr;
raw_ptr<MockAssistantSidePanelCoordinator> side_panel_ref_ = nullptr;
raw_ptr<autofill_assistant::MockHeadlessScriptController>
external_script_controller_ref_ = nullptr;
raw_ptr<MockApcScrimManager> scrim_manager_ref_ = nullptr;
raw_ptr<MockApcExternalActionDelegate> apc_external_action_delegate_ref_ =
nullptr;
raw_ptr<password_manager::StubPasswordManagerClient>
password_manager_client_ref_ = nullptr;
raw_ptr<autofill_assistant::WebsiteLoginManager> website_login_manager_ref_ =
nullptr;
// The last registered side panel observer - may be null or dangling.
raw_ptr<AssistantSidePanelCoordinator::Observer> side_panel_observer_ =
nullptr;
// The object that is tested.
raw_ptr<TestApcClientImpl> test_apc_client_ = nullptr;
std::unique_ptr<autofill_assistant::MockRuntimeManager>
mock_runtime_manager_ =
std::make_unique<autofill_assistant::MockRuntimeManager>();
};
TEST_F(ApcClientImplTest, CreateAndStartApcFlow_Success) {
raw_ptr<ApcClient> client =
ApcClient::GetOrCreateForWebContents(web_contents());
// There is one client per WebContents.
EXPECT_EQ(client, apc_client());
// The `ApcClient` is paused.
EXPECT_FALSE(client->IsRunning());
// Prepare to extract the callback to the coordinator.
ApcOnboardingCoordinator::Callback coordinator_callback;
base::MockCallback<ApcClient::ResultCallback> result_callback1,
result_callback2;
EXPECT_CALL(*coordinator(), PerformOnboarding)
.WillOnce(MoveArg<0>(&coordinator_callback));
EXPECT_CALL(*runtime_manager(),
SetUIState(autofill_assistant::UIState::kShown));
EXPECT_CALL(*apc_external_action_delegate(), ShowStartingScreen(GURL(kUrl1)));
EXPECT_CALL(*scrim_manager(), Show());
client->Start(GURL(kUrl1), kUsername1, /*skip_login=*/false,
result_callback1.Get());
EXPECT_TRUE(client->IsRunning());
// We cannot start a second flow.
EXPECT_CALL(result_callback2, Run(false));
client->Start(GURL(kUrl1), kUsername1, /*skip_login=*/false,
result_callback2.Get(),
/*debug_run_information=*/absl::nullopt);
// Prepare to extract the callback to the external script controller.
base::OnceCallback<void(
autofill_assistant::HeadlessScriptController::ScriptResult)>
external_script_controller_callback;
EXPECT_CALL(*external_script_controller(), StartScript(_, _))
.Times(1)
.WillOnce(MoveArg<1>(&external_script_controller_callback));
// Successful onboarding.
std::move(coordinator_callback).Run(true);
EXPECT_TRUE(client->IsRunning());
autofill_assistant::HeadlessScriptController::ScriptResult script_result = {
.success = true};
EXPECT_CALL(*runtime_manager(),
SetUIState(autofill_assistant::UIState::kNotShown));
EXPECT_CALL(result_callback1, Run(true));
// Prepare to extract the callback from the completion screen call.
base::RepeatingClosure show_completion_screen_callback;
EXPECT_CALL(*apc_external_action_delegate(), ShowCompletionScreen(_))
.Times(1)
.WillOnce(MoveArg<0>(&show_completion_screen_callback));
std::move(external_script_controller_callback).Run(script_result);
std::move(show_completion_screen_callback).Run();
EXPECT_FALSE(client->IsRunning());
}
TEST_F(ApcClientImplTest, CreateAndStartApcFlow_ScriptFails) {
raw_ptr<ApcClient> client =
ApcClient::GetOrCreateForWebContents(web_contents());
// Prepare to extract the callback to the coordinator.
ApcOnboardingCoordinator::Callback coordinator_callback;
base::MockCallback<ApcClient::ResultCallback> result_callback1,
result_callback2;
EXPECT_CALL(*coordinator(), PerformOnboarding)
.WillOnce(MoveArg<0>(&coordinator_callback));
client->Start(GURL(kUrl1), kUsername1, /*skip_login=*/false,
result_callback1.Get());
// Prepare to extract the callback to the external script controller.
base::OnceCallback<void(
autofill_assistant::HeadlessScriptController::ScriptResult)>
external_script_controller_callback;
EXPECT_CALL(*external_script_controller(), StartScript(_, _))
.Times(1)
.WillOnce(MoveArg<1>(&external_script_controller_callback));
// Successful onboarding.
std::move(coordinator_callback).Run(true);
autofill_assistant::HeadlessScriptController::ScriptResult script_result = {
.success = false};
EXPECT_CALL(*apc_external_action_delegate(), ShowErrorScreen());
std::move(external_script_controller_callback).Run(script_result);
}
TEST_F(ApcClientImplTest, CreateAndStartApcFlow_fromSettings) {
// Prepare to extract the callback to the coordinator.
ApcOnboardingCoordinator::Callback coordinator_callback;
EXPECT_CALL(*coordinator(), PerformOnboarding)
.WillOnce(MoveArg<0>(&coordinator_callback));
apc_client()->Start(GURL(kUrl1), kUsername1, /*skip_login=*/false,
/*callback=*/base::DoNothing(),
/*debug_run_information=*/absl::nullopt);
// Prepare to extract the script_params to the external script
// controller.
base::flat_map<std::string, std::string> params_map;
EXPECT_CALL(*external_script_controller(), StartScript(_, _))
.Times(1)
.WillOnce(MoveArg<0>(¶ms_map));
// Successful onboarding.
std::move(coordinator_callback).Run(true);
EXPECT_TRUE(apc_client()->IsRunning());
EXPECT_THAT(params_map[kPasswordChangeSkipLoginParameter], StrEq("false"));
EXPECT_THAT(params_map[kSourceParameter],
StrEq(kSourcePasswordChangeSettings));
}
TEST_F(ApcClientImplTest, CreateAndStartApcFlow_fromLeakWarning) {
// Prepare to extract the callback to the coordinator.
ApcOnboardingCoordinator::Callback coordinator_callback;
EXPECT_CALL(*coordinator(), PerformOnboarding)
.Times(1)
.WillOnce(MoveArg<0>(&coordinator_callback));
// `skip_login = true` equals a trigger from leak warning.
apc_client()->Start(GURL(kUrl1), kUsername1, /*skip_login=*/true,
/*callback=*/base::DoNothing(),
/*debug_run_information=*/absl::nullopt);
// Prepare to extract the script_params to the external script
// controller.
base::flat_map<std::string, std::string> params_map;
EXPECT_CALL(*external_script_controller(), StartScript(_, _))
.Times(1)
.WillOnce(MoveArg<0>(¶ms_map));
// Successful onboarding.
std::move(coordinator_callback).Run(true);
EXPECT_THAT(params_map[kPasswordChangeSkipLoginParameter], StrEq("true"));
EXPECT_THAT(params_map[kSourceParameter],
StrEq(kSourcePasswordChangeLeakWarning));
}
TEST_F(ApcClientImplTest, CreateAndStartApcFlow_withDebugInformation) {
// Prepare to extract the callback to the coordinator.
ApcOnboardingCoordinator::Callback coordinator_callback;
EXPECT_CALL(*coordinator(), PerformOnboarding)
.Times(1)
.WillOnce(MoveArg<0>(&coordinator_callback));
apc_client()->Start(
GURL(kUrl1), kUsername1, /*skip_login=*/false,
/*callback=*/base::DoNothing(),
ApcClient::DebugRunInformation{.bundle_id = kDebugBundleId,
.socket_id = kDebugSocketId});
// Prepare to extract the script_params to the external script
// controller.
base::flat_map<std::string, std::string> params_map;
EXPECT_CALL(*external_script_controller(), StartScript(_, _))
.Times(1)
.WillOnce(MoveArg<0>(¶ms_map));
// Successful onboarding.
std::move(coordinator_callback).Run(true);
EXPECT_EQ(params_map[kDebugBundleIdParameter], kDebugBundleId);
EXPECT_EQ(params_map[kDebugSocketIdParameter], kDebugSocketId);
}
TEST_F(ApcClientImplTest, CreateAndStartApcFlow_WithFailedOnboarding) {
// Prepare to extract the callback to the coordinator.
ApcOnboardingCoordinator::Callback coordinator_callback;
EXPECT_CALL(*coordinator(), PerformOnboarding)
.Times(1)
.WillOnce(MoveArg<0>(&coordinator_callback));
apc_client()->Start(GURL(kUrl1), kUsername1, /*skip_login=*/true,
/*callback=*/base::DoNothing(),
/*debug_run_information=*/absl::nullopt);
// Fail onboarding.
std::move(coordinator_callback).Run(false);
EXPECT_FALSE(apc_client()->IsRunning());
}
TEST_F(ApcClientImplTest, CreateAndStartApcFlow_WithUnifiedSidePanelDisabled) {
base::test::ScopedFeatureList override_feature_list;
override_feature_list.InitWithFeatures({}, {features::kUnifiedSidePanel});
raw_ptr<ApcClient> client =
ApcClient::GetOrCreateForWebContents(web_contents());
// There is one client per WebContents.
EXPECT_EQ(client, apc_client());
// The `ApcClient` is paused.
EXPECT_FALSE(client->IsRunning());
EXPECT_CALL(*runtime_manager(),
SetUIState(autofill_assistant::UIState::kShown))
.Times(0);
// Starting it does not work.
client->Start(GURL(kUrl1), kUsername1, /*skip_login=*/true,
/*callback=*/base::DoNothing(),
/*debug_run_information=*/absl::nullopt);
EXPECT_FALSE(client->IsRunning());
}
TEST_F(ApcClientImplTest,
CreateAndStartApcFlow_WithoutPasswordClientManagerFlowStops) {
apc_client()->InjectPasswordManagerClientForTesting(nullptr);
apc_client()->Start(GURL(kUrl1), kUsername1, /*skip_login=*/true,
/*callback=*/base::DoNothing(),
/*debug_run_information=*/absl::nullopt);
// Fail run.
EXPECT_FALSE(apc_client()->IsRunning());
}
TEST_F(ApcClientImplTest, StopApcFlow) {
raw_ptr<ApcClient> client =
ApcClient::GetOrCreateForWebContents(web_contents());
base::MockCallback<ApcClient::ResultCallback> result_callback;
client->Start(GURL(kUrl1), kUsername1, /*skip_login=*/true,
result_callback.Get(), /*debug_run_information=*/absl::nullopt);
// Calling `Stop()` twice only triggers the callback the first time around.
EXPECT_CALL(result_callback, Run(false)).Times(1);
client->Stop();
client->Stop();
}
TEST_F(ApcClientImplTest, OnHidden_WithOngoingApcFlow) {
ASSERT_FALSE(side_panel_observer());
// Prepare to extract the callback to the coordinator.
ApcOnboardingCoordinator::Callback coordinator_callback;
EXPECT_CALL(*coordinator(), PerformOnboarding)
.Times(1)
.WillOnce(MoveArg<0>(&coordinator_callback));
EXPECT_CALL(*runtime_manager(),
SetUIState(autofill_assistant::UIState::kShown));
apc_client()->Start(GURL(kUrl1), kUsername1, /*skip_login=*/true,
/*callback=*/base::DoNothing(),
/*debug_run_information=*/absl::nullopt);
std::move(coordinator_callback).Run(true);
EXPECT_TRUE(apc_client()->IsRunning());
// The `ApcClientImpl` is registered as an observer to the side panel.
ASSERT_EQ(side_panel_observer(), apc_client());
EXPECT_CALL(*runtime_manager(),
SetUIState(autofill_assistant::UIState::kNotShown));
// Simulate hiding the side panel.
side_panel_observer()->OnHidden();
EXPECT_FALSE(apc_client()->IsRunning());
}
TEST_F(ApcClientImplTest, PromptForConsent) {
// `ApcClient` should forward the consent request to the onboarding
// coordinator.
ApcOnboardingCoordinator::Callback coordinator_callback;
EXPECT_CALL(*coordinator(), PerformOnboarding)
.Times(1)
.WillOnce(MoveArg<0>(&coordinator_callback));
base::MockCallback<ApcClient::OnboardingResultCallback> result_callback;
apc_client()->PromptForConsent(result_callback.Get());
EXPECT_TRUE(apc_client()->IsRunning());
EXPECT_CALL(result_callback, Run(true));
std::move(coordinator_callback).Run(true);
EXPECT_FALSE(apc_client()->IsRunning());
}
TEST_F(ApcClientImplTest, RevokeConsent) {
// `ApcClient` should forward the consent revokation to the onboarding
// coordinator.
ApcOnboardingCoordinator::Callback coordinator_callback;
EXPECT_CALL(
*coordinator(),
RevokeConsent(std::vector<int>({kDescriptionId1, kDescriptionId2})));
apc_client()->RevokeConsent({kDescriptionId1, kDescriptionId2});
}