1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
// Copyright 2018 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/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 /* requesting_origin */,
url /* embedding_origin */);
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 /* primary_url*/, url /* secondary_url*/, content_type, setting);
}
};
// Test that Background Fetch permission is "allow" by default, when queried
// from a top level frame.
TEST_F(BackgroundFetchPermissionContextTest, TestOutcomeAllowWithFrame) {
GURL url("https://example.com");
BackgroundFetchPermissionContext permission_context(profile());
EXPECT_EQ(GetPermissonStatus(url, &permission_context, /*with_frame =*/true),
CONTENT_SETTING_ALLOW);
}
// Test that Background Fetch permission is "prompt" when queried from a worker
// context, if the Automatic Downloads content setting is set to
// 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, /*with_frame =*/false),
CONTENT_SETTING_ASK);
}
// Test that Background Fetch permission is "deny" when queried from a worker
// context, if the Automatic Downloads content settings is set to
// CONTENT_SETTING_BLOCK.
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, /*with_frame =*/false),
CONTENT_SETTING_BLOCK);
}
// Test that Background Fetch permission is "prompt" when queried from a worker
// context, if the Automatic Downloads content setting is CONTENT_SETTING_ASK.
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, /*with_frame =*/false),
CONTENT_SETTING_ASK);
}
} // namespace
|