Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / policy / policy_network_browsertest.cc
blobec2d90c8d493ad33308427b02e8b29ed124852b6
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/bind.h"
6 #include "base/command_line.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/run_loop.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/common/chrome_switches.h"
11 #include "chrome/test/base/in_process_browser_test.h"
12 #include "components/policy/core/browser/browser_policy_connector.h"
13 #include "components/policy/core/common/mock_configuration_policy_provider.h"
14 #include "components/policy/core/common/policy_map.h"
15 #include "components/policy/core/common/policy_types.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/test/browser_test.h"
18 #include "net/http/http_transaction_factory.h"
19 #include "net/url_request/url_request_context.h"
20 #include "net/url_request/url_request_context_getter.h"
21 #include "policy/policy_constants.h"
23 namespace {
25 void VerifyQuicEnabledStatus(net::URLRequestContextGetter* getter,
26 bool quic_enabled_expected,
27 const base::Closure& done_callback) {
28 net::URLRequestContext* context = getter->GetURLRequestContext();
29 bool quic_enabled = context->http_transaction_factory()->GetSession()->
30 params().enable_quic;
31 EXPECT_EQ(quic_enabled_expected, quic_enabled);
33 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
34 done_callback);
37 void VerifyQuicEnabledStatusInIOThread(bool quic_enabled_expected) {
38 base::RunLoop run_loop;
39 content::BrowserThread::PostTask(
40 content::BrowserThread::IO,
41 FROM_HERE,
42 base::Bind(
43 VerifyQuicEnabledStatus,
44 make_scoped_refptr(g_browser_process->system_request_context()),
45 quic_enabled_expected,
46 run_loop.QuitClosure()));
47 run_loop.Run();
50 } // namespace
52 namespace policy {
54 // The tests are based on the assumption that command line flag kEnableQuic
55 // guarantees that QUIC protocol is enabled which is the case at the moment
56 // when these are being written.
57 class QuicAllowedPolicyTestBase: public InProcessBrowserTest {
58 public:
59 QuicAllowedPolicyTestBase() : InProcessBrowserTest(){}
61 protected:
62 void SetUpInProcessBrowserTestFixture() override {
63 base::CommandLine::ForCurrentProcess()->AppendSwitch(switches::kEnableQuic);
64 EXPECT_CALL(provider_, IsInitializationComplete(testing::_))
65 .WillRepeatedly(testing::Return(true));
67 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
68 PolicyMap values;
69 GetQuicAllowedPolicy(&values);
70 provider_.UpdateChromePolicy(values);
73 virtual void GetQuicAllowedPolicy(PolicyMap* values) = 0;
75 private:
76 MockConfigurationPolicyProvider provider_;
77 DISALLOW_COPY_AND_ASSIGN(QuicAllowedPolicyTestBase);
80 // Policy QuicAllowed set to false.
81 class QuicAllowedPolicyIsFalse: public QuicAllowedPolicyTestBase {
82 public:
83 QuicAllowedPolicyIsFalse() : QuicAllowedPolicyTestBase(){}
85 protected:
86 void GetQuicAllowedPolicy(PolicyMap* values) override {
87 values->Set(key::kQuicAllowed, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE,
88 POLICY_SOURCE_CLOUD, new base::FundamentalValue(false), nullptr);
91 private:
92 DISALLOW_COPY_AND_ASSIGN(QuicAllowedPolicyIsFalse);
95 IN_PROC_BROWSER_TEST_F(QuicAllowedPolicyIsFalse, QuicDisallowed) {
96 VerifyQuicEnabledStatusInIOThread(false);
99 // Policy QuicAllowed set to true.
100 class QuicAllowedPolicyIsTrue: public QuicAllowedPolicyTestBase {
101 public:
102 QuicAllowedPolicyIsTrue() : QuicAllowedPolicyTestBase(){}
104 protected:
105 void GetQuicAllowedPolicy(PolicyMap* values) override {
106 values->Set(key::kQuicAllowed, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE,
107 POLICY_SOURCE_CLOUD, new base::FundamentalValue(true), nullptr);
110 private:
111 DISALLOW_COPY_AND_ASSIGN(QuicAllowedPolicyIsTrue);
114 IN_PROC_BROWSER_TEST_F(QuicAllowedPolicyIsTrue, QuicAllowed) {
115 VerifyQuicEnabledStatusInIOThread(true);
118 // Policy QuicAllowed is not set.
119 class QuicAllowedPolicyIsNotSet: public QuicAllowedPolicyTestBase {
120 public:
121 QuicAllowedPolicyIsNotSet() : QuicAllowedPolicyTestBase(){}
123 protected:
124 void GetQuicAllowedPolicy(PolicyMap* values) override {
127 private:
128 DISALLOW_COPY_AND_ASSIGN(QuicAllowedPolicyIsNotSet);
131 IN_PROC_BROWSER_TEST_F(QuicAllowedPolicyIsNotSet, NoQuicRegulations) {
132 VerifyQuicEnabledStatusInIOThread(true);
135 } // namespace policy