File: app_access_notifier_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 (671 lines) | stat: -rw-r--r-- 29,295 bytes parent folder | download | duplicates (5)
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
// Copyright 2021 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/app_access/app_access_notifier.h"

#include <memory>
#include <vector>

#include "ash/constants/ash_features.h"
#include "ash/public/cpp/ash_prefs.h"
#include "ash/root_window_controller.h"
#include "ash/shell.h"
#include "ash/system/notification_center/notification_center_tray.h"
#include "ash/system/privacy/privacy_indicators_controller.h"
#include "ash/system/privacy/privacy_indicators_tray_item_view.h"
#include "ash/system/status_area_widget.h"
#include "ash/system/unified/unified_system_tray.h"
#include "ash/test/ash_test_helper.h"
#include "base/memory/raw_ptr.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.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/apps/app_service/app_service_test.h"
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/account_id/account_id.h"
#include "components/prefs/testing_pref_service.h"
#include "components/services/app_service/public/cpp/app_capability_access_cache.h"
#include "components/services/app_service/public/cpp/app_capability_access_cache_wrapper.h"
#include "components/services/app_service/public/cpp/app_registry_cache_wrapper.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/services/app_service/public/cpp/capability_access.h"
#include "components/user_manager/scoped_user_manager.h"
#include "components/user_manager/user.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/display/test/display_manager_test_api.h"
#include "ui/message_center/message_center.h"

namespace {

constexpr char kPrivacyIndicatorsAppTypeHistogramName[] =
    "Ash.PrivacyIndicators.AppAccessUpdate.Type";
constexpr char kPrivacyIndicatorsLaunchSettingsHistogramName[] =
    "Ash.PrivacyIndicators.LaunchSettings";

// Check the visibility of privacy indicators and their camera/microphone icons
// in all displays.
ash::PrivacyIndicatorsTrayItemView* GetPrivacyIndicatorsView(
    ash::RootWindowController* root_window_controller) {
  return root_window_controller->GetStatusAreaWidget()
      ->notification_center_tray()
      ->privacy_indicators_view();
}

void ExpectPrivacyIndicatorsVisible(bool visible) {
  for (auto* root_window_controller :
       ash::Shell::Get()->GetAllRootWindowControllers()) {
    EXPECT_EQ(GetPrivacyIndicatorsView(root_window_controller)->GetVisible(),
              visible);
  }
}

void ExpectPrivacyIndicatorsCameraIconVisible(bool visible) {
  for (auto* root_window_controller :
       ash::Shell::Get()->GetAllRootWindowControllers()) {
    EXPECT_EQ(GetPrivacyIndicatorsView(root_window_controller)
                  ->camera_icon()
                  ->GetVisible(),
              visible);
  }
}

void ExpectPrivacyIndicatorsMicrophoneIconVisible(bool visible) {
  for (auto* root_window_controller :
       ash::Shell::Get()->GetAllRootWindowControllers()) {
    EXPECT_EQ(GetPrivacyIndicatorsView(root_window_controller)
                  ->microphone_icon()
                  ->GetVisible(),
              visible);
  }
}

}  // namespace

class TestAppAccessNotifier : public AppAccessNotifier {
 public:
  TestAppAccessNotifier() = default;
  TestAppAccessNotifier(const TestAppAccessNotifier&) = delete;
  TestAppAccessNotifier& operator=(const TestAppAccessNotifier&) = delete;
  ~TestAppAccessNotifier() override = default;

  void SetFakeActiveUserAccountId(AccountId id) {
    user_account_id_ = id;
    CheckActiveUserChanged();
  }

  AccountId GetActiveUserAccountId() override { return user_account_id_; }

 private:
  AccountId user_account_id_ = EmptyAccountId();
};

