#include "chrome/browser/background_fetch/background_fetch_permission_context.h"
#include <string>
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_profile.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/permissions/permission_util.h"
#include "content/public/browser/permission_descriptor_util.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class BackgroundFetchPermissionContextTest
: public ChromeRenderViewHostTestHarness {
protected:
BackgroundFetchPermissionContextTest() = default;
BackgroundFetchPermissionContextTest(
const BackgroundFetchPermissionContextTest&) = delete;
BackgroundFetchPermissionContextTest& operator=(
const BackgroundFetchPermissionContextTest&) = delete;
~BackgroundFetchPermissionContextTest() override = default;
ContentSetting GetPermissonStatus(
const GURL& url,
BackgroundFetchPermissionContext* permission_context,
bool with_frame) {
content::RenderFrameHost* render_frame_host = nullptr;
if (with_frame) {
content::WebContentsTester::For(web_contents())->NavigateAndCommit(url);
render_frame_host = web_contents()->GetPrimaryMainFrame();
}
auto permission_result = permission_context->GetPermissionStatus(
content::PermissionDescriptorUtil::
CreatePermissionDescriptorForPermissionType(
blink::PermissionType::BACKGROUND_FETCH),
render_frame_host, url ,
url );
return permissions::PermissionUtil::PermissionStatusToContentSetting(
permission_result.status);
}
void SetContentSetting(const GURL& url,
ContentSettingsType content_type,
ContentSetting setting) {
auto* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(profile());
ASSERT_TRUE(host_content_settings_map);
host_content_settings_map->SetContentSettingDefaultScope(
url , url , content_type, setting);
}
};
TEST_F(BackgroundFetchPermissionContextTest, TestOutcomeAllowWithFrame) {
GURL url("https://example.com");
BackgroundFetchPermissionContext permission_context(profile());
EXPECT_EQ(GetPermissonStatus(url, &permission_context, true),
CONTENT_SETTING_ALLOW);
}
TEST_F(BackgroundFetchPermissionContextTest, TestOutcomeAllowWithoutFrame) {
GURL url("https://example.com");
SetContentSetting(url, ContentSettingsType::AUTOMATIC_DOWNLOADS,
CONTENT_SETTING_ALLOW);
BackgroundFetchPermissionContext permission_context(profile());
EXPECT_EQ(GetPermissonStatus(url, &permission_context, false),
CONTENT_SETTING_ASK);
}
TEST_F(BackgroundFetchPermissionContextTest, TestOutcomeDenyWithoutFrame) {
GURL url("https://example.com");
SetContentSetting(url, ContentSettingsType::AUTOMATIC_DOWNLOADS,
CONTENT_SETTING_BLOCK);
BackgroundFetchPermissionContext permission_context(profile());
EXPECT_EQ(GetPermissonStatus(url, &permission_context, false),
CONTENT_SETTING_BLOCK);
}
TEST_F(BackgroundFetchPermissionContextTest, TestOutcomePromptWithoutFrame) {
auto* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(profile());
ASSERT_TRUE(host_content_settings_map);
GURL url("https://example.com");
SetContentSetting(url, ContentSettingsType::AUTOMATIC_DOWNLOADS,
CONTENT_SETTING_ASK);
BackgroundFetchPermissionContext permission_context(profile());
EXPECT_EQ(GetPermissonStatus(url, &permission_context, false),
CONTENT_SETTING_ASK);
}
}