File: video_conference_app_service_client_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (824 lines) | stat: -rw-r--r-- 33,262 bytes parent folder | download | duplicates (6)
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/video_conference/video_conference_app_service_client.h"

#include <cstdlib>
#include <utility>
#include <vector>

#include "ash/constants/ash_features.h"
#include "ash/constants/ash_switches.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/system/video_conference/fake_video_conference_tray_controller.h"
#include "ash/system/video_conference/video_conference_common.h"
#include "ash/test/test_window_builder.h"
#include "base/command_line.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/unguessable_token.h"
#include "chrome/browser/apps/app_service/app_service_proxy_ash.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/ash/crosapi/crosapi_ash.h"
#include "chrome/browser/ash/crosapi/crosapi_manager.h"
#include "chrome/browser/ash/video_conference/video_conference_manager_ash.h"
#include "chrome/browser/chromeos/video_conference/video_conference_manager_client_common.h"
#include "chrome/browser/chromeos/video_conference/video_conference_ukm_helper.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chromeos/crosapi/mojom/video_conference.mojom.h"
#include "components/services/app_service/public/cpp/app_capability_access_cache_wrapper.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/services/app_service/public/cpp/capability_access_update.h"
#include "components/services/app_service/public/cpp/instance_registry.h"
#include "components/ukm/test_ukm_recorder.h"
#include "components/user_manager/user_manager.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_utils.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "testing/gmock/include/gmock/gmock.h"

namespace ash {
namespace {

using AppIdString = std::string;
using UkmEntry = ukm::builders::VideoConferencingEvent;

constexpr char kAppId1[] = "random_app_id_1";
constexpr char kAppName1[] = "random_app_name_1";
constexpr char kAppId2[] = "random_app_id_2";
constexpr char kAppName2[] = "random_app_name_2";

// Creates an app with given id and permissions.
apps::AppPtr MakeApp(const AppIdString& app_id,
                     bool has_camera_permission,
                     bool has_microphone_permission,
                     apps::AppType app_type) {
  apps::AppPtr app = std::make_unique<apps::App>(app_type, app_id);
  if (app_id == kAppId1) {
    app->name = kAppName1;
  }
  if (app_id == kAppId2) {
    app->name = kAppName2;
  }
  if (base::Contains(::video_conference::kSkipAppIds, app_id)) {
    app->name = base::StrCat({"AppName-", app_id});
  }

  app->publisher_id = base::StrCat({"PublisherId-", app_id});

  // Set camera_permission_value as apps::TriState (kAsk only for Arc++) for
  // better coverage.
  apps::TriState camera_permission_state =
      !has_camera_permission            ? apps::TriState::kBlock
      : app_type == apps::AppType::kArc ? apps::TriState::kAsk
                                        : apps::TriState::kAllow;

  app->permissions.push_back(std::make_unique<apps::Permission>(
      apps::PermissionType::kCamera, camera_permission_state,
      /*is_managed=*/false));
  app->permissions.push_back(std::make_unique<apps::Permission>(
      apps::PermissionType::kMicrophone, has_microphone_permission,
      /*is_managed=*/false));
  return app;
}

// This fake instance class simulates the creation, destruction, hiding and
// showing an app instance.
class FakeAppInstance {
 public:
  FakeAppInstance(apps::InstanceRegistry* instance_registry,
                  const AppIdString& app_id) {
    instance_registry_ = instance_registry;
    window_ = TestWindowBuilder().Build();
    instance_ = std::make_unique<apps::Instance>(
        app_id, base::UnguessableToken::Create(), window_.get());
  }

  aura::Window* window() { return window_.get(); }

  void Start() {
    auto instance = instance_->Clone();
    instance->UpdateState(apps::InstanceState::kStarted, base::Time::Now());
    instance_registry_->OnInstance(std::move(instance));
  }

  void Close() {
    auto instance = instance_->Clone();
    instance->UpdateState(apps::InstanceState::kDestroyed, base::Time::Now());
    instance_registry_->OnInstance(std::move(instance));
  }

  void Show() {
    window_->Show();
    // Ideally, the following should be automatically triggered by showing the
    // window_; but that is not the case for now.
    auto instance = instance_->Clone();
    instance->UpdateState(apps::InstanceState::kActive, base::Time::Now());
    instance_registry_->OnInstance(std::move(instance));
  }

  void Hide() {
    window_->Hide();
    // Ideally, the following should be automatically triggered by hiding the
    // window_; but that is not the case for now.
    auto instance = instance_->Clone();
    instance->UpdateState(apps::InstanceState::kHidden, base::Time::Now());
    instance_registry_->OnInstance(std::move(instance));
  }

