OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include "base/test/scoped_feature_list.h" |
| 6 #include "chrome/browser/net/profile_network_context_service.h" |
| 7 #include "chrome/browser/net/profile_network_context_service_factory.h" |
5 #include "chrome/browser/net/system_network_context_manager.h" | 8 #include "chrome/browser/net/system_network_context_manager.h" |
6 | 9 #include "chrome/browser/profiles/profile.h" |
7 #include "base/test/scoped_feature_list.h" | 10 #include "chrome/browser/ui/browser.h" |
8 #include "chrome/test/base/in_process_browser_test.h" | 11 #include "chrome/test/base/in_process_browser_test.h" |
9 #include "content/public/common/content_features.h" | 12 #include "content/public/common/content_features.h" |
10 #include "content/public/common/content_switches.h" | 13 #include "content/public/common/content_switches.h" |
11 #include "content/public/common/network_service.mojom.h" | 14 #include "content/public/common/network_service.mojom.h" |
12 #include "content/public/common/resource_response.h" | 15 #include "content/public/common/resource_response.h" |
13 #include "content/public/common/resource_response_info.h" | 16 #include "content/public/common/resource_response_info.h" |
14 #include "content/public/common/url_loader.mojom.h" | 17 #include "content/public/common/url_loader.mojom.h" |
15 #include "content/public/common/url_loader_factory.mojom.h" | 18 #include "content/public/common/url_loader_factory.mojom.h" |
16 #include "content/public/test/test_url_loader_client.h" | 19 #include "content/public/test/test_url_loader_client.h" |
17 #include "mojo/common/data_pipe_utils.h" | 20 #include "mojo/common/data_pipe_utils.h" |
18 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
19 #include "net/http/http_response_headers.h" | 22 #include "net/http/http_response_headers.h" |
20 #include "net/test/embedded_test_server/embedded_test_server.h" | 23 #include "net/test/embedded_test_server/embedded_test_server.h" |
21 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" | 24 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
22 | 25 |
23 namespace { | 26 namespace { |
24 | 27 |
25 enum class NetworkServiceState { | 28 enum class NetworkServiceState { |
26 kDisabled, | 29 kDisabled, |
27 kEnabled, | 30 kEnabled, |
28 }; | 31 }; |
29 | 32 |
30 class SystemNetworkContextManagerTest | 33 enum class NetworkContextType { |
| 34 kSystem, |
| 35 kProfile, |
| 36 kIncognitoProfile, |
| 37 }; |
| 38 |
| 39 struct TestCase { |
| 40 NetworkServiceState network_service_state; |
| 41 NetworkContextType network_context_type; |
| 42 }; |
| 43 |
| 44 // Tests the system, profile, and incognito profile NetworkContexts. |
| 45 class NetworkContextConfigurationBrowserTest |
31 : public InProcessBrowserTest, | 46 : public InProcessBrowserTest, |
32 public testing::WithParamInterface<NetworkServiceState> { | 47 public testing::WithParamInterface<TestCase> { |
33 public: | 48 public: |
34 SystemNetworkContextManagerTest() { | 49 NetworkContextConfigurationBrowserTest() { |
35 EXPECT_TRUE(embedded_test_server()->Start()); | 50 EXPECT_TRUE(embedded_test_server()->Start()); |
36 } | 51 } |
37 | 52 |
38 ~SystemNetworkContextManagerTest() override {} | 53 ~NetworkContextConfigurationBrowserTest() override {} |
39 | 54 |
40 void SetUpInProcessBrowserTestFixture() override { | 55 void SetUpInProcessBrowserTestFixture() override { |
41 feature_list_.InitAndEnableFeature(features::kNetworkService); | 56 feature_list_.InitAndEnableFeature(features::kNetworkService); |
42 } | 57 } |
43 | 58 |
44 void SetUpOnMainThread() override { | 59 void SetUpOnMainThread() override { |
45 SystemNetworkContextManager::Context()->CreateURLLoaderFactory( | 60 switch (GetParam().network_context_type) { |
46 MakeRequest(&loader_factory_), 0); | 61 case NetworkContextType::kSystem: { |
| 62 network_context_ = SystemNetworkContextManager::Context(); |
| 63 break; |
| 64 } |
| 65 case NetworkContextType::kProfile: { |
| 66 network_context_ = ProfileNetworkContextServiceFactory::GetInstance() |
| 67 ->GetForContext(browser()->profile()) |
| 68 ->MainContext(); |
| 69 break; |
| 70 } |
| 71 case NetworkContextType::kIncognitoProfile: { |
| 72 Browser* incognito = CreateIncognitoBrowser(); |
| 73 network_context_ = ProfileNetworkContextServiceFactory::GetInstance() |
| 74 ->GetForContext(incognito->profile()) |
| 75 ->MainContext(); |
| 76 break; |
| 77 } |
| 78 } |
| 79 network_context_->CreateURLLoaderFactory(MakeRequest(&loader_factory_), 0); |
47 } | 80 } |
48 | 81 |
49 content::mojom::URLLoaderFactory* loader_factory() { | 82 content::mojom::URLLoaderFactory* loader_factory() { |
50 return loader_factory_.get(); | 83 return loader_factory_.get(); |
51 } | 84 } |
52 | 85 |
53 private: | 86 private: |
| 87 content::mojom::NetworkContext* network_context_ = nullptr; |
54 content::mojom::URLLoaderFactoryPtr loader_factory_; | 88 content::mojom::URLLoaderFactoryPtr loader_factory_; |
55 base::test::ScopedFeatureList feature_list_; | 89 base::test::ScopedFeatureList feature_list_; |
56 }; | 90 }; |
57 | 91 |
58 IN_PROC_BROWSER_TEST_P(SystemNetworkContextManagerTest, BasicRequest) { | 92 IN_PROC_BROWSER_TEST_P(NetworkContextConfigurationBrowserTest, BasicRequest) { |
59 content::mojom::URLLoaderAssociatedPtr loader; | 93 content::mojom::URLLoaderAssociatedPtr loader; |
60 content::ResourceRequest request; | 94 content::ResourceRequest request; |
61 content::TestURLLoaderClient client; | 95 content::TestURLLoaderClient client; |
62 request.url = embedded_test_server()->GetURL("/echo"); | 96 request.url = embedded_test_server()->GetURL("/echo"); |
63 request.method = "GET"; | 97 request.method = "GET"; |
64 request.request_initiator = url::Origin(); | 98 request.request_initiator = url::Origin(); |
65 loader_factory()->CreateLoaderAndStart( | 99 loader_factory()->CreateLoaderAndStart( |
66 mojo::MakeRequest(&loader), 2, 1, content::mojom::kURLLoadOptionNone, | 100 mojo::MakeRequest(&loader), 2, 1, content::mojom::kURLLoadOptionNone, |
67 request, client.CreateInterfacePtr(), | 101 request, client.CreateInterfacePtr(), |
68 net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS)); | 102 net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS)); |
69 client.RunUntilResponseReceived(); | 103 client.RunUntilResponseReceived(); |
70 ASSERT_TRUE(client.response_head().headers); | 104 ASSERT_TRUE(client.response_head().headers); |
71 EXPECT_EQ(200, client.response_head().headers->response_code()); | 105 EXPECT_EQ(200, client.response_head().headers->response_code()); |
72 | 106 |
73 client.RunUntilResponseBodyArrived(); | 107 client.RunUntilResponseBodyArrived(); |
74 // TODO(mmenke): Is blocking the UI Thread while reading the response really | 108 // TODO(mmenke): Is blocking the UI Thread while reading the response really |
75 // the best way to test requests in a browser test? | 109 // the best way to test requests in a browser test? |
76 std::string response_body; | 110 std::string response_body; |
77 EXPECT_TRUE(mojo::common::BlockingCopyToString(client.response_body_release(), | 111 EXPECT_TRUE(mojo::common::BlockingCopyToString(client.response_body_release(), |
78 &response_body)); | 112 &response_body)); |
79 EXPECT_EQ("Echo", response_body); | 113 EXPECT_EQ("Echo", response_body); |
80 | 114 |
81 client.RunUntilComplete(); | 115 client.RunUntilComplete(); |
82 EXPECT_EQ(net::OK, client.completion_status().error_code); | 116 EXPECT_EQ(net::OK, client.completion_status().error_code); |
83 } | 117 } |
84 | 118 |
85 INSTANTIATE_TEST_CASE_P( | 119 INSTANTIATE_TEST_CASE_P( |
86 /* no prefix */, | 120 SystemNetworkContext, |
87 SystemNetworkContextManagerTest, | 121 NetworkContextConfigurationBrowserTest, |
88 ::testing::Values(NetworkServiceState::kDisabled, | 122 ::testing::Values(TestCase({NetworkServiceState::kDisabled, |
89 NetworkServiceState::kEnabled)); | 123 NetworkContextType::kSystem}), |
| 124 TestCase({NetworkServiceState::kEnabled, |
| 125 NetworkContextType::kSystem}))); |
| 126 |
| 127 INSTANTIATE_TEST_CASE_P( |
| 128 ProfileMainNetworkContext, |
| 129 NetworkContextConfigurationBrowserTest, |
| 130 ::testing::Values(TestCase({NetworkServiceState::kDisabled, |
| 131 NetworkContextType::kProfile}), |
| 132 TestCase({NetworkServiceState::kEnabled, |
| 133 NetworkContextType::kProfile}))); |
| 134 |
| 135 INSTANTIATE_TEST_CASE_P( |
| 136 IncognitoProfileMainNetworkContext, |
| 137 NetworkContextConfigurationBrowserTest, |
| 138 ::testing::Values(TestCase({NetworkServiceState::kDisabled, |
| 139 NetworkContextType::kIncognitoProfile}), |
| 140 TestCase({NetworkServiceState::kEnabled, |
| 141 NetworkContextType::kIncognitoProfile}))); |
90 | 142 |
91 } // namespace | 143 } // namespace |
OLD | NEW |