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
|
// Copyright 2022 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/chromeos/video_conference/video_conference_manager_client.h"
#include <string>
#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_tray_controller.h"
#include "base/command_line.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/unguessable_token.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_media_listener.h"
#include "chrome/browser/chromeos/video_conference/video_conference_web_app.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_activity_simulator.h"
#include "chrome/browser/ui/tabs/tab_enums.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chromeos/crosapi/mojom/video_conference.mojom.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_user_data.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace video_conference {
namespace {
constexpr char kTestURL1[] = "about:blank";
constexpr char kTestURL2[] = "https://localhost";
} // namespace
// Fake class for testing `VideoConferenceManagerClientImpl`. Overrides
// `NotifyManager()` to not send any updates to the VC manager and provides
// access to the internal `id_to_webcontents` map on the client.
class FakeVideoConferenceManagerClient
: public VideoConferenceManagerClientImpl {
public:
FakeVideoConferenceManagerClient() = default;
FakeVideoConferenceManagerClient(const FakeVideoConferenceManagerClient&) =
delete;
FakeVideoConferenceManagerClient& operator=(
const FakeVideoConferenceManagerClient&) = delete;
~FakeVideoConferenceManagerClient() override = default;
std::map<base::UnguessableToken, raw_ptr<content::WebContents>>
id_to_webcontents() {
return id_to_webcontents_;
}
const base::UnguessableToken& client_id() { return client_id_; }
VideoConferencePermissions GetAggregatedPermissions() {
return VideoConferenceManagerClientImpl::GetAggregatedPermissions();
}
bool camera_system_disabled() {
return media_listener_->camera_system_disabled_;
}
bool microphone_system_disabled() {
return media_listener_->microphone_system_disabled_;
}
crosapi::mojom::VideoConferenceMediaUsageStatusPtr& status() {
return status_;
}
protected:
void NotifyManager(
crosapi::mojom::VideoConferenceMediaUsageStatusPtr status) override {}
};
class VideoConferenceManagerClientTest : public InProcessBrowserTest {
public:
VideoConferenceManagerClientTest() = default;
VideoConferenceManagerClientTest(const VideoConferenceManagerClientTest&) =
delete;
VideoConferenceManagerClientTest& operator=(
const VideoConferenceManagerClientTest&) = delete;
~VideoConferenceManagerClientTest() override = default;
void SetUp() override {
scoped_feature_list_.InitAndEnableFeature(
ash::features::kFeatureManagementVideoConference);
InProcessBrowserTest::SetUp();
}
// Creates and returns a new `WebContents` at the given tab `index`.
content::WebContents* CreateWebContentsAt(int index) {
EXPECT_TRUE(
AddTabAtIndex(index, GURL(kTestURL1), ui::PAGE_TRANSITION_TYPED));
return browser()->tab_strip_model()->GetWebContentsAt(index);
}
// Removed `WebContents` at the given tab `index`.
void RemoveWebContentsAt(int index) {
browser()->tab_strip_model()->CloseWebContentsAt(index,
TabCloseTypes::CLOSE_NONE);
}
void UpdateWebContentsTitle(content::WebContents* contents,
const std::u16string& title) {
content::NavigationEntry* entry =
contents->GetController().GetLastCommittedEntry();
ASSERT_TRUE(entry);
contents->UpdateTitleForEntry(entry, title);
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
// Tests creating VcWebApps and removing them by closing tabs.
IN_PROC_BROWSER_TEST_F(VideoConferenceManagerClientTest,
TabCreationAndRemoval) {
FakeVideoConferenceManagerClient client;
auto* web_contents1 = CreateWebContentsAt(0);
auto* web_contents2 = CreateWebContentsAt(1);
auto* web_contents3 = CreateWebContentsAt(2);
client.CreateVideoConferenceWebApp(web_contents1);
EXPECT_EQ(client.id_to_webcontents().size(), 1u);
client.CreateVideoConferenceWebApp(web_contents2);
EXPECT_EQ(client.id_to_webcontents().size(), 2u);
client.CreateVideoConferenceWebApp(web_contents3);
EXPECT_EQ(client.id_to_webcontents().size(), 3u);
// It's important to close tabs from right-to-left as otherwise the indices
// change.
RemoveWebContentsAt(2);
EXPECT_EQ(client.id_to_webcontents().size(), 2u);
RemoveWebContentsAt(1);
EXPECT_EQ(client.id_to_webcontents().size(), 1u);
RemoveWebContentsAt(0);
EXPECT_EQ(client.id_to_webcontents().size(), 0u);
}
// Tests that a change in the primary page of the web contents of a VcWebApp
// removes it from the client.
IN_PROC_BROWSER_TEST_F(VideoConferenceManagerClientTest,
WebContentsPrimaryPageChange) {
FakeVideoConferenceManagerClient client;
TabActivitySimulator tab_activity_simulator;
auto* web_contents = CreateWebContentsAt(0);
auto* vc_app = client.CreateVideoConferenceWebApp(web_contents);
EXPECT_EQ(client.id_to_webcontents().size(), 1u);
// Ensure tab is in focus.
vc_app->ActivateApp();
// Navigate to a different URL and trigger a primary page change event.
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), GURL(kTestURL2)));
// There should no longer be a WebContentsUserData associated with this
// `web_contents`.
EXPECT_FALSE(
content::WebContentsUserData<VideoConferenceWebApp>::FromWebContents(
web_contents));
EXPECT_EQ(client.id_to_webcontents().size(), 0u);
}
// Tests `GetMediaApps` returns `VideoConferenceMediaAppInfo`s with expected
// values.
IN_PROC_BROWSER_TEST_F(VideoConferenceManagerClientTest, GetMediaApps) {
FakeVideoConferenceManagerClient client;
auto* web_contents1 = CreateWebContentsAt(0);
UpdateWebContentsTitle(web_contents1, u"app1");
auto* web_contents2 = CreateWebContentsAt(1);
UpdateWebContentsTitle(web_contents2, u"app2");
auto* web_contents3 = CreateWebContentsAt(2);
auto* vc_app1 = client.CreateVideoConferenceWebApp(web_contents1);
auto* vc_app2 = client.CreateVideoConferenceWebApp(web_contents2);
auto* vc_app3 = client.CreateVideoConferenceWebApp(web_contents3);
vc_app1->state().is_capturing_camera = true;
vc_app1->state().is_capturing_microphone = true;
vc_app2->state().is_capturing_microphone = true;
vc_app1->state().is_capturing_screen = true;
vc_app2->state().is_capturing_screen = true;
vc_app3->state().is_capturing_screen = true;
std::map<base::UnguessableToken, VideoConferenceWebApp*> id_to_vc_app = {
{vc_app1->state().id, vc_app1},
{vc_app2->state().id, vc_app2},
{vc_app3->state().id, vc_app3},
};
client.GetMediaApps(base::BindLambdaForTesting(
[&](std::vector<crosapi::mojom::VideoConferenceMediaAppInfoPtr> apps) {
EXPECT_EQ(apps.size(), 3u);
for (auto& app : apps) {
auto* vc_app = id_to_vc_app[app->id];
EXPECT_EQ(vc_app->state().is_capturing_camera,
app->is_capturing_camera);
EXPECT_EQ(vc_app->state().is_capturing_microphone,
app->is_capturing_microphone);
EXPECT_EQ(vc_app->state().is_capturing_screen,
app->is_capturing_screen);
EXPECT_EQ(vc_app->GetWebContents().GetTitle(), app->title);
}
}));
}
// Tests setting/clearing system statuses for camera and microphone.
IN_PROC_BROWSER_TEST_F(VideoConferenceManagerClientTest,
SetSystemMediaDeviceStatus) {
FakeVideoConferenceManagerClient client;
auto* vc_manager = crosapi::CrosapiManager::Get()
->crosapi_ash()
->video_conference_manager_ash();
vc_manager->RegisterCppClient(&client, client.client_id());
ash::FakeVideoConferenceTrayController* controller =
static_cast<ash::FakeVideoConferenceTrayController*>(
ash::VideoConferenceTrayController::Get());
ASSERT_TRUE(controller);
EXPECT_EQ(controller->device_used_while_disabled_records().size(), 0u);
EXPECT_FALSE(client.camera_system_disabled());
EXPECT_FALSE(client.microphone_system_disabled());
vc_manager->SetSystemMediaDeviceStatus(
crosapi::mojom::VideoConferenceMediaDevice::kCamera,
/*disabled=*/true);
EXPECT_TRUE(client.camera_system_disabled());
EXPECT_FALSE(client.microphone_system_disabled());
vc_manager->SetSystemMediaDeviceStatus(
crosapi::mojom::VideoConferenceMediaDevice::kMicrophone,
/*disabled=*/true);
EXPECT_TRUE(client.camera_system_disabled());
EXPECT_TRUE(client.microphone_system_disabled());
vc_manager->SetSystemMediaDeviceStatus(
crosapi::mojom::VideoConferenceMediaDevice::kMicrophone,
/*disabled=*/false);
EXPECT_TRUE(client.camera_system_disabled());
EXPECT_FALSE(client.microphone_system_disabled());
vc_manager->SetSystemMediaDeviceStatus(
crosapi::mojom::VideoConferenceMediaDevice::kCamera,
/*disabled=*/false);
EXPECT_FALSE(client.camera_system_disabled());
EXPECT_FALSE(client.microphone_system_disabled());
}
// Tests client updates relating to adding and removing VC web apps and title
// changes.
IN_PROC_BROWSER_TEST_F(VideoConferenceManagerClientTest, ClientUpdate) {
FakeVideoConferenceManagerClient client;
auto* vc_manager = crosapi::CrosapiManager::Get()
->crosapi_ash()
->video_conference_manager_ash();
vc_manager->RegisterCppClient(&client, client.client_id());
ash::FakeVideoConferenceTrayController* controller =
static_cast<ash::FakeVideoConferenceTrayController*>(
ash::VideoConferenceTrayController::Get());
ASSERT_TRUE(controller);
// Add a new VC web app on the client.
EXPECT_TRUE(AddTabAtIndex(0, GURL("about:blank"), ui::PAGE_TRANSITION_LINK));
auto* web_contents = browser()->tab_strip_model()->GetWebContentsAt(0);
client.CreateVideoConferenceWebApp(web_contents);
// Confirm the update received by the controller has `added_or_removed_app`
// set to true.
EXPECT_EQ(controller->last_client_update()->added_or_removed_app,
crosapi::mojom::VideoConferenceAppUpdate::kAppAdded);
EXPECT_FALSE(controller->last_client_update()->title_change_info);
// Update the title and confirm correct fields were set.
std::u16string new_title = u"New Title";
UpdateWebContentsTitle(web_contents, new_title);
EXPECT_EQ(controller->last_client_update()->added_or_removed_app,
crosapi::mojom::VideoConferenceAppUpdate::kNone);
EXPECT_TRUE(controller->last_client_update()->title_change_info);
EXPECT_EQ(controller->last_client_update()->title_change_info->new_title,
new_title);
// Remove the VC web app by closing the corresponding WebContents.
browser()->tab_strip_model()->CloseWebContentsAt(0,
TabCloseTypes::CLOSE_NONE);
EXPECT_EQ(controller->last_client_update()->added_or_removed_app,
crosapi::mojom::VideoConferenceAppUpdate::kAppRemoved);
EXPECT_FALSE(controller->last_client_update()->title_change_info);
}
// Tests aggregated media usage status received on `HandleMediaUsageUpdate`.
IN_PROC_BROWSER_TEST_F(VideoConferenceManagerClientTest, MediaUsageUpdate) {
FakeVideoConferenceManagerClient client;
EXPECT_FALSE(client.status()->has_media_app);
EXPECT_FALSE(client.status()->is_capturing_camera);
EXPECT_FALSE(client.status()->is_capturing_microphone);
EXPECT_FALSE(client.status()->is_capturing_screen);
auto* web_contents1 = CreateWebContentsAt(0);
UpdateWebContentsTitle(web_contents1, u"app1");
auto* web_contents2 = CreateWebContentsAt(1);
UpdateWebContentsTitle(web_contents2, u"app2");
auto* web_contents3 = CreateWebContentsAt(2);
auto* vc_app1 = client.CreateVideoConferenceWebApp(web_contents1);
auto* vc_app2 = client.CreateVideoConferenceWebApp(web_contents2);
auto* vc_app3 = client.CreateVideoConferenceWebApp(web_contents3);
client.HandleMediaUsageUpdate();
EXPECT_TRUE(client.status()->has_media_app);
EXPECT_FALSE(client.status()->is_capturing_camera);
EXPECT_FALSE(client.status()->is_capturing_microphone);
EXPECT_FALSE(client.status()->is_capturing_screen);
vc_app1->state().is_capturing_camera = true;
client.HandleMediaUsageUpdate();
EXPECT_TRUE(client.status()->has_media_app);
EXPECT_TRUE(client.status()->is_capturing_camera);
EXPECT_FALSE(client.status()->is_capturing_microphone);
EXPECT_FALSE(client.status()->is_capturing_screen);
vc_app2->state().is_capturing_microphone = true;
client.HandleMediaUsageUpdate();
EXPECT_TRUE(client.status()->has_media_app);
EXPECT_TRUE(client.status()->is_capturing_camera);
EXPECT_TRUE(client.status()->is_capturing_microphone);
EXPECT_FALSE(client.status()->is_capturing_screen);
vc_app3->state().is_capturing_screen = true;
client.HandleMediaUsageUpdate();
EXPECT_TRUE(client.status()->has_media_app);
EXPECT_TRUE(client.status()->is_capturing_camera);
EXPECT_TRUE(client.status()->is_capturing_microphone);
EXPECT_TRUE(client.status()->is_capturing_screen);
RemoveWebContentsAt(2);
RemoveWebContentsAt(1);
RemoveWebContentsAt(0);
client.HandleMediaUsageUpdate();
EXPECT_FALSE(client.status()->has_media_app);
EXPECT_FALSE(client.status()->is_capturing_camera);
EXPECT_FALSE(client.status()->is_capturing_microphone);
EXPECT_FALSE(client.status()->is_capturing_screen);
}
// Tests if `ReturnToApp` correctly activates tab of the `VideoConferenceWebApp`
// corresponding to the `id` provided.
IN_PROC_BROWSER_TEST_F(VideoConferenceManagerClientTest, ReturnToApp) {
FakeVideoConferenceManagerClient client;
auto* web_contents1 = CreateWebContentsAt(0);
auto* web_contents2 = CreateWebContentsAt(1);
auto* vc_app1 = client.CreateVideoConferenceWebApp(web_contents1);
auto* vc_app2 = client.CreateVideoConferenceWebApp(web_contents2);
client.ReturnToApp(
vc_app1->state().id, base::BindLambdaForTesting([&](bool success) {
EXPECT_TRUE(success);
EXPECT_EQ(browser()->tab_strip_model()->active_index(), 0);
}));
client.ReturnToApp(
vc_app2->state().id, base::BindLambdaForTesting([&](bool success) {
EXPECT_TRUE(success);
EXPECT_EQ(browser()->tab_strip_model()->active_index(), 1);
}));
client.ReturnToApp(
vc_app1->state().id, base::BindLambdaForTesting([&](bool success) {
EXPECT_TRUE(success);
EXPECT_EQ(browser()->tab_strip_model()->active_index(), 0);
}));
}
// Tests that for extensions, permissions equate to capturing statuses.
IN_PROC_BROWSER_TEST_F(VideoConferenceManagerClientTest, ExtensionPermissions) {
FakeVideoConferenceManagerClient client;
auto* web_contents = CreateWebContentsAt(0);
auto* vc_app = client.CreateVideoConferenceWebApp(web_contents);
// Make vc_app an extension.
vc_app->state().is_extension = true;
auto permissions = client.GetAggregatedPermissions();
EXPECT_FALSE(permissions.has_camera_permission);
EXPECT_FALSE(permissions.has_microphone_permission);
// Set capturing to true.
vc_app->state().is_capturing_camera = true;
permissions = client.GetAggregatedPermissions();
EXPECT_TRUE(permissions.has_camera_permission);
EXPECT_FALSE(permissions.has_microphone_permission);
// Make vc_app a non-extension.
vc_app->state().is_extension = false;
permissions = client.GetAggregatedPermissions();
EXPECT_FALSE(permissions.has_camera_permission);
EXPECT_FALSE(permissions.has_microphone_permission);
}
} // namespace video_conference
|