#include "chrome/browser/media/webrtc/capture_policy_utils.h"
#include "base/containers/contains.h"
#include "base/test/test_future.h"
#include "base/values.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/testing_pref_service.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "chrome/browser/ash/crosapi/crosapi_manager.h"
#include "chrome/browser/ash/crosapi/idle_service_ash.h"
#include "chrome/browser/ash/crosapi/test_crosapi_dependency_registry.h"
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "chromeos/ash/components/login/login_state/login_state.h"
#include "components/account_id/account_id.h"
#include "components/user_manager/scoped_user_manager.h"
#include "components/user_manager/user_type.h"
#endif
namespace {
constexpr char kTestSite1[] = …;
constexpr char kTestSite1Pattern[] = …;
constexpr char kTestSite1NonMatchingPattern[] = …;
#if BUILDFLAG(IS_CHROMEOS_ASH)
constexpr char kAccountId[] = "[email protected]";
#endif
}
class CapturePolicyUtilsTest : public testing::Test { … };
TEST_F(CapturePolicyUtilsTest, SimpleAllowTest) { … }
TEST_F(CapturePolicyUtilsTest, SimpleDenyTest) { … }
TEST_F(CapturePolicyUtilsTest, SimpleOverrideUnrestricted) { … }
TEST_F(CapturePolicyUtilsTest, SimpleOverrideWindowTabs) { … }
TEST_F(CapturePolicyUtilsTest, SimpleOverrideTabs) { … }
TEST_F(CapturePolicyUtilsTest, SimpleOverrideSameOriginTabs) { … }
TEST_F(CapturePolicyUtilsTest, SimpleOverrideNoMatches) { … }
TEST_F(CapturePolicyUtilsTest, TestWildcard) { … }
TEST_F(CapturePolicyUtilsTest, TestOverrideMoreRestrictive) { … }
TEST_F(CapturePolicyUtilsTest, TestSubdomainOverrides) { … }
std::vector<DesktopMediaList::Type> GetFullMediaList() { … }
TEST_F(CapturePolicyUtilsTest, FilterMediaListUnrestricted) { … }
TEST_F(CapturePolicyUtilsTest, FilterMediaListRestrictedWindow) { … }
TEST_F(CapturePolicyUtilsTest, FilterMediaListRestrictedTab) { … }
TEST_F(CapturePolicyUtilsTest, FilterMediaListRestrictedSameOrigin) { … }
#if BUILDFLAG(IS_CHROMEOS_ASH)
class MultiCaptureTest
: public testing::Test,
public ::testing::WithParamInterface<
std::tuple<bool, std::vector<std::string>, std::string>> {
public:
void SetUp() override {
testing::Test::SetUp();
fake_user_manager_.Reset(std::make_unique<ash::FakeChromeUserManager>());
CHECK(profile_manager_.SetUp());
profile_ = profile_manager_.CreateTestingProfile(kAccountId);
AccountId account_id = AccountId::FromUserEmail(kAccountId);
fake_user_manager_->AddUserWithAffiliationAndTypeAndProfile(
account_id, true, user_manager::UserType::kRegular,
profile_);
fake_user_manager_->LoginUser(account_id);
crosapi::IdleServiceAsh::DisableForTesting();
if (!ash::LoginState::IsInitialized()) {
ash::LoginState::Initialize();
}
cros_api_manager_ = crosapi::CreateCrosapiManagerWithTestRegistry();
HostContentSettingsMap* content_settings =
HostContentSettingsMapFactory::GetForProfile(profile());
for (const std::string& url : AllowedOrigins()) {
content_settings->SetContentSettingDefaultScope(
GURL(url), GURL(url), ContentSettingsType::ALL_SCREEN_CAPTURE,
ContentSetting::CONTENT_SETTING_ALLOW);
}
}
void TearDown() override {
profile_ = nullptr;
}
Profile* profile() { return profile_; }
bool IsMainProfile() const { return std::get<0>(GetParam()); }
std::vector<std::string> AllowedOrigins() const {
return std::get<1>(GetParam());
}
std::string CurrentOrigin() const { return std::get<2>(GetParam()); }
protected:
bool ExpectedIsMultiCaptureAllowed() {
std::vector<std::string> allowed_urls = AllowedOrigins();
return
#if BUILDFLAG(IS_CHROMEOS_LACROS)
IsMainProfile() &&
#endif
base::Contains(allowed_urls, CurrentOrigin());
}
bool ExpectedIsMultiCaptureAllowedForAnyUrl() {
return
#if BUILDFLAG(IS_CHROMEOS_LACROS)
IsMainProfile() &&
#endif
!AllowedOrigins().empty();
}
private:
raw_ptr<TestingProfile> profile_;
content::BrowserTaskEnvironment task_environment_;
std::unique_ptr<crosapi::CrosapiManager> cros_api_manager_;
user_manager::TypedScopedUserManager<ash::FakeChromeUserManager>
fake_user_manager_;
TestingProfileManager profile_manager_{TestingBrowserProcess::GetGlobal()};
};
TEST_P(MultiCaptureTest, IsMultiCaptureAllowedBasedOnPolicy) {
base::test::TestFuture<bool> future;
capture_policy::CheckGetAllScreensMediaAllowed(
profile(), GURL(CurrentOrigin()), future.GetCallback());
ASSERT_TRUE(future.Wait());
EXPECT_EQ(ExpectedIsMultiCaptureAllowed(), future.Get<bool>());
}
TEST_P(MultiCaptureTest, IsMultiCaptureAllowedForAnyUrl) {
base::test::TestFuture<bool> future;
capture_policy::CheckGetAllScreensMediaAllowedForAnyOrigin(
profile(), future.GetCallback());
ASSERT_TRUE(future.Wait());
EXPECT_EQ(ExpectedIsMultiCaptureAllowedForAnyUrl(), future.Get<bool>());
}
INSTANTIATE_TEST_SUITE_P(
MultiCaptureTestCases,
MultiCaptureTest,
::testing::Combine(
::testing::Bool(),
::testing::ValuesIn({std::vector<std::string>{},
std::vector<std::string>{
"https://www.google.com"}}),
::testing::ValuesIn({std::string("https://www.google.com"),
std::string("https://www.notallowed.com")})));
#endif