File: system_tray_client_impl_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 (670 lines) | stat: -rw-r--r-- 26,594 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
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
// Copyright 2016 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/ash/system/system_tray_client_impl.h"

#include "ash/constants/web_app_id_constants.h"
#include "ash/public/cpp/ash_view_ids.h"
#include "ash/public/cpp/login_screen_test_api.h"
#include "ash/public/cpp/login_types.h"
#include "ash/public/cpp/system_tray_test_api.h"
#include "ash/shell.h"
#include "ash/system/model/enterprise_domain_model.h"
#include "ash/system/model/system_tray_model.h"
#include "base/i18n/time_formatting.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_base.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/ash/login/lock/screen_locker_tester.h"
#include "chrome/browser/ash/login/login_manager_test.h"
#include "chrome/browser/ash/login/test/local_state_mixin.h"
#include "chrome/browser/ash/login/test/login_manager_mixin.h"
#include "chrome/browser/ash/login/test/user_policy_mixin.h"
#include "chrome/browser/ash/policy/core/browser_policy_connector_ash.h"
#include "chrome/browser/ash/policy/core/device_cloud_policy_manager_ash.h"
#include "chrome/browser/ash/policy/core/device_cloud_policy_store_ash.h"
#include "chrome/browser/ash/policy/core/device_policy_cros_browser_test.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/ash/login/user_adding_screen.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/webui_url_constants.h"
#include "chromeos/ash/components/install_attributes/stub_install_attributes.h"
#include "chromeos/ash/components/policy/device_policy/cached_device_policy_updater.h"
#include "chromeos/ash/components/settings/cros_settings_names.h"
#include "chromeos/strings/grit/chromeos_strings.h"
#include "components/account_id/account_id.h"
#include "components/policy/core/common/cloud/cloud_policy_constants.h"
#include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
#include "components/policy/proto/device_management_backend.pb.h"
#include "components/prefs/pref_service.h"
#include "components/services/app_service/public/cpp/app_registry_cache.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/services/app_service/public/cpp/intent_filter_util.h"
#include "components/user_manager/known_user.h"
#include "components/user_manager/user_manager.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_utils.h"
#include "google_apis/gaia/gaia_id.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/chromeos/devicetype_utils.h"
#include "url/gurl.h"

using ::ash::ProfileHelper;
using user_manager::UserManager;

namespace {

const char kManager[] = "admin@example.com";
const char16_t kManager16[] = u"admin@example.com";
const char kNewUser[] = "new_test_user@gmail.com";
const GaiaId::Literal kNewGaiaID("11111");
const char kManagedUser[] = "user@example.com";
const GaiaId::Literal kManagedGaiaID("33333");

}  // namespace

using SystemTrayClientEnterpriseTest = policy::DevicePolicyCrosBrowserTest;

IN_PROC_BROWSER_TEST_F(SystemTrayClientEnterpriseTest, TrayEnterprise) {
  auto test_api = ash::SystemTrayTestApi::Create();

  // Managed devices show an item in the menu.
  EXPECT_TRUE(test_api->IsBubbleViewVisible(ash::VIEW_ID_QS_MANAGED_BUTTON,
                                            true /* open_tray */));
  std::u16string expected_text =
      l10n_util::GetStringFUTF16(IDS_ASH_SHORT_MANAGED_BY, u"example.com");
  EXPECT_EQ(expected_text,
            test_api->GetBubbleViewTooltip(ash::VIEW_ID_QS_MANAGED_BUTTON));

  // Clicking the item opens the management page.
  test_api->ClickBubbleView(ash::VIEW_ID_QS_MANAGED_BUTTON);
  EXPECT_EQ(
      GURL(chrome::kChromeUIManagementURL),
      browser()->tab_strip_model()->GetActiveWebContents()->GetVisibleURL());
}

using ConsumerDeviceTest = MixinBasedInProcessBrowserTest;

// Verify that the management device mode is indeed none of the
// enterprise licenses.
IN_PROC_BROWSER_TEST_F(ConsumerDeviceTest, WithNoLicense) {
  EXPECT_EQ(ash::Shell::Get()
                ->system_tray_model()
                ->enterprise_domain()
                ->management_device_mode(),
            ash::ManagementDeviceMode::kNone);
}