 private:
  std::unique_ptr<aura::Window> window_;
  std::unique_ptr<apps::Instance> instance_;
  raw_ptr<apps::InstanceRegistry> instance_registry_;
};

}  // namespace

class VideoConferenceAppServiceClientTest : public InProcessBrowserTest {
 public:
  void SetUp() override {
    scoped_feature_list_.InitAndEnableFeature(
        ash::features::kFeatureManagementVideoConference);

    InProcessBrowserTest::SetUp();
  }

  void SetUpOnMainThread() override {
    InProcessBrowserTest::SetUpOnMainThread();

    client_ = VideoConferenceAppServiceClient::GetForTesting();

    test_ukm_recorder_ = std::make_unique<ukm::TestUkmRecorder>();
    client_->test_ukm_recorder_ = test_ukm_recorder_.get();

    Profile* profile = ProfileManager::GetActiveUserProfile();
    app_service_proxy_ = apps::AppServiceProxyFactory::GetForProfile(profile);
    instance_registry_ = &app_service_proxy_->InstanceRegistry();
    capability_cache_ =
        apps::AppCapabilityAccessCacheWrapper::Get()
            .GetAppCapabilityAccessCache(user_manager::UserManager::Get()
                                             ->GetActiveUser()
                                             ->GetAccountId());
  }

  // This function creates an app with given id and name, and adds the app into
  // AppRegistryCache of current profile.
  void InstallApp(const std::string& app_id,
                  apps::AppType app_type = apps::AppType::kArc) {
    std::vector<apps::AppPtr> deltas;
    deltas.push_back(MakeApp(app_id, /*has_camera_permission=*/false,
                             /*has_microphone_permission=*/false, app_type));
    app_service_proxy_->OnApps(std::move(deltas), apps::AppType::kUnknown,
                               /*should_notify_initialized=*/false);
  }

  // Update the permission of current `app_id`.
  void UpdateAppPermision(const std::string& app_id,
                          bool has_camera_permission,
                          bool has_microphone_permission) {
    std::vector<apps::AppPtr> deltas;
    deltas.push_back(MakeApp(app_id, has_camera_permission,
                             has_microphone_permission, GetAppType(app_id)));
    app_service_proxy_->OnApps(std::move(deltas), apps::AppType::kUnknown,
                               /*should_notify_initialized=*/false);
  }

  // Set the camera/michrophone accessing info for app with `app_id`.
  void SetAppCapabilityAccess(const AppIdString& app_id,
                              bool is_capturing_camera,
                              bool is_capturing_microphone) {
    auto delta = std::make_unique<apps::CapabilityAccess>(app_id);
    delta->camera = is_capturing_camera;
    delta->microphone = is_capturing_microphone;

    std::vector<apps::CapabilityAccessPtr> deltas;
    deltas.push_back(std::move(delta));

    capability_cache_->OnCapabilityAccesses(std::move(deltas));
  }

  // Adds {id, state} pair to client_->id_to_app_state_.
  void AddAppState(const AppIdString& app_id,
                   const VideoConferenceAppServiceClient::AppState& state) {
    (client_->id_to_app_state_)[app_id] = state;
  }

  std::string GetAppName(const AppIdString& app_id) {
    return client_->GetAppName(app_id);
  }

  apps::AppType GetAppType(const AppIdString& app_id) {
    return client_->GetAppType(app_id);
  }

  VideoConferenceAppServiceClient::VideoConferencePermissions GetAppPermission(
      const AppIdString& app_id) {
    return client_->GetAppPermission(app_id);
  }

  std::vector<crosapi::mojom::VideoConferenceMediaAppInfoPtr> GetMediaApps() {
    std::vector<crosapi::mojom::VideoConferenceMediaAppInfoPtr> media_app_info;

    client_->GetMediaApps(base::BindLambdaForTesting(
        [&media_app_info](
            std::vector<crosapi::mojom::VideoConferenceMediaAppInfoPtr>
                result) { media_app_info = std::move(result); }));

    return media_app_info;
  }

  // Returns current VideoConferenceMediaState in the VideoConferenceManagerAsh
  VideoConferenceMediaState GetMediaStateInVideoConferenceManagerAsh() {
    return crosapi::CrosapiManager::Get()
        ->crosapi_ash()
        ->video_conference_manager_ash()
        ->GetAggregatedState();
  }

