File: browser_command_handler_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (696 lines) | stat: -rw-r--r-- 26,777 bytes parent folder | download | duplicates (4)
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
// Copyright 2020 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/browser/ui/webui/browser_command/browser_command_handler.h"

#include <memory>

#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/command_updater_impl.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/user_education/tutorial_identifiers.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/testing_profile.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "components/saved_tab_groups/public/features.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "components/user_education/common/help_bubble/help_bubble_factory_registry.h"
#include "components/user_education/common/tutorial/tutorial_identifier.h"
#include "components/user_education/common/tutorial/tutorial_registry.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/window_open_disposition.h"
#include "ui/base/window_open_disposition_utils.h"
#include "ui/webui/resources/js/browser_command/browser_command.mojom.h"

using browser_command::mojom::ClickInfo;
using browser_command::mojom::ClickInfoPtr;
using browser_command::mojom::Command;
using browser_command::mojom::CommandHandler;

namespace {

std::vector<Command> supported_commands = {
    Command::kUnknownCommand,  // Included for SupportedCommands test
    Command::kOpenSafetyCheck,
    Command::kOpenSafeBrowsingEnhancedProtectionSettings,
    Command::kOpenFeedbackForm,
    Command::kOpenPrivacyGuide,
    Command::kStartTabGroupTutorial,
    Command::kOpenPasswordManager,
    Command::kNoOpCommand,
    Command::kOpenPerformanceSettings,
    Command::kOpenNTPAndStartCustomizeChromeTutorial,
    Command::kStartPasswordManagerTutorial,
    Command::kStartSavedTabGroupTutorial,
    Command::kOpenAISettings,
    Command::kOpenSafetyCheckFromWhatsNew,
    Command::kOpenPaymentsSettings,
    Command::kOpenGlic,
    Command::kOpenGlicSettings,
};

const ui::ElementContext kTestContext1(1);

class TestCommandHandler : public BrowserCommandHandler {
 public:
  explicit TestCommandHandler(Profile* profile)
      : BrowserCommandHandler(mojo::PendingReceiver<CommandHandler>(),
                              profile,
                              supported_commands,
                              /*web_contents=*/nullptr) {}
  ~TestCommandHandler() override = default;

  void NavigateToEnhancedProtectionSetting() override {
    // The functionality of opening a URL is removed, as it cannot be executed
    // in a unittest.
  }

  void NavigateToURL(const GURL&, WindowOpenDisposition) override {
    // The functionality of opening a URL is removed, as it cannot be executed
    // in a unittest.
  }

  void OpenFeedbackForm() override {
    // The functionality of opening the feedback form is removed, as it cannot
    // be executed in a unittest.
  }

  void OpenPasswordManager() override {
    // The functionality of opening the password manager is removed, as it
    // cannot be executed in a unittest.
  }

  void OpenAISettings() override {
    // The functionality of opening the AI settings is removed, as it
    // cannot be executed in a unittest.
  }

  void OpenGlic() override {
    // The functionality of opening Glic is removed, as it
    // cannot be executed in a unittest.
  }

  bool TutorialServiceExists() override { return tutorial_service_exists_; }

  CommandUpdater* GetCommandUpdater() override {
    if (command_updater_) {
      return command_updater_.get();
    }
    return BrowserCommandHandler::GetCommandUpdater();
  }

  void SetCommandUpdater(std::unique_ptr<CommandUpdater> command_updater) {
    command_updater_ = std::move(command_updater);
    // Ensure that all commands are also updated in the new |command_updater|.
    EnableSupportedCommands();
  }

  void SetTutorialServiceExists(bool tutorial_service_exists) {
    tutorial_service_exists_ = tutorial_service_exists;
  }

  void SetBrowserSupportsTabGroups(bool is_supported) {
    tab_groups_feature_supported_ = is_supported;
  }

  void SetDefaultSearchProviderToGoogle(bool is_google) {
    default_search_provider_is_google_ = is_google;
  }

  void SetBrowserSupportsSavedTabGroups(bool is_supported) {
    saved_tab_groups_feature_supported_ = is_supported;
  }

 protected:
  bool BrowserSupportsTabGroups() override {
    return tab_groups_feature_supported_;
  }