class EnterpriseManagedTest : public MixinBasedInProcessBrowserTest {
 public:
  EnterpriseManagedTest() { device_state_.set_skip_initial_policy_setup(true); }
  ~EnterpriseManagedTest() override = default;
  EnterpriseManagedTest(const EnterpriseManagedTest&) = delete;
  void operator=(const EnterpriseManagedTest&) = delete;

 protected:
  policy::DevicePolicyCrosTestHelper* policy_helper() {
    return &policy_helper_;
  }

 private:
  ash::DeviceStateMixin device_state_{
      &mixin_host_,
      ash::DeviceStateMixin::State::OOBE_COMPLETED_CLOUD_ENROLLED};
  policy::DevicePolicyCrosTestHelper policy_helper_;
};

// Verify that the management device mode is indeed Kiosk Sku.
IN_PROC_BROWSER_TEST_F(EnterpriseManagedTest, WithKioskSku) {
  policy_helper()->device_policy()->policy_data().set_license_sku(
      policy::kKioskSkuName);
  policy_helper()->RefreshPolicyAndWaitUntilDeviceCloudPolicyUpdated();

  EXPECT_EQ(ash::Shell::Get()
                ->system_tray_model()
                ->enterprise_domain()
                ->management_device_mode(),
            ash::ManagementDeviceMode::kKioskSku);
}

// Verify that the management device mode is indeed education license.
IN_PROC_BROWSER_TEST_F(EnterpriseManagedTest, WithEducationLicense) {
  policy_helper()->device_policy()->policy_data().set_market_segment(
      enterprise_management::PolicyData::ENROLLED_EDUCATION);
  policy_helper()->RefreshPolicyAndWaitUntilDeviceCloudPolicyUpdated();

  EXPECT_EQ(ash::Shell::Get()
                ->system_tray_model()
                ->enterprise_domain()
                ->management_device_mode(),
            ash::ManagementDeviceMode::kChromeEducation);
}

// Verify that the management device mode is indeed enterprise license.
IN_PROC_BROWSER_TEST_F(EnterpriseManagedTest, WithEnterpriseLicense) {
  policy_helper()->device_policy()->policy_data().set_market_segment(
      enterprise_management::PolicyData::ENROLLED_ENTERPRISE);
  policy_helper()->RefreshPolicyAndWaitUntilDeviceCloudPolicyUpdated();

  EXPECT_EQ(ash::Shell::Get()
                ->system_tray_model()
                ->enterprise_domain()
                ->management_device_mode(),
            ash::ManagementDeviceMode::kChromeEnterprise);
}

// Verify that the management device mode is indeed unknown when the market
// segment of the device policy data does not have value.
IN_PROC_BROWSER_TEST_F(EnterpriseManagedTest, WithUnknownLicense) {
  policy_helper()->RefreshPolicyAndWaitUntilDeviceCloudPolicyUpdated();

  EXPECT_EQ(ash::Shell::Get()
                ->system_tray_model()
                ->enterprise_domain()
                ->management_device_mode(),
            ash::ManagementDeviceMode::kOther);
}

class SystemTrayClientClockTest : public ash::LoginManagerTest {
 public:
  SystemTrayClientClockTest() : LoginManagerTest() {
    // Use consumer emails to avoid having to fake a policy fetch.
    login_mixin_.AppendRegularUsers(2);
    account_id1_ = login_mixin_.users()[0].account_id;
    account_id2_ = login_mixin_.users()[1].account_id;
  }

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

  ~SystemTrayClientClockTest() override = default;

  void SetupUserProfile(const AccountId& account_id, bool use_24_hour_clock) {
    const user_manager::User* user = UserManager::Get()->FindUser(account_id);
    Profile* profile = ProfileHelper::Get()->GetProfileByUser(user);
    profile->GetPrefs()->SetBoolean(prefs::kUse24HourClock, use_24_hour_clock);
    // Allow clock setting to be sent to ash over mojo.
    content::RunAllPendingInMessageLoop();
  }

 protected:
  AccountId account_id1_;
  AccountId account_id2_;
  ash::LoginManagerMixin login_mixin_{&mixin_host_};
};