class AppAccessNotifierTest : public testing::Test,
                              public ::testing::WithParamInterface<bool> {
 public:
  AppAccessNotifierTest()
      : testing_profile_manager_(TestingBrowserProcess::GetGlobal()) {}
  AppAccessNotifierTest(const AppAccessNotifierTest&) = delete;
  AppAccessNotifierTest& operator=(const AppAccessNotifierTest&) = delete;
  ~AppAccessNotifierTest() override = default;

  bool IsCrosPrivacyHubEnabled() const { return GetParam(); }

  void SetUp() override {
    if (!IsCrosPrivacyHubEnabled()) {
      scoped_feature_list_.InitAndDisableFeature(
          ash::features::kCrosPrivacyHub);
    }

    ASSERT_TRUE(testing_profile_manager_.SetUp());

    // Setting ash prefs for testing multi-display.
    ash::RegisterLocalStatePrefs(local_state_.registry(), /*for_test=*/true);

    ash::AshTestHelper::InitParams params;
    params.local_state = &local_state_;
    ash_test_helper_.SetUp(std::move(params));

    auto fake_user_manager = std::make_unique<ash::FakeChromeUserManager>();
    fake_user_manager_ = fake_user_manager.get();
    scoped_user_manager_ = std::make_unique<user_manager::ScopedUserManager>(
        std::move(fake_user_manager));

    app_access_notifier_ = std::make_unique<TestAppAccessNotifier>();

    SetupPrimaryUser();
  }

  void TearDown() override {
    app_access_notifier_.reset();
    ash_test_helper_.TearDown();
  }

  void SetupPrimaryUser() {
    auto* primary_profile = testing_profile_manager_.CreateTestingProfile(
        account_id_primary_user_.GetUserEmail());
    fake_user_manager_->AddUserWithAffiliationAndTypeAndProfile(
        account_id_primary_user_, false, user_manager::UserType::kRegular,
        primary_profile);

    registry_cache_primary_user_.SetAccountId(account_id_primary_user_);
    apps::AppRegistryCacheWrapper::Get().AddAppRegistryCache(
        account_id_primary_user_, &registry_cache_primary_user_);
    capability_access_cache_primary_user_.SetAccountId(
        account_id_primary_user_);
    apps::AppCapabilityAccessCacheWrapper::Get().AddAppCapabilityAccessCache(
        account_id_primary_user_, &capability_access_cache_primary_user_);

    SetActiveUserAccountId(/*is_primary=*/true);
    WaitForAppServiceProxyReady(
        apps::AppServiceProxyFactory::GetForProfile(primary_profile));
  }

  void SetupSecondaryUser() {
    auto* secondary_profile = testing_profile_manager_.CreateTestingProfile(
        account_id_secondary_user_.GetUserEmail());
    fake_user_manager_->AddUserWithAffiliationAndTypeAndProfile(
        account_id_secondary_user_, false, user_manager::UserType::kRegular,
        secondary_profile);

    registry_cache_secondary_user_.SetAccountId(account_id_secondary_user_);
    apps::AppRegistryCacheWrapper::Get().AddAppRegistryCache(
        account_id_secondary_user_, &registry_cache_secondary_user_);
    capability_access_cache_secondary_user_.SetAccountId(
        account_id_secondary_user_);
    apps::AppCapabilityAccessCacheWrapper::Get().AddAppCapabilityAccessCache(
        account_id_secondary_user_, &capability_access_cache_secondary_user_);

    SetActiveUserAccountId(/*is_primary=*/false);
    WaitForAppServiceProxyReady(
        apps::AppServiceProxyFactory::GetForProfile(secondary_profile));
  }

  std::vector<std::u16string> GetAppsAccessingCamera() {
    return app_access_notifier_->GetAppsAccessingCamera();
  }

  std::vector<std::u16string> GetAppsAccessingMicrophone() {
    return app_access_notifier_->GetAppsAccessingMicrophone();
  }

  static apps::AppPtr MakeApp(const std::string& app_id,
                              const char* name,
                              apps::AppType app_type) {
    apps::AppPtr app = std::make_unique<apps::App>(app_type, app_id);
    app->name = name;
    app->short_name = name;
    return app;
  }

  static apps::CapabilityAccessPtr MakeCapabilityAccess(
      const std::string& app_id,
      std::optional<bool> camera,
      std::optional<bool> microphone) {
    auto access = std::make_unique<apps::CapabilityAccess>(app_id);
    access->camera = camera;
    access->microphone = microphone;
    return access;
  }

  void LaunchAppUsingCameraOrMicrophone(
      const std::string& id,
      const char* name,
      bool use_camera,
      bool use_microphone,
      apps::AppType app_type = apps::AppType::kChromeApp) {
    apps::AppServiceProxy* proxy = apps::AppServiceProxyFactory::GetForProfile(
        ProfileManager::GetActiveUserProfile());
    apps::AppCapabilityAccessCache* cap_cache =
        app_access_notifier_->GetActiveUserAppCapabilityAccessCache();

    std::vector<apps::AppPtr> registry_deltas;
    registry_deltas.push_back(MakeApp(id, name, app_type));
    proxy->OnApps(std::move(registry_deltas), apps::AppType::kUnknown,
                  /*should_notify_initialized=*/false);

    std::vector<apps::CapabilityAccessPtr> capability_access_deltas;
    capability_access_deltas.push_back(
        MakeCapabilityAccess(id, use_camera, use_microphone));
    cap_cache->OnCapabilityAccesses(std::move(capability_access_deltas));
  }

  // Set the active account, whether to use the primary or secondary fake user
  // account.
  void SetActiveUserAccountId(bool is_primary) {
    auto id =
        is_primary ? account_id_primary_user_ : account_id_secondary_user_;
    app_access_notifier_->SetFakeActiveUserAccountId(id);

    fake_user_manager_->LoginUser(id);
    fake_user_manager_->SwitchActiveUser(id);
  }

  const AccountId account_id_primary_user_ =
      AccountId::FromUserEmail("primary_profile");
  const AccountId account_id_secondary_user_ =
      AccountId::FromUserEmail("secondary_profile");

  std::unique_ptr<TestAppAccessNotifier> app_access_notifier_;

  apps::AppRegistryCache registry_cache_primary_user_;
  apps::AppCapabilityAccessCache capability_access_cache_primary_user_;
  apps::AppRegistryCache registry_cache_secondary_user_;
  apps::AppCapabilityAccessCache capability_access_cache_secondary_user_;

  raw_ptr<ash::FakeChromeUserManager, DanglingUntriaged> fake_user_manager_ =
      nullptr;
  std::unique_ptr<user_manager::ScopedUserManager> scoped_user_manager_;

  // This instance is needed for setting up `ash_test_helper_`.
  // See //docs/threading_and_tasks_testing.md.
  content::BrowserTaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};

  TestingProfileManager testing_profile_manager_;

  // Use this for testing multi-display.
  TestingPrefServiceSimple local_state_;

  ash::AshTestHelper ash_test_helper_;

  base::test::ScopedFeatureList scoped_feature_list_;
};