  bool DefaultSearchProviderIsGoogle() override {
    return default_search_provider_is_google_;
  }

  bool BrowserSupportsSavedTabGroups() override {
    return saved_tab_groups_feature_supported_;
  }

 private:
  bool tutorial_service_exists_ = false;
  std::unique_ptr<CommandUpdater> command_updater_;

  bool tab_groups_feature_supported_ = true;
  bool default_search_provider_is_google_ = true;
  bool saved_tab_groups_feature_supported_ = true;
};

class TestTutorialService : public user_education::TutorialService {
 public:
  TestTutorialService(
      user_education::TutorialRegistry* tutorial_registry,
      user_education::HelpBubbleFactoryRegistry* help_bubble_factory_registry)
      : user_education::TutorialService(tutorial_registry,
                                        help_bubble_factory_registry) {}
  ~TestTutorialService() override = default;
  std::u16string GetBodyIconAltText(bool is_last_step) const override {
    return std::u16string();
  }

  void StartTutorial(user_education::TutorialIdentifier id,
                     ui::ElementContext context,
                     base::OnceClosure completed_callback,
                     base::OnceClosure aborted_callback,
                     base::RepeatingClosure restart_callback) override {
    running_id_ = id;
  }

  bool IsRunningTutorial(
      std::optional<user_education::TutorialIdentifier> id) const override {
    return id.has_value() ? id == running_id_ : running_id_.has_value();
  }

 private:
  std::optional<user_education::TutorialIdentifier> running_id_;
};

class MockTutorialService : public TestTutorialService {
 public:
  MockTutorialService(
      user_education::TutorialRegistry* tutorial_registry,
      user_education::HelpBubbleFactoryRegistry* help_bubble_factory_registry)
      : TestTutorialService(tutorial_registry, help_bubble_factory_registry) {}
  ~MockTutorialService() override = default;

  MOCK_METHOD(void,
              StartTutorial,
              (user_education::TutorialIdentifier,
               ui::ElementContext,
               base::OnceClosure,
               base::OnceClosure,
               base::RepeatingClosure));
  MOCK_METHOD(void,
              LogStartedFromWhatsNewPage,
              (user_education::TutorialIdentifier, bool));
  MOCK_CONST_METHOD1(IsRunningTutorial,
                     bool(std::optional<user_education::TutorialIdentifier>));
};

class MockCommandHandler : public TestCommandHandler {
 public:
  explicit MockCommandHandler(Profile* profile) : TestCommandHandler(profile) {}
  ~MockCommandHandler() override = default;

  MOCK_METHOD(void, StartTutorial, (StartTutorialInPage::Params params));

  MOCK_METHOD(void, NavigateToEnhancedProtectionSetting, ());

  MOCK_METHOD(void, NavigateToURL, (const GURL&, WindowOpenDisposition));

  MOCK_METHOD(void, OpenFeedbackForm, ());

  MOCK_METHOD(void, OpenPasswordManager, ());

  MOCK_METHOD(void, OpenAISettings, ());

  MOCK_METHOD(void, ShowCustomizeChromeToolbar, ());

  MOCK_METHOD(void, OpenGlic, ());

  MOCK_METHOD(void, OpenGlicSettings, ());
};

class MockCommandUpdater : public CommandUpdaterImpl {
 public:
  explicit MockCommandUpdater(CommandUpdaterDelegate* delegate)
      : CommandUpdaterImpl(delegate) {}
  ~MockCommandUpdater() override = default;

  MOCK_CONST_METHOD1(IsCommandEnabled, bool(int id));
  MOCK_CONST_METHOD1(SupportsCommand, bool(int id));
};

// Callback used for testing
// BrowserCommandHandler::CanExecuteCommand().
void CanExecuteCommandCallback(base::OnceClosure quit_closure,
                               bool* expected_can_show,
                               bool can_show) {
  *expected_can_show = can_show;
  std::move(quit_closure).Run();
}

// Callback used for testing BrowserCommandHandler::ExecuteCommand().
void ExecuteCommandCallback(base::OnceClosure quit_closure,
                            bool* expected_command_executed,
                            bool command_executed) {
  *expected_command_executed = command_executed;
  std::move(quit_closure).Run();
}

// A shorthand for conversion between ClickInfo and WindowOpenDisposition.
WindowOpenDisposition DispositionFromClick(const ClickInfo& info) {
  return ui::DispositionFromClick(info.middle_button, info.alt_key,
                                  info.ctrl_key, info.meta_key, info.shift_key);
}

class TestingChildProfile : public TestingProfile {
 public:
  bool IsChild() const override { return true; }
};

}  // namespace

