chromium/chrome/browser/policy/test/content_settings_policy_browsertest.cc

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

#include <string>

#include "base/command_line.h"
#include "base/strings/strcat.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/policy/policy_test_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/usb/usb_chooser_context.h"
#include "chrome/browser/usb/usb_chooser_context_factory.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/browser/private_network_settings.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/policy_constants.h"
#include "content/public/browser/permission_controller.h"
#include "content/public/browser/permission_result.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/test/mock_bluetooth_adapter.h"
#include "services/device/public/cpp/test/fake_usb_device_info.h"
#include "services/network/public/cpp/features.h"
#include "third_party/blink/public/common/features_generated.h"
#include "third_party/blink/public/common/permissions/permission_utils.h"
#include "url/gurl.h"
#include "url/origin.h"

namespace policy {

namespace {
const char kURL[] =;
const char kCookieValue[] =;
// Assigned to Philip J. Fry to fix eventually.
// TODO(maksims): use year 3000 when we get rid off the 32-bit
// versions. https://crbug.com/619828
const char kCookieOptions[] =;
constexpr int kBlockAll =;

bool IsJavascriptEnabled(content::WebContents* contents) {}

}  // namespace

IN_PROC_BROWSER_TEST_F(PolicyTest, PRE_PRE_DefaultCookiesSetting) {}

IN_PROC_BROWSER_TEST_F(PolicyTest, PRE_DefaultCookiesSetting) {}

IN_PROC_BROWSER_TEST_F(PolicyTest, DefaultCookiesSetting) {}

IN_PROC_BROWSER_TEST_F(PolicyTest, PRE_PRE_WebsiteCookiesSetting) {}

IN_PROC_BROWSER_TEST_F(PolicyTest, PRE_WebsiteCookiesSetting) {}

IN_PROC_BROWSER_TEST_F(PolicyTest, WebsiteCookiesSetting) {}

IN_PROC_BROWSER_TEST_F(PolicyTest, Javascript) {}

class WebBluetoothPolicyTest : public PolicyTest {};

// crbug.com/1061063
#if BUILDFLAG(IS_MAC) && defined(ARCH_CPU_ARM64)
#define MAYBE_Block
#else
#define MAYBE_Block
#endif  // BUILDFLAG(IS_MAC) && defined(ARCH_CPU_ARM64)
IN_PROC_BROWSER_TEST_F(WebBluetoothPolicyTest, MAYBE_Block) {}

IN_PROC_BROWSER_TEST_F(PolicyTest, WebUsbDefault) {}

IN_PROC_BROWSER_TEST_F(PolicyTest, WebUsbAllowDevicesForUrls) {}

IN_PROC_BROWSER_TEST_F(PolicyTest, ShouldAllowInsecurePrivateNetworkRequests) {}

class ScrollToTextFragmentPolicyTest
    : public PolicyTest,
      public ::testing::WithParamInterface<bool> {};

IN_PROC_BROWSER_TEST_P(ScrollToTextFragmentPolicyTest, RunPolicyTest) {}

INSTANTIATE_TEST_SUITE_P();

class SensorsPolicyTest : public PolicyTest {};

IN_PROC_BROWSER_TEST_F(SensorsPolicyTest, BlockSensorApi) {}

IN_PROC_BROWSER_TEST_F(SensorsPolicyTest, DynamicRefresh) {}

#if BUILDFLAG(IS_CHROMEOS)
class WebPrintingPolicyTest : public PolicyTest {
 public:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    feature_list_.InitAndEnableFeature(blink::features::kWebPrinting);
    PolicyTest::SetUpCommandLine(command_line);
  }

