Chromium Code Reviews| Index: components/data_use_measurement/content/data_use_measurement_unittest.cc |
| diff --git a/components/data_use_measurement/content/data_use_measurement_unittest.cc b/components/data_use_measurement/content/data_use_measurement_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b633a0d9456a514923d98676efac8b925be03058 |
| --- /dev/null |
| +++ b/components/data_use_measurement/content/data_use_measurement_unittest.cc |
| @@ -0,0 +1,138 @@ |
| +// 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_use_measurement/content/data_use_measurement.h" |
| + |
| +#include <string> |
| + |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/test/histogram_tester.h" |
| +#include "content/public/browser/resource_request_info.h" |
| +#include "net/base/network_change_notifier.h" |
| +#include "net/base/request_priority.h" |
| +#include "net/socket/socket_test_util.h" |
| +#include "net/url_request/url_request.h" |
| +#include "net/url_request/url_request_test_util.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "url/gurl.h" |
| + |
| +#if defined(OS_ANDROID) |
| +#include "base/android/application_status_listener.h" |
| +#endif |
| + |
| +namespace data_use_measurement { |
| + |
| +class DataUseMeasurementTest : public testing::Test { |
| + public: |
| + DataUseMeasurementTest() : context_(new net::TestURLRequestContext(true)) { |
| + // During the test it is expected to not have cellular connection. |
| + DCHECK(!net::NetworkChangeNotifier::IsConnectionCellular( |
| + net::NetworkChangeNotifier::GetConnectionType())); |
| + context_->set_client_socket_factory(&socket_factory_); |
| + context_->set_network_delegate(&test_network_delegate_); |
| + context_->Init(); |
| + reads_[0] = net::MockRead( |
| + "HTTP/1.1 200 OK\r\n" |
| + "Content-Length: 12\r\n\r\n"); |
| + reads_[1] = net::MockRead("Test Content"); |
| + socket_provider_.reset(new net::StaticSocketDataProvider( |
|
sclittle
2015/09/09 00:13:37
nit: Use a different MockReads array and SocketDat
amohammadkhan
2015/09/09 20:45:48
Done.
|
| + reads_, arraysize(reads_), nullptr, 0)); |
| + socket_factory_.AddSocketDataProvider(socket_provider_.get()); |
| + } |
| + |
| + // This function makes a user request and confirms that its effect is |
| + // reflected in proper histograms. |
| + void TestForAUserRequest(const std::string& target_dimension) { |
| + net::TestDelegate test_delegate; |
| + |
| + socket_provider_->Reset(); |
| + base::HistogramTester histogram_tester; |
| + scoped_ptr<net::URLRequest> request(context_->CreateRequest( |
| + GURL("http://foo.com"), net::DEFAULT_PRIORITY, &test_delegate)); |
| + request->SetUserData( |
| + data_use_measurement::DataUseUserData::kUserDataKey, |
| + new data_use_measurement::DataUseUserData( |
| + data_use_measurement::DataUseUserData::SUGGESTIONS)); |
| + request->Start(); |
| + loop_.RunUntilIdle(); |
| + data_use_measurement_.ReportDataUseUMA(request.get()); |
| + histogram_tester.ExpectTotalCount("DataUse.TrafficSize.System.Downstream." + |
| + target_dimension + kConnectionType, |
| + 1); |
| + histogram_tester.ExpectTotalCount("DataUse.TrafficSize.System.Upstream." + |
| + target_dimension + kConnectionType, |
| + 1); |
| + // One upload and one download message, so total count should be 2. |
| + histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 2); |
| + } |
| + |
| + // This function makes a service request and confirms that its effect is |
| + // reflected in proper histograms. |
| + void TestForAServiceRequest(const std::string& target_dimension) { |
| + net::TestDelegate test_delegate; |
| + socket_provider_->Reset(); |
| + |
| + base::HistogramTester histogram_tester; |
| + scoped_ptr<net::URLRequest> request(context_->CreateRequest( |
| + GURL("http://foo.com"), net::DEFAULT_PRIORITY, &test_delegate)); |
| + content::ResourceRequestInfo::AllocateForTesting( |
| + request.get(), content::RESOURCE_TYPE_MAIN_FRAME, nullptr, -2, -2, -2, |
| + true, false, true, true); |
| + request->Start(); |
| + loop_.RunUntilIdle(); |
| + data_use_measurement_.ReportDataUseUMA(request.get()); |
| + histogram_tester.ExpectTotalCount("DataUse.TrafficSize.User.Downstream." + |
| + target_dimension + kConnectionType, |
| + 1); |
| + histogram_tester.ExpectTotalCount("DataUse.TrafficSize.User.Upstream." + |
| + target_dimension + kConnectionType, |
| + 1); |
| + histogram_tester.ExpectTotalCount( |
| + "DataUse.MessageSize.AllServices.Upstream." + target_dimension + |
| + kConnectionType, |
| + 0); |
| + histogram_tester.ExpectTotalCount( |
| + "DataUse.MessageSize.AllServices.Downstream." + target_dimension + |
| + kConnectionType, |
| + 0); |
| + } |
| + |
| + DataUseMeasurement* data_use_measurement() { return &data_use_measurement_; } |
| + |
| + private: |
| + scoped_ptr<net::StaticSocketDataProvider> socket_provider_; |
|
sclittle
2015/09/09 00:13:37
nit: remove this and the MockReads below it, and m
amohammadkhan
2015/09/09 20:45:48
Done.
|
| + net::MockRead reads_[2]; |
| + DataUseMeasurement data_use_measurement_; |
| + base::MessageLoopForIO loop_; |
|
sclittle
2015/09/09 00:13:37
nit: Just for convention, move loop_ to the top of
amohammadkhan
2015/09/09 20:45:48
Done.
|
| + net::MockClientSocketFactory socket_factory_; |
| + net::TestNetworkDelegate test_network_delegate_; |
|
sclittle
2015/09/09 00:13:37
nit: This test_network_delegate looks like it isn'
amohammadkhan
2015/09/09 20:45:48
Done.
|
| + scoped_ptr<net::TestURLRequestContext> context_; |
| + const std::string kConnectionType = "NotCellular"; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(DataUseMeasurementTest); |
| +}; |
| + |
| +// This test function tests recording of data use information in UMA histogram |
| +// when packet is originated from user or services. |
| +// TODO(amohammadkhan): Add tests for Cellular/non-cellular connection types |
| +// when support for testing is provided in its class. |
| +TEST_F(DataUseMeasurementTest, UserNotUserTest) { |
| +#if defined(OS_ANDROID) |
| + data_use_measurement()->OnApplicationStateChangeForTesting( |
| + base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES); |
| +#endif |
| + TestForAServiceRequest("Foreground."); |
| + TestForAUserRequest("Foreground."); |
| +} |
| + |
| +#if defined(OS_ANDROID) |
| +TEST_F(DataUseMeasurementTest, ApplicationStateTest) { |
| + data_use_measurement()->OnApplicationStateChangeForTesting( |
| + base::android::APPLICATION_STATE_HAS_STOPPED_ACTIVITIES); |
| + TestForAServiceRequest("Background."); |
| + TestForAUserRequest("Background."); |
| +} |
| +#endif |
| + |
| +} // namespace data_use_measurement |