#include "chrome/browser/ui/views/desktop_capture/desktop_media_picker_views.h"
#include <map>
#include <string>
#include <utility>
#include <vector>
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "build/build_config.h"
#include "chrome/browser/media/webrtc/desktop_media_picker_controller.h"
#include "chrome/browser/media/webrtc/desktop_media_picker_manager.h"
#include "chrome/browser/media/webrtc/fake_desktop_media_list.h"
#include "chrome/browser/ui/views/desktop_capture/desktop_media_list_controller.h"
#include "chrome/browser/ui/views/desktop_capture/desktop_media_list_view.h"
#include "chrome/browser/ui/views/desktop_capture/desktop_media_picker_views_test_api.h"
#include "chrome/browser/ui/views/desktop_capture/desktop_media_source_view.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/views/chrome_test_views_delegate.h"
#include "components/web_modal/test_web_contents_modal_dialog_host.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/base/ui_base_switches.h"
#include "ui/events/event_utils.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/button/checkbox.h"
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/controls/tabbed_pane/tabbed_pane.h"
#include "ui/views/test/button_test_api.h"
#include "ui/views/test/scoped_views_test_helper.h"
#include "ui/views/test/test_views_delegate.h"
#include "ui/views/test/widget_test.h"
#include "ui/views/widget/any_widget_observer.h"
#include "ui/views/widget/widget.h"
#include "ui/views/window/dialog_delegate.h"
#if BUILDFLAG(IS_MAC)
#include "base/mac/mac_util.h"
#endif
DesktopMediaID;
namespace views {
class TestDialogObserver : public DesktopMediaPickerManager::DialogObserver { … };
std::vector<DesktopMediaList::Type> GetSourceTypes(bool prefer_current_tab,
bool new_order) { … }
DesktopMediaID::Type GetSourceIdType(DesktopMediaList::Type type) { … }
std::string GetTypeAsTestNameString(const DesktopMediaList::Type& type) { … }
struct PickerConfiguration { … };
namespace {
constexpr std::array<PickerConfiguration, 3> kDefaultProductSourceConfigs = …;
}
class DesktopMediaPickerViewsTestBase : public testing::Test { … };
class DesktopMediaPickerViewsTest
: public DesktopMediaPickerViewsTestBase,
public testing::WithParamInterface<PickerConfiguration> { … };
INSTANTIATE_TEST_SUITE_P(…);
TEST_P(DesktopMediaPickerViewsTest, DoneCallbackCalledWhenWindowClosed) { … }
TEST_P(DesktopMediaPickerViewsTest, DoneCallbackCalledOnOkButtonPressed) { … }
TEST_P(DesktopMediaPickerViewsTest, DoneCallbackNotCalledOnDoubleTap) { … }
TEST_P(DesktopMediaPickerViewsTest, CancelButtonAlwaysEnabled) { … }
TEST_P(DesktopMediaPickerViewsTest, AudioCheckboxDefaultStates) { … }
TEST_P(DesktopMediaPickerViewsTest, DistinctAudioCheckboxesHaveDistinctState) { … }
TEST_P(DesktopMediaPickerViewsTest, CurrentTabAndAnyTabShareAudioState) { … }
TEST_P(DesktopMediaPickerViewsTest, AudioCheckboxVisibility) { … }
TEST_P(DesktopMediaPickerViewsTest, DoneWithAudioShare) { … }
TEST_P(DesktopMediaPickerViewsTest, OkButtonEnabledDuringAcceptSpecific) { … }
#if BUILDFLAG(IS_MAC)
TEST_P(DesktopMediaPickerViewsTest, OnPermissionUpdateWithPermissions) {
if (base::mac::MacOSMajorVersion() < 13) {
GTEST_SKIP()
<< "ScreenCapturePermissionChecker only created for MacOS 13 and later";
}
test_api_.OnPermissionUpdate(true);
test_api_.SelectTabForSourceType(DesktopMediaList::Type::kScreen);
EXPECT_TRUE(test_api_.GetActivePane()->IsContentPaneVisible());
EXPECT_FALSE(test_api_.GetActivePane()->IsPermissionPaneVisible());
test_api_.SelectTabForSourceType(DesktopMediaList::Type::kWindow);
EXPECT_TRUE(test_api_.GetActivePane()->IsContentPaneVisible());
EXPECT_FALSE(test_api_.GetActivePane()->IsPermissionPaneVisible());
test_api_.SelectTabForSourceType(DesktopMediaList::Type::kWebContents);
EXPECT_TRUE(test_api_.GetActivePane()->IsContentPaneVisible());
EXPECT_FALSE(test_api_.GetActivePane()->IsPermissionPaneVisible());
}
TEST_P(DesktopMediaPickerViewsTest, OnPermissionUpdateWithoutPermissions) {
if (base::mac::MacOSMajorVersion() < 13) {
GTEST_SKIP()
<< "ScreenCapturePermissionChecker only created for MacOS 13 and later";
}
test_api_.OnPermissionUpdate(false);
test_api_.SelectTabForSourceType(DesktopMediaList::Type::kScreen);
EXPECT_FALSE(test_api_.GetActivePane()->IsContentPaneVisible());
EXPECT_TRUE(test_api_.GetActivePane()->IsPermissionPaneVisible());
test_api_.SelectTabForSourceType(DesktopMediaList::Type::kWindow);
EXPECT_FALSE(test_api_.GetActivePane()->IsContentPaneVisible());
EXPECT_TRUE(test_api_.GetActivePane()->IsPermissionPaneVisible());
test_api_.SelectTabForSourceType(DesktopMediaList::Type::kWebContents);
EXPECT_TRUE(test_api_.GetActivePane()->IsContentPaneVisible());
EXPECT_FALSE(test_api_.GetActivePane()->IsPermissionPaneVisible());
}
#endif
class DesktopMediaPickerViewsPerTypeTest
: public DesktopMediaPickerViewsTestBase,
public testing::WithParamInterface<
std::tuple<PickerConfiguration, DesktopMediaList::Type>> { … };
INSTANTIATE_TEST_SUITE_P(…);
TEST_P(DesktopMediaPickerViewsPerTypeTest, SelectMediaSourceViewOnSingleClick) { … }
TEST_P(DesktopMediaPickerViewsPerTypeTest, AddAndRemoveMediaSource) { … }
TEST_P(DesktopMediaPickerViewsPerTypeTest, FocusMediaSourceViewToSelect) { … }
TEST_P(DesktopMediaPickerViewsPerTypeTest, OkButtonDisabledWhenNoSelection) { … }
TEST_P(DesktopMediaPickerViewsPerTypeTest, ClearSelection) { … }
class DesktopMediaPickerViewsSystemAudioTest
: public DesktopMediaPickerViewsTestBase { … };
TEST_F(DesktopMediaPickerViewsSystemAudioTest,
SystemAudioCheckboxVisibleIfExcludeSystemAudioNotSpecified) { … }
TEST_F(DesktopMediaPickerViewsSystemAudioTest,
SystemAudioCheckboxInvisibleIfExcludeSystemAudioSpecified) { … }
TEST_F(DesktopMediaPickerViewsSystemAudioTest,
IfAudioNotRequestedThenExcludeSystemAudioHasNoEffect) { … }
class DesktopMediaPickerViewsSingleTabPaneTest
: public DesktopMediaPickerViewsTestBase { … };
TEST_F(DesktopMediaPickerViewsSingleTabPaneTest, TabListPreferredSizeNotZero) { … }
TEST_F(DesktopMediaPickerViewsSingleTabPaneTest, TabListHasFixedHeight) { … }
TEST_F(DesktopMediaPickerViewsSingleTabPaneTest,
CannotAcceptTabWithoutSelection) { … }
TEST_F(DesktopMediaPickerViewsSingleTabPaneTest, AccessibleProperties) { … }
class DesktopMediaPickerPreferredDisplaySurfaceTest
: public DesktopMediaPickerViewsTestBase,
public testing::WithParamInterface<
std::tuple<std::tuple<bool, bool>,
blink::mojom::PreferredDisplaySurface>> { … };
INSTANTIATE_TEST_SUITE_P(…);
TEST_P(DesktopMediaPickerPreferredDisplaySurfaceTest,
SelectedTabMatchesPreferredDisplaySurface) { … }
class DesktopMediaPickerDoubleClickTest
: public DesktopMediaPickerViewsTestBase,
public testing::WithParamInterface<
std::pair<DesktopMediaList::Type, DesktopMediaID::Type>> { … };
INSTANTIATE_TEST_SUITE_P(…);
TEST_P(DesktopMediaPickerDoubleClickTest, DoneCallbackNotCalledOnDoubleClick) { … }
class DelegatedSourceListTest : public DesktopMediaPickerViewsTestBase { … };
TEST_F(DelegatedSourceListTest, EnsureFocus) { … }
#if BUILDFLAG(IS_MAC)
TEST_F(DelegatedSourceListTest, TestSelection) {
SetSourceTypes(
{DesktopMediaList::Type::kWebContents},
{DesktopMediaList::Type::kScreen, DesktopMediaList::Type::kWindow});
CreatePickerViews(false, true);
test_api_.SelectTabForSourceType(DesktopMediaList::Type::kScreen);
media_lists_[DesktopMediaList::Type::kScreen]->AddSourceByFullMediaID(
DesktopMediaID(GetSourceIdType(DesktopMediaList::Type::kScreen), 10));
ASSERT_TRUE(test_api_.GetSelectedSourceId().has_value());
EXPECT_EQ(10, test_api_.GetSelectedSourceId().value());
}
#else
TEST_F(DelegatedSourceListTest, TestSelection) { … }
#endif
TEST_F(DelegatedSourceListTest, SinglePaneReject) { … }
TEST_F(DelegatedSourceListTest, NoFallbackPaneReject) { … }
TEST_F(DelegatedSourceListTest, SwitchToWebContents) { … }
TEST_F(DelegatedSourceListTest, EnsureNoWebContentsSelected) { … }
#if BUILDFLAG(IS_MAC)
TEST_F(DelegatedSourceListTest, ReselectButtonPresence) {
SetSourceTypes(
{DesktopMediaList::Type::kWebContents},
{DesktopMediaList::Type::kScreen, DesktopMediaList::Type::kWindow});
CreatePickerViews(false, true);
test_api_.SelectTabForSourceType(DesktopMediaList::Type::kWebContents);
EXPECT_EQ(nullptr, test_api_.GetReselectButton());
test_api_.SelectTabForSourceType(DesktopMediaList::Type::kScreen);
ASSERT_EQ(nullptr, test_api_.GetReselectButton());
test_api_.SelectTabForSourceType(DesktopMediaList::Type::kWindow);
ASSERT_EQ(nullptr, test_api_.GetReselectButton());
}
#else
TEST_F(DelegatedSourceListTest, ReselectButtonPresence) { … }
TEST_F(DelegatedSourceListTest, ReselectButtonEnabledState) { … }
TEST_F(DelegatedSourceListTest, ReselectTriggersShowDelegatedSourceList) { … }
#endif
}