class BrowserCommandHandlerTest : public testing::Test {
 public:
  BrowserCommandHandlerTest() = default;
  ~BrowserCommandHandlerTest() override = default;

  void SetUp() override {
    command_handler_ = std::make_unique<MockCommandHandler>(&profile_);
  }

  void TearDown() override { testing::Test::TearDown(); }

  MockCommandUpdater* mock_command_updater() {
    return static_cast<MockCommandUpdater*>(
        command_handler_->GetCommandUpdater());
  }

  bool CanExecuteCommand(Command command_id) {
    base::RunLoop run_loop;
    bool can_show = false;
    command_handler_->CanExecuteCommand(
        command_id, base::BindOnce(&CanExecuteCommandCallback,
                                   run_loop.QuitClosure(), &can_show));
    run_loop.Run();
    return can_show;
  }

  bool ExecuteCommand(Command command_id, ClickInfoPtr click_info) {
    base::RunLoop run_loop;
    bool command_executed = false;
    command_handler_->ExecuteCommand(
        command_id, std::move(click_info),
        base::BindOnce(&ExecuteCommandCallback, run_loop.QuitClosure(),
                       &command_executed));
    run_loop.Run();
    return command_executed;
  }

 protected:
  content::BrowserTaskEnvironment task_environment_;
  TestingProfile profile_;
  std::unique_ptr<MockCommandHandler> command_handler_;
};

TEST_F(BrowserCommandHandlerTest, SupportedCommands) {
  base::HistogramTester histogram_tester;

  // Mock out the command updater to test enabling and disabling commands.
  command_handler_->SetCommandUpdater(
      std::make_unique<MockCommandUpdater>(command_handler_.get()));

  // Unsupported commands do not get executed and no histogram is logged.
  EXPECT_CALL(*mock_command_updater(),
              SupportsCommand(static_cast<int>(Command::kUnknownCommand)))
      .WillOnce(testing::Return(false));

  EXPECT_FALSE(ExecuteCommand(Command::kUnknownCommand, ClickInfo::New()));
  histogram_tester.ExpectTotalCount(
      BrowserCommandHandler::kPromoBrowserCommandHistogramName, 0);

  // Disabled commands do not get executed and no histogram is logged.
  EXPECT_CALL(*mock_command_updater(),
              SupportsCommand(static_cast<int>(Command::kUnknownCommand)))
      .WillOnce(testing::Return(true));
  EXPECT_CALL(*mock_command_updater(),
              IsCommandEnabled(static_cast<int>(Command::kUnknownCommand)))
      .WillOnce(testing::Return(false));

  EXPECT_FALSE(ExecuteCommand(Command::kUnknownCommand, ClickInfo::New()));
  histogram_tester.ExpectTotalCount(
      BrowserCommandHandler::kPromoBrowserCommandHistogramName, 0);

  // Only supported and enabled commands get executed for which a histogram is
  // logged.
  EXPECT_CALL(*mock_command_updater(),
              SupportsCommand(static_cast<int>(Command::kUnknownCommand)))
      .WillOnce(testing::Return(true));
  EXPECT_CALL(*mock_command_updater(),
              IsCommandEnabled(static_cast<int>(Command::kUnknownCommand)))
      .WillOnce(testing::Return(true));

  EXPECT_TRUE(ExecuteCommand(Command::kUnknownCommand, ClickInfo::New()));
  histogram_tester.ExpectBucketCount(
      BrowserCommandHandler::kPromoBrowserCommandHistogramName, 0, 1);
}