  void SetUpOnMainThread() override {
    PolicyTest::SetUpOnMainThread();
    ASSERT_TRUE(embedded_test_server()->Start());
    ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestingUrl()));
  }

 protected:
  static constexpr int32_t kBlockSetting = 2;
  static constexpr int32_t kAskSetting = 3;

  GURL GetTestingUrl() const {
    return embedded_test_server()->GetURL("/empty.html");
  }

  ContentSetting GetWebPrintingDefaultContentSetting() {
    return HostContentSettingsMapFactory::GetForProfile(browser()->profile())
        ->GetDefaultContentSetting(ContentSettingsType::WEB_PRINTING,
                                   /*provider_id=*/nullptr);
  }

  ContentSetting GetWebPrintingContentSetting(const GURL& url) {
    return HostContentSettingsMapFactory::GetForProfile(browser()->profile())
        ->GetContentSetting(/*primary_url=*/url, /*secondary_url=*/url,
                            ContentSettingsType::WEB_PRINTING);
  }

  void SetDefaultWebPrintingSetting(int32_t setting) {
    PolicyMap policies;
    SetPolicy(&policies, key::kDefaultWebPrintingSetting, base::Value(setting));
    UpdateProviderPolicy(policies);
  }

  void SetWebPrintingAllowedFor(const GURL& url) {
    PolicyMap policies;
    SetPolicy(&policies, key::kWebPrintingAllowedForUrls,
              base::Value(base::Value::List().Append(url.spec())));
    UpdateProviderPolicy(policies);
  }

  void SetWebPrintingBlockedFor(const GURL& url) {
    PolicyMap policies;
    SetPolicy(&policies, key::kWebPrintingBlockedForUrls,
              base::Value(base::Value::List().Append(url.spec())));
    UpdateProviderPolicy(policies);
  }

 private:
  base::test::ScopedFeatureList feature_list_;
};

IN_PROC_BROWSER_TEST_F(WebPrintingPolicyTest, DefaultWebPrintingSetting) {
  EXPECT_EQ(CONTENT_SETTING_ASK, GetWebPrintingDefaultContentSetting());
  EXPECT_EQ(CONTENT_SETTING_ASK, GetWebPrintingContentSetting(GetTestingUrl()));

  SetDefaultWebPrintingSetting(kBlockSetting);

  EXPECT_EQ(CONTENT_SETTING_BLOCK, GetWebPrintingDefaultContentSetting());
  EXPECT_EQ(CONTENT_SETTING_BLOCK,
            GetWebPrintingContentSetting(GetTestingUrl()));

  SetDefaultWebPrintingSetting(kAskSetting);

  EXPECT_EQ(CONTENT_SETTING_ASK, GetWebPrintingDefaultContentSetting());
  EXPECT_EQ(CONTENT_SETTING_ASK, GetWebPrintingContentSetting(GetTestingUrl()));
}

IN_PROC_BROWSER_TEST_F(WebPrintingPolicyTest, WebPrintingAllowedForUrls) {
  SetWebPrintingAllowedFor(GetTestingUrl());

  EXPECT_EQ(CONTENT_SETTING_ASK, GetWebPrintingDefaultContentSetting());
  EXPECT_EQ(CONTENT_SETTING_ALLOW,
            GetWebPrintingContentSetting(GetTestingUrl()));
}

IN_PROC_BROWSER_TEST_F(WebPrintingPolicyTest, WebPrintingBlockedForUrls) {
  SetWebPrintingBlockedFor(GetTestingUrl());

  EXPECT_EQ(CONTENT_SETTING_ASK, GetWebPrintingDefaultContentSetting());
  EXPECT_EQ(CONTENT_SETTING_BLOCK,
            GetWebPrintingContentSetting(GetTestingUrl()));
}
#endif

#if !BUILDFLAG(IS_ANDROID)
class DirectSocketsPolicyTest : public PolicyTest {};

IN_PROC_BROWSER_TEST_F(DirectSocketsPolicyTest, DefaultDirectSocketsSetting) {}

IN_PROC_BROWSER_TEST_F(DirectSocketsPolicyTest, DirectSocketsAllowedForUrls) {}

IN_PROC_BROWSER_TEST_F(DirectSocketsPolicyTest, DirectSocketsBlockedForUrls) {}
#endif

}  // namespace policy