INSTANTIATE_TEST_SUITE_P(All,
                         AppAccessNotifierTest,
                         /*IsCrosPrivacyHubEnabled()=*/testing::Bool());

TEST_P(AppAccessNotifierTest, NoAppsLaunched) {
  // The list of apps using mic or camera should be empty.
  EXPECT_EQ(std::vector<std::u16string>(), GetAppsAccessingCamera());
  EXPECT_EQ(std::vector<std::u16string>(), GetAppsAccessingMicrophone());
}

TEST_P(AppAccessNotifierTest, AppLaunchedNotUsingCameraAndMic) {
  LaunchAppUsingCameraOrMicrophone("id_rose", "name_rose", /*use_camera=*/false,
                                   /*use_microphone=*/false);

  // The list of apps using mic or camera should be empty.
  EXPECT_EQ(std::vector<std::u16string>(), GetAppsAccessingCamera());
  EXPECT_EQ(std::vector<std::u16string>(), GetAppsAccessingMicrophone());
}

TEST_P(AppAccessNotifierTest, AppLaunchedUsingCameraAndMic) {
  LaunchAppUsingCameraOrMicrophone("id_rose", "name_rose", /*use_camera=*/true,
                                   /*use_microphone=*/true);

  // List of application names should only contain "name_rose".
  EXPECT_EQ(std::vector<std::u16string>({u"name_rose"}),
            GetAppsAccessingCamera());
  EXPECT_EQ(std::vector<std::u16string>({u"name_rose"}),
            GetAppsAccessingMicrophone());
}

