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

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

Issue 1818613002: Implement UMA log throttling for cellular connections (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/data_use_measurement/content/data_use_measurement.h" 5 #include "components/data_use_measurement/content/data_use_measurement.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/test/histogram_tester.h" 11 #include "base/test/histogram_tester.h"
12 #include "build/build_config.h" 12 #include "build/build_config.h"
13 #include "content/public/browser/resource_request_info.h" 13 #include "content/public/browser/resource_request_info.h"
14 #include "net/base/network_change_notifier.h" 14 #include "net/base/network_change_notifier.h"
15 #include "net/base/request_priority.h" 15 #include "net/base/request_priority.h"
16 #include "net/socket/socket_test_util.h" 16 #include "net/socket/socket_test_util.h"
17 #include "net/url_request/url_request.h" 17 #include "net/url_request/url_request.h"
18 #include "net/url_request/url_request_test_util.h" 18 #include "net/url_request/url_request_test_util.h"
19 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "url/gurl.h" 20 #include "url/gurl.h"
21 21
22 #if defined(OS_ANDROID) 22 #if defined(OS_ANDROID)
23 #include "base/android/application_status_listener.h" 23 #include "base/android/application_status_listener.h"
24 #endif 24 #endif
25 25
26 namespace data_use_measurement { 26 namespace data_use_measurement {
27 27
28 class DataUseMeasurementTest : public testing::Test { 28 class DataUseMeasurementTest : public testing::Test {
29 public: 29 public:
30 DataUseMeasurementTest() { 30 DataUseMeasurementTest()
31 : data_use_measurement_(
32 base::Bind(&DataUseMeasurementTest::FakeDataUseforwarder,
33 base::Unretained(this))) {
31 // During the test it is expected to not have cellular connection. 34 // During the test it is expected to not have cellular connection.
32 DCHECK(!net::NetworkChangeNotifier::IsConnectionCellular( 35 DCHECK(!net::NetworkChangeNotifier::IsConnectionCellular(
33 net::NetworkChangeNotifier::GetConnectionType())); 36 net::NetworkChangeNotifier::GetConnectionType()));
34 } 37 }
35 38
36 // This function makes a user request and confirms that its effect is 39 // Sends a request and reports data use attaching either user data or service
37 // reflected in proper histograms. 40 // data based on |is_user_request|.
38 void TestForAUserRequest(const std::string& target_dimension) { 41 void SendRequest(bool is_user_request) {
39 net::TestDelegate test_delegate; 42 net::TestDelegate test_delegate;
40 InitializeContext(); 43 InitializeContext();
41 base::HistogramTester histogram_tester;
42 net::MockRead reads[] = {net::MockRead("HTTP/1.1 200 OK\r\n" 44 net::MockRead reads[] = {net::MockRead("HTTP/1.1 200 OK\r\n"
43 "Content-Length: 12\r\n\r\n"), 45 "Content-Length: 12\r\n\r\n"),
44 net::MockRead("Test Content")}; 46 net::MockRead("Test Content")};
45 net::StaticSocketDataProvider socket_data(reads, arraysize(reads), nullptr, 47 net::StaticSocketDataProvider socket_data(reads, arraysize(reads), nullptr,
46 0); 48 0);
47 socket_factory_->AddSocketDataProvider(&socket_data); 49 socket_factory_->AddSocketDataProvider(&socket_data);
48 50
49 scoped_ptr<net::URLRequest> request(context_->CreateRequest( 51 scoped_ptr<net::URLRequest> request(context_->CreateRequest(
50 GURL("http://foo.com"), net::DEFAULT_PRIORITY, &test_delegate)); 52 GURL("http://foo.com"), net::DEFAULT_PRIORITY, &test_delegate));
51 request->SetUserData( 53 if (is_user_request) {
52 data_use_measurement::DataUseUserData::kUserDataKey, 54 request->SetUserData(
53 new data_use_measurement::DataUseUserData( 55 data_use_measurement::DataUseUserData::kUserDataKey,
54 data_use_measurement::DataUseUserData::SUGGESTIONS)); 56 new data_use_measurement::DataUseUserData(
57 data_use_measurement::DataUseUserData::SUGGESTIONS));
58 } else {
59 content::ResourceRequestInfo::AllocateForTesting(
60 request.get(), content::RESOURCE_TYPE_MAIN_FRAME, nullptr, -2, -2, -2,
61 true, false, true, true, false);
62 }
63
55 request->Start(); 64 request->Start();
56 loop_.RunUntilIdle(); 65 loop_.RunUntilIdle();
57 66
58 data_use_measurement_.ReportDataUseUMA(request.get()); 67 data_use_measurement_.ReportDataUseUMA(request.get());
68 }
69
70 // This function makes a user request and confirms that its effect is
71 // reflected in proper histograms.
72 void TestForAUserRequest(const std::string& target_dimension) {
73 base::HistogramTester histogram_tester;
74 SendRequest(true);
59 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.System.Downstream." + 75 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.System.Downstream." +
60 target_dimension + kConnectionType, 76 target_dimension + kConnectionType,
61 1); 77 1);
62 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.System.Upstream." + 78 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.System.Upstream." +
63 target_dimension + kConnectionType, 79 target_dimension + kConnectionType,
64 1); 80 1);
65 // One upload and one download message, so total count should be 2. 81 // One upload and one download message, so total count should be 2.
66 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 2); 82 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 2);
67 } 83 }
68 84
69 // This function makes a service request and confirms that its effect is 85 // This function makes a service request and confirms that its effect is
70 // reflected in proper histograms. 86 // reflected in proper histograms.
71 void TestForAServiceRequest(const std::string& target_dimension) { 87 void TestForAServiceRequest(const std::string& target_dimension) {
72 net::TestDelegate test_delegate;
73 InitializeContext();
74 base::HistogramTester histogram_tester; 88 base::HistogramTester histogram_tester;
75 89 SendRequest(false);
76 net::MockRead reads[] = {net::MockRead("HTTP/1.1 200 OK\r\n"
77 "Content-Length: 12\r\n\r\n"),
78 net::MockRead("Test Content")};
79 net::StaticSocketDataProvider socket_data(reads, arraysize(reads), nullptr,
80 0);
81 socket_factory_->AddSocketDataProvider(&socket_data);
82
83 scoped_ptr<net::URLRequest> request(context_->CreateRequest(
84 GURL("http://foo.com"), net::DEFAULT_PRIORITY, &test_delegate));
85 content::ResourceRequestInfo::AllocateForTesting(
86 request.get(), content::RESOURCE_TYPE_MAIN_FRAME, nullptr, -2, -2, -2,
87 true, false, true, true, false);
88 request->Start();
89 loop_.RunUntilIdle();
90
91 data_use_measurement_.ReportDataUseUMA(request.get());
92 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.User.Downstream." + 90 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.User.Downstream." +
93 target_dimension + kConnectionType, 91 target_dimension + kConnectionType,
94 1); 92 1);
95 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.User.Upstream." + 93 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.User.Upstream." +
96 target_dimension + kConnectionType, 94 target_dimension + kConnectionType,
97 1); 95 1);
98 histogram_tester.ExpectTotalCount( 96 histogram_tester.ExpectTotalCount(
99 "DataUse.MessageSize.AllServices.Upstream." + target_dimension + 97 "DataUse.MessageSize.AllServices.Upstream." + target_dimension +
100 kConnectionType, 98 kConnectionType,
101 0); 99 0);
102 histogram_tester.ExpectTotalCount( 100 histogram_tester.ExpectTotalCount(
103 "DataUse.MessageSize.AllServices.Downstream." + target_dimension + 101 "DataUse.MessageSize.AllServices.Downstream." + target_dimension +
104 kConnectionType, 102 kConnectionType,
105 0); 103 0);
106 } 104 }
107 105
108 DataUseMeasurement* data_use_measurement() { return &data_use_measurement_; } 106 DataUseMeasurement* data_use_measurement() { return &data_use_measurement_; }
109 107
108 bool IsDataUseForwarderCalled() { return is_data_use_forwarder_called_; }
109
110 private: 110 private:
111 void InitializeContext() { 111 void InitializeContext() {
112 context_.reset(new net::TestURLRequestContext(true)); 112 context_.reset(new net::TestURLRequestContext(true));
113 socket_factory_.reset(new net::MockClientSocketFactory()); 113 socket_factory_.reset(new net::MockClientSocketFactory());
114 context_->set_client_socket_factory(socket_factory_.get()); 114 context_->set_client_socket_factory(socket_factory_.get());
115 context_->Init(); 115 context_->Init();
116 } 116 }
117 117
118 void FakeDataUseforwarder(const std::string& service_name,
119 int message_size,
120 bool is_celllular) {
121 is_data_use_forwarder_called_ = true;
122 }
123
118 base::MessageLoopForIO loop_; 124 base::MessageLoopForIO loop_;
119 DataUseMeasurement data_use_measurement_; 125 DataUseMeasurement data_use_measurement_;
120 scoped_ptr<net::MockClientSocketFactory> socket_factory_; 126 scoped_ptr<net::MockClientSocketFactory> socket_factory_;
121 scoped_ptr<net::TestURLRequestContext> context_; 127 scoped_ptr<net::TestURLRequestContext> context_;
122 const std::string kConnectionType = "NotCellular"; 128 const std::string kConnectionType = "NotCellular";
129 bool is_data_use_forwarder_called_ = false;
123 130
124 DISALLOW_COPY_AND_ASSIGN(DataUseMeasurementTest); 131 DISALLOW_COPY_AND_ASSIGN(DataUseMeasurementTest);
125 }; 132 };
126 133
127 // This test function tests recording of data use information in UMA histogram 134 // This test function tests recording of data use information in UMA histogram
128 // when packet is originated from user or services when the app is in the 135 // when packet is originated from user or services when the app is in the
129 // foreground or the OS is not Android. 136 // foreground or the OS is not Android.
130 // TODO(amohammadkhan): Add tests for Cellular/non-cellular connection types 137 // TODO(amohammadkhan): Add tests for Cellular/non-cellular connection types
131 // when support for testing is provided in its class. 138 // when support for testing is provided in its class.
132 TEST_F(DataUseMeasurementTest, UserNotUserTest) { 139 TEST_F(DataUseMeasurementTest, UserNotUserTest) {
(...skipping 10 matching lines...) Expand all
143 // when packet is originated from user or services when the app is in the 150 // when packet is originated from user or services when the app is in the
144 // background and OS is Android. 151 // background and OS is Android.
145 TEST_F(DataUseMeasurementTest, ApplicationStateTest) { 152 TEST_F(DataUseMeasurementTest, ApplicationStateTest) {
146 data_use_measurement()->OnApplicationStateChangeForTesting( 153 data_use_measurement()->OnApplicationStateChangeForTesting(
147 base::android::APPLICATION_STATE_HAS_STOPPED_ACTIVITIES); 154 base::android::APPLICATION_STATE_HAS_STOPPED_ACTIVITIES);
148 TestForAServiceRequest("Background."); 155 TestForAServiceRequest("Background.");
149 TestForAUserRequest("Background."); 156 TestForAUserRequest("Background.");
150 } 157 }
151 #endif 158 #endif
152 159
160 TEST_F(DataUseMeasurementTest, DataUseForwarderIsCalled) {
161 EXPECT_FALSE(IsDataUseForwarderCalled());
162 SendRequest(true);
163 EXPECT_TRUE(IsDataUseForwarderCalled());
164 }
165
153 } // namespace data_use_measurement 166 } // namespace data_use_measurement
OLDNEW
« no previous file with comments | « components/data_use_measurement/content/data_use_measurement.cc ('k') | components/metrics.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698