Chromium Code Reviews| Index: components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data_unittest.cc |
| diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3894bd23affad653ef73c0fe466cf294192c3ac5 |
| --- /dev/null |
| +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data_unittest.cc |
| @@ -0,0 +1,136 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h" |
| + |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/prefs/testing_pref_service.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/time/time.h" |
| +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_auth_request_handler.h" |
| +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h" |
| +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h" |
| +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.h" |
| +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_params_test_utils.h" |
| +#include "net/base/capturing_net_log.h" |
| +#include "net/url_request/url_request_interceptor.h" |
| +#include "net/url_request/url_request_test_util.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace data_reduction_proxy { |
| + |
| +// Used only to verify that a wrapped network delegate gets called. |
| +class CountingNetworkDelegate : public net::NetworkDelegateImpl { |
|
mmenke
2015/01/22 17:14:49
This class should be in an anonymous namespace.
bengr
2015/01/23 20:09:04
Done.
|
| + public: |
| + CountingNetworkDelegate() : created_requests_(0) { |
| + } |
| + |
| + ~CountingNetworkDelegate() final { |
| + } |
| + |
| + int OnBeforeURLRequest(net::URLRequest* request, |
| + const net::CompletionCallback& callback, |
| + GURL* new_url) final { |
| + created_requests_++; |
| + return net::OK; |
| + } |
| + |
| + int created_requests() const { |
| + return created_requests_; |
| + } |
| + |
| + private: |
| + int created_requests_; |
| +}; |
| + |
| +class DataReductionProxyIODataTest : public testing::Test { |
| + public: |
| + |
|
mmenke
2015/01/22 17:14:49
No blank line after public
bengr
2015/01/23 20:09:04
Done.
|
| + void SetUp() override { |
| + DataReductionProxyParams* params = |
| + new TestDataReductionProxyParams( |
| + TestDataReductionProxyParams::kAllowed, |
| + TestDataReductionProxyParams::HAS_EVERYTHING & |
| + ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & |
| + ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN); |
| + settings_.reset(new DataReductionProxySettings(params)); |
| + RegisterSimpleProfilePrefs(prefs_.registry()); |
| + } |
| + |
| + void RequestCallback(int err) { |
| + |
|
mmenke
2015/01/22 17:14:49
Remove blank line.
bengr
2015/01/23 20:09:04
Done.
|
| + } |
| + |
| + protected: |
|
mmenke
2015/01/22 17:14:49
Google style guide forbids protect member variable
bengr
2015/01/23 20:09:04
Done.
|
| + base::MessageLoopForIO loop_; |
| + net::TestDelegate delegate_; |
| + net::TestURLRequestContext context_; |
| + net::CapturingNetLog net_log_; |
|
mmenke
2015/01/22 17:14:49
Since you aren't actually digging through the log,
bengr
2015/01/23 20:09:04
Done.
|
| + TestingPrefServiceSimple prefs_; |
| + scoped_ptr<DataReductionProxySettings> settings_; |
| +}; |
| + |
| +TEST_F(DataReductionProxyIODataTest, TestConstruction) { |
| + DataReductionProxyStatisticsPrefs* statistics_prefs = |
| + new DataReductionProxyStatisticsPrefs( |
| + &prefs_, loop_.message_loop_proxy(), base::TimeDelta()); |
| + scoped_ptr<DataReductionProxyIOData> io_data( |
| + new DataReductionProxyIOData( |
| + Client::UNKNOWN, |
| + make_scoped_ptr(statistics_prefs), |
| + settings_.get(), |
| + &net_log_, |
| + loop_.message_loop_proxy(), |
| + loop_.message_loop_proxy())); |
| + |
| + // Check that io_data creates an interceptor. Such an interceptor is |
| + // thoroughly tested by DataReductionProxyInterceptoTest. |
| + scoped_ptr<net::URLRequestInterceptor> interceptor = |
| + io_data->CreateInterceptor(); |
| + EXPECT_NE(nullptr, interceptor.get()); |
| + |
| + // At this point io_data->statistics_prefs() should be set and compression |
| + // statistics logging should be enabled. |
| + EXPECT_EQ(statistics_prefs, io_data->statistics_prefs()); |
| + |
| + // Check if explicitly enabling compression statistics logging results in |
| + // a new logging object being created. |
| + io_data->EnableCompressionStatisticsLogging(&prefs_, base::TimeDelta()); |
| + EXPECT_NE(statistics_prefs, io_data->statistics_prefs()); |
| + EXPECT_TRUE(io_data->statistics_prefs()); |
| + |
| + // When creating a network delegate, expect that it properly wraps a |
| + // network delegate. Such a network delegate is thoroughly tested by |
| + // DataReductionProxyNetworkDelegateTest. |
| + scoped_ptr<net::URLRequest> fake_request = |
| + context_.CreateRequest( |
| + GURL("http://www.foo.com/"), net::IDLE, &delegate_, NULL); |
| + CountingNetworkDelegate* wrapped_network_delegate = |
| + new CountingNetworkDelegate(); |
| + scoped_ptr<DataReductionProxyNetworkDelegate> network_delegate = |
| + io_data->CreateNetworkDelegate( |
| + make_scoped_ptr(wrapped_network_delegate), false); |
| + network_delegate->NotifyBeforeURLRequest( |
| + fake_request.get(), |
| + base::Bind(&DataReductionProxyIODataTest::RequestCallback, |
| + base::Unretained(this)), nullptr); |
| + EXPECT_EQ(1, wrapped_network_delegate->created_requests()); |
| + EXPECT_EQ(nullptr, io_data->usage_stats()); |
| + |
| + // Creating a second delegate with bypass statistics tracking should result |
| + // in usage stats being created. |
| + io_data->CreateNetworkDelegate(make_scoped_ptr(new CountingNetworkDelegate()), |
| + true); |
| + EXPECT_NE(nullptr, io_data->usage_stats()); |
| + |
| + // The Data Reduction Proxy isn't actually enabled here. |
| + io_data->InitPrefsOnUIThread(&prefs_); |
| + EXPECT_FALSE(io_data->IsEnabled()); |
| + io_data->DestroyPrefsOnUIThread(); |
| + io_data->ShutdownStatisicsPrefsOnUIThread(); |
| + |
| +} |
| + |
| +} |
|
mmenke
2015/01/22 17:14:49
} // namespace data_reduction_proxy
bengr
2015/01/23 20:09:04
Done.
|