// Test that clock type is taken from user profile for current active user.
IN_PROC_BROWSER_TEST_F(SystemTrayClientClockTest, TestMultiProfile24HourClock) {
  auto tray_test_api = ash::SystemTrayTestApi::Create();

  // Login a user with a 24-hour clock.
  LoginUser(account_id1_);
  SetupUserProfile(account_id1_, true /* use_24_hour_clock */);
  EXPECT_TRUE(tray_test_api->Is24HourClock());

  // Add a user with a 12-hour clock.
  ash::UserAddingScreen::Get()->Start();
  content::RunAllPendingInMessageLoop();
  AddUser(account_id2_);
  SetupUserProfile(account_id2_, false /* use_24_hour_clock */);
  EXPECT_FALSE(tray_test_api->Is24HourClock());

  // Switch back to the user with the 24-hour clock.
  UserManager::Get()->SwitchActiveUser(account_id1_);
  // Allow clock setting to be sent to ash over mojo.
  content::RunAllPendingInMessageLoop();
  EXPECT_TRUE(tray_test_api->Is24HourClock());
}

// Test that on the login and lock screen clock type is taken from user profile
// of the focused pod.
IN_PROC_BROWSER_TEST_F(SystemTrayClientClockTest, PRE_FocusedPod24HourClock) {
  auto tray_test_api = ash::SystemTrayTestApi::Create();

  // Login a user with a 24-hour clock.
  LoginUser(account_id1_);
  SetupUserProfile(account_id1_, true /* use_24_hour_clock */);
  EXPECT_TRUE(tray_test_api->Is24HourClock());

  // Add a user with a 12-hour clock.
  ash::UserAddingScreen::Get()->Start();
  AddUser(account_id2_);
  SetupUserProfile(account_id2_, false /* use_24_hour_clock */);
  EXPECT_FALSE(tray_test_api->Is24HourClock());

  // Test lock screen.
  ash::ScreenLockerTester locker;
  locker.Lock();

  EXPECT_TRUE(ash::LoginScreenTestApi::FocusUser(account_id1_));
  EXPECT_TRUE(tray_test_api->Is24HourClock());

  EXPECT_TRUE(ash::LoginScreenTestApi::FocusUser(account_id2_));
  EXPECT_FALSE(tray_test_api->Is24HourClock());
}

IN_PROC_BROWSER_TEST_F(SystemTrayClientClockTest, FocusedPod24HourClock) {
  auto tray_test_api = ash::SystemTrayTestApi::Create();
  // Test login screen.
  EXPECT_TRUE(ash::LoginScreenTestApi::FocusUser(account_id1_));
  EXPECT_TRUE(tray_test_api->Is24HourClock());

  EXPECT_TRUE(ash::LoginScreenTestApi::FocusUser(account_id2_));
  EXPECT_FALSE(tray_test_api->Is24HourClock());
}

class SystemTrayClientClockUnknownPrefTest : public SystemTrayClientClockTest {
 public:
  // SystemTrayClientClockTest:
  void SetUpInProcessBrowserTestFixture() override {
    SystemTrayClientClockTest::SetUpInProcessBrowserTestFixture();
    policy::CachedDevicePolicyUpdater updater;
    updater.payload().mutable_use_24hour_clock()->set_use_24hour_clock(true);
    updater.Commit();
  }

  void SetUpLocalStatePrefService(PrefService* local_state) override {
    SystemTrayClientClockTest::SetUpLocalStatePrefService(local_state);
    user_manager::KnownUser known_user(local_state);
    // First user does not have a preference.
    ASSERT_FALSE(known_user.FindBoolPath(account_id1_, ::prefs::kUse24HourClock)
                     .has_value());

    // Set preference for the second user only.
    known_user.SetBooleanPref(account_id2_, ::prefs::kUse24HourClock, false);
  }

 protected:
  ash::ScopedStubInstallAttributes install_attributes_{
      ash::StubInstallAttributes::CreateCloudManaged("fake-domain.com",
                                                     "fake-id")};
  ash::DeviceStateMixin device_state_{
      &mixin_host_,
      ash::DeviceStateMixin::State::OOBE_COMPLETED_CONSUMER_OWNED};
};