TEST_P(AppAccessNotifierTest, MultipleAppsLaunchedUsingBothCameraAndMic) {
  LaunchAppUsingCameraOrMicrophone("id_rose", "name_rose", /*use_camera=*/true,
                                   /*use_microphone=*/true);
  LaunchAppUsingCameraOrMicrophone("id_mars", "name_mars", /*use_camera=*/true,
                                   /*use_microphone=*/true);
  LaunchAppUsingCameraOrMicrophone("id_zara", "name_zara", /*use_camera=*/true,
                                   /*use_microphone=*/true);
  LaunchAppUsingCameraOrMicrophone(
      "id_oscar", "name_oscar", /*use_camera=*/false, /*use_microphone=*/false);

  // Only the applications using the sensor should be in the respective list and
  // the list should be ordered by the most recently launced app first.
  // "name_oscar" should not be in any of the lists.
  EXPECT_EQ(
      std::vector<std::u16string>({u"name_zara", u"name_mars", u"name_rose"}),
      GetAppsAccessingCamera());
  EXPECT_EQ(
      std::vector<std::u16string>({u"name_zara", u"name_mars", u"name_rose"}),
      GetAppsAccessingMicrophone());

  // Oscar starts using camera, Oscar should be the front element of the camera
  // list now. The mic list should stay the same.
  LaunchAppUsingCameraOrMicrophone(
      "id_oscar", "name_oscar", /*use_camera=*/true, /*use_microphone=*/false);
  EXPECT_EQ(std::vector<std::u16string>(
                {u"name_oscar", u"name_zara", u"name_mars", u"name_rose"}),
            GetAppsAccessingCamera());
  EXPECT_EQ(
      std::vector<std::u16string>({u"name_zara", u"name_mars", u"name_rose"}),
      GetAppsAccessingMicrophone());

  // Oscar starts using mic, Oscar should be the front element of the mic list
  // as well now.
  LaunchAppUsingCameraOrMicrophone(
      "id_oscar", "name_oscar", /*use_camera=*/true, /*use_microphone=*/true);
  EXPECT_EQ(std::vector<std::u16string>(
                {u"name_oscar", u"name_zara", u"name_mars", u"name_rose"}),
            GetAppsAccessingCamera());
  EXPECT_EQ(std::vector<std::u16string>(
                {u"name_oscar", u"name_zara", u"name_mars", u"name_rose"}),
            GetAppsAccessingMicrophone());

  // If we "kill" Oscar (set to no longer be using the mic or camera), Oscar
  // should not be in the returned lists anymore. Zara should be at the front of
  // both of the lists.
  LaunchAppUsingCameraOrMicrophone(
      "id_oscar", "name_oscar", /*use_camera=*/false, /*use_microphone=*/false);
  EXPECT_EQ(
      std::vector<std::u16string>({u"name_zara", u"name_mars", u"name_rose"}),
      GetAppsAccessingCamera());
  EXPECT_EQ(
      std::vector<std::u16string>({u"name_zara", u"name_mars", u"name_rose"}),
      GetAppsAccessingMicrophone());
}

TEST_P(AppAccessNotifierTest, MultipleUsers) {
  // Prepare the secondary user.
  SetupSecondaryUser();

  // Primary user is the active user.
  SetActiveUserAccountId(/*is_primary=*/true);

  // Primary user launches an app using both sensors.
  LaunchAppUsingCameraOrMicrophone("id_primary_user", "name_primary_user",
                                   /*use_camera=*/true,
                                   /*use_microphone=*/true);

  // App we just launched should be the front and only element of both the
  // lists.
  EXPECT_EQ(std::vector<std::u16string>({u"name_primary_user"}),
            GetAppsAccessingCamera());
  EXPECT_EQ(std::vector<std::u16string>({u"name_primary_user"}),
            GetAppsAccessingMicrophone());

  // Secondary user is now the primary user.
  SetActiveUserAccountId(/*is_primary=*/false);

  // Secondary user launches an app using both sensors.
  LaunchAppUsingCameraOrMicrophone("id_secondary_user", "name_secondary_user",
                                   /*use_camera=*/true,
                                   /*use_microphone=*/true);

  // App we just launched should be the front and only element of both the
  // lists.
  EXPECT_EQ(std::vector<std::u16string>({u"name_secondary_user"}),
            GetAppsAccessingCamera());
  EXPECT_EQ(std::vector<std::u16string>({u"name_secondary_user"}),
            GetAppsAccessingMicrophone());

  // Switch back to the primary user and "kill" the app it was running, no app
  // name to show.
  SetActiveUserAccountId(/*is_primary=*/true);
  LaunchAppUsingCameraOrMicrophone("id_primary_user", "name_primary_user",
                                   /*use_camera=*/false,
                                   /*use_microphone=*/false);
  EXPECT_EQ(std::vector<std::u16string>(), GetAppsAccessingCamera());
  EXPECT_EQ(std::vector<std::u16string>(), GetAppsAccessingMicrophone());

  // Now switch back to the secondary user, verify that the same app as before
  // shows up in the lists.
  SetActiveUserAccountId(/*is_primary=*/false);
  EXPECT_EQ(std::vector<std::u16string>({u"name_secondary_user"}),
            GetAppsAccessingCamera());
  EXPECT_EQ(std::vector<std::u16string>({u"name_secondary_user"}),
            GetAppsAccessingMicrophone());

  // Now "kill" our secondary user's app and verify that there's no name to
  // show.
  LaunchAppUsingCameraOrMicrophone("id_secondary_user", "name_secondary_user",
                                   /*use_camera=*/false,
                                   /*use_microphone=*/false);
  EXPECT_EQ(std::vector<std::u16string>(), GetAppsAccessingCamera());
  EXPECT_EQ(std::vector<std::u16string>(), GetAppsAccessingMicrophone());
}

