Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1347)

Side by Side Diff: components/data_use_measurement/content/data_use_measurement_unittest.cc

Issue 1279543002: Support needed to measure user and service traffic in Chrome. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@NewHistogram
Patch Set: Addressed comments and rebased. Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "components/data_use_measurement/content/data_use_measurement.h"
6
7 #include <string>
8
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"
18 #include "url/gurl.h"
19
20 #if defined(OS_ANDROID)
21 #include "base/android/application_status_listener.h"
22 #endif
23
24 namespace data_use_measurement {
25
26 class DataUseMeasurementTest : public testing::Test {
27 public:
28 DataUseMeasurementTest() {
29 // During the test it is expected to not have cellular connection.
30 DCHECK(!net::NetworkChangeNotifier::IsConnectionCellular(
31 net::NetworkChangeNotifier::GetConnectionType()));
32 }
33
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;
38 InitializeContext();
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,
44 0);
45 socket_factory_->AddSocketDataProvider(&socket_data);
46
47 scoped_ptr<net::URLRequest> request(context_->CreateRequest(
48 GURL("http://foo.com"), net::DEFAULT_PRIORITY, &test_delegate));
49 request->SetUserData(
50 data_use_measurement::DataUseUserData::kUserDataKey,
51 new data_use_measurement::DataUseUserData(
52 data_use_measurement::DataUseUserData::SUGGESTIONS));
53 request->Start();
54 loop_.RunUntilIdle();
55
56 data_use_measurement_.ReportDataUseUMA(request.get());
57 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.System.Downstream." +
58 target_dimension + kConnectionType,
59 1);
60 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.System.Upstream." +
61 target_dimension + kConnectionType,
62 1);
63 // One upload and one download message, so total count should be 2.
64 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 2);
65 }
66
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;
71 InitializeContext();
72 base::HistogramTester histogram_tester;
73
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,
78 0);
79 socket_factory_->AddSocketDataProvider(&socket_data);
80
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);
86 request->Start();
87 loop_.RunUntilIdle();
88
89 data_use_measurement_.ReportDataUseUMA(request.get());
90 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.User.Downstream." +
91 target_dimension + kConnectionType,
92 1);
93 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.User.Upstream." +
94 target_dimension + kConnectionType,
95 1);
96 histogram_tester.ExpectTotalCount(
97 "DataUse.MessageSize.AllServices.Upstream." + target_dimension +
98 kConnectionType,
99 0);
100 histogram_tester.ExpectTotalCount(
101 "DataUse.MessageSize.AllServices.Downstream." + target_dimension +
102 kConnectionType,
103 0);
104 }
105
106 DataUseMeasurement* data_use_measurement() { return &data_use_measurement_; }
107
108 private:
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());
113 context_->Init();
114 }
115
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";
121
122 DISALLOW_COPY_AND_ASSIGN(DataUseMeasurementTest);
123 };
124
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);
134 #endif
135 TestForAServiceRequest("Foreground.");
136 TestForAUserRequest("Foreground.");
137 }
138
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.");
148 }
149 #endif
150
151 } // namespace data_use_measurement
OLDNEW
« no previous file with comments | « components/data_use_measurement/content/data_use_measurement.cc ('k') | components/data_use_measurement/core/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698