IN_PROC_BROWSER_TEST_F(SystemTrayClientClockUnknownPrefTest, SwitchToDefault) {
  // Check default value.
  ASSERT_EQ(base::GetHourClockType(), base::k12HourClock);

  auto tray_test_api = ash::SystemTrayTestApi::Create();
  EXPECT_EQ(ash::LoginScreenTestApi::GetFocusedUser(), account_id1_);
  // Should be system setting because the first user does not have a preference.
  EXPECT_TRUE(tray_test_api->Is24HourClock());

  // Check user with the set preference.
  EXPECT_TRUE(ash::LoginScreenTestApi::FocusUser(account_id2_));
  EXPECT_FALSE(tray_test_api->Is24HourClock());

  // Should get back to the system settings.
  EXPECT_TRUE(ash::LoginScreenTestApi::FocusUser(account_id1_));
  EXPECT_TRUE(tray_test_api->Is24HourClock());
}

class SystemTrayClientEnterpriseAccountTest : public ash::LoginManagerTest {
 protected:
  SystemTrayClientEnterpriseAccountTest() {
    std::unique_ptr<ash::ScopedUserPolicyUpdate> scoped_user_policy_update =
        user_policy_mixin_.RequestPolicyUpdate();
    scoped_user_policy_update->policy_data()->set_managed_by(kManager);
  }

  const ash::LoginManagerMixin::TestUserInfo unmanaged_user_{
      AccountId::FromUserEmailGaiaId(kNewUser, kNewGaiaID)};
  const ash::LoginManagerMixin::TestUserInfo managed_user_{
      AccountId::FromUserEmailGaiaId(kManagedUser, kManagedGaiaID)};
  ash::UserPolicyMixin user_policy_mixin_{&mixin_host_,
                                          managed_user_.account_id};
  ash::LoginManagerMixin login_mixin_{&mixin_host_,
                                      {managed_user_, unmanaged_user_}};
};

IN_PROC_BROWSER_TEST_F(SystemTrayClientEnterpriseAccountTest,
                       TrayEnterpriseManagedAccount) {
  auto test_api = ash::SystemTrayTestApi::Create();

  // User hasn't signed in yet, user management should not be shown in tray.
  EXPECT_FALSE(test_api->IsBubbleViewVisible(ash::VIEW_ID_QS_MANAGED_BUTTON,
                                             true /* open_tray */));

  // After login, the tray should show user management information.
  LoginUser(managed_user_.account_id);
  EXPECT_TRUE(test_api->IsBubbleViewVisible(ash::VIEW_ID_QS_MANAGED_BUTTON,
                                            true /* open_tray */));
  EXPECT_EQ(l10n_util::GetStringFUTF16(IDS_ASH_SHORT_MANAGED_BY, kManager16),
            test_api->GetBubbleViewTooltip(ash::VIEW_ID_QS_MANAGED_BUTTON));

  // Switch to unmanaged account should still show the managed string (since the
  // primary user is managed user). However, the string should not contain the
  // account manager of the primary user.
  ash::UserAddingScreen::Get()->Start();
  AddUser(unmanaged_user_.account_id);
  EXPECT_TRUE(test_api->IsBubbleViewVisible(ash::VIEW_ID_QS_MANAGED_BUTTON,
                                            true /* open_tray */));
  EXPECT_EQ(l10n_util::GetStringFUTF16(IDS_ASH_ENTERPRISE_DEVICE_MANAGED,
                                       ui::GetChromeOSDeviceName()),
            test_api->GetBubbleViewTooltip(ash::VIEW_ID_QS_MANAGED_BUTTON));

  // Switch back to managed account.
  UserManager::Get()->SwitchActiveUser(managed_user_.account_id);
  EXPECT_TRUE(test_api->IsBubbleViewVisible(ash::VIEW_ID_QS_MANAGED_BUTTON,
                                            true /* open_tray */));
  EXPECT_EQ(l10n_util::GetStringFUTF16(IDS_ASH_SHORT_MANAGED_BY, kManager16),
            test_api->GetBubbleViewTooltip(ash::VIEW_ID_QS_MANAGED_BUTTON));
}

IN_PROC_BROWSER_TEST_F(SystemTrayClientEnterpriseAccountTest,
                       TrayEnterpriseUnmanagedAccount) {
  auto test_api = ash::SystemTrayTestApi::Create();

  EXPECT_FALSE(test_api->IsBubbleViewVisible(ash::VIEW_ID_QS_MANAGED_BUTTON,
                                             true /* open_tray */));

  // After login with unmanaged user, the tray should not show user management
  // information.
  LoginUser(unmanaged_user_.account_id);
  EXPECT_FALSE(test_api->IsBubbleViewVisible(ash::VIEW_ID_QS_MANAGED_BUTTON,
                                             true /* open_tray */));
}

