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

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