TEST_F(BrowserCommandHandlerTest, CanExecuteCommand_OpenSafetyCheck) {
  // By default, showing the Safety Check promo is allowed.
  EXPECT_TRUE(
      CanExecuteCommand(Command::kOpenSafeBrowsingEnhancedProtectionSettings));

  // If the browser is managed, showing the Safety Check promo is not allowed.
  TestingProfile::Builder builder;
  builder.OverridePolicyConnectorIsManagedForTesting(true);
  std::unique_ptr<TestingProfile> profile = builder.Build();
  command_handler_ = std::make_unique<MockCommandHandler>(profile.get());
  EXPECT_FALSE(CanExecuteCommand(Command::kOpenSafetyCheck));
}

TEST_F(BrowserCommandHandlerTest, OpenSafetyCheckCommand) {
  // The OpenSafetyCheck command opens a new settings window with the Safety
  // Check, and the correct disposition.
  ClickInfoPtr info = ClickInfo::New();
  info->middle_button = true;
  info->meta_key = true;
  EXPECT_CALL(
      *command_handler_,
      NavigateToURL(GURL(chrome::GetSettingsUrl(chrome::kSafetyCheckSubPage)),
                    DispositionFromClick(*info)));
  EXPECT_TRUE(ExecuteCommand(Command::kOpenSafetyCheck, std::move(info)));
}

TEST_F(BrowserCommandHandlerTest, OpenSafetyCheckFromWhatsNewCommand) {
  EXPECT_TRUE(CanExecuteCommand(Command::kOpenSafetyCheckFromWhatsNew));
  // The OpenSafetyCheck command opens a new settings window with the Safety
  // Check, and the correct disposition.
  ClickInfoPtr info = ClickInfo::New();
  info->middle_button = true;
  info->meta_key = true;
  EXPECT_CALL(
      *command_handler_,
      NavigateToURL(GURL(chrome::GetSettingsUrl(chrome::kSafetyCheckSubPage)),
                    DispositionFromClick(*info)));
  EXPECT_TRUE(
      ExecuteCommand(Command::kOpenSafetyCheckFromWhatsNew, std::move(info)));
}

TEST_F(BrowserCommandHandlerTest,
       CanShowSafeBrowsingEnhancedProtectionCommandPromo_NoPolicies) {
  EXPECT_TRUE(
      CanExecuteCommand(Command::kOpenSafeBrowsingEnhancedProtectionSettings));
}

TEST_F(
    BrowserCommandHandlerTest,
    CanShowSafeBrowsingEnhancedProtectionCommandPromo_EnhancedProtectionEnabled) {
  TestingProfile::Builder builder;
  std::unique_ptr<TestingProfile> profile = builder.Build();
  profile->GetTestingPrefService()->SetUserPref(
      prefs::kSafeBrowsingEnhanced, std::make_unique<base::Value>(true));
  command_handler_ = std::make_unique<MockCommandHandler>(profile.get());

  EXPECT_FALSE(
      CanExecuteCommand(Command::kOpenSafeBrowsingEnhancedProtectionSettings));
}

TEST_F(
    BrowserCommandHandlerTest,
    CanShowSafeBrowsingEnhancedProtectionCommandPromo_HasSafeBrowsingManaged_NoProtection) {
  TestingProfile::Builder builder;
  std::unique_ptr<TestingProfile> profile = builder.Build();
  profile->GetTestingPrefService()->SetManagedPref(
      prefs::kSafeBrowsingEnabled, std::make_unique<base::Value>(false));
  profile->GetTestingPrefService()->SetManagedPref(
      prefs::kSafeBrowsingEnhanced, std::make_unique<base::Value>(false));
  command_handler_ = std::make_unique<MockCommandHandler>(profile.get());

  EXPECT_FALSE(
      CanExecuteCommand(Command::kOpenSafeBrowsingEnhancedProtectionSettings));
}

TEST_F(
    BrowserCommandHandlerTest,
    CanShowSafeBrowsingEnhancedProtectionCommandPromo_HasSafeBrowsingManaged_StandardProtection) {
  TestingProfile::Builder builder;
  std::unique_ptr<TestingProfile> profile = builder.Build();
  profile->GetTestingPrefService()->SetManagedPref(
      prefs::kSafeBrowsingEnabled, std::make_unique<base::Value>(true));
  profile->GetTestingPrefService()->SetManagedPref(
      prefs::kSafeBrowsingEnhanced, std::make_unique<base::Value>(false));
  command_handler_ = std::make_unique<MockCommandHandler>(profile.get());

  EXPECT_FALSE(
      CanExecuteCommand(Command::kOpenSafeBrowsingEnhancedProtectionSettings));
}