TEST_P(AppAccessNotifierTest, MultipleUsersMultipleApps) {
  // Prepare the secondary user.
  SetupSecondaryUser();

  // Primary user is the active user.
  SetActiveUserAccountId(/*is_primary=*/true);

  // Primary user launches an app using both sensors.
  LaunchAppUsingCameraOrMicrophone("id_primary_user", "name_primary_user",
                                   /*use_camera=*/true,
                                   /*use_microphone=*/true);

  // App we just launched should be the front and only element of both the
  // lists.
  EXPECT_EQ(std::vector<std::u16string>({u"name_primary_user"}),
            GetAppsAccessingCamera());
  EXPECT_EQ(std::vector<std::u16string>({u"name_primary_user"}),
            GetAppsAccessingMicrophone());

  // Primary user launches a second app using both sensors.
  LaunchAppUsingCameraOrMicrophone(
      "id_primary_user_another_app", "name_primary_user_another_app",
      /*use_camera=*/true, /*use_microphone=*/true);

  // The lists should contain two application names ordered by most recently
  // launched.
  EXPECT_EQ(std::vector<std::u16string>(
                {u"name_primary_user_another_app", u"name_primary_user"}),
            GetAppsAccessingCamera());
  EXPECT_EQ(std::vector<std::u16string>(
                {u"name_primary_user_another_app", u"name_primary_user"}),
            GetAppsAccessingMicrophone());

  // Secondary user is now the primary user.
  SetActiveUserAccountId(/*is_primary=*/false);

  // Secondary user launches an app using both sensors.
  LaunchAppUsingCameraOrMicrophone("id_secondary_user", "name_secondary_user",
                                   /*use_camera=*/true,
                                   /*use_microphone=*/true);

  // App we just launched should be the front and only element of both the
  // lists.
  EXPECT_EQ(std::vector<std::u16string>({u"name_secondary_user"}),
            GetAppsAccessingCamera());
  EXPECT_EQ(std::vector<std::u16string>({u"name_secondary_user"}),
            GetAppsAccessingMicrophone());

  // Secondary user launches a second app using both sensors.
  LaunchAppUsingCameraOrMicrophone(
      "id_secondary_user_another_app", "name_secondary_user_another_app",
      /*use_camera=*/true, /*use_microphone=*/true);

  // The lists should contain two application names ordered by most recently
  // launched.
  EXPECT_EQ(std::vector<std::u16string>(
                {u"name_secondary_user_another_app", u"name_secondary_user"}),
            GetAppsAccessingCamera());
  EXPECT_EQ(std::vector<std::u16string>(
                {u"name_secondary_user_another_app", u"name_secondary_user"}),
            GetAppsAccessingMicrophone());

  // Switch back to the primary user.
  SetActiveUserAccountId(/*is_primary=*/true);

  // Both of the apps we launced for the primary user should be in the lists
  // ordered by most recently launched.
  EXPECT_EQ(std::vector<std::u16string>(
                {u"name_primary_user_another_app", u"name_primary_user"}),
            GetAppsAccessingCamera());
  EXPECT_EQ(std::vector<std::u16string>(
                {u"name_primary_user_another_app", u"name_primary_user"}),
            GetAppsAccessingMicrophone());
}