 protected:
  raw_ptr<apps::AppServiceProxy, DanglingUntriaged> app_service_proxy_ =
      nullptr;
  raw_ptr<apps::InstanceRegistry, DanglingUntriaged> instance_registry_ =
      nullptr;
  raw_ptr<apps::AppCapabilityAccessCache, DanglingUntriaged> capability_cache_ =
      nullptr;
  raw_ptr<VideoConferenceAppServiceClient, DanglingUntriaged> client_ = nullptr;
  std::unique_ptr<ukm::TestUkmRecorder> test_ukm_recorder_;

  base::test::ScopedFeatureList scoped_feature_list_;
};

IN_PROC_BROWSER_TEST_F(VideoConferenceAppServiceClientTest, GetAppName) {
  // AppName should be empty if it is not installed.
  EXPECT_EQ(GetAppName(kAppId1), std::string());

  InstallApp(kAppId1);

  // AppName should be correct if installed.
  EXPECT_EQ(GetAppName(kAppId1), kAppName1);
}

IN_PROC_BROWSER_TEST_F(VideoConferenceAppServiceClientTest, GetAppType) {
  // AppType should be kUnknown if it is not installed.
  EXPECT_EQ(GetAppType(kAppId1), apps::AppType::kUnknown);

  InstallApp(kAppId1);

  // AppType for the test app installed should be Arc.
  EXPECT_EQ(GetAppType(kAppId1), apps::AppType::kArc);
}

IN_PROC_BROWSER_TEST_F(VideoConferenceAppServiceClientTest, GetAppPermission) {
  InstallApp(kAppId1);

  VideoConferenceAppServiceClient::VideoConferencePermissions permission =
      GetAppPermission(kAppId1);
  EXPECT_FALSE(permission.has_camera_permission);
  EXPECT_FALSE(permission.has_microphone_permission);

  UpdateAppPermision(kAppId1, /*has_camera_permission=*/false,
                     /*has_microphone_permission=*/true);
  permission = GetAppPermission(kAppId1);
  EXPECT_FALSE(permission.has_camera_permission);
  EXPECT_TRUE(permission.has_microphone_permission);

  UpdateAppPermision(kAppId1, /*has_camera_permission=*/true,
                     /*has_microphone_permission=*/true);
  permission = GetAppPermission(kAppId1);
  EXPECT_TRUE(permission.has_camera_permission);
  EXPECT_TRUE(permission.has_microphone_permission);

  UpdateAppPermision(kAppId1, /*has_camera_permission=*/true,
                     /*has_microphone_permission=*/false);
  permission = GetAppPermission(kAppId1);
  EXPECT_TRUE(permission.has_camera_permission);
  EXPECT_FALSE(permission.has_microphone_permission);

  UpdateAppPermision(kAppId1, /*has_camera_permission=*/false,
                     /*has_microphone_permission=*/false);
  permission = GetAppPermission(kAppId1);
  EXPECT_FALSE(permission.has_camera_permission);
  EXPECT_FALSE(permission.has_microphone_permission);
}

IN_PROC_BROWSER_TEST_F(VideoConferenceAppServiceClientTest, GetMediaApps) {
  // Add {kAppId1, state1} pair to the client_.
  const base::UnguessableToken token1 = base::UnguessableToken::Create();
  const VideoConferenceAppServiceClient::AppState state1{
      token1, base::Time::Now(), true, true};
  AddAppState(kAppId1, state1);

  // Add {kAppId2, state2} pair to the client_.
  const base::UnguessableToken token2 = base::UnguessableToken::Create();
  const VideoConferenceAppServiceClient::AppState state2{
      token2, base::Time::Now(), true, false};
  AddAppState(kAppId2, state2);

  std::vector<crosapi::mojom::VideoConferenceMediaAppInfoPtr> media_app_info =
      GetMediaApps();

  // GetMediaApps will not return anything because unrecognized apps will be
  // skipped.
  EXPECT_TRUE(media_app_info.empty());

  InstallApp(kAppId1);

  // GetMediaApps should return kAppId1 since it is installed.
  media_app_info = GetMediaApps();
  ASSERT_EQ(media_app_info.size(), 1u);

  crosapi::mojom::VideoConferenceMediaAppInfoPtr expected_media_app_info =
      crosapi::mojom::VideoConferenceMediaAppInfo::New(
          /*id=*/token1,
          /*last_activity_time=*/state1.last_activity_time,
          /*is_capturing_camera=*/state1.is_capturing_camera,
          /*is_capturing_microphone=*/state1.is_capturing_microphone,
          /*is_capturing_screen=*/false,
          /*title=*/base::UTF8ToUTF16(std::string(kAppName1)),
          /*url=*/std::nullopt,
          /*app_type=*/crosapi::mojom::VideoConferenceAppType::kArcApp);

  EXPECT_TRUE(media_app_info[0].Equals(expected_media_app_info));
}

IN_PROC_BROWSER_TEST_F(VideoConferenceAppServiceClientTest, ReturnToApp) {
  // Add two instance for kAppId1.
  FakeAppInstance instance1(instance_registry_, kAppId1);
  instance1.Start();
  FakeAppInstance instance2(instance_registry_, kAppId1);
  instance2.Start();

  aura::Window* window1 = instance1.window();
  aura::Window* window2 = instance2.window();
  window1->Hide();
  window2->Hide();

  const base::UnguessableToken token1 = base::UnguessableToken::Create();
  bool reactivated_app = false;

  // Return to token1 should not do anything since the token1 is not in the
  // client_->id_to_app_state_.
  client_->ReturnToApp(
      token1, base::BindLambdaForTesting([&reactivated_app](bool result) {
        reactivated_app = result;
      }));

  EXPECT_FALSE(reactivated_app);
  EXPECT_FALSE(window1->IsVisible());
  EXPECT_FALSE(window2->IsVisible());

  // Add pair {token1, state1} to client_->id_to_app_state_.
  const VideoConferenceAppServiceClient::AppState state1{
      token1, base::Time::Now(), true, true};
  AddAppState(kAppId1, state1);

  // Return to token1 should show all instances associated with kAppId1.
  client_->ReturnToApp(
      token1, base::BindLambdaForTesting([&reactivated_app](bool result) {
        reactivated_app = result;
      }));

  EXPECT_TRUE(reactivated_app);
  EXPECT_TRUE(window1->IsVisible());
  EXPECT_TRUE(window2->IsVisible());
}

IN_PROC_BROWSER_TEST_F(VideoConferenceAppServiceClientTest, MediaCapturing) {
  // Install two apps so that they will can be tracked inside GetMediaApps.
  InstallApp(kAppId1);
  InstallApp(kAppId2);
  FakeAppInstance instance1(instance_registry_, kAppId1);
  instance1.Start();
  FakeAppInstance instance2(instance_registry_, kAppId2);
  instance2.Start();

  // no-camera, no-mic should not start a tracking of the app.
  SetAppCapabilityAccess(kAppId1, /*is_capturing_camera=*/false,
                         /*is_capturing_microphone=*/false);
  EXPECT_TRUE(GetMediaApps().empty());

  std::vector<crosapi::mojom::VideoConferenceMediaAppInfoPtr> media_app_info;

  // has-camera, no-mic should start the tracking of the app.
  SetAppCapabilityAccess(kAppId1, /*is_capturing_camera=*/true,
                         /*is_capturing_microphone=*/false);
  media_app_info = GetMediaApps();
  crosapi::mojom::VideoConferenceMediaAppInfoPtr expected_media_app_info =
      crosapi::mojom::VideoConferenceMediaAppInfo::New(
          /*id=*/media_app_info[0]->id,
          /*last_activity_time=*/media_app_info[0]->last_activity_time,
          /*is_capturing_camera=*/true,
          /*is_capturing_microphone=*/false,
          /*is_capturing_screen=*/false,
          /*title=*/media_app_info[0]->title, /*url=*/std::nullopt,
          /*app_type=*/crosapi::mojom::VideoConferenceAppType::kArcApp);
  ASSERT_EQ(media_app_info.size(), 1u);
  EXPECT_TRUE(media_app_info[0].Equals(expected_media_app_info));

  // has-camera, has-mic should change the value of GetMediaApps.
  SetAppCapabilityAccess(kAppId1, /*is_capturing_camera=*/true,
                         /*is_capturing_microphone=*/true);
  media_app_info = GetMediaApps();
  ASSERT_EQ(media_app_info.size(), 1u);
  expected_media_app_info->is_capturing_microphone = true;
  EXPECT_TRUE(media_app_info[0].Equals(expected_media_app_info));

  // no-camera, has-mic should change the value of GetMediaApps.
  SetAppCapabilityAccess(kAppId1, /*is_capturing_camera=*/false,
                         /*is_capturing_microphone=*/true);
  media_app_info = GetMediaApps();
  ASSERT_EQ(media_app_info.size(), 1u);
  expected_media_app_info->is_capturing_camera = false;
  EXPECT_TRUE(media_app_info[0].Equals(expected_media_app_info));

  // no-camera, no-mic should change the value of GetMediaApps; but not removing
  // the tracking app.
  SetAppCapabilityAccess(kAppId1, /*is_capturing_camera=*/false,
                         /*is_capturing_microphone=*/false);
  media_app_info = GetMediaApps();
  ASSERT_EQ(media_app_info.size(), 1u);
  expected_media_app_info->is_capturing_microphone = false;
  EXPECT_TRUE(media_app_info[0].Equals(expected_media_app_info));
}

IN_PROC_BROWSER_TEST_F(VideoConferenceAppServiceClientTest, LastActivityTime) {
  // Start an instance of kAppId1.
  InstallApp(kAppId1);
  FakeAppInstance instance1(instance_registry_, kAppId1);
  instance1.Start();

  // has-camera, has-mic should start tracking of the kAppId1.
  SetAppCapabilityAccess(kAppId1, /*is_capturing_camera=*/true,
                         /*is_capturing_microphone=*/true);

  std::vector<crosapi::mojom::VideoConferenceMediaAppInfoPtr> media_app_info;

  media_app_info = GetMediaApps();
  crosapi::mojom::VideoConferenceMediaAppInfoPtr expected_media_app_info =
      crosapi::mojom::VideoConferenceMediaAppInfo::New(
          /*id=*/media_app_info[0]->id,
          /*last_activity_time=*/media_app_info[0]->last_activity_time,
          /*is_capturing_camera=*/true,
          /*is_capturing_microphone=*/true,
          /*is_capturing_screen=*/false,
          /*title=*/media_app_info[0]->title, /*url=*/std::nullopt,
          /*app_type=*/crosapi::mojom::VideoConferenceAppType::kArcApp);
  ASSERT_EQ(media_app_info.size(), 1u);
  EXPECT_TRUE(media_app_info[0].Equals(expected_media_app_info));

  // Hide should not update last activity time.
  instance1.Hide();
  media_app_info = GetMediaApps();
  ASSERT_EQ(media_app_info.size(), 1u);
  EXPECT_TRUE(media_app_info[0].Equals(expected_media_app_info));

  // Show should update last activity time.
  instance1.Show();
  media_app_info = GetMediaApps();
  ASSERT_EQ(media_app_info.size(), 1u);
  EXPECT_GT(media_app_info[0]->last_activity_time,
            expected_media_app_info->last_activity_time);
}

IN_PROC_BROWSER_TEST_F(VideoConferenceAppServiceClientTest, CloseApp) {
  // Start two instance of kAppId1.
  InstallApp(kAppId1);
  FakeAppInstance instance1(instance_registry_, kAppId1);
  instance1.Start();
  FakeAppInstance instance2(instance_registry_, kAppId1);
  instance2.Start();

  // No media app should be recorded till now.
  EXPECT_TRUE(GetMediaApps().empty());

  // has-camera, has-mic should start a tracking of the app.
  SetAppCapabilityAccess(kAppId1, /*is_capturing_camera=*/true,
                         /*is_capturing_microphone=*/true);

  std::vector<crosapi::mojom::VideoConferenceMediaAppInfoPtr> media_app_info;

  media_app_info = GetMediaApps();
  crosapi::mojom::VideoConferenceMediaAppInfoPtr expected_media_app_info =
      crosapi::mojom::VideoConferenceMediaAppInfo::New(
          /*id=*/media_app_info[0]->id,
          /*last_activity_time=*/media_app_info[0]->last_activity_time,
          /*is_capturing_camera=*/true,
          /*is_capturing_microphone=*/true,
          /*is_capturing_screen=*/false,
          /*title=*/media_app_info[0]->title, /*url=*/std::nullopt,
          /*app_type=*/crosapi::mojom::VideoConferenceAppType::kArcApp);
  ASSERT_EQ(media_app_info.size(), 1u);
  EXPECT_TRUE(media_app_info[0].Equals(expected_media_app_info));

  // Closing instance1 should not remove tracking of kAppId1.
  instance1.Close();
  // Wait for the VideoConferenceAppServiceClient::MaybeRemoveApp to be called
  // in the PostTask.
  base::RunLoop().RunUntilIdle();
  media_app_info = GetMediaApps();
  ASSERT_EQ(media_app_info.size(), 1u);
  EXPECT_TRUE(media_app_info[0].Equals(expected_media_app_info));

  // Closing instance2 should remove trackingg of kAppId1.
  instance2.Close();
  // Wait for the VideoConferenceAppServiceClient::MaybeRemoveApp to be called
  // in the PostTask.
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(GetMediaApps().empty());

  // This should not add the app tracking back because there is no running
  // instance.
  SetAppCapabilityAccess(kAppId1, /*is_capturing_camera=*/false,
                         /*is_capturing_microphone=*/true);
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(GetMediaApps().empty());

  // This should not add the app tracking back because there is no running
  // instance.
  SetAppCapabilityAccess(kAppId1, /*is_capturing_camera=*/false,
                         /*is_capturing_microphone=*/false);
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(GetMediaApps().empty());
}

IN_PROC_BROWSER_TEST_F(VideoConferenceAppServiceClientTest,
                       HandleMediaUsageUpdate) {
  // Install two apps with permissions.
  InstallApp(kAppId1);
  InstallApp(kAppId2);
  UpdateAppPermision(kAppId1, /*has_camera_permission=*/true,
                     /*has_microphone_permission=*/false);
  UpdateAppPermision(kAppId2, /*has_camera_permission=*/false,
                     /*has_microphone_permission=*/true);

  // Start two running instance.
  FakeAppInstance instance1(instance_registry_, kAppId1);
  instance1.Start();
  FakeAppInstance instance2(instance_registry_, kAppId2);
  instance2.Start();

  VideoConferenceMediaState state = GetMediaStateInVideoConferenceManagerAsh();
  EXPECT_FALSE(state.has_media_app);
  EXPECT_FALSE(state.has_camera_permission);
  EXPECT_FALSE(state.has_microphone_permission);
  EXPECT_FALSE(state.is_capturing_camera);
  EXPECT_FALSE(state.is_capturing_microphone);
  EXPECT_FALSE(state.is_capturing_screen);

  // Accessing camera should start a tracking of the kAppId1.
  SetAppCapabilityAccess(kAppId1, /*is_capturing_camera=*/true,
                         /*is_capturing_microphone=*/false);

  state = GetMediaStateInVideoConferenceManagerAsh();
  EXPECT_TRUE(state.has_media_app);
  EXPECT_TRUE(state.has_camera_permission);
  EXPECT_TRUE(state.is_capturing_camera);

  // Accessing microphone should start a tracking of the kAppId2.
  SetAppCapabilityAccess(kAppId2, /*is_capturing_camera=*/false,
                         /*is_capturing_microphone=*/true);

  state = GetMediaStateInVideoConferenceManagerAsh();
  EXPECT_TRUE(state.has_media_app);
  EXPECT_TRUE(state.has_microphone_permission);
  EXPECT_TRUE(state.is_capturing_microphone);

  // This should stop the accessing, but not the permission.
  SetAppCapabilityAccess(kAppId1, /*is_capturing_camera=*/false,
                         /*is_capturing_microphone=*/false);

  state = GetMediaStateInVideoConferenceManagerAsh();
  EXPECT_TRUE(state.has_camera_permission);
  EXPECT_FALSE(state.is_capturing_camera);

  // Closing instance1 should remove tracking of kAppId1.
  instance1.Close();
  // Wait for the VideoConferenceAppServiceClient::MaybeRemoveApp to be called
  // in the PostTask.
  base::RunLoop().RunUntilIdle();

  state = GetMediaStateInVideoConferenceManagerAsh();
  EXPECT_FALSE(state.has_camera_permission);
  EXPECT_FALSE(state.is_capturing_camera);

  SetAppCapabilityAccess(kAppId2, /*is_capturing_camera=*/false,
                         /*is_capturing_microphone=*/false);

  state = GetMediaStateInVideoConferenceManagerAsh();
  EXPECT_TRUE(state.has_microphone_permission);
  EXPECT_FALSE(state.is_capturing_microphone);

  // Closing instance2 should remove trackingg of kAppId2.
  instance2.Close();
  // Wait for the VideoConferenceAppServiceClient::MaybeRemoveApp to be called
  // in the PostTask.
  base::RunLoop().RunUntilIdle();

  state = GetMediaStateInVideoConferenceManagerAsh();
  EXPECT_FALSE(state.has_media_app);
  EXPECT_FALSE(state.has_camera_permission);
  EXPECT_FALSE(state.has_microphone_permission);
  EXPECT_FALSE(state.is_capturing_camera);
  EXPECT_FALSE(state.is_capturing_microphone);
  EXPECT_FALSE(state.is_capturing_screen);
}

IN_PROC_BROWSER_TEST_F(VideoConferenceAppServiceClientTest,
                       OnlyCertainAppsAreTracked) {
  for (const auto type :
       {apps::AppType::kUnknown, apps::AppType::kCrostini,
        apps::AppType::kChromeApp, apps::AppType::kWeb,
        apps::AppType::kPluginVm, apps::AppType::kRemote,
        apps::AppType::kBorealis, apps::AppType::kSystemWeb,
        apps::AppType::kExtension, apps::AppType::kBruschetta}) {
    // Create a fake id.
    const std::string app_id = base::NumberToString(static_cast<int>(type));
    // Install the app with given type.
    InstallApp(app_id, type);
    // Start the app.
    FakeAppInstance instance(instance_registry_, app_id);
    instance.Start();

    // has-camera, has-mic should not start tracking of the app only because
    // that we are not tracking the AppType listed above.
    SetAppCapabilityAccess(app_id, /*is_capturing_camera=*/true,
                           /*is_capturing_microphone=*/true);

    EXPECT_TRUE(GetMediaApps().empty());
  }
}

IN_PROC_BROWSER_TEST_F(VideoConferenceAppServiceClientTest,
                       HandleDeviceUsedWhileDisabled) {
  // Notify disabling state of camera and microphone from
  // video_conference_manager_ash.
  crosapi::CrosapiManager::Get()
      ->crosapi_ash()
      ->video_conference_manager_ash()
      ->SetSystemMediaDeviceStatus(
          crosapi::mojom::VideoConferenceMediaDevice::kCamera,
          /*disabled=*/true);
  crosapi::CrosapiManager::Get()
      ->crosapi_ash()
      ->video_conference_manager_ash()
      ->SetSystemMediaDeviceStatus(
          crosapi::mojom::VideoConferenceMediaDevice::kMicrophone,
          /*disabled=*/true);

  FakeVideoConferenceTrayController* fake_try_controller =
      static_cast<FakeVideoConferenceTrayController*>(
          VideoConferenceTrayController::Get());

  InstallApp(kAppId1);

  // Accessing camera will trigger NotifyDeviceUsedWhileDisabled.
  SetAppCapabilityAccess(kAppId1, /*is_capturing_camera=*/true,
                         /*is_capturing_microphone=*/false);
  ASSERT_EQ(fake_try_controller->device_used_while_disabled_records().size(),
            1u);
  EXPECT_THAT(fake_try_controller->device_used_while_disabled_records().back(),
              testing::Pair(crosapi::mojom::VideoConferenceMediaDevice::kCamera,
                            base::UTF8ToUTF16(std::string(kAppName1))));

  // Accessing microphone will trigger NotifyDeviceUsedWhileDisabled.
  SetAppCapabilityAccess(kAppId1, /*is_capturing_camera=*/true,
                         /*is_capturing_microphone=*/true);
  ASSERT_EQ(fake_try_controller->device_used_while_disabled_records().size(),
            2u);
  EXPECT_THAT(
      fake_try_controller->device_used_while_disabled_records().back(),
      testing::Pair(crosapi::mojom::VideoConferenceMediaDevice::kMicrophone,
                    base::UTF8ToUTF16(std::string(kAppName1))));

  // Stopping microphone access should not trigger
  // NotifyDeviceUsedWhileDisabled.
  SetAppCapabilityAccess(kAppId1, /*is_capturing_camera=*/true,
                         /*is_capturing_microphone=*/false);
  ASSERT_EQ(fake_try_controller->device_used_while_disabled_records().size(),
            2u);

  // Stopping camera access should not trigger NotifyDeviceUsedWhileDisabled.
  SetAppCapabilityAccess(kAppId1, /*is_capturing_camera=*/false,
                         /*is_capturing_microphone=*/false);
  ASSERT_EQ(fake_try_controller->device_used_while_disabled_records().size(),
            2u);

  // Notify enabling state of camera and microphone from
  // video_conference_manager_ash.
  crosapi::CrosapiManager::Get()
      ->crosapi_ash()
      ->video_conference_manager_ash()
      ->SetSystemMediaDeviceStatus(
          crosapi::mojom::VideoConferenceMediaDevice::kCamera,
          /*disabled=*/false);
  crosapi::CrosapiManager::Get()
      ->crosapi_ash()
      ->video_conference_manager_ash()
      ->SetSystemMediaDeviceStatus(
          crosapi::mojom::VideoConferenceMediaDevice::kMicrophone,
          /*disabled=*/false);

  // Accessing camera should not trigger NotifyDeviceUsedWhileDisabled because
  // camera is not disabled.
  SetAppCapabilityAccess(kAppId1, /*is_capturing_camera=*/true,
                         /*is_capturing_microphone=*/false);
  ASSERT_EQ(fake_try_controller->device_used_while_disabled_records().size(),
            2u);

  // Accessing microphone should not trigger NotifyDeviceUsedWhileDisabled
  // because microphone is not disabled.
  SetAppCapabilityAccess(kAppId1, /*is_capturing_camera=*/true,
                         /*is_capturing_microphone=*/true);
  ASSERT_EQ(fake_try_controller->device_used_while_disabled_records().size(),
            2u);
}

IN_PROC_BROWSER_TEST_F(VideoConferenceAppServiceClientTest,
                       SomeAppsAreNotTracked) {
  // Install all apps that should be skipped.
  for (const std::string& app_id : ::video_conference::kSkipAppIds) {
    InstallApp(app_id);
    // Accessing mic and camera should trigger tracking except the app_id is
    // skipped.
    SetAppCapabilityAccess(app_id, /*is_capturing_camera=*/true,
                           /*is_capturing_microphone=*/true);
  }

  EXPECT_TRUE(GetMediaApps().empty());
}

IN_PROC_BROWSER_TEST_F(VideoConferenceAppServiceClientTest, UkmTest) {
  // Install two apps with permissions.
  InstallApp(kAppId1);
  InstallApp(kAppId2);
  UpdateAppPermision(kAppId1, /*has_camera_permission=*/true,
                     /*has_microphone_permission=*/false);
  UpdateAppPermision(kAppId2, /*has_camera_permission=*/false,
                     /*has_microphone_permission=*/true);

  // Start two running instance.
  FakeAppInstance instance1(instance_registry_, kAppId1);
  instance1.Start();
  FakeAppInstance instance2(instance_registry_, kAppId2);
  instance2.Start();

  // Accessing camera should start a tracking of the kAppId1.
  SetAppCapabilityAccess(kAppId1, /*is_capturing_camera=*/true,
                         /*is_capturing_microphone=*/false);
  // Stopping camera access.
  SetAppCapabilityAccess(kAppId1, /*is_capturing_camera=*/false,
                         /*is_capturing_microphone=*/false);

  // Closing instance1 should remove tracking of kAppId1, thus triggers ukm
  // logging.
  instance1.Close();
  // Wait for the VideoConferenceAppServiceClient::MaybeRemoveApp to be called
  // in the PostTask.
  base::RunLoop().RunUntilIdle();

  auto* vc_entry0 =
      test_ukm_recorder_->GetEntriesByName(UkmEntry::kEntryName)[0].get();
  test_ukm_recorder_->ExpectEntryMetric(vc_entry0,
                                        UkmEntry::kDidCaptureCameraName, true);
  test_ukm_recorder_->ExpectEntryMetric(
      vc_entry0, UkmEntry::kDidCaptureMicrophoneName, false);
  test_ukm_recorder_->ExpectEntryMetric(vc_entry0,
                                        UkmEntry::kDidCaptureScreenName, false);
  test_ukm_recorder_->ExpectEntryMetric(
      vc_entry0, UkmEntry::kMicrophoneCaptureDurationName, 0);
  test_ukm_recorder_->ExpectEntryMetric(
      vc_entry0, UkmEntry::kScreenCaptureDurationName, 0);

  SetAppCapabilityAccess(kAppId2, /*is_capturing_camera=*/true,
                         /*is_capturing_microphone=*/true);

  // Closing instance2 should remove tracking of kAppId2, thus triggers ukm
  // logging.
  instance2.Close();
  // Wait for the VideoConferenceAppServiceClient::MaybeRemoveApp to be called
  // in the PostTask.
  base::RunLoop().RunUntilIdle();

  auto* vc_entry1 =
      test_ukm_recorder_->GetEntriesByName(UkmEntry::kEntryName)[1].get();
  test_ukm_recorder_->ExpectEntryMetric(vc_entry1,
                                        UkmEntry::kDidCaptureCameraName, true);
  test_ukm_recorder_->ExpectEntryMetric(
      vc_entry1, UkmEntry::kDidCaptureMicrophoneName, true);
  test_ukm_recorder_->ExpectEntryMetric(vc_entry1,
                                        UkmEntry::kDidCaptureScreenName, false);
  test_ukm_recorder_->ExpectEntryMetric(
      vc_entry1, UkmEntry::kScreenCaptureDurationName, 0);
}

}  // namespace ash