TEST_F(
    BrowserCommandHandlerTest,
    CanShowSafeBrowsingEnhancedProtectionCommandPromo_HasSafeBrowsingManaged_EnhancedProtection) {
  TestingProfile::Builder builder;
  std::unique_ptr<TestingProfile> profile = builder.Build();
  profile->GetTestingPrefService()->SetManagedPref(
      prefs::kSafeBrowsingEnabled, std::make_unique<base::Value>(true));
  profile->GetTestingPrefService()->SetManagedPref(
      prefs::kSafeBrowsingEnhanced, std::make_unique<base::Value>(true));
  command_handler_ = std::make_unique<MockCommandHandler>(profile.get());

  EXPECT_FALSE(
      CanExecuteCommand(Command::kOpenSafeBrowsingEnhancedProtectionSettings));
}

TEST_F(BrowserCommandHandlerTest, OpenSafeBrowsingEnhancedProtectionCommand) {
  // The kOpenSafeBrowsingEnhancedProtectionSettings command opens a new
  // settings window with the Safe Browsing settings with the Enhanced
  // Protection section expanded, and an In-product help bubble
  ClickInfoPtr info = ClickInfo::New();
  info->middle_button = true;
  info->meta_key = true;
  EXPECT_CALL(*command_handler_, NavigateToEnhancedProtectionSetting());
  EXPECT_TRUE(ExecuteCommand(
      Command::kOpenSafeBrowsingEnhancedProtectionSettings, std::move(info)));
}

TEST_F(BrowserCommandHandlerTest, OpenFeedbackFormCommand) {
  // Open feedback form command calls open feedback form.
  ClickInfoPtr info = ClickInfo::New();
  EXPECT_CALL(*command_handler_, OpenFeedbackForm());
  EXPECT_TRUE(ExecuteCommand(Command::kOpenFeedbackForm, std::move(info)));
}

TEST_F(BrowserCommandHandlerTest, CanExecuteCommand_OpenPrivacyGuide) {
  EXPECT_TRUE(CanExecuteCommand(Command::kOpenPrivacyGuide));

  // Showing the Privacy Guide promo is not allowed if the browser is managed.
  {
    TestingProfile::Builder builder;
    builder.OverridePolicyConnectorIsManagedForTesting(true);
    std::unique_ptr<TestingProfile> profile = builder.Build();
    command_handler_ = std::make_unique<MockCommandHandler>(profile.get());
    EXPECT_FALSE(CanExecuteCommand(Command::kOpenPrivacyGuide));
  }

  // Neither is it if the profile belongs to a child.
  {
    TestingChildProfile profile;
    command_handler_ = std::make_unique<MockCommandHandler>(&profile);
    EXPECT_FALSE(CanExecuteCommand(Command::kOpenPrivacyGuide));
  }
}

TEST_F(BrowserCommandHandlerTest, OpenPrivacyGuideCommand) {
  // The OpenPrivacyGuide command opens a new settings window with the Privacy
  // Guide, and the correct disposition.
  ClickInfoPtr info = ClickInfo::New();
  info->middle_button = true;
  info->meta_key = true;
  EXPECT_CALL(
      *command_handler_,
      NavigateToURL(GURL(chrome::GetSettingsUrl(chrome::kPrivacyGuideSubPage)),
                    DispositionFromClick(*info)));
  EXPECT_TRUE(ExecuteCommand(Command::kOpenPrivacyGuide, std::move(info)));
}

