1 // Copyright 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 "components/data_use_measurement/content/data_use_measurement.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/test/histogram_tester.h"
11 #include "content/public/browser/resource_request_info.h"
12 #include "net/base/network_change_notifier.h"
13 #include "net/base/request_priority.h"
14 #include "net/socket/socket_test_util.h"
15 #include "net/url_request/url_request.h"
16 #include "net/url_request/url_request_test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
20 #if defined(OS_ANDROID)
21 #include "base/android/application_status_listener.h"
24 namespace data_use_measurement
{
26 class DataUseMeasurementTest
: public testing::Test
{
28 DataUseMeasurementTest() {
29 // During the test it is expected to not have cellular connection.
30 DCHECK(!net::NetworkChangeNotifier::IsConnectionCellular(
31 net::NetworkChangeNotifier::GetConnectionType()));
34 // This function makes a user request and confirms that its effect is
35 // reflected in proper histograms.
36 void TestForAUserRequest(const std::string
& target_dimension
) {
37 net::TestDelegate test_delegate
;
39 base::HistogramTester histogram_tester
;
40 net::MockRead reads
[] = {net::MockRead("HTTP/1.1 200 OK\r\n"
41 "Content-Length: 12\r\n\r\n"),
42 net::MockRead("Test Content")};
43 net::StaticSocketDataProvider
socket_data(reads
, arraysize(reads
), nullptr,
45 socket_factory_
->AddSocketDataProvider(&socket_data
);
47 scoped_ptr
<net::URLRequest
> request(context_
->CreateRequest(
48 GURL("http://foo.com"), net::DEFAULT_PRIORITY
, &test_delegate
));
50 data_use_measurement::DataUseUserData::kUserDataKey
,
51 new data_use_measurement::DataUseUserData(
52 data_use_measurement::DataUseUserData::SUGGESTIONS
));
56 data_use_measurement_
.ReportDataUseUMA(request
.get());
57 histogram_tester
.ExpectTotalCount("DataUse.TrafficSize.System.Downstream." +
58 target_dimension
+ kConnectionType
,
60 histogram_tester
.ExpectTotalCount("DataUse.TrafficSize.System.Upstream." +
61 target_dimension
+ kConnectionType
,
63 // One upload and one download message, so total count should be 2.
64 histogram_tester
.ExpectTotalCount("DataUse.MessageSize.Suggestions", 2);
67 // This function makes a service request and confirms that its effect is
68 // reflected in proper histograms.
69 void TestForAServiceRequest(const std::string
& target_dimension
) {
70 net::TestDelegate test_delegate
;
72 base::HistogramTester histogram_tester
;
74 net::MockRead reads
[] = {net::MockRead("HTTP/1.1 200 OK\r\n"
75 "Content-Length: 12\r\n\r\n"),
76 net::MockRead("Test Content")};
77 net::StaticSocketDataProvider
socket_data(reads
, arraysize(reads
), nullptr,
79 socket_factory_
->AddSocketDataProvider(&socket_data
);
81 scoped_ptr
<net::URLRequest
> request(context_
->CreateRequest(
82 GURL("http://foo.com"), net::DEFAULT_PRIORITY
, &test_delegate
));
83 content::ResourceRequestInfo::AllocateForTesting(
84 request
.get(), content::RESOURCE_TYPE_MAIN_FRAME
, nullptr, -2, -2, -2,
85 true, false, true, true);
89 data_use_measurement_
.ReportDataUseUMA(request
.get());
90 histogram_tester
.ExpectTotalCount("DataUse.TrafficSize.User.Downstream." +
91 target_dimension
+ kConnectionType
,
93 histogram_tester
.ExpectTotalCount("DataUse.TrafficSize.User.Upstream." +
94 target_dimension
+ kConnectionType
,
96 histogram_tester
.ExpectTotalCount(
97 "DataUse.MessageSize.AllServices.Upstream." + target_dimension
+
100 histogram_tester
.ExpectTotalCount(
101 "DataUse.MessageSize.AllServices.Downstream." + target_dimension
+
106 DataUseMeasurement
* data_use_measurement() { return &data_use_measurement_
; }
109 void InitializeContext() {
110 context_
.reset(new net::TestURLRequestContext(true));
111 socket_factory_
.reset(new net::MockClientSocketFactory());
112 context_
->set_client_socket_factory(socket_factory_
.get());
116 base::MessageLoopForIO loop_
;
117 DataUseMeasurement data_use_measurement_
;
118 scoped_ptr
<net::MockClientSocketFactory
> socket_factory_
;
119 scoped_ptr
<net::TestURLRequestContext
> context_
;
120 const std::string kConnectionType
= "NotCellular";
122 DISALLOW_COPY_AND_ASSIGN(DataUseMeasurementTest
);
125 // This test function tests recording of data use information in UMA histogram
126 // when packet is originated from user or services when the app is in the
127 // foreground or the OS is not Android.
128 // TODO(amohammadkhan): Add tests for Cellular/non-cellular connection types
129 // when support for testing is provided in its class.
130 TEST_F(DataUseMeasurementTest
, UserNotUserTest
) {
131 #if defined(OS_ANDROID)
132 data_use_measurement()->OnApplicationStateChangeForTesting(
133 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES
);
135 TestForAServiceRequest("Foreground.");
136 TestForAUserRequest("Foreground.");
139 #if defined(OS_ANDROID)
140 // This test function tests recording of data use information in UMA histogram
141 // when packet is originated from user or services when the app is in the
142 // background and OS is Android.
143 TEST_F(DataUseMeasurementTest
, ApplicationStateTest
) {
144 data_use_measurement()->OnApplicationStateChangeForTesting(
145 base::android::APPLICATION_STATE_HAS_STOPPED_ACTIVITIES
);
146 TestForAServiceRequest("Background.");
147 TestForAUserRequest("Background.");
151 } // namespace data_use_measurement