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

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: Addressing blundell's comments. 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 {
mmenke 2015/09/02 21:48:09 This test fixture doesn't really seem to have anyt
amohammadkhan 2015/09/03 04:31:18 Done.
161 public:
162 ChromeNetworkDelegateDataUseMeasurementTest()
163 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
mmenke 2015/09/02 21:48:09 context_(true) { // Delay initialization context
amohammadkhan 2015/09/03 04:31:18 Done.
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_);
mmenke 2015/09/02 21:48:09 The NetworkDelegate is being destroyed before the
amohammadkhan 2015/09/03 04:31:18 I agree. I used a similar approach to other classe
184 }
185
186 // This function queries a URLRequest. If |from_user| is true, it attaches a
187 // ResourceRequestInfo to the URLRequest, because requests from users have
188 // this info. If |from_user| is false, the request is presumed to be from a
189 // service, and the service name is set in the request's user data.
190 void QueryURLDataUse(bool from_user) {
mmenke 2015/09/02 21:48:09 This only really depends on the context. I'd sugg
amohammadkhan 2015/09/03 04:31:18 Done.
191 scoped_ptr<net::URLRequest> request(context_.CreateRequest(
192 GURL("http://example.com"), net::DEFAULT_PRIORITY, &delegate_));
193 if (from_user) {
194 content::ResourceRequestInfo::AllocateForTesting(
195 request.get(), content::RESOURCE_TYPE_MAIN_FRAME, NULL, -2, -2, -2,
196 true, false, true, true);
197 } else {
198 request->SetUserData(
199 data_use_measurement::DataUseUserData::kUserDataKey,
200 new data_use_measurement::DataUseUserData(
201 data_use_measurement::DataUseUserData::SUGGESTIONS));
202 }
203 request->Start();
204 base::MessageLoop::current()->RunUntilIdle();
mmenke 2015/09/02 21:48:09 I think it's much more robust to do: // Use a new
amohammadkhan 2015/09/03 04:31:18 Done.
205 }
206
207 private:
208 extensions::EventRouterForwarder* forwarder() {
209 #if defined(ENABLE_EXTENSIONS)
210 return forwarder_.get();
211 #else
212 return NULL;
Lei Zhang 2015/09/02 18:14:43 nit: nullptr, ditto elsewhere.
amohammadkhan 2015/09/03 04:31:18 Done.
213 #endif
214 }
215
216 content::TestBrowserThreadBundle thread_bundle_;
217 #if defined(ENABLE_EXTENSIONS)
218 scoped_refptr<extensions::EventRouterForwarder> forwarder_;
219 #endif
220 TestingProfile profile_;
221 BooleanPrefMember enable_referrers_;
222 scoped_ptr<net::URLRequest> request_;
223 net::TestURLRequestContext context_;
224 net::NetworkDelegate* network_delegate_;
mmenke 2015/09/02 21:48:09 Can't this just be: net::NetworkDelegate network_
amohammadkhan 2015/09/03 04:31:18 I think it can't, because we are making a new Netw
225 net::TestDelegate delegate_;
226 };
227
228 // This function tests data use measurement for different types of requests by
229 // making two queries. First query is similar to a query from a service, so it
230 // should affect DataUse.TrafficSize.System.Dimensions and
231 // DataUse.MessageSize.ServiceName histograms. The second query is similar to a
232 // query from a user, so it should affect DataUse.TrafficSize.User.Dimensions.
233 // AppState and ConnectionType dimensions are always Foreground and NotCellular
234 // respectively.
235 TEST_F(ChromeNetworkDelegateDataUseMeasurementTest, MainTest) {
mmenke 2015/09/02 21:48:09 I'd suggest two different tests.
amohammadkhan 2015/09/03 04:31:18 Done.
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.TrafficSize.System.Downstream.Foreground.NotCellular", 1);
243 histogram_tester.ExpectTotalCount(
244 "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 1);
245 // One upload and one download message, so totalCount shoudl be 2.
mmenke 2015/09/02 21:48:09 nit: Should
mmenke 2015/09/02 21:48:09 Check that the user counts are 0?
amohammadkhan 2015/09/03 04:31:19 Done.
amohammadkhan 2015/09/03 04:31:19 Done.
246 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 2);
247
248 QueryURLDataUse(1); // A query from user
249 histogram_tester.ExpectTotalCount(
250 "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 1);
251 histogram_tester.ExpectTotalCount(
252 "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 1);
mmenke 2015/09/02 21:48:09 I suggest querying the exact bucket we expect, and
amohammadkhan 2015/09/03 04:31:18 The problem is that if I specify an exact size for
253 }
254
155 // Privacy Mode disables Channel Id if cookies are blocked (cr223191) 255 // Privacy Mode disables Channel Id if cookies are blocked (cr223191)
156 class ChromeNetworkDelegatePrivacyModeTest : public testing::Test { 256 class ChromeNetworkDelegatePrivacyModeTest : public testing::Test {
157 public: 257 public:
158 ChromeNetworkDelegatePrivacyModeTest() 258 ChromeNetworkDelegatePrivacyModeTest()
159 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), 259 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
160 #if defined(ENABLE_EXTENSIONS) 260 #if defined(ENABLE_EXTENSIONS)
161 forwarder_(new extensions::EventRouterForwarder()), 261 forwarder_(new extensions::EventRouterForwarder()),
162 #endif 262 #endif
163 cookie_settings_(CookieSettingsFactory::GetForProfile(&profile_).get()), 263 cookie_settings_(CookieSettingsFactory::GetForProfile(&profile_).get()),
164 kBlockedSite("http://ads.thirdparty.com"), 264 kBlockedSite("http://ads.thirdparty.com"),
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 kBlockedFirstPartySite)); 361 kBlockedFirstPartySite));
262 362
263 cookie_settings_->SetCookieSetting( 363 cookie_settings_->SetCookieSetting(
264 ContentSettingsPattern::FromURL(kBlockedFirstPartySite), 364 ContentSettingsPattern::FromURL(kBlockedFirstPartySite),
265 ContentSettingsPattern::Wildcard(), 365 ContentSettingsPattern::Wildcard(),
266 CONTENT_SETTING_BLOCK); 366 CONTENT_SETTING_BLOCK);
267 // Privacy mode is disabled as kAllowedSite is still getting cookies 367 // Privacy mode is disabled as kAllowedSite is still getting cookies
268 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite, 368 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite,
269 kBlockedFirstPartySite)); 369 kBlockedFirstPartySite));
270 } 370 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698