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

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: Adding sclittle to OWNERs and little fix in ChromeNetworkDelegate test. 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 "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.Download." + target_dimension + connection_type_, 1);
58 histogram_tester.ExpectTotalCount(
59 "DataUse.NotUser.Upload." + target_dimension + connection_type_, 1);
60 // One upload and one download message, so total count should be 2.
61 histogram_tester.ExpectTotalCount("DataUse.Service.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);
69 request2->Start();
70 loop_.Run();
71 data_use_measurement_.ReportDataUseUMA(request2.get());
72 histogram_tester.ExpectTotalCount(
73 "DataUse.User.Download." + target_dimension + connection_type_, 1);
74 histogram_tester.ExpectTotalCount(
75 "DataUse.User.Upload." + target_dimension + connection_type_, 1);
76 }
77
78 void UpdateConnectionType() {
79 if (net::NetworkChangeNotifier::IsConnectionCellular(
80 net::NetworkChangeNotifier::GetConnectionType()))
Alexei Svitkine (slow) 2015/08/31 17:26:31 Nit: {}'s
amohammadkhan 2015/09/01 04:52:49 Done.
81 connection_type_ = "Cellular";
82 else
83 connection_type_ = "NotCellular";
84 }
85
86 base::MessageLoopForIO loop_;
87 net::TestURLRequestContext context_;
88 net::TestNetworkDelegate test_network_delegate_;
89 DataUseMeasurement data_use_measurement_;
90 net::TestDelegate test_delegate_;
91 std::string connection_type_;
92 net::MockClientSocketFactory socket_factory_;
93 };
94
95 // This test function tests recording of data use information in UMA histogram
96 // when packet is originated from user or services.
97 // TODO(amohammadkhan): Add tests for Cellular/non-cellular connection types
98 // when support for testing is provided in its class.
99 TEST_F(DataUseMeasurementTest, UserNotUserTest) {
100 data_use_measurement_.OnApplicationStateChange(
101 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES);
102 UpdateConnectionType();
103 TestForAUserAndAServicePacket("Foreground.");
104 }
105
106 #if defined(OS_ANDROID)
107 TEST_F(DataUseMeasurementTest, ApplicationStateTest) {
108 data_use_measurement_.OnApplicationStateChange(
109 base::android::APPLICATION_STATE_HAS_STOPPED_ACTIVITIES);
110 UpdateConnectionType();
111 TestForAUserAndAServicePacket("Background.");
112 }
113 #endif
114
115 } // namespace data_use_measurement
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698