OLD | NEW |
---|---|
(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 "base/test/histogram_tester.h" | |
8 #include "content/public/browser/resource_request_info.h" | |
9 #include "net/base/network_change_notifier.h" | |
10 #include "net/socket/socket_test_util.h" | |
11 #include "net/url_request/url_request.h" | |
12 #include "net/url_request/url_request_test_util.h" | |
13 #include "testing/gmock/include/gmock/gmock.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 #if defined(OS_ANDROID) | |
17 #include "base/android/application_status_listener.h" | |
18 #endif | |
19 | |
20 namespace data_use_measurement { | |
21 | |
22 class DataUseMeasurementTest : public testing::Test { | |
23 public: | |
24 DataUseMeasurementTest() : context_(new net::TestURLRequestContext(true)) { | |
25 context_->set_client_socket_factory(&socket_factory_); | |
26 context_->set_network_delegate(&test_network_delegate_); | |
27 context_->Init(); | |
28 | |
29 UpdateConnectionType(); | |
30 } | |
31 | |
32 protected: | |
mmenke
2015/09/04 20:51:46
No need for the protected section.
amohammadkhan
2015/09/05 00:58:02
Done.
| |
33 // This function makes one user request and one service request and confirms | |
34 // that their effect is reflected in proper histograms. | |
35 void TestForAUserAndAServicePacket(const std::string& target_dimension) { | |
36 net::MockRead reads[] = {net::MockRead("HTTP/1.1 200 OK\r\n" | |
37 "Content-Length: 12\r\n\r\n"), | |
38 net::MockRead("Test Content"), | |
39 net::MockRead("HTTP/1.1 200 OK\r\n" | |
40 "Content-Length: 13\r\n\r\n"), | |
41 net::MockRead("Test Content2")}; | |
mmenke
2015/09/04 20:51:46
Relying on reusing the socket is really ugly. I s
amohammadkhan
2015/09/05 00:58:02
Done.
| |
42 | |
43 net::StaticSocketDataProvider socket_data(reads, arraysize(reads), nullptr, | |
44 0); | |
45 socket_factory_.AddSocketDataProvider(&socket_data); | |
46 base::HistogramTester histogram_tester; | |
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_.Run(); | |
55 data_use_measurement_.ReportDataUseUMA(request.get()); | |
56 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.System.Downstream." + | |
57 target_dimension + connection_type_, | |
58 1); | |
59 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.System.Upstream." + | |
60 target_dimension + connection_type_, | |
61 1); | |
62 // One upload and one download message, so total count should be 2. | |
63 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 2); | |
64 | |
65 scoped_ptr<net::URLRequest> request2(context_->CreateRequest( | |
mmenke
2015/09/04 20:51:46
include base/memory/scoped_ptr.h
amohammadkhan
2015/09/05 00:58:02
Done.
| |
66 GURL("http://foo.com"), net::DEFAULT_PRIORITY, &test_delegate_)); | |
mmenke
2015/09/04 20:51:46
include url/gurl.h and net/base/request_priority.h
amohammadkhan
2015/09/05 00:58:02
Done.
| |
67 content::ResourceRequestInfo::AllocateForTesting( | |
68 request2.get(), content::RESOURCE_TYPE_MAIN_FRAME, nullptr, -2, -2, -2, | |
69 true, false, true, true); | |
70 request2->Start(); | |
71 loop_.Run(); | |
72 data_use_measurement_.ReportDataUseUMA(request2.get()); | |
73 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.User.Downstream." + | |
74 target_dimension + connection_type_, | |
75 1); | |
76 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.User.Upstream." + | |
77 target_dimension + connection_type_, | |
78 1); | |
mmenke
2015/09/04 20:51:46
Check DataUse.MessageSize.AllServices, too?
amohammadkhan
2015/09/05 00:58:02
Done.
| |
79 } | |
80 | |
81 void UpdateConnectionType() { | |
82 if (net::NetworkChangeNotifier::IsConnectionCellular( | |
83 net::NetworkChangeNotifier::GetConnectionType())) { | |
mmenke
2015/09/04 20:51:46
This always returns CONNECTION_TYPE_UNKNOWN when w
amohammadkhan
2015/09/05 00:58:02
Done.
| |
84 connection_type_ = "Cellular"; | |
85 } else { | |
86 connection_type_ = "NotCellular"; | |
87 } | |
88 } | |
89 | |
90 DataUseMeasurement data_use_measurement_; | |
mmenke
2015/09/04 20:51:46
This can be private.
amohammadkhan
2015/09/05 00:58:02
Done.
| |
91 | |
92 private: | |
93 base::MessageLoopForIO loop_; | |
94 scoped_ptr<net::TestURLRequestContext> context_; | |
95 net::TestNetworkDelegate test_network_delegate_; | |
96 net::TestDelegate test_delegate_; | |
mmenke
2015/09/04 20:51:46
Suggest removing this as a member variable, and ju
amohammadkhan
2015/09/05 00:58:02
Done.
| |
97 std::string connection_type_; | |
mmenke
2015/09/04 20:51:46
include <string>
amohammadkhan
2015/09/05 00:58:02
Done.
| |
98 net::MockClientSocketFactory socket_factory_; | |
mmenke
2015/09/04 20:51:46
Order these based on dependencies:
socket_factory
amohammadkhan
2015/09/05 00:58:02
Done.
| |
99 | |
100 DISALLOW_COPY_AND_ASSIGN(DataUseMeasurementTest); | |
101 }; | |
102 | |
103 // This test function tests recording of data use information in UMA histogram | |
104 // when packet is originated from user or services. | |
105 // TODO(amohammadkhan): Add tests for Cellular/non-cellular connection types | |
106 // when support for testing is provided in its class. | |
107 TEST_F(DataUseMeasurementTest, UserNotUserTest) { | |
108 #if defined(OS_ANDROID) | |
109 data_use_measurement_.OnApplicationStateChangeForTesting( | |
110 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES); | |
111 #endif | |
112 UpdateConnectionType(); | |
113 TestForAUserAndAServicePacket("Foreground."); | |
114 } | |
115 | |
116 #if defined(OS_ANDROID) | |
117 TEST_F(DataUseMeasurementTest, ApplicationStateTest) { | |
118 data_use_measurement_.OnApplicationStateChangeForTesting( | |
119 base::android::APPLICATION_STATE_HAS_STOPPED_ACTIVITIES); | |
120 UpdateConnectionType(); | |
121 TestForAUserAndAServicePacket("Background."); | |
122 } | |
123 #endif | |
124 | |
125 } // namespace data_use_measurement | |
OLD | NEW |