TEST_F(BrowserCommandHandlerTest, StartTabGroupTutorialCommand) {
  // Command cannot be executed if the tutorial service doesn't exist.
  command_handler_->SetTutorialServiceExists(false);
  EXPECT_FALSE(CanExecuteCommand(Command::kStartTabGroupTutorial));

  // Create mock service so the command can be executed.
  auto bubble_factory_registry =
      std::make_unique<user_education::HelpBubbleFactoryRegistry>();
  user_education::TutorialRegistry registry;
  MockTutorialService service(&registry, bubble_factory_registry.get());

  // Allow command to be executed.
  command_handler_->SetTutorialServiceExists(true);

  // If the browsers Tab Strip does not support tutorials, dont run the command.
  command_handler_->SetBrowserSupportsTabGroups(false);
  EXPECT_FALSE(CanExecuteCommand(Command::kStartTabGroupTutorial));

  // If the browser supports tab groups and has a tutorial service it should
  // allow running commands.
  command_handler_->SetBrowserSupportsTabGroups(true);
  EXPECT_TRUE(CanExecuteCommand(Command::kStartTabGroupTutorial));

  // The StartTabGroupTutorial command should start the tab group tutorial.
  {
    ClickInfoPtr info = ClickInfo::New();
    EXPECT_CALL(*command_handler_, StartTutorial)
        .WillOnce([&](StartTutorialInPage::Params params) {
          EXPECT_EQ(params.tutorial_id, kTabGroupTutorialId);
        });
    EXPECT_TRUE(
        ExecuteCommand(Command::kStartTabGroupTutorial, std::move(info)));
  }
}

TEST_F(BrowserCommandHandlerTest, OpenPasswordManagerCommand) {
  // By default, opening the password manager is allowed.
  EXPECT_TRUE(CanExecuteCommand(Command::kOpenPasswordManager));
  ClickInfoPtr info = ClickInfo::New();
  info->middle_button = true;
  info->meta_key = true;
  // The OpenPassswordManager command opens a new settings window with the
  // password manager and the correct disposition.
  EXPECT_CALL(*command_handler_, OpenPasswordManager());
  EXPECT_TRUE(ExecuteCommand(Command::kOpenPasswordManager, std::move(info)));
}

TEST_F(BrowserCommandHandlerTest, OpenPerformanceSettings) {
  EXPECT_TRUE(CanExecuteCommand(Command::kOpenPerformanceSettings));

  // Confirm executing the command works.
  ClickInfoPtr info = ClickInfo::New();
  info->middle_button = true;
  info->meta_key = true;
  // The OpenPerformanceSettings command opens a new settings window with the
  // performance page open.
  EXPECT_CALL(
      *command_handler_,
      NavigateToURL(GURL(chrome::GetSettingsUrl(chrome::kPerformanceSubPage)),
                    DispositionFromClick(*info)));
  EXPECT_TRUE(
      ExecuteCommand(Command::kOpenPerformanceSettings, std::move(info)));
}

TEST_F(BrowserCommandHandlerTest,
       OpenNTPAndStartCustomizeChromeTutorialCommand) {
  // Command cannot be executed if the tutorial service doesn't exist.
  command_handler_->SetTutorialServiceExists(false);
  EXPECT_FALSE(
      CanExecuteCommand(Command::kOpenNTPAndStartCustomizeChromeTutorial));

  // Create mock service so the command can be executed.
  auto bubble_factory_registry =
      std::make_unique<user_education::HelpBubbleFactoryRegistry>();
  user_education::TutorialRegistry registry;
  MockTutorialService service(&registry, bubble_factory_registry.get());

  // Allow command to be executed.
  command_handler_->SetTutorialServiceExists(true);

  // If the search provider is not set to Google, dont run the command
  command_handler_->SetDefaultSearchProviderToGoogle(false);
  EXPECT_FALSE(
      CanExecuteCommand(Command::kOpenNTPAndStartCustomizeChromeTutorial));
  command_handler_->SetDefaultSearchProviderToGoogle(true);

  // If the browser has feature enabled and google is default search
  // provider, allow running command
  EXPECT_TRUE(
      CanExecuteCommand(Command::kOpenNTPAndStartCustomizeChromeTutorial));

  // The OpenNTPAndStartCustomizeChromeTutorialCommand command should
  // start the customize chrome tutorial.
  {
    ClickInfoPtr info = ClickInfo::New();
    EXPECT_CALL(*command_handler_, StartTutorial)
        .WillOnce([&](StartTutorialInPage::Params params) {
          EXPECT_EQ(params.tutorial_id, kSidePanelCustomizeChromeTutorialId);
        });
    EXPECT_TRUE(ExecuteCommand(Command::kOpenNTPAndStartCustomizeChromeTutorial,
                               std::move(info)));
  }
}

