#include "chrome/browser/ui/views/tab_sharing/tab_sharing_ui_views.h"
#include <map>
#include <string>
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/browser_features.h"
#include "chrome/browser/favicon/favicon_utils.h"
#include "chrome/browser/media/webrtc/media_capture_devices_dispatcher.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/tabs/tab_enums.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/tabs/tab_strip_user_gesture_details.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/infobars/content/content_infobar_manager.h"
#include "components/infobars/core/infobar.h"
#include "components/url_formatter/elide_url.h"
#include "components/vector_icons/vector_icons.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_process_host_observer.h"
#include "content/public/common/content_features.h"
#include "content/public/common/result_codes.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/captured_surface_test_utils.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/favicon_size.h"
#include "ui/gfx/image/image_unittest_util.h"
#include "ui/views/widget/widget.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/chromeos/policy/dlp/dlp_content_tab_helper.h"
#endif
namespace {
_;
Not;
TabSharingInfoBarButton;
content::WebContents* GetWebContents(Browser* browser, int tab) { … }
content::GlobalRenderFrameHostId GetGlobalId(Browser* browser, int tab) { … }
infobars::ContentInfoBarManager* GetInfoBarManager(Browser* browser, int tab) { … }
TabSharingInfoBarDelegate* GetDelegate(Browser* browser, int tab) { … }
std::u16string GetInfobarMessageText(Browser* browser, int tab) { … }
bool HasShareThisTabInsteadButton(Browser* browser, int tab) { … }
std::u16string GetShareThisTabInsteadButtonLabel(Browser* browser, int tab) { … }
ui::ImageModel GetShareThisTabInsteadButtonImage(Browser* browser, int tab) { … }
bool ShareThisTabInsteadButtonIsEnabled(Browser* browser, int tab) { … }
bool HasQuickNavButton(Browser* browser, int tab) { … }
std::u16string GetQuickNavButtonLabel(Browser* browser, int tab) { … }
ui::ImageModel GetQuickNavButtonImage(Browser* browser, int tab) { … }
bool HasCscIndicatorButton(Browser* browser, int tab) { … }
std::u16string GetCscIndicatorButtonLabel(Browser* browser, int tab) { … }
ui::ImageModel GetCscIndicatorButtonImage(Browser* browser, int tab) { … }
std::u16string GetExpectedSwitchToMessage(Browser* browser, int tab) { … }
content::DesktopMediaID GetDesktopMediaID(Browser* browser, int tab) { … }
views::Widget* GetContentsBorder(Browser* browser) { … }
scoped_refptr<MediaStreamCaptureIndicator> GetCaptureIndicator() { … }
void ActivateTab(Browser* browser, int tab) { … }
constexpr int kNullTabIndex = …;
const std::u16string kShareThisTabInsteadMessage = …;
const std::u16string kViewTabMessage = …;
#if BUILDFLAG(IS_CHROMEOS)
const policy::DlpContentRestrictionSet kEmptyRestrictionSet;
const policy::DlpContentRestrictionSet kScreenshareRestrictionSet(
policy::DlpContentRestriction::kScreenShare,
policy::DlpRulesManager::Level::kBlock);
#endif
}
class TabSharingUIViewsBrowserTest
: public InProcessBrowserTest,
public ::testing::WithParamInterface<bool> { … };
INSTANTIATE_TEST_SUITE_P(…);
IN_PROC_BROWSER_TEST_P(TabSharingUIViewsBrowserTest, StartSharing) { … }
IN_PROC_BROWSER_TEST_P(TabSharingUIViewsBrowserTest, SwitchSharedTab) { … }
IN_PROC_BROWSER_TEST_P(TabSharingUIViewsBrowserTest,
ChangeCapturingTabFavicon) { … }
IN_PROC_BROWSER_TEST_P(TabSharingUIViewsBrowserTest, ChangeCapturedTabFavicon) { … }
IN_PROC_BROWSER_TEST_P(TabSharingUIViewsBrowserTest, ChangeOtherTabFavicon) { … }
IN_PROC_BROWSER_TEST_P(TabSharingUIViewsBrowserTest, StopSharing) { … }
IN_PROC_BROWSER_TEST_P(TabSharingUIViewsBrowserTest, CloseTab) { … }
IN_PROC_BROWSER_TEST_P(TabSharingUIViewsBrowserTest,
BorderWidgetShouldCloseWhenBrowserCloses) { … }
IN_PROC_BROWSER_TEST_P(TabSharingUIViewsBrowserTest,
CloseTabInIncognitoBrowser) { … }
IN_PROC_BROWSER_TEST_P(TabSharingUIViewsBrowserTest, KillTab) { … }
IN_PROC_BROWSER_TEST_P(TabSharingUIViewsBrowserTest, KillSharedTab) { … }
IN_PROC_BROWSER_TEST_P(TabSharingUIViewsBrowserTest,
InfobarLabelUpdatedOnNavigation) { … }
IN_PROC_BROWSER_TEST_P(TabSharingUIViewsBrowserTest,
InfobarGainsCapturedSurfaceControlIndicator) { … }
IN_PROC_BROWSER_TEST_P(TabSharingUIViewsBrowserTest,
SourceChangesRemembersIfCapturedSurfaceControlInactive) { … }
IN_PROC_BROWSER_TEST_P(TabSharingUIViewsBrowserTest,
SourceChangesRemembersIfCapturedSurfaceControlActive) { … }
#if BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_P(TabSharingUIViewsBrowserTest,
SharingWithDlpAndNavigation) {
ApplyDlpForAllUsers();
policy::DlpContentTabHelper::ScopedIgnoreDlpRulesManager
ignore_dlp_rules_manager =
policy::DlpContentTabHelper::IgnoreDlpRulesManagerForTesting();
ASSERT_TRUE(embedded_test_server()->Start());
GURL kUrlRestricted =
embedded_test_server()->GetURL("restricted.com", "/title1.html");
GURL kUrlUnrestricted =
embedded_test_server()->GetURL("unrestricted.com", "/title1.html");
policy::DlpContentRestrictionSet::SetRestrictionsForURLForTesting(
kUrlRestricted, kScreenshareRestrictionSet);
policy::DlpContentRestrictionSet::SetRestrictionsForURLForTesting(
kUrlUnrestricted, kEmptyRestrictionSet);
AddTabs(browser(), 2);
ASSERT_EQ(browser()->tab_strip_model()->count(), 3);
CreateUiAndStartSharing(browser(), 0, 1);
VerifyUi(UiExpectations{.browser = browser(),
.capturing_tab = 0,
.captured_tab = 1,
.infobar_count = 1,
.has_border = true,
.tab_with_disabled_button = kNullTabIndex});
constexpr int kRestrictedTab = 2;
content::WebContents* web_contents =
browser()->tab_strip_model()->GetWebContentsAt(kRestrictedTab);
ASSERT_TRUE(content::NavigateToURL(web_contents, kUrlRestricted));
VerifyUi(UiExpectations{.browser = browser(),
.capturing_tab = 0,
.captured_tab = 1,
.infobar_count = 1,
.has_border = true,
.tab_with_disabled_button = kRestrictedTab});
ASSERT_TRUE(content::NavigateToURL(web_contents, kUrlUnrestricted));
VerifyUi(UiExpectations{.browser = browser(),
.capturing_tab = 0,
.captured_tab = 1,
.infobar_count = 1,
.has_border = true,
.tab_with_disabled_button = kNullTabIndex});
}
#endif
class MultipleTabSharingUIViewsBrowserTest : public InProcessBrowserTest { … };
IN_PROC_BROWSER_TEST_F(MultipleTabSharingUIViewsBrowserTest, VerifyUi) { … }
IN_PROC_BROWSER_TEST_F(MultipleTabSharingUIViewsBrowserTest, StopSharing) { … }
IN_PROC_BROWSER_TEST_F(MultipleTabSharingUIViewsBrowserTest, CloseTabs) { … }
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_CHROMEOS_ASH) && \
!BUILDFLAG(IS_CHROMEOS_LACROS)
IN_PROC_BROWSER_TEST_F(
MultipleTabSharingUIViewsBrowserTest,
NormalModeCapturerDoesNotProduceInfobarInGuestModeTabOpenedBeforeCapture) { … }
IN_PROC_BROWSER_TEST_F(
MultipleTabSharingUIViewsBrowserTest,
NormalModeCapturerDoesNotProduceInfobarInGuestModeTabOpenedAfterCapture) { … }
IN_PROC_BROWSER_TEST_F(
MultipleTabSharingUIViewsBrowserTest,
GuestModeCapturerDoesNotProduceInfobarInNormalModeTabOpenedBeforeCapture) { … }
IN_PROC_BROWSER_TEST_F(
MultipleTabSharingUIViewsBrowserTest,
GuestModeCapturerDoesNotProduceInfobarInNormalModeTabOpenedAfterCapture) { … }
IN_PROC_BROWSER_TEST_F(MultipleTabSharingUIViewsBrowserTest,
TabsAddedInGuestModeHaveInfobarIfGuestModeCapture) { … }
#endif
class TabSharingUIViewsPreferCurrentTabBrowserTest
: public InProcessBrowserTest { … };
IN_PROC_BROWSER_TEST_F(TabSharingUIViewsPreferCurrentTabBrowserTest,
VerifyUiWhenSelfCapturing) { … }
IN_PROC_BROWSER_TEST_F(TabSharingUIViewsPreferCurrentTabBrowserTest,
VerifyUiWhenCapturingAnotherTab) { … }