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

Side by Side Diff: chrome/browser/net/chrome_network_delegate_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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/net/chrome_network_delegate.h" 5 #include "chrome/browser/net/chrome_network_delegate.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/memory/ref_counted.h" 8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
11 #include "base/prefs/pref_member.h" 11 #include "base/prefs/pref_member.h"
12 #include "base/test/histogram_tester.h"
12 #include "chrome/browser/content_settings/cookie_settings_factory.h" 13 #include "chrome/browser/content_settings/cookie_settings_factory.h"
13 #include "chrome/browser/net/safe_search_util.h" 14 #include "chrome/browser/net/safe_search_util.h"
15 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/common/pref_names.h" 16 #include "chrome/common/pref_names.h"
15 #include "chrome/test/base/testing_pref_service_syncable.h" 17 #include "chrome/test/base/testing_pref_service_syncable.h"
16 #include "chrome/test/base/testing_profile.h" 18 #include "chrome/test/base/testing_profile.h"
17 #include "components/content_settings/core/browser/cookie_settings.h" 19 #include "components/content_settings/core/browser/cookie_settings.h"
18 #include "components/content_settings/core/common/pref_names.h" 20 #include "components/content_settings/core/common/pref_names.h"
21 #include "components/data_use_measurement/core/data_use_user_data.h"
22 #include "content/public/browser/resource_request_info.h"
19 #include "content/public/common/content_switches.h" 23 #include "content/public/common/content_switches.h"
24 #include "content/public/common/resource_type.h"
20 #include "content/public/test/test_browser_thread_bundle.h" 25 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "net/base/request_priority.h" 26 #include "net/base/request_priority.h"
22 #include "net/url_request/url_request.h" 27 #include "net/url_request/url_request.h"
23 #include "net/url_request/url_request_test_util.h" 28 #include "net/url_request/url_request_test_util.h"
24 #include "testing/gtest/include/gtest/gtest.h" 29 #include "testing/gtest/include/gtest/gtest.h"
25 30
26 #if defined(ENABLE_EXTENSIONS) 31 #if defined(ENABLE_EXTENSIONS)
27 #include "chrome/browser/extensions/event_router_forwarder.h" 32 #include "chrome/browser/extensions/event_router_forwarder.h"
28 #endif 33 #endif
29 34
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 // Loop over all combinations of the two policies. 150 // Loop over all combinations of the two policies.
146 for (int i = 0; i < 4; i++) { 151 for (int i = 0; i < 4; i++) {
147 bool google_safe_search = i % 2; 152 bool google_safe_search = i % 2;
148 bool youtube_safety_mode = i / 2; 153 bool youtube_safety_mode = i / 2;
149 SetSafeSearch(google_safe_search, youtube_safety_mode); 154 SetSafeSearch(google_safe_search, youtube_safety_mode);
150 155
151 QueryURL(google_safe_search, youtube_safety_mode); 156 QueryURL(google_safe_search, youtube_safety_mode);
152 } 157 }
153 } 158 }
154 159
160 // This class tests recoding data use of user and services in histograms.
161 class ChromeNetworkDelegateDataUseMeasurementTest : public testing::Test {
162 public:
163 ChromeNetworkDelegateDataUseMeasurementTest()
164 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
165 #if defined(ENABLE_EXTENSIONS)
166 forwarder_ = new extensions::EventRouterForwarder();
167 #endif
168 }
169
170 void SetUp() override {
171 ChromeNetworkDelegate::InitializePrefsOnUIThread(
172 &enable_referrers_, NULL, NULL, NULL, profile_.GetTestingPrefService());
173 }
174
175 protected:
176 scoped_ptr<net::NetworkDelegate> CreateNetworkDelegate() {
177 scoped_ptr<ChromeNetworkDelegate> network_delegate(
178 new ChromeNetworkDelegate(forwarder(), &enable_referrers_));
179 return network_delegate.Pass();
180 }
181
182 void SetDelegate(net::NetworkDelegate* delegate) {
183 network_delegate_ = delegate;
184 context_.set_network_delegate(network_delegate_);
185 }
186
187 // This function queries a URLRequest. If |from_user| is true, so it attaches
188 // a ResourceRequestInfo to the URLRequest, because requests from users have
189 // this info. If |from_user| is false, the request assumed to be from one of
190 // the services so it attaches a DataUseUserData. ServiceName is used as the
191 // name of the service owning this request.
192 void QueryURLDataUse(bool from_user) {
193 scoped_ptr<net::URLRequest> request(context_.CreateRequest(
194 GURL("http://example.com"), net::DEFAULT_PRIORITY, &delegate_));
195 // The render_process_id should be negative, all the other parameters are
196 // arbitrary.
197 if (from_user) {
198 content::ResourceRequestInfo::AllocateForTesting(
199 request.get(), content::RESOURCE_TYPE_MAIN_FRAME, NULL,
200 2, // render_process_id. It should be negative for testing.
201 -2, -2, true, false, true, true);
202 } else {
203 request->SetUserData(
204 data_use_measurement::DataUseUserData::kUserDataKey,
205 new data_use_measurement::DataUseUserData(
206 data_use_measurement::DataUseUserData::SUGGESTIONS));
207 }
208 request->Start();
209 base::MessageLoop::current()->RunUntilIdle();
210 }
211
212 private:
213 extensions::EventRouterForwarder* forwarder() {
Alexei Svitkine (slow) 2015/08/31 17:26:31 If this compiles when ENABLE_EXTENSIONS is not def
amohammadkhan 2015/09/01 04:52:48 Done. (I changed it, but after running tests on t
214 #if defined(ENABLE_EXTENSIONS)
215 return forwarder_.get();
216 #else
217 return NULL;
218 #endif
219 }
220
221 content::TestBrowserThreadBundle thread_bundle_;
222 #if defined(ENABLE_EXTENSIONS)
223 scoped_refptr<extensions::EventRouterForwarder> forwarder_;
224 #endif
225 TestingProfile profile_;
226 BooleanPrefMember enable_referrers_;
227 scoped_ptr<net::URLRequest> request_;
228 net::TestURLRequestContext context_;
229 net::NetworkDelegate* network_delegate_;
230 net::TestDelegate delegate_;
231 };
232
233 // This function tests data use measurement for different types of requests by
234 // making two queries. First query is similar to a query from a service, so it
235 // should affect DataUse.NotUser.Dimensions and DataUse.service.ServiceName
236 // histograms. The second query is similar to a query from a user, so it should
237 // affect DataUse.User.Dimensions. AppState and ConnectionType dimensions are
238 // always Foreground and NotCellular respectively. Because Background dimension
239 // is only defined for android platform and when Chrome is in background and
240 // Cellular ConnectionType is only exist on mobile devices.
241 TEST_F(ChromeNetworkDelegateDataUseMeasurementTest, MainTest) {
242 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate());
243 SetDelegate(delegate.get());
244 base::HistogramTester histogram_tester;
245
246 QueryURLDataUse(0); // A query from a service
247 histogram_tester.ExpectTotalCount(
248 "DataUse.NotUser.Download.Foreground.NotCellular", 1);
249 histogram_tester.ExpectTotalCount(
250 "DataUse.NotUser.Upload.Foreground.NotCellular", 1);
251 // One upload and one download message, so totalCount shoudl be 2.
252 histogram_tester.ExpectTotalCount("DataUse.Service.Suggestions", 2);
253 // The upload nad download messages are empty, so their size should be 0.
254 histogram_tester.ExpectUniqueSample("DataUse.Service.Suggestions", 0, 2);
255
256 QueryURLDataUse(1); // A query from user
257 histogram_tester.ExpectTotalCount(
258 "DataUse.User.Download.Foreground.NotCellular", 1);
259 histogram_tester.ExpectTotalCount(
260 "DataUse.User.Upload.Foreground.NotCellular", 1);
261 }
262
155 // Privacy Mode disables Channel Id if cookies are blocked (cr223191) 263 // Privacy Mode disables Channel Id if cookies are blocked (cr223191)
156 class ChromeNetworkDelegatePrivacyModeTest : public testing::Test { 264 class ChromeNetworkDelegatePrivacyModeTest : public testing::Test {
157 public: 265 public:
158 ChromeNetworkDelegatePrivacyModeTest() 266 ChromeNetworkDelegatePrivacyModeTest()
159 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), 267 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
160 #if defined(ENABLE_EXTENSIONS) 268 #if defined(ENABLE_EXTENSIONS)
161 forwarder_(new extensions::EventRouterForwarder()), 269 forwarder_(new extensions::EventRouterForwarder()),
162 #endif 270 #endif
163 cookie_settings_(CookieSettingsFactory::GetForProfile(&profile_).get()), 271 cookie_settings_(CookieSettingsFactory::GetForProfile(&profile_).get()),
164 kBlockedSite("http://ads.thirdparty.com"), 272 kBlockedSite("http://ads.thirdparty.com"),
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 kBlockedFirstPartySite)); 369 kBlockedFirstPartySite));
262 370
263 cookie_settings_->SetCookieSetting( 371 cookie_settings_->SetCookieSetting(
264 ContentSettingsPattern::FromURL(kBlockedFirstPartySite), 372 ContentSettingsPattern::FromURL(kBlockedFirstPartySite),
265 ContentSettingsPattern::Wildcard(), 373 ContentSettingsPattern::Wildcard(),
266 CONTENT_SETTING_BLOCK); 374 CONTENT_SETTING_BLOCK);
267 // Privacy mode is disabled as kAllowedSite is still getting cookies 375 // Privacy mode is disabled as kAllowedSite is still getting cookies
268 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite, 376 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite,
269 kBlockedFirstPartySite)); 377 kBlockedFirstPartySite));
270 } 378 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698