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

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: Addressing reviewer's comments. 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() : context_(new net::TestURLRequestContext(true)) {
29 // During the test it is expected to not have cellular connection.
30 DCHECK(!net::NetworkChangeNotifier::IsConnectionCellular(
31 net::NetworkChangeNotifier::GetConnectionType()));
32 context_->set_client_socket_factory(&socket_factory_);
33 context_->set_network_delegate(&test_network_delegate_);
34 context_->Init();
35 reads_[0] = net::MockRead(
36 "HTTP/1.1 200 OK\r\n"
37 "Content-Length: 12\r\n\r\n");
38 reads_[1] = net::MockRead("Test Content");
39 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.
40 reads_, arraysize(reads_), nullptr, 0));
41 socket_factory_.AddSocketDataProvider(socket_provider_.get());
42 }
43
44 // This function makes a user request and confirms that its effect is
45 // reflected in proper histograms.
46 void TestForAUserRequest(const std::string& target_dimension) {
47 net::TestDelegate test_delegate;
48
49 socket_provider_->Reset();
50 base::HistogramTester histogram_tester;
51 scoped_ptr<net::URLRequest> request(context_->CreateRequest(
52 GURL("http://foo.com"), net::DEFAULT_PRIORITY, &test_delegate));
53 request->SetUserData(
54 data_use_measurement::DataUseUserData::kUserDataKey,
55 new data_use_measurement::DataUseUserData(
56 data_use_measurement::DataUseUserData::SUGGESTIONS));
57 request->Start();
58 loop_.RunUntilIdle();
59 data_use_measurement_.ReportDataUseUMA(request.get());
60 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.System.Downstream." +
61 target_dimension + kConnectionType,
62 1);
63 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.System.Upstream." +
64 target_dimension + kConnectionType,
65 1);
66 // One upload and one download message, so total count should be 2.
67 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 2);
68 }
69
70 // This function makes a service request and confirms that its effect is
71 // reflected in proper histograms.
72 void TestForAServiceRequest(const std::string& target_dimension) {
73 net::TestDelegate test_delegate;
74 socket_provider_->Reset();
75
76 base::HistogramTester histogram_tester;
77 scoped_ptr<net::URLRequest> request(context_->CreateRequest(
78 GURL("http://foo.com"), net::DEFAULT_PRIORITY, &test_delegate));
79 content::ResourceRequestInfo::AllocateForTesting(
80 request.get(), content::RESOURCE_TYPE_MAIN_FRAME, nullptr, -2, -2, -2,
81 true, false, true, true);
82 request->Start();
83 loop_.RunUntilIdle();
84 data_use_measurement_.ReportDataUseUMA(request.get());
85 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.User.Downstream." +
86 target_dimension + kConnectionType,
87 1);
88 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.User.Upstream." +
89 target_dimension + kConnectionType,
90 1);
91 histogram_tester.ExpectTotalCount(
92 "DataUse.MessageSize.AllServices.Upstream." + target_dimension +
93 kConnectionType,
94 0);
95 histogram_tester.ExpectTotalCount(
96 "DataUse.MessageSize.AllServices.Downstream." + target_dimension +
97 kConnectionType,
98 0);
99 }
100
101 DataUseMeasurement* data_use_measurement() { return &data_use_measurement_; }
102
103 private:
104 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.
105 net::MockRead reads_[2];
106 DataUseMeasurement data_use_measurement_;
107 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.
108 net::MockClientSocketFactory socket_factory_;
109 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.
110 scoped_ptr<net::TestURLRequestContext> context_;
111 const std::string kConnectionType = "NotCellular";
112
113 DISALLOW_COPY_AND_ASSIGN(DataUseMeasurementTest);
114 };
115
116 // This test function tests recording of data use information in UMA histogram
117 // when packet is originated from user or services.
118 // TODO(amohammadkhan): Add tests for Cellular/non-cellular connection types
119 // when support for testing is provided in its class.
120 TEST_F(DataUseMeasurementTest, UserNotUserTest) {
121 #if defined(OS_ANDROID)
122 data_use_measurement()->OnApplicationStateChangeForTesting(
123 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES);
124 #endif
125 TestForAServiceRequest("Foreground.");
126 TestForAUserRequest("Foreground.");
127 }
128
129 #if defined(OS_ANDROID)
130 TEST_F(DataUseMeasurementTest, ApplicationStateTest) {
131 data_use_measurement()->OnApplicationStateChangeForTesting(
132 base::android::APPLICATION_STATE_HAS_STOPPED_ACTIVITIES);
133 TestForAServiceRequest("Background.");
134 TestForAUserRequest("Background.");
135 }
136 #endif
137
138 } // 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