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

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

Powered by Google App Engine
This is Rietveld 408576698