TEST_P(AppAccessNotifierTest, GetShortNameFromAppId) {
  // Test that GetAppShortNameFromAppId works properly.
  const std::string id = "test_app_id";
  LaunchAppUsingCameraOrMicrophone(id, "test_app_name", /*use_camera=*/false,
                                   /*use_microphone=*/true);
  EXPECT_EQ(AppAccessNotifier::GetAppShortNameFromAppId(id), u"test_app_name");
}

TEST_P(AppAccessNotifierTest, AppAccessNotification) {
  // Test that notifications get created/removed when an app is accessing camera
  // or microphone.
  const std::string id1 = "test_app_id_1";
  const std::string id2 = "test_app_id_2";
  const std::string notification_id1 =
      ash::GetPrivacyIndicatorsNotificationId(id1);
  const std::string notification_id2 =
      ash::GetPrivacyIndicatorsNotificationId(id2);

  LaunchAppUsingCameraOrMicrophone(id1, "test_app_name", /*use_camera=*/false,
                                   /*use_microphone=*/true);
  LaunchAppUsingCameraOrMicrophone(id2, "test_app_name", /*use_camera=*/true,
                                   /*use_microphone=*/false);
  EXPECT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
      notification_id1));
  EXPECT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
      notification_id2));

  LaunchAppUsingCameraOrMicrophone(id1, "test_app_name", /*use_camera=*/false,
                                   /*use_microphone=*/false);
  // Fast forward by the minimum duration the privacy indicator should be held.
  task_environment_.FastForwardBy(
      ash::PrivacyIndicatorsController::kPrivacyIndicatorsMinimumHoldDuration);
  LaunchAppUsingCameraOrMicrophone(id2, "test_app_name", /*use_camera=*/false,
                                   /*use_microphone=*/false);
  // Fast forward by the minimum duration the privacy indicator should be held.
  task_environment_.FastForwardBy(
      ash::PrivacyIndicatorsController::kPrivacyIndicatorsMinimumHoldDuration);
  EXPECT_FALSE(message_center::MessageCenter::Get()->FindNotificationById(
      notification_id1));
  EXPECT_FALSE(message_center::MessageCenter::Get()->FindNotificationById(
      notification_id2));

  LaunchAppUsingCameraOrMicrophone(id1, "test_app_name", /*use_camera=*/true,
                                   /*use_microphone=*/true);
  EXPECT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
      notification_id1));
}

TEST_P(AppAccessNotifierTest, PrivacyIndicatorsVisibility) {
  // Uses normal animation duration so that the icons would not be immediately
  // hidden after the animation.
  ui::ScopedAnimationDurationScaleMode animation_scale(
      ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);

  // Make sure privacy indicators work on multiple displays.
  display::test::DisplayManagerTestApi(ash::Shell::Get()->display_manager())
      .UpdateDisplay("800x700,801+0-800x700");

  ExpectPrivacyIndicatorsVisible(/*visible=*/false);

  // Privacy indicators should show up if at least camera or microphone is being
  // accessed. The icons should show up accordingly (only at the start of the
  // animation).
  LaunchAppUsingCameraOrMicrophone("test_app_id", "test_app_name",
                                   /*use_camera=*/true,
                                   /*use_microphone=*/true);
  // Fast forward by the minimum duration the privacy indicator should be held.
  task_environment_.FastForwardBy(
      ash::PrivacyIndicatorsController::kPrivacyIndicatorsMinimumHoldDuration);
  ExpectPrivacyIndicatorsVisible(/*visible=*/true);
  ExpectPrivacyIndicatorsCameraIconVisible(/*visible=*/true);
  ExpectPrivacyIndicatorsMicrophoneIconVisible(/*visible=*/true);

  LaunchAppUsingCameraOrMicrophone("test_app_id", "test_app_name",
                                   /*use_camera=*/false,
                                   /*use_microphone=*/false);
  // Fast forward by the minimum duration the privacy indicator should be held.
  task_environment_.FastForwardBy(
      ash::PrivacyIndicatorsController::kPrivacyIndicatorsMinimumHoldDuration);
  ExpectPrivacyIndicatorsVisible(/*visible=*/false);
  ExpectPrivacyIndicatorsCameraIconVisible(/*visible=*/false);
  ExpectPrivacyIndicatorsMicrophoneIconVisible(/*visible=*/false);

  LaunchAppUsingCameraOrMicrophone("test_app_id", "test_app_name",
                                   /*use_camera=*/true,
                                   /*use_microphone=*/false);
  ExpectPrivacyIndicatorsVisible(/*visible=*/true);
  ExpectPrivacyIndicatorsCameraIconVisible(/*visible=*/true);
  ExpectPrivacyIndicatorsMicrophoneIconVisible(/*visible=*/false);

  LaunchAppUsingCameraOrMicrophone("test_app_id", "test_app_name",
                                   /*use_camera=*/false,
                                   /*use_microphone=*/true);
  ExpectPrivacyIndicatorsVisible(/*visible=*/true);
  ExpectPrivacyIndicatorsCameraIconVisible(/*visible=*/false);
  ExpectPrivacyIndicatorsMicrophoneIconVisible(/*visible=*/true);
}