TEST_F(BrowserCommandHandlerTest, StartPasswordManagerTutorialCommand) {
  // Command cannot be executed if the tutorial service doesn't exist.
  command_handler_->SetTutorialServiceExists(false);
  EXPECT_FALSE(CanExecuteCommand(Command::kStartPasswordManagerTutorial));

  // Create mock service so the command can be executed.
  auto bubble_factory_registry =
      std::make_unique<user_education::HelpBubbleFactoryRegistry>();
  user_education::TutorialRegistry registry;
  MockTutorialService service(&registry, bubble_factory_registry.get());

  // Allow command to be executed.
  command_handler_->SetTutorialServiceExists(true);

  EXPECT_TRUE(CanExecuteCommand(Command::kStartPasswordManagerTutorial));

  ClickInfoPtr info = ClickInfo::New();
  EXPECT_CALL(*command_handler_, StartTutorial)
      .WillOnce([&](StartTutorialInPage::Params params) {
        EXPECT_EQ(params.tutorial_id, kPasswordManagerTutorialId);
      });
  EXPECT_TRUE(
      ExecuteCommand(Command::kStartPasswordManagerTutorial, std::move(info)));

  EXPECT_CALL(service, IsRunningTutorial).WillOnce(testing::Return(true));
  EXPECT_CALL(service, LogStartedFromWhatsNewPage)
      .WillOnce(
          [&](user_education::TutorialIdentifier tutorial_id, bool is_running) {
            EXPECT_EQ(tutorial_id, kPasswordManagerTutorialId);
            EXPECT_TRUE(is_running);
            return;
          });

  // Manually call tutorial started callback.
  command_handler_->OnTutorialStarted(kPasswordManagerTutorialId, &service);
}

TEST_F(BrowserCommandHandlerTest, OpenAISettingsCommand) {
  // By default, opening the password manager is allowed.
  EXPECT_TRUE(CanExecuteCommand(Command::kOpenAISettings));
  ClickInfoPtr info = ClickInfo::New();
  info->middle_button = true;
  info->meta_key = true;
  // The OpenAISettings command opens a new settings window with the
  // AI settings and the correct disposition.
  EXPECT_CALL(*command_handler_, OpenAISettings());
  EXPECT_TRUE(ExecuteCommand(Command::kOpenAISettings, std::move(info)));
}

TEST_F(BrowserCommandHandlerTest, OpenPaymentsSettingsCommand) {
  // The OpenPaymentsSettings command opens a new settings window with the
  // Payments settings sub page, and the correct disposition.
  EXPECT_TRUE(CanExecuteCommand(Command::kOpenPaymentsSettings));
  ClickInfoPtr info = ClickInfo::New();
  info->middle_button = true;
  info->meta_key = true;
  EXPECT_CALL(
      *command_handler_,
      NavigateToURL(GURL(chrome::GetSettingsUrl(chrome::kPaymentsSubPage)),
                    DispositionFromClick(*info)));
  EXPECT_TRUE(ExecuteCommand(Command::kOpenPaymentsSettings, std::move(info)));
}

TEST_F(BrowserCommandHandlerTest, OpenGlicCommand) {
  // By default, opening Glic is allowed.
  EXPECT_TRUE(CanExecuteCommand(Command::kOpenGlic));
  ClickInfoPtr info = ClickInfo::New();
  info->middle_button = true;
  info->meta_key = true;
  // The OpenGlic command opens glic.
  EXPECT_CALL(*command_handler_, OpenGlic());
  EXPECT_TRUE(ExecuteCommand(Command::kOpenGlic, std::move(info)));
}

TEST_F(BrowserCommandHandlerTest, OpenGlicSettingsCommand) {
  // The OpenGlicSettings command opens a new settings window
  // with the Glic settings sub page, and the correct disposition.
  EXPECT_TRUE(CanExecuteCommand(Command::kOpenGlicSettings));
  ClickInfoPtr info = ClickInfo::New();
  info->middle_button = true;
  info->meta_key = true;
  EXPECT_CALL(*command_handler_, OpenGlicSettings());
  EXPECT_TRUE(ExecuteCommand(Command::kOpenGlicSettings, std::move(info)));
}