class SystemTrayClientEnterpriseSessionRestoreTest
    : public SystemTrayClientEnterpriseAccountTest {
 protected:
  SystemTrayClientEnterpriseSessionRestoreTest() {
    login_mixin_.set_session_restore_enabled();
  }
};

IN_PROC_BROWSER_TEST_F(SystemTrayClientEnterpriseSessionRestoreTest,
                       PRE_SessionRestore) {
  LoginUser(managed_user_.account_id);
}

IN_PROC_BROWSER_TEST_F(SystemTrayClientEnterpriseSessionRestoreTest,
                       SessionRestore) {
  auto test_api = ash::SystemTrayTestApi::Create();

  // Verify that tray is showing info on chrome restart.
  EXPECT_TRUE(test_api->IsBubbleViewVisible(ash::VIEW_ID_QS_MANAGED_BUTTON,
                                            true /* open_tray */));
  EXPECT_EQ(l10n_util::GetStringFUTF16(IDS_ASH_SHORT_MANAGED_BY, kManager16),
            test_api->GetBubbleViewTooltip(ash::VIEW_ID_QS_MANAGED_BUTTON));
}

class SystemTrayClientShowCalendarTest : public ash::LoginManagerTest {
 public:
  SystemTrayClientShowCalendarTest() {
    login_mixin_.AppendRegularUsers(1);
    account_id_ = login_mixin_.users()[0].account_id;
  }

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

  ~SystemTrayClientShowCalendarTest() override = default;

  apps::AppPtr MakeApp(const char* app_id, const char* name) {
    auto google_meet_filter =
        apps_util::MakeIntentFilterForUrlScope(GURL("https://meet.google.com"));
    auto calendar_filter = apps_util::MakeIntentFilterForUrlScope(
        GURL("https://calendar.google.com"));
    apps::AppPtr app =
        std::make_unique<apps::App>(apps::AppType::kChromeApp, app_id);
    app->name = name;
    app->short_name = name;
    app->readiness = apps::Readiness::kReady;
    app->handles_intents = true;
    app->intent_filters.push_back(google_meet_filter->Clone());
    app->intent_filters.push_back(calendar_filter->Clone());
    return app;
  }

  apps::AppServiceProxyAsh* proxy() {
    const user_manager::User* user = UserManager::Get()->FindUser(account_id_);
    Profile* profile = ProfileHelper::Get()->GetProfileByUser(user);
    return apps::AppServiceProxyFactory::GetForProfile(profile);
  }

  void InstallApp(const char* app_id, const char* name) {
    std::vector<apps::AppPtr> registry_deltas;
    registry_deltas.push_back(MakeApp(app_id, name));
    proxy()->OnApps(std::move(registry_deltas), apps::AppType::kChromeApp,
                    /*should_notify_initialized=*/false);
  }

  void SetPreferredApp(const char* app_id) {
    proxy()->SetSupportedLinksPreference(app_id);
  }

 protected:
  AccountId account_id_;
  ash::LoginManagerMixin login_mixin_{&mixin_host_};
};

IN_PROC_BROWSER_TEST_F(SystemTrayClientShowCalendarTest, NoEventUrl) {
  auto tray_test_api = ash::SystemTrayTestApi::Create();

  // Login a user.
  LoginUser(account_id_);

  // Show the calendar, no URL and no PWA installed. We expect to navigate to
  // `date`.
  const char kExpectedUrlStr[] =
      "https://calendar.google.com/calendar/r/week/2021/11/18";
  GURL final_url;
  bool opened_pwa = false;
  base::Time date;
  ASSERT_TRUE(base::Time::FromString("18 Nov 2021 10:00 GMT", &date));
  ash::Shell::Get()->system_tray_model()->client()->ShowCalendarEvent(
      std::nullopt, date, opened_pwa, final_url);
  EXPECT_FALSE(opened_pwa);
  EXPECT_EQ(final_url.spec(), GURL(kExpectedUrlStr).spec());

  // Now install the calendar PWA.
  InstallApp(ash::kGoogleCalendarAppId, "Google Calendar");
  opened_pwa = false;
  final_url = GURL();
  ash::Shell::Get()->system_tray_model()->client()->ShowCalendarEvent(
      std::nullopt, date, opened_pwa, final_url);
  EXPECT_TRUE(opened_pwa);
  EXPECT_EQ(final_url.spec(), GURL(kExpectedUrlStr).spec());
}

