chromium/chrome/browser/media/webrtc/capture_handle_browsertest.cc

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>
#include <string>
#include <vector>

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/memory/raw_ptr.h"
#include "base/path_service.h"
#include "base/strings/strcat.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/media/webrtc/webrtc_browsertest_base.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/permissions/permission_request_manager.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_base.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/prerender_test_util.h"
#include "ui/gl/gl_switches.h"

WebContents;

namespace {

// The captured tab is identified by its title.
const char kCapturedTabTitle[] =;

// Capturing page (top-level document).
const char kCapturingPageMain[] =;
// Capturing page (embedded document).
const char kCapturingPageEmbedded[] =;
// Captured page.
const char kCapturedPageMain[] =;
// Similar contents to kCapturedPageMain, but on a different page, which can
// be served same-origin or cross-origin.
const char kCapturedPageOther[] =;

const char* kArbitraryOrigin =;
const char* kNoEmbeddedCaptureHandle =;

enum class BrowserType {};

std::string StringifyPermittedOrigins(
    const std::vector<std::string>& permitted_origins) {}

std::string StringifyCaptureHandle(WebContents* web_contents,
                                   bool expose_origin,
                                   const std::string& handle) {}

// Conveniently pack together a captured tab and the capture-handle that
// is expected to be observed by capturers from a permitted origin.
struct TabInfo {};

TabInfo MakeTabInfoFromActiveTab(Browser* browser) {}

}  // namespace

// Essentially depends on InProcessBrowserTest, but WebRtcTestBase provides
// detection of JS errors.
class CaptureHandleBrowserTest : public WebRtcTestBase {};

IN_PROC_BROWSER_TEST_F(CaptureHandleBrowserTest,
                       HandleAndOriginExposedIfAllPermitted) {}

IN_PROC_BROWSER_TEST_F(CaptureHandleBrowserTest,
                       HandleAndOriginExposedIfCapturerOriginPermitted) {}

IN_PROC_BROWSER_TEST_F(CaptureHandleBrowserTest,
                       HandleAndOriginNotExposedIfCapturerOriginNotPermitted) {}

// TODO(crbug.com/40185394): Test disabled on Mac due to multiple failing bots.
#if BUILDFLAG(IS_MAC)
#define MAYBE_HandleNotExposedIfTopLevelAllowlistedButCallingFrameNotAllowlisted
#else
#define MAYBE_HandleNotExposedIfTopLevelAllowlistedButCallingFrameNotAllowlisted
#endif

IN_PROC_BROWSER_TEST_F(
    CaptureHandleBrowserTest,
    MAYBE_HandleNotExposedIfTopLevelAllowlistedButCallingFrameNotAllowlisted) {}

// TODO(crbug.com/40185394): Test disabled on Mac due to multiple failing bots.
// TODO(crbug.com/1287616, crbug.com/1362946): Flaky on Chrome OS and Windows.
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_WIN)
#define MAYBE_HandleExposedIfCallingFrameAllowlistedEvenIfTopLevelNotAllowlisted
#else
#define MAYBE_HandleExposedIfCallingFrameAllowlistedEvenIfTopLevelNotAllowlisted
#endif

IN_PROC_BROWSER_TEST_F(
    CaptureHandleBrowserTest,
    MAYBE_HandleExposedIfCallingFrameAllowlistedEvenIfTopLevelNotAllowlisted) {}

IN_PROC_BROWSER_TEST_F(CaptureHandleBrowserTest, CanExposeOnlyHandle) {}

IN_PROC_BROWSER_TEST_F(CaptureHandleBrowserTest,
                       CanExposeEmptyHandleIfExposingOrigin) {}

IN_PROC_BROWSER_TEST_F(CaptureHandleBrowserTest,
                       EmptyCaptureHandleConfigMeansCaptureHandleNotExposed) {}

IN_PROC_BROWSER_TEST_F(
    CaptureHandleBrowserTest,
    CallingSetCaptureHandleConfigWithEmptyConfigFiresEventAndClearsValue) {}

IN_PROC_BROWSER_TEST_F(
    CaptureHandleBrowserTest,
    CallingSetCaptureHandleConfigWithNewHandleChangesConfigAndFiresEvent) {}

IN_PROC_BROWSER_TEST_F(
    CaptureHandleBrowserTest,
    CallingSetCaptureHandleConfigWithNewOriginValueChangesConfigAndFiresEvent) {}

IN_PROC_BROWSER_TEST_F(
    CaptureHandleBrowserTest,
    PermittedOriginsChangeThatRemovesCapturerCausesEventAndEmptyConfig) {}

IN_PROC_BROWSER_TEST_F(
    CaptureHandleBrowserTest,
    PermittedOriginsChangeThatAddsCapturerCausesEventAndConfigExposure) {}

IN_PROC_BROWSER_TEST_F(
    CaptureHandleBrowserTest,
    PermittedOriginsChangeThatDoesNotAffectCapturerDoesNotCauseEventOrChange) {}

IN_PROC_BROWSER_TEST_F(CaptureHandleBrowserTest,
                       SameDocumentNavigationDoesNotClearTheCaptureHandle) {}

IN_PROC_BROWSER_TEST_F(CaptureHandleBrowserTest,
                       CrossDocumentNavigationClearsTheCaptureHandle) {}

IN_PROC_BROWSER_TEST_F(CaptureHandleBrowserTest,
                       CrossOriginNavigationClearsTheCaptureHandle) {}

IN_PROC_BROWSER_TEST_F(CaptureHandleBrowserTest,
                       SelfCaptureSanityWhenPermitted) {}

IN_PROC_BROWSER_TEST_F(CaptureHandleBrowserTest,
                       SelfCaptureSanityWhenNotPermitted) {}

// TODO(crbug.com/40772597): Disabled because of flakiness.
#if BUILDFLAG(IS_WIN)
#define MAYBE_RegularTabCannotReadIncognitoTabCaptureHandle
#else
#define MAYBE_RegularTabCannotReadIncognitoTabCaptureHandle
#endif
IN_PROC_BROWSER_TEST_F(CaptureHandleBrowserTest,
                       MAYBE_RegularTabCannotReadIncognitoTabCaptureHandle) {}

// TODO(crbug.com/40790671): Disabled because of flakiness.
IN_PROC_BROWSER_TEST_F(CaptureHandleBrowserTest,
                       DISABLED_IncognitoTabCannotReadRegularTabCaptureHandle) {}

IN_PROC_BROWSER_TEST_F(CaptureHandleBrowserTest,
                       IncognitoTabCannotReadIncognitoTabCaptureHandle) {}

// TODO(crbug.com/40772597): Disabled because of flakiness.
#if BUILDFLAG(IS_WIN)
#define MAYBE_IncognitoTabCanReadIncognitoTabCaptureHandleIfSelfCapture
#else
#define MAYBE_IncognitoTabCanReadIncognitoTabCaptureHandleIfSelfCapture
#endif
IN_PROC_BROWSER_TEST_F(
    CaptureHandleBrowserTest,
    MAYBE_IncognitoTabCanReadIncognitoTabCaptureHandleIfSelfCapture) {}

class CaptureHandleBrowserTestPrerender : public CaptureHandleBrowserTest {};

// Verifies that pre-rendered pages don't change the capture handle config.
IN_PROC_BROWSER_TEST_F(CaptureHandleBrowserTestPrerender,
                       EmptyConfigForCrossDocumentNavigations) {}