Adds method to provide custom AudioManager at runtime.
[chromium-blink-merge.git] / chrome / browser / io_thread_unittest.cc
blobf2820d905e3e40a7dc8273b49f7a5d311967af99
1 // Copyright 2014 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/command_line.h"
6 #include "base/metrics/field_trial.h"
7 #include "base/test/mock_entropy_provider.h"
8 #include "chrome/browser/io_thread.h"
9 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
10 #include "net/http/http_network_session.h"
11 #include "net/http/http_server_properties_impl.h"
12 #include "net/quic/quic_protocol.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace test {
18 using ::testing::ElementsAre;
20 class IOThreadPeer {
21 public:
22 static void ConfigureQuicGlobals(
23 const base::CommandLine& command_line,
24 base::StringPiece quic_trial_group,
25 const std::map<std::string, std::string>& quic_trial_params,
26 IOThread::Globals* globals) {
27 IOThread::ConfigureQuicGlobals(command_line, quic_trial_group,
28 quic_trial_params, globals);
31 static void ConfigureSpdyGlobals(
32 const base::CommandLine& command_line,
33 base::StringPiece spdy_trial_group,
34 const std::map<std::string, std::string>& spdy_trial_params,
35 IOThread::Globals* globals) {
36 IOThread::ConfigureSpdyGlobals(command_line, spdy_trial_group,
37 spdy_trial_params, globals);
40 static void InitializeNetworkSessionParamsFromGlobals(
41 const IOThread::Globals& globals,
42 net::HttpNetworkSession::Params* params) {
43 IOThread::InitializeNetworkSessionParamsFromGlobals(globals, params);
47 class IOThreadTest : public testing::Test {
48 public:
49 IOThreadTest() : command_line_(base::CommandLine::NO_PROGRAM) {
50 globals_.http_server_properties.reset(new net::HttpServerPropertiesImpl());
53 void ConfigureQuicGlobals() {
54 IOThreadPeer::ConfigureQuicGlobals(command_line_, field_trial_group_,
55 field_trial_params_, &globals_);
58 void ConfigureSpdyGlobals() {
59 IOThreadPeer::ConfigureSpdyGlobals(command_line_, field_trial_group_,
60 field_trial_params_, &globals_);
63 void InitializeNetworkSessionParams(net::HttpNetworkSession::Params* params) {
64 IOThreadPeer::InitializeNetworkSessionParamsFromGlobals(globals_, params);
67 base::CommandLine command_line_;
68 IOThread::Globals globals_;
69 std::string field_trial_group_;
70 std::map<std::string, std::string> field_trial_params_;
73 TEST_F(IOThreadTest, InitializeNetworkSessionParamsFromGlobals) {
74 globals_.quic_connection_options.push_back(net::kPACE);
75 globals_.quic_connection_options.push_back(net::kTBBR);
76 globals_.quic_connection_options.push_back(net::kTIME);
78 net::HttpNetworkSession::Params params;
79 InitializeNetworkSessionParams(&params);
80 EXPECT_EQ(globals_.quic_connection_options,
81 params.quic_connection_options);
84 TEST_F(IOThreadTest, SpdyFieldTrialHoldbackEnabled) {
85 net::HttpStreamFactory::set_spdy_enabled(true);
86 field_trial_group_ = "SpdyDisabled";
87 ConfigureSpdyGlobals();
88 EXPECT_FALSE(net::HttpStreamFactory::spdy_enabled());
91 TEST_F(IOThreadTest, SpdyFieldTrialSpdy31Enabled) {
92 bool use_alternate_protocols = false;
93 field_trial_group_ = "Spdy31Enabled";
94 ConfigureSpdyGlobals();
95 EXPECT_THAT(globals_.next_protos,
96 ElementsAre(net::kProtoHTTP11,
97 net::kProtoSPDY31));
98 globals_.use_alternate_protocols.CopyToIfSet(&use_alternate_protocols);
99 EXPECT_TRUE(use_alternate_protocols);
102 TEST_F(IOThreadTest, SpdyFieldTrialSpdy4Enabled) {
103 bool use_alternate_protocols = false;
104 field_trial_group_ = "Spdy4Enabled";
105 ConfigureSpdyGlobals();
106 EXPECT_THAT(globals_.next_protos,
107 ElementsAre(net::kProtoHTTP11, net::kProtoSPDY31,
108 net::kProtoSPDY4_14, net::kProtoSPDY4));
109 globals_.use_alternate_protocols.CopyToIfSet(&use_alternate_protocols);
110 EXPECT_TRUE(use_alternate_protocols);
113 TEST_F(IOThreadTest, SpdyFieldTrialDefault) {
114 field_trial_group_ = "";
115 ConfigureSpdyGlobals();
116 EXPECT_THAT(globals_.next_protos,
117 ElementsAre(net::kProtoHTTP11, net::kProtoSPDY31,
118 net::kProtoSPDY4_14, net::kProtoSPDY4));
119 bool use_alternate_protocols = false;
120 globals_.use_alternate_protocols.CopyToIfSet(&use_alternate_protocols);
121 EXPECT_TRUE(use_alternate_protocols);
124 TEST_F(IOThreadTest, SpdyFieldTrialParametrized) {
125 field_trial_params_["enable_spdy31"] = "false";
126 // Undefined parameter "enable_http2_14" should default to false.
127 field_trial_params_["enable_http2"] = "true";
128 field_trial_group_ = "ParametrizedHTTP2Only";
129 ConfigureSpdyGlobals();
130 EXPECT_THAT(globals_.next_protos,
131 ElementsAre(net::kProtoHTTP11, net::kProtoSPDY4));
132 bool use_alternate_protocols = false;
133 globals_.use_alternate_protocols.CopyToIfSet(&use_alternate_protocols);
134 EXPECT_TRUE(use_alternate_protocols);
137 TEST_F(IOThreadTest, SpdyCommandLineEnable) {
138 command_line_.AppendSwitch("enable-spdy4");
139 // Command line should overwrite field trial group.
140 field_trial_group_ = "SpdyDisabled";
141 ConfigureSpdyGlobals();
142 EXPECT_THAT(globals_.next_protos,
143 ElementsAre(net::kProtoHTTP11, net::kProtoSPDY31,
144 net::kProtoSPDY4_14, net::kProtoSPDY4));
145 bool use_alternate_protocols = false;
146 globals_.use_alternate_protocols.CopyToIfSet(&use_alternate_protocols);
147 EXPECT_TRUE(use_alternate_protocols);
150 TEST_F(IOThreadTest, SpdyCommandLineDisable) {
151 command_line_.AppendSwitch("enable-npn-http");
152 // Command line should overwrite field trial group.
153 field_trial_group_ = "Spdy4Enabled";
154 ConfigureSpdyGlobals();
155 EXPECT_THAT(globals_.next_protos, ElementsAre(net::kProtoHTTP11));
156 bool use_alternate_protocols = true;
157 globals_.use_alternate_protocols.CopyToIfSet(&use_alternate_protocols);
158 EXPECT_FALSE(use_alternate_protocols);
161 TEST_F(IOThreadTest, SpdyCommandLineUseSpdyOff) {
162 command_line_.AppendSwitchASCII("use-spdy", "off");
163 // Command line should overwrite field trial group.
164 field_trial_group_ = "Spdy4Enabled";
165 ConfigureSpdyGlobals();
166 EXPECT_EQ(0u, globals_.next_protos.size());
169 TEST_F(IOThreadTest, SpdyCommandLineUseSpdyDisableAltProtocols) {
170 command_line_.AppendSwitchASCII("use-spdy", "no-alt-protocols");
171 ConfigureSpdyGlobals();
172 bool use_alternate_protocols = true;
173 globals_.use_alternate_protocols.CopyToIfSet(&use_alternate_protocols);
174 EXPECT_FALSE(use_alternate_protocols);
177 TEST_F(IOThreadTest, DisableQuicByDefault) {
178 ConfigureQuicGlobals();
179 net::HttpNetworkSession::Params params;
180 InitializeNetworkSessionParams(&params);
181 EXPECT_FALSE(params.enable_quic);
182 EXPECT_FALSE(params.enable_quic_for_proxies);
183 EXPECT_FALSE(IOThread::ShouldEnableQuicForDataReductionProxy());
186 TEST_F(IOThreadTest, EnableQuicFromFieldTrialGroup) {
187 field_trial_group_ = "Enabled";
189 ConfigureQuicGlobals();
190 net::HttpNetworkSession::Params default_params;
191 net::HttpNetworkSession::Params params;
192 InitializeNetworkSessionParams(&params);
193 EXPECT_TRUE(params.enable_quic);
194 EXPECT_TRUE(params.enable_quic_for_proxies);
195 EXPECT_EQ(1350u, params.quic_max_packet_length);
196 EXPECT_EQ(1.0, params.alternate_protocol_probability_threshold);
197 EXPECT_EQ(default_params.quic_supported_versions,
198 params.quic_supported_versions);
199 EXPECT_EQ(net::QuicTagVector(), params.quic_connection_options);
200 EXPECT_FALSE(params.quic_always_require_handshake_confirmation);
201 EXPECT_FALSE(params.quic_disable_connection_pooling);
202 EXPECT_EQ(0.25f, params.quic_load_server_info_timeout_srtt_multiplier);
203 EXPECT_FALSE(params.quic_enable_connection_racing);
204 EXPECT_FALSE(params.quic_enable_non_blocking_io);
205 EXPECT_FALSE(params.quic_disable_disk_cache);
206 EXPECT_EQ(0, params.quic_max_number_of_lossy_connections);
207 EXPECT_EQ(1.0f, params.quic_packet_loss_threshold);
208 EXPECT_FALSE(IOThread::ShouldEnableQuicForDataReductionProxy());
211 TEST_F(IOThreadTest, EnableQuicFromQuicProxyFieldTrialGroup) {
212 base::FieldTrialList field_trial_list(new base::MockEntropyProvider());
213 base::FieldTrialList::CreateFieldTrial(
214 data_reduction_proxy::DataReductionProxyParams::GetQuicFieldTrialName(),
215 "Enabled");
217 ConfigureQuicGlobals();
218 net::HttpNetworkSession::Params params;
219 InitializeNetworkSessionParams(&params);
220 EXPECT_FALSE(params.enable_quic);
221 EXPECT_TRUE(params.enable_quic_for_proxies);
222 EXPECT_TRUE(IOThread::ShouldEnableQuicForDataReductionProxy());
223 EXPECT_EQ(256 * 1024, params.quic_socket_receive_buffer_size);
226 TEST_F(IOThreadTest, EnableQuicFromCommandLine) {
227 command_line_.AppendSwitch("enable-quic");
229 ConfigureQuicGlobals();
230 net::HttpNetworkSession::Params params;
231 InitializeNetworkSessionParams(&params);
232 EXPECT_TRUE(params.enable_quic);
233 EXPECT_TRUE(params.enable_quic_for_proxies);
234 EXPECT_FALSE(IOThread::ShouldEnableQuicForDataReductionProxy());
237 TEST_F(IOThreadTest, EnablePacingFromCommandLine) {
238 command_line_.AppendSwitch("enable-quic");
239 command_line_.AppendSwitch("enable-quic-pacing");
241 ConfigureQuicGlobals();
242 net::HttpNetworkSession::Params params;
243 InitializeNetworkSessionParams(&params);
244 net::QuicTagVector options;
245 options.push_back(net::kPACE);
246 EXPECT_EQ(options, params.quic_connection_options);
249 TEST_F(IOThreadTest, EnablePacingFromFieldTrialParams) {
250 field_trial_group_ = "Enabled";
251 field_trial_params_["enable_pacing"] = "true";
253 ConfigureQuicGlobals();
254 net::HttpNetworkSession::Params params;
255 InitializeNetworkSessionParams(&params);
256 net::QuicTagVector options;
257 options.push_back(net::kPACE);
258 EXPECT_EQ(options, params.quic_connection_options);
261 TEST_F(IOThreadTest, PacketLengthFromCommandLine) {
262 command_line_.AppendSwitch("enable-quic");
263 command_line_.AppendSwitchASCII("quic-max-packet-length", "1450");
265 ConfigureQuicGlobals();
266 net::HttpNetworkSession::Params params;
267 InitializeNetworkSessionParams(&params);
268 EXPECT_EQ(1450u, params.quic_max_packet_length);
271 TEST_F(IOThreadTest, PacketLengthFromFieldTrialParams) {
272 field_trial_group_ = "Enabled";
273 field_trial_params_["max_packet_length"] = "1450";
275 ConfigureQuicGlobals();
276 net::HttpNetworkSession::Params params;
277 InitializeNetworkSessionParams(&params);
278 EXPECT_EQ(1450u, params.quic_max_packet_length);
281 TEST_F(IOThreadTest, QuicVersionFromCommandLine) {
282 command_line_.AppendSwitch("enable-quic");
283 std::string version =
284 net::QuicVersionToString(net::QuicSupportedVersions().back());
285 command_line_.AppendSwitchASCII("quic-version", version);
287 ConfigureQuicGlobals();
288 net::HttpNetworkSession::Params params;
289 InitializeNetworkSessionParams(&params);
290 net::QuicVersionVector supported_versions;
291 supported_versions.push_back(net::QuicSupportedVersions().back());
292 EXPECT_EQ(supported_versions, params.quic_supported_versions);
295 TEST_F(IOThreadTest, QuicVersionFromFieldTrialParams) {
296 field_trial_group_ = "Enabled";
297 field_trial_params_["quic_version"] =
298 net::QuicVersionToString(net::QuicSupportedVersions().back());
300 ConfigureQuicGlobals();
301 net::HttpNetworkSession::Params params;
302 InitializeNetworkSessionParams(&params);
303 net::QuicVersionVector supported_versions;
304 supported_versions.push_back(net::QuicSupportedVersions().back());
305 EXPECT_EQ(supported_versions, params.quic_supported_versions);
308 TEST_F(IOThreadTest, QuicConnectionOptionsFromCommandLine) {
309 command_line_.AppendSwitch("enable-quic");
310 command_line_.AppendSwitchASCII("quic-connection-options",
311 "PACE,TIME,TBBR,REJ");
313 ConfigureQuicGlobals();
314 net::HttpNetworkSession::Params params;
315 InitializeNetworkSessionParams(&params);
317 net::QuicTagVector options;
318 options.push_back(net::kPACE);
319 options.push_back(net::kTIME);
320 options.push_back(net::kTBBR);
321 options.push_back(net::kREJ);
322 EXPECT_EQ(options, params.quic_connection_options);
325 TEST_F(IOThreadTest, QuicConnectionOptionsFromFieldTrialParams) {
326 field_trial_group_ = "Enabled";
327 field_trial_params_["connection_options"] = "PACE,TIME,TBBR,REJ";
329 ConfigureQuicGlobals();
330 net::HttpNetworkSession::Params params;
331 InitializeNetworkSessionParams(&params);
333 net::QuicTagVector options;
334 options.push_back(net::kPACE);
335 options.push_back(net::kTIME);
336 options.push_back(net::kTBBR);
337 options.push_back(net::kREJ);
338 EXPECT_EQ(options, params.quic_connection_options);
341 TEST_F(IOThreadTest,
342 QuicAlwaysRequireHandshakeConfirmationFromFieldTrialParams) {
343 field_trial_group_ = "Enabled";
344 field_trial_params_["always_require_handshake_confirmation"] = "true";
345 ConfigureQuicGlobals();
346 net::HttpNetworkSession::Params params;
347 InitializeNetworkSessionParams(&params);
348 EXPECT_TRUE(params.quic_always_require_handshake_confirmation);
351 TEST_F(IOThreadTest,
352 QuicDisableConnectionPoolingFromFieldTrialParams) {
353 field_trial_group_ = "Enabled";
354 field_trial_params_["disable_connection_pooling"] = "true";
355 ConfigureQuicGlobals();
356 net::HttpNetworkSession::Params params;
357 InitializeNetworkSessionParams(&params);
358 EXPECT_TRUE(params.quic_disable_connection_pooling);
361 TEST_F(IOThreadTest, QuicLoadServerInfoTimeToSmoothedRttFromFieldTrialParams) {
362 field_trial_group_ = "Enabled";
363 field_trial_params_["load_server_info_time_to_srtt"] = "0.5";
364 ConfigureQuicGlobals();
365 net::HttpNetworkSession::Params params;
366 InitializeNetworkSessionParams(&params);
367 EXPECT_EQ(0.5f, params.quic_load_server_info_timeout_srtt_multiplier);
370 TEST_F(IOThreadTest, QuicEnableConnectionRacing) {
371 field_trial_group_ = "Enabled";
372 field_trial_params_["enable_connection_racing"] = "true";
373 ConfigureQuicGlobals();
374 net::HttpNetworkSession::Params params;
375 InitializeNetworkSessionParams(&params);
376 EXPECT_TRUE(params.quic_enable_connection_racing);
379 TEST_F(IOThreadTest, QuicEnableNonBlockingIO) {
380 field_trial_group_ = "Enabled";
381 field_trial_params_["enable_non_blocking_io"] = "true";
382 ConfigureQuicGlobals();
383 net::HttpNetworkSession::Params params;
384 InitializeNetworkSessionParams(&params);
385 EXPECT_TRUE(params.quic_enable_non_blocking_io);
388 TEST_F(IOThreadTest, QuicDisableDiskCache) {
389 field_trial_group_ = "Enabled";
390 field_trial_params_["disable_disk_cache"] = "true";
391 ConfigureQuicGlobals();
392 net::HttpNetworkSession::Params params;
393 InitializeNetworkSessionParams(&params);
394 EXPECT_TRUE(params.quic_disable_disk_cache);
397 TEST_F(IOThreadTest, QuicMaxNumberOfLossyConnectionsFieldTrialParams) {
398 field_trial_group_ = "Enabled";
399 field_trial_params_["max_number_of_lossy_connections"] = "5";
400 ConfigureQuicGlobals();
401 net::HttpNetworkSession::Params params;
402 InitializeNetworkSessionParams(&params);
403 EXPECT_EQ(5, params.quic_max_number_of_lossy_connections);
406 TEST_F(IOThreadTest, QuicPacketLossThresholdFieldTrialParams) {
407 field_trial_group_ = "Enabled";
408 field_trial_params_["packet_loss_threshold"] = "0.5";
409 ConfigureQuicGlobals();
410 net::HttpNetworkSession::Params params;
411 InitializeNetworkSessionParams(&params);
412 EXPECT_EQ(0.5f, params.quic_packet_loss_threshold);
415 TEST_F(IOThreadTest, QuicReceiveBufferSize) {
416 field_trial_group_ = "Enabled";
417 field_trial_params_["receive_buffer_size"] = "1048576";
418 ConfigureQuicGlobals();
419 net::HttpNetworkSession::Params params;
420 InitializeNetworkSessionParams(&params);
421 EXPECT_EQ(1048576, params.quic_socket_receive_buffer_size);
424 TEST_F(IOThreadTest,
425 AlternateProtocolProbabilityThresholdFromFlag) {
426 command_line_.AppendSwitchASCII("alternate-protocol-probability-threshold",
427 ".5");
429 ConfigureQuicGlobals();
430 net::HttpNetworkSession::Params params;
431 InitializeNetworkSessionParams(&params);
432 EXPECT_EQ(.5, params.alternate_protocol_probability_threshold);
435 TEST_F(IOThreadTest,
436 AlternateProtocolProbabilityThresholdFromEnableQuicFlag) {
437 command_line_.AppendSwitch("enable-quic");
439 ConfigureQuicGlobals();
440 net::HttpNetworkSession::Params params;
441 InitializeNetworkSessionParams(&params);
442 EXPECT_EQ(0, params.alternate_protocol_probability_threshold);
445 TEST_F(IOThreadTest,
446 AlternateProtocolProbabilityThresholdFromParams) {
447 field_trial_group_ = "Enabled";
448 field_trial_params_["alternate_protocol_probability_threshold"] = ".5";
450 ConfigureQuicGlobals();
451 net::HttpNetworkSession::Params params;
452 InitializeNetworkSessionParams(&params);
453 EXPECT_EQ(.5, params.alternate_protocol_probability_threshold);
456 } // namespace test