chromium/chrome/browser/media/webrtc/capture_policy_utils_unittest.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 "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  // BUILDFLAG(IS_CHROMEOS_ASH)

namespace {
constexpr char kTestSite1[] =;
constexpr char kTestSite1Pattern[] =;
constexpr char kTestSite1NonMatchingPattern[] =;

#if BUILDFLAG(IS_CHROMEOS_ASH)
constexpr char kAccountId[] = "[email protected]";
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)
}  // namespace

class CapturePolicyUtilsTest : public testing::Test {};

// Test that the default policy allows all capture.
TEST_F(CapturePolicyUtilsTest, SimpleAllowTest) {}

// Test that setting |kScreenCaptureAllowed| to false, denies all capture.
TEST_F(CapturePolicyUtilsTest, SimpleDenyTest) {}

// Test that the FullCapture allowed list overrides |kScreenCaptureAllowed|.
TEST_F(CapturePolicyUtilsTest, SimpleOverrideUnrestricted) {}

// Test that the Window/Tab allowed list overrides |kScreenCaptureAllowed|.
TEST_F(CapturePolicyUtilsTest, SimpleOverrideWindowTabs) {}

// Test that the Tab allowed list overrides |kScreenCaptureAllowed|.
TEST_F(CapturePolicyUtilsTest, SimpleOverrideTabs) {}

// Test that the Same Origin Tab allowed list overrides |kScreenCaptureAllowed|.
TEST_F(CapturePolicyUtilsTest, SimpleOverrideSameOriginTabs) {}

// Test that an item that doesn't match any list still respects the default.
TEST_F(CapturePolicyUtilsTest, SimpleOverrideNoMatches) {}

// Ensure that a full wildcard policy is accepted.
TEST_F(CapturePolicyUtilsTest, TestWildcard) {}

// Ensure that if a URL appears in multiple lists that it returns the most
// restrictive list that it is included in.
TEST_F(CapturePolicyUtilsTest, TestOverrideMoreRestrictive) {}

// Ensure that if a URL appears in multiple lists that it returns the most
// restrictive list that it is included in.
TEST_F(CapturePolicyUtilsTest, TestSubdomainOverrides) {}

// Returns a std::vector<DesktopMediaList::Type> containing all values.
std::vector<DesktopMediaList::Type> GetFullMediaList() {}

// Test FilterMediaList with the different values of AllowedScreenCaptureLevel
// and ensure that values are filtered out and remain in sorted out.
TEST_F(CapturePolicyUtilsTest, FilterMediaListUnrestricted) {}

TEST_F(CapturePolicyUtilsTest, FilterMediaListRestrictedWindow) {}

TEST_F(CapturePolicyUtilsTest, FilterMediaListRestrictedTab) {}

// We don't do the SameOrigin filter at the MediaTypes level, so this should be
// the same.
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, /*is_affiliated=*/true, user_manager::UserType::kRegular,
        profile_);
    fake_user_manager_->LoginUser(account_id);

    // Settings required to create startup data.
    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;
    // ash::LoginState::Shutdown();
  }

  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  // BUILDFLAG(IS_CHROMEOS_LACROS)
        base::Contains(allowed_urls, CurrentOrigin());
  }

  bool ExpectedIsMultiCaptureAllowedForAnyUrl() {
    return
#if BUILDFLAG(IS_CHROMEOS_LACROS)
        IsMainProfile() &&
#endif  // BUILDFLAG(IS_CHROMEOS_LACROS)
        !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(
        // Is main profile?
        ::testing::Bool(),
        // Allowed origins
        ::testing::ValuesIn({std::vector<std::string>{},
                             std::vector<std::string>{
                                 "https://www.google.com"}}),
        // Origin to test
        ::testing::ValuesIn({std::string("https://www.google.com"),
                             std::string("https://www.notallowed.com")})));

#endif  // BUILDFLAG(IS_CHROMEOS_ASH)