File: video_conference_ash_feature_client_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (286 lines) | stat: -rw-r--r-- 11,523 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
// 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_ash_feature_client.h"

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

#include "ash/constants/ash_features.h"
#include "ash/constants/ash_switches.h"
#include "ash/system/video_conference/fake_video_conference_tray_controller.h"
#include "ash/system/video_conference/video_conference_common.h"
#include "base/command_line.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/ash/borealis/borealis_prefs.h"
#include "chrome/browser/ash/crosapi/crosapi_ash.h"
#include "chrome/browser/ash/crosapi/crosapi_manager.h"
#include "chrome/browser/ash/crostini/crostini_pref_names.h"
#include "chrome/browser/ash/plugin_vm/plugin_vm_pref_names.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/profiles/profile_manager.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chromeos/crosapi/mojom/video_conference.mojom.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_test.h"
#include "testing/gmock/include/gmock/gmock.h"

namespace ash {

constexpr char kCrostiniVmId[] = "Linux";
constexpr char kPluginVmId[] = "PluginVm";
constexpr char kBorealisId[] = "Borealis";

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

    InProcessBrowserTest::SetUp();
  }

  // Update the permission of current `app_id`.
  void UpdateAppPermision(const std::string& app_id,
                          bool has_camera_permission,
                          bool has_microphone_permission) {
    auto* prefs = ProfileManager::GetActiveUserProfile()->GetPrefs();
    if (app_id == kCrostiniVmId) {
      prefs->SetBoolean(crostini::prefs::kCrostiniMicAllowed,
                        has_microphone_permission);
      CHECK(!has_camera_permission)
          << "Camera is not supported for CrostiniVm yet.";
    }
    if (app_id == kBorealisId) {
      prefs->SetBoolean(borealis::prefs::kBorealisMicAllowed,
                        has_microphone_permission);
      CHECK(!has_camera_permission)
          << "Camera is not supported for CrostiniVm yet.";
    }
    if (app_id == kPluginVmId) {
      prefs->SetBoolean(plugin_vm::prefs::kPluginVmCameraAllowed,
                        has_camera_permission);
      prefs->SetBoolean(plugin_vm::prefs::kPluginVmMicAllowed,
                        has_microphone_permission);
    }
  }

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

    VideoConferenceAshFeatureClient::Get()->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:
  base::test::ScopedFeatureList scoped_feature_list_;
};