IN_PROC_BROWSER_TEST_F(SystemTrayClientShowCalendarTest, OfficialEventUrl) {
  auto tray_test_api = ash::SystemTrayTestApi::Create();

  LoginUser(account_id_);

  // Show the calendar, with an event URL that has the "official" prefix, no PWA
  // installed.
  const char kOfficialCalendarEventUrl[] =
      "https://calendar.google.com/calendar/event?eid=m00n";
  GURL event_url(kOfficialCalendarEventUrl);
  GURL final_url;
  bool opened_pwa = false;
  base::Time date;
  ASSERT_TRUE(base::Time::FromString("18 Nov 2021 10:00 GMT", &date));
  ash::Shell::Get()->system_tray_model()->client()->ShowCalendarEvent(
      event_url, date, opened_pwa, final_url);
  EXPECT_FALSE(opened_pwa);
  EXPECT_EQ(final_url.spec(), event_url.spec());

  // Install the calendar PWA.
  InstallApp(ash::kGoogleCalendarAppId, "Google Calendar");
  opened_pwa = false;
  final_url = GURL();
  ash::Shell::Get()->system_tray_model()->client()->ShowCalendarEvent(
      event_url, date, opened_pwa, final_url);
  EXPECT_TRUE(opened_pwa);
  EXPECT_EQ(final_url.spec(), event_url.spec());
}

IN_PROC_BROWSER_TEST_F(SystemTrayClientShowCalendarTest, UnofficialEventUrl) {
  auto tray_test_api = ash::SystemTrayTestApi::Create();

  LoginUser(account_id_);

  // Show the calendar, with an event URL that does not have the "official"
  // prefix, no PWA installed.
  const char kOfficialCalendarEventUrl[] =
      "https://calendar.google.com/calendar/event?eid=m00n";
  const char kUnofficialCalendarEventUrl[] =
      "https://www.google.com/calendar/event?eid=m00n";
  GURL event_url(kUnofficialCalendarEventUrl);
  GURL final_url;
  bool opened_pwa = false;
  base::Time date;
  ASSERT_TRUE(base::Time::FromString("18 Nov 2021 10:00 GMT", &date));
  ash::Shell::Get()->system_tray_model()->client()->ShowCalendarEvent(
      event_url, date, opened_pwa, final_url);
  EXPECT_FALSE(opened_pwa);
  EXPECT_EQ(final_url.spec(), GURL(kOfficialCalendarEventUrl).spec());

  // Install the calendar PWA.
  InstallApp(ash::kGoogleCalendarAppId, "Google Calendar");
  opened_pwa = false;
  final_url = GURL();
  ash::Shell::Get()->system_tray_model()->client()->ShowCalendarEvent(
      event_url, date, opened_pwa, final_url);
  EXPECT_TRUE(opened_pwa);
  EXPECT_EQ(final_url.spec(), GURL(kOfficialCalendarEventUrl).spec());
}

class SystemTrayClientShowVideoConferenceTest
    : public SystemTrayClientShowCalendarTest {
 public:
  SystemTrayClientShowVideoConferenceTest() = default;

  ~SystemTrayClientShowVideoConferenceTest() override = default;

 protected:
  // ash::LoginManagerTest:
  void SetUpOnMainThread() override {
    ash::LoginManagerTest::SetUpOnMainThread();
    LoginUser(account_id_);
    browser_ = CreateBrowser(
        ash::ProfileHelper::Get()->GetProfileByAccountId(account_id_));
    ASSERT_TRUE(browser_);
  }

  raw_ptr<Browser, DanglingUntriaged> browser_ = nullptr;
};

IN_PROC_BROWSER_TEST_F(SystemTrayClientShowVideoConferenceTest,
                       LaunchGoogleMeetUrlInBrowser_WhenAppIsNotInstalled) {
  const auto kVideoConferenceUrl = GURL("https://meet.google.com/abc-123");

  ash::Shell::Get()->system_tray_model()->client()->ShowVideoConference(
      kVideoConferenceUrl);

  EXPECT_EQ(
      GURL(kVideoConferenceUrl),
      browser_->tab_strip_model()->GetActiveWebContents()->GetVisibleURL());
}