TEST_P(AppAccessNotifierTest, RecordAppType) {
  base::HistogramTester histograms;
  LaunchAppUsingCameraOrMicrophone("test_app_id1", "test_app_name",
                                   /*use_camera=*/true,
                                   /*use_microphone=*/false,
                                   /*app_type=*/apps::AppType::kArc);
  histograms.ExpectBucketCount(kPrivacyIndicatorsAppTypeHistogramName,
                               apps::AppType::kArc, 1);

  LaunchAppUsingCameraOrMicrophone("test_app_id2", "test_app_name",
                                   /*use_camera=*/false,
                                   /*use_microphone=*/true,
                                   /*app_type=*/apps::AppType::kChromeApp);
  histograms.ExpectBucketCount(kPrivacyIndicatorsAppTypeHistogramName,
                               apps::AppType::kChromeApp, 1);

  LaunchAppUsingCameraOrMicrophone("test_app_id3", "test_app_name",
                                   /*use_camera=*/false,
                                   /*use_microphone=*/false,
                                   /*app_type=*/apps::AppType::kChromeApp);
  histograms.ExpectBucketCount(kPrivacyIndicatorsAppTypeHistogramName,
                               apps::AppType::kChromeApp, 2);

  LaunchAppUsingCameraOrMicrophone("test_app_id4", "test_app_name",
                                   /*use_camera=*/false,
                                   /*use_microphone=*/true,
                                   /*app_type=*/apps::AppType::kSystemWeb);
  histograms.ExpectBucketCount(kPrivacyIndicatorsAppTypeHistogramName,
                               apps::AppType::kSystemWeb, 1);
}

TEST_P(AppAccessNotifierTest, RecordLaunchSettings) {
  // Make sure histograms with app type is being recorded after launching
  // settings.
  base::HistogramTester histograms;
  LaunchAppUsingCameraOrMicrophone("test_app_id1", "test_app_name",
                                   /*use_camera=*/true,
                                   /*use_microphone=*/false,
                                   /*app_type=*/apps::AppType::kArc);
  AppAccessNotifier::LaunchAppSettings("test_app_id1");
  histograms.ExpectBucketCount(kPrivacyIndicatorsLaunchSettingsHistogramName,
                               apps::AppType::kArc, 1);

  LaunchAppUsingCameraOrMicrophone("test_app_id2", "test_app_name",
                                   /*use_camera=*/false,
                                   /*use_microphone=*/true,
                                   /*app_type=*/apps::AppType::kChromeApp);
  AppAccessNotifier::LaunchAppSettings("test_app_id2");
  histograms.ExpectBucketCount(kPrivacyIndicatorsLaunchSettingsHistogramName,
                               apps::AppType::kChromeApp, 1);
}

// Tests that the privacy indicators notification of a system web app should not
// have a launch settings callback (thus it will not have a launch settings
// button).
TEST_P(AppAccessNotifierTest, SystemWebAppWithoutSettingsCallback) {
  const std::string app_id = "test_app_id";
  LaunchAppUsingCameraOrMicrophone(app_id, "test_app_name",
                                   /*use_camera=*/true,
                                   /*use_microphone=*/false,
                                   /*app_type=*/apps::AppType::kSystemWeb);
  const std::string notification_id =
      ash::GetPrivacyIndicatorsNotificationId(app_id);

  auto* notification =
      message_center::MessageCenter::Get()->FindNotificationById(
          notification_id);
  ASSERT_TRUE(notification);

  auto* delegate = static_cast<ash::PrivacyIndicatorsNotificationDelegate*>(
      notification->delegate());
  EXPECT_FALSE(delegate->launch_settings_callback());
}