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
|
// Copyright 2024 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/views/media_preview/media_coordinator.h"
#include "chrome/browser/media/prefs/capture_device_ranking.h"
#include "chrome/browser/ui/views/frame/test_with_browser_view.h"
#include "chrome/browser/ui/views/media_preview/media_preview_metrics.h"
#include "chrome/browser/ui/views/media_preview/media_view.h"
#include "components/media_effects/test/fake_audio_service.h"
#include "components/media_effects/test/fake_video_capture_service.h"
#include "components/media_effects/test/scoped_media_device_info.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
constexpr char kCameraId1[] = "camera_id_1";
constexpr char kCameraName1[] = "camera_name_1";
constexpr char kCameraId2[] = "camera_id_2";
constexpr char kCameraName2[] = "camera_name_2";
constexpr char kMicId1[] = "mic_id_1";
constexpr char kMicName1[] = "mic_name_1";
constexpr char kGroupId1[] = "group_id_1";
constexpr char kMicId2[] = "mic_id_2";
constexpr char kMicName2[] = "mic_name_2";
constexpr char kGroupId2[] = "group_id_2";
media_preview_metrics::Context GetMetricsContext(
MediaCoordinator::ViewType type) {
return {media_preview_metrics::UiLocation::kPermissionPrompt,
media_coordinator::GetPreviewTypeFromMediaCoordinatorViewType(type),
media_coordinator::GetPromptTypeFromMediaCoordinatorViewType(type),
nullptr};
}
MATCHER(VideoCaptureDeviceInfoEq, "") {
return std::get<0>(arg).descriptor.device_id ==
std::get<1>(arg).descriptor.device_id;
}
} // namespace
using testing::ElementsAre;
using testing::Pointwise;
class MediaCoordinatorTest : public TestWithBrowserView {
protected:
void SetUp() override {
TestWithBrowserView::SetUp();
media_device_info_.emplace();
}
void InitializeCoordinator(MediaCoordinator::ViewType view_type) {
coordinator_.emplace(view_type, parent_view_, /*is_subsection=*/false,
MediaCoordinator::EligibleDevices{},
profile()->GetWeakPtr(),
/*allow_device_selection=*/true,
GetMetricsContext(view_type), /*delegate=*/nullptr);
}
void TearDown() override {
coordinator_.reset();
TestWithBrowserView::TearDown();
}
void AddCameras() {
ASSERT_TRUE(
video_service_.AddFakeCameraBlocking({kCameraName1, kCameraId1}));
ASSERT_TRUE(
video_service_.AddFakeCameraBlocking({kCameraName2, kCameraId2}));
}
void AddMics() {
ASSERT_TRUE(audio_service_.AddFakeInputDeviceBlocking(
{kMicName1, kMicId1, kGroupId1}));
ASSERT_TRUE(audio_service_.AddFakeInputDeviceBlocking(
{kMicName2, kMicId2, kGroupId2}));
}
media_effects::ScopedFakeAudioService audio_service_;
media_effects::ScopedFakeVideoCaptureService video_service_;
std::optional<media_effects::ScopedMediaDeviceInfo> media_device_info_;
MediaView parent_view_;
std::optional<MediaCoordinator> coordinator_;
};
TEST_F(MediaCoordinatorTest, CamerasRankingUpdate) {
InitializeCoordinator(MediaCoordinator::ViewType::kCameraOnly);
AddCameras();
const media::VideoCaptureDeviceInfo kCamera1{{kCameraName1, kCameraId1}};
const media::VideoCaptureDeviceInfo kCamera2{{kCameraName2, kCameraId2}};
// Preference ranking defaults to noop.
std::vector camera_infos{kCamera2, kCamera1};
media_prefs::PreferenceRankVideoDeviceInfos(*profile()->GetPrefs(),
camera_infos);
EXPECT_THAT(camera_infos,
Pointwise(VideoCaptureDeviceInfoEq(), {kCamera2, kCamera1}));
// Ranking is updated to make the currently selected device most preferred.
coordinator_->UpdateDevicePreferenceRanking();
media_prefs::PreferenceRankVideoDeviceInfos(*profile()->GetPrefs(),
camera_infos);
EXPECT_THAT(camera_infos,
Pointwise(VideoCaptureDeviceInfoEq(), {kCamera1, kCamera2}));
}
TEST_F(MediaCoordinatorTest, MicsRankingUpdate) {
InitializeCoordinator(MediaCoordinator::ViewType::kMicOnly);
AddMics();
const media::AudioDeviceDescription kMic1{kMicName1, kMicId1, kGroupId1};
const media::AudioDeviceDescription kMic2{kMicName2, kMicId2, kGroupId2};
// Preference ranking defaults to noop.
std::vector mic_infos{kMic2, kMic1};
media_prefs::PreferenceRankAudioDeviceInfos(*profile()->GetPrefs(),
mic_infos);
EXPECT_THAT(mic_infos, ElementsAre(kMic2, kMic1));
// Ranking is updated to make the currently selected device most preferred.
coordinator_->UpdateDevicePreferenceRanking();
media_prefs::PreferenceRankAudioDeviceInfos(*profile()->GetPrefs(),
mic_infos);
EXPECT_THAT(mic_infos, ElementsAre(kMic1, kMic2));
}
TEST_F(MediaCoordinatorTest, CamerasAndMicsRankingUpdate) {
InitializeCoordinator(MediaCoordinator::ViewType::kBoth);
AddCameras();
AddMics();
const media::VideoCaptureDeviceInfo kCamera1{{kCameraName1, kCameraId1}};
const media::VideoCaptureDeviceInfo kCamera2{{kCameraName2, kCameraId2}};
const media::AudioDeviceDescription kMic1{kMicName1, kMicId1, kGroupId1};
const media::AudioDeviceDescription kMic2{kMicName2, kMicId2, kGroupId2};
// Preference ranking defaults to noop.
std::vector camera_infos{kCamera2, kCamera1};
std::vector mic_infos{kMic2, kMic1};
media_prefs::PreferenceRankVideoDeviceInfos(*profile()->GetPrefs(),
camera_infos);
EXPECT_THAT(camera_infos,
Pointwise(VideoCaptureDeviceInfoEq(), {kCamera2, kCamera1}));
media_prefs::PreferenceRankAudioDeviceInfos(*profile()->GetPrefs(),
mic_infos);
EXPECT_THAT(mic_infos, ElementsAre(kMic2, kMic1));
// Ranking is updated to make the currently selected device most preferred.
coordinator_->UpdateDevicePreferenceRanking();
media_prefs::PreferenceRankVideoDeviceInfos(*profile()->GetPrefs(),
camera_infos);
EXPECT_THAT(camera_infos,
Pointwise(VideoCaptureDeviceInfoEq(), {kCamera1, kCamera2}));
media_prefs::PreferenceRankAudioDeviceInfos(*profile()->GetPrefs(),
mic_infos);
EXPECT_THAT(mic_infos, ElementsAre(kMic1, kMic2));
}
|