IN_PROC_BROWSER_TEST_F(VideoConferenceAshfeatureClientTest, GetMediaApps) {
  {
    // Notifying Crostini is using Mic.
    VideoConferenceAshFeatureClient::Get()->OnVmDeviceUpdated(
        VmCameraMicManager::VmType::kCrostiniVm,
        VmCameraMicManager::DeviceType::kMic, true);

    // GetMediaApps should return kCrostinId.
    auto media_app_info = GetMediaApps();
    ASSERT_EQ(media_app_info.size(), 1u);
    const auto& info0 = media_app_info[0];

    crosapi::mojom::VideoConferenceMediaAppInfoPtr expected_media_app_info =
        crosapi::mojom::VideoConferenceMediaAppInfo::New(
            /*id=*/info0->id,
            /*last_activity_time=*/info0->last_activity_time,
            /*is_capturing_camera=*/false,
            /*is_capturing_microphone=*/true,
            /*is_capturing_screen=*/false,
            /*title=*/base::UTF8ToUTF16(std::string(kCrostiniVmId)),
            /*url=*/std::nullopt,
            /*app_type=*/crosapi::mojom::VideoConferenceAppType::kCrostiniVm);

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

    // Stop accessing should remove the app.
    VideoConferenceAshFeatureClient::Get()->OnVmDeviceUpdated(
        VmCameraMicManager::VmType::kCrostiniVm,
        VmCameraMicManager::DeviceType::kMic, false);

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

  {
    // Notifying PluginVm is using Mic and Camera.
    VideoConferenceAshFeatureClient::Get()->OnVmDeviceUpdated(
        VmCameraMicManager::VmType::kPluginVm,
        VmCameraMicManager::DeviceType::kMic, true);
    VideoConferenceAshFeatureClient::Get()->OnVmDeviceUpdated(
        VmCameraMicManager::VmType::kPluginVm,
        VmCameraMicManager::DeviceType::kCamera, true);

    // GetMediaApps should return PluginVm.
    auto media_app_info = GetMediaApps();
    ASSERT_EQ(media_app_info.size(), 1u);
    const auto& info0 = media_app_info[0];

    crosapi::mojom::VideoConferenceMediaAppInfoPtr expected_media_app_info =
        crosapi::mojom::VideoConferenceMediaAppInfo::New(
            /*id=*/info0->id,
            /*last_activity_time=*/info0->last_activity_time,
            /*is_capturing_camera=*/true,
            /*is_capturing_microphone=*/true,
            /*is_capturing_screen=*/false,
            /*title=*/base::UTF8ToUTF16(std::string(kPluginVmId)),
            /*url=*/std::nullopt,
            /*app_type=*/crosapi::mojom::VideoConferenceAppType::kPluginVm);

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

IN_PROC_BROWSER_TEST_F(VideoConferenceAshfeatureClientTest,
                       HandleMediaUsageUpdate) {
  // Set initial permissions.
  UpdateAppPermision(kCrostiniVmId, /*has_camera_permission=*/false,
                     /*has_microphone_permission=*/true);
  UpdateAppPermision(kPluginVmId, /*has_camera_permission=*/true,
                     /*has_microphone_permission=*/false);

  // All state should be false initially.
  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);

  // Notifying Crostini is using Mic.
  VideoConferenceAshFeatureClient::Get()->OnVmDeviceUpdated(
      VmCameraMicManager::VmType::kCrostiniVm,
      VmCameraMicManager::DeviceType::kMic, true);

  // State should have update for microphone from CrostiniVm.
  state = GetMediaStateInVideoConferenceManagerAsh();
  EXPECT_TRUE(state.has_media_app);
  EXPECT_TRUE(state.has_microphone_permission);
  EXPECT_TRUE(state.is_capturing_microphone);
  EXPECT_FALSE(state.has_camera_permission);
  EXPECT_FALSE(state.is_capturing_camera);

  // Notifying PluginVm is using Mic.
  VideoConferenceAshFeatureClient::Get()->OnVmDeviceUpdated(
      VmCameraMicManager::VmType::kPluginVm,
      VmCameraMicManager::DeviceType::kMic, true);

  // Extra permission obtained from PluginVm.
  state = GetMediaStateInVideoConferenceManagerAsh();
  EXPECT_TRUE(state.has_camera_permission);
  EXPECT_FALSE(state.is_capturing_camera);

  // Notifying PluginVm is using Camera.
  VideoConferenceAshFeatureClient::Get()->OnVmDeviceUpdated(
      VmCameraMicManager::VmType::kPluginVm,
      VmCameraMicManager::DeviceType::kCamera, true);

  // Expecting camera capturing from PluginVm.
  state = GetMediaStateInVideoConferenceManagerAsh();
  EXPECT_TRUE(state.has_camera_permission);
  EXPECT_TRUE(state.is_capturing_camera);

  // Notifying Stopping accessing from PluginVm.
  VideoConferenceAshFeatureClient::Get()->OnVmDeviceUpdated(
      VmCameraMicManager::VmType::kPluginVm,
      VmCameraMicManager::DeviceType::kMic, false);
  VideoConferenceAshFeatureClient::Get()->OnVmDeviceUpdated(
      VmCameraMicManager::VmType::kPluginVm,
      VmCameraMicManager::DeviceType::kCamera, false);

  // Camera permission and capturing should be gone.
  state = GetMediaStateInVideoConferenceManagerAsh();
  EXPECT_FALSE(state.has_camera_permission);
  EXPECT_FALSE(state.is_capturing_camera);

  // Notifying Stopping accessing from Crostini.
  VideoConferenceAshFeatureClient::Get()->OnVmDeviceUpdated(
      VmCameraMicManager::VmType::kCrostiniVm,
      VmCameraMicManager::DeviceType::kMic, false);

  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(VideoConferenceAshfeatureClientTest,
                       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());

  // Notifying Crostini is using Mic.
  VideoConferenceAshFeatureClient::Get()->OnVmDeviceUpdated(
      VmCameraMicManager::VmType::kCrostiniVm,
      VmCameraMicManager::DeviceType::kMic, true);

  // One UsedWhileDisabled call should be sent to
  // FakeVideoConferenceTrayController.
  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::kMicrophone,
                    base::UTF8ToUTF16(std::string(kCrostiniVmId))));

  // Notifying PluginVm is using Camera.
  VideoConferenceAshFeatureClient::Get()->OnVmDeviceUpdated(
      VmCameraMicManager::VmType::kPluginVm,
      VmCameraMicManager::DeviceType::kCamera, true);

  // Another UsedWhileDisabled call should be sent to
  // FakeVideoConferenceTrayController.
  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::kCamera,
                            base::UTF8ToUTF16(std::string(kPluginVmId))));
}

}  // namespace ash