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: | |
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")}; | |
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( | |
57 "DataUse.NotUser.Downstream." + target_dimension + connection_type_, 1); | |
58 histogram_tester.ExpectTotalCount( | |
59 "DataUse.NotUser.Upstream." + target_dimension + connection_type_, 1); | |
60 // One upload and one download message, so total count should be 2. | |
61 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 2); | |
62 | |
63 scoped_ptr<net::URLRequest> request2(context_.CreateRequest( | |
64 GURL("http://foo.com"), net::DEFAULT_PRIORITY, &test_delegate_)); | |
65 content::ResourceRequestInfo::AllocateForTesting( | |
66 request2.get(), content::RESOURCE_TYPE_MAIN_FRAME, NULL, | |
67 -2, // render_process_id | |
68 -2, -2, true, false, true, true); | |
bengr
2015/09/01 20:16:01
What are these -2's and other parameters? I.e., wh
amohammadkhan
2015/09/01 23:02:43
I was using negative process id to prevent an erro
| |
69 request2->Start(); | |
70 loop_.Run(); | |
71 data_use_measurement_.ReportDataUseUMA(request2.get()); | |
72 histogram_tester.ExpectTotalCount( | |
73 "DataUse.User.Downstream." + target_dimension + connection_type_, 1); | |
74 histogram_tester.ExpectTotalCount( | |
75 "DataUse.User.Upstream." + target_dimension + connection_type_, 1); | |
76 } | |
77 | |
78 void UpdateConnectionType() { | |
79 if (net::NetworkChangeNotifier::IsConnectionCellular( | |
80 net::NetworkChangeNotifier::GetConnectionType())) { | |
81 connection_type_ = "Cellular"; | |
82 } else { | |
83 connection_type_ = "NotCellular"; | |
84 } | |
85 } | |
86 | |
87 base::MessageLoopForIO loop_; | |
88 net::TestURLRequestContext context_; | |
89 net::TestNetworkDelegate test_network_delegate_; | |
90 DataUseMeasurement data_use_measurement_; | |
91 net::TestDelegate test_delegate_; | |
92 std::string connection_type_; | |
93 net::MockClientSocketFactory socket_factory_; | |
94 }; | |
95 | |
96 // This test function tests recording of data use information in UMA histogram | |
97 // when packet is originated from user or services. | |
98 // TODO(amohammadkhan): Add tests for Cellular/non-cellular connection types | |
99 // when support for testing is provided in its class. | |
100 TEST_F(DataUseMeasurementTest, UserNotUserTest) { | |
101 #if defined(OS_ANDROID) | |
102 data_use_measurement_.OnApplicationStateChangeForTesting( | |
103 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES); | |
104 #endif | |
105 UpdateConnectionType(); | |
106 TestForAUserAndAServicePacket("Foreground."); | |
107 } | |
108 | |
109 #if defined(OS_ANDROID) | |
110 TEST_F(DataUseMeasurementTest, ApplicationStateTest) { | |
111 #if defined(OS_ANDROID) | |
112 data_use_measurement_.OnApplicationStateChangeForTesting( | |
113 base::android::APPLICATION_STATE_HAS_STOPPED_ACTIVITIES); | |
114 #endif | |
115 UpdateConnectionType(); | |
116 TestForAUserAndAServicePacket("Background."); | |
117 } | |
118 #endif | |
119 | |
120 } // namespace data_use_measurement | |
OLD | NEW |