IN_PROC_BROWSER_TEST_F(
    SystemTrayClientShowVideoConferenceTest,
    LaunchGoogleMeetUrlInBrowser_WhenAppIsInstalledButNotPreferred) {
  const auto kVideoConferenceUrl = GURL("https://meet.google.com/abc-123");
  InstallApp(ash::kGoogleMeetAppId, "Google Meet");

  ash::Shell::Get()->system_tray_model()->client()->ShowVideoConference(
      kVideoConferenceUrl);

  EXPECT_EQ(
      GURL(kVideoConferenceUrl),
      browser_->tab_strip_model()->GetActiveWebContents()->GetVisibleURL());
}

IN_PROC_BROWSER_TEST_F(
    SystemTrayClientShowVideoConferenceTest,
    LaunchGoogleMeetUrlInApp_WhenAppIsInstalledAndPreferred) {
  const auto kVideoConferenceUrl = GURL("https://meet.google.com/abc-123");
  InstallApp(ash::kGoogleMeetAppId, "Google Meet");
  SetPreferredApp(ash::kGoogleMeetAppId);

  ASSERT_EQ(
      ash::kGoogleMeetAppId,
      proxy()->PreferredAppsList().FindPreferredAppForUrl(kVideoConferenceUrl));

  ash::Shell::Get()->system_tray_model()->client()->ShowVideoConference(
      kVideoConferenceUrl);

  // Expect the url not to have opened in the browser.
  EXPECT_NE(
      GURL(kVideoConferenceUrl),
      browser_->tab_strip_model()->GetActiveWebContents()->GetVisibleURL());
}

IN_PROC_BROWSER_TEST_F(SystemTrayClientShowVideoConferenceTest,
                       Launch3PVideoConferenceUrlInBrowser) {
  const auto kVideoConferenceUrl = GURL("https://some.third.party.com/abc-123");

  ash::Shell::Get()->system_tray_model()->client()->ShowVideoConference(
      kVideoConferenceUrl);

  EXPECT_EQ(
      GURL(kVideoConferenceUrl),
      browser_->tab_strip_model()->GetActiveWebContents()->GetVisibleURL());
}

class SystemTrayClientShowChannelInfoGiveFeedbackTest
    : public ash::LoginManagerTest {
 public:
  SystemTrayClientShowChannelInfoGiveFeedbackTest() {
    login_mixin_.AppendRegularUsers(1);
    account_id_ = login_mixin_.users()[0].account_id;
  }

  SystemTrayClientShowChannelInfoGiveFeedbackTest(
      const SystemTrayClientShowCalendarTest&) = delete;
  SystemTrayClientShowChannelInfoGiveFeedbackTest& operator=(
      const SystemTrayClientShowChannelInfoGiveFeedbackTest&) = delete;

  ~SystemTrayClientShowChannelInfoGiveFeedbackTest() override = default;

 protected:
  AccountId account_id_;
  ash::LoginManagerMixin login_mixin_{&mixin_host_};
};

// TODO(crbug.com/40857702): Flaky on release bots.
#if defined(NDEBUG)
#define MAYBE_RecordFeedbackSourceChannelIndicator \
  DISABLED_RecordFeedbackSourceChannelIndicator
#else
#define MAYBE_RecordFeedbackSourceChannelIndicator \
  RecordFeedbackSourceChannelIndicator
#endif
IN_PROC_BROWSER_TEST_F(SystemTrayClientShowChannelInfoGiveFeedbackTest,
                       MAYBE_RecordFeedbackSourceChannelIndicator) {
  base::HistogramTester histograms;
  auto tray_test_api = ash::SystemTrayTestApi::Create();

  LoginUser(account_id_);

  histograms.ExpectBucketCount("Feedback.RequestSource",
                               feedback::kFeedbackSourceChannelIndicator,
                               /*expected_count=*/0);
  ash::Shell::Get()
      ->system_tray_model()
      ->client()
      ->ShowChannelInfoGiveFeedback();
  histograms.ExpectBucketCount("Feedback.RequestSource",
                               feedback::kFeedbackSourceChannelIndicator,
                               /*expected_count=*/1);
}