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

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 reviewers' 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/run_loop.h"
13 #include "base/test/histogram_tester.h"
12 #include "chrome/browser/content_settings/cookie_settings_factory.h" 14 #include "chrome/browser/content_settings/cookie_settings_factory.h"
13 #include "chrome/browser/net/safe_search_util.h" 15 #include "chrome/browser/net/safe_search_util.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 "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
30 TEST(ChromeNetworkDelegateTest, DisableFirstPartyOnlyCookiesIffFlagDisabled) { 34 #if !defined(OS_IOS)
31 BooleanPrefMember pref_member_; 35 #include "components/data_use_measurement/core/data_use_user_data.h"
32 scoped_ptr<ChromeNetworkDelegate> delegate; 36 #endif
33 37
34 #if defined(ENABLE_EXTENSIONS) 38 namespace {
35 scoped_refptr<extensions::EventRouterForwarder> forwarder = 39
36 new extensions::EventRouterForwarder(); 40 // This function queries a URLRequest. If |from_user| is true, it attaches a
37 delegate.reset(new ChromeNetworkDelegate(forwarder.get(), &pref_member_)); 41 // ResourceRequestInfo to the URLRequest, because requests from users have
38 #else 42 // this info. If |from_user| is false, the request is presumed to be from a
39 delegate.reset(new ChromeNetworkDelegate(nullptr, &pref_member_)); 43 // service, and the service name is set in the request's user data.
40 #endif 44 void QueryURLDataUse(const net::URLRequestContext& context, bool from_user) {
mmenke 2015/09/03 16:02:58 Suggest QueryURLDataUse -> RequestURL. I've never
amohammadkhan 2015/09/03 23:10:36 Done.
41 EXPECT_FALSE(delegate->FirstPartyOnlyCookieExperimentEnabled()); 45 net::TestDelegate test_delegate;
46 test_delegate.set_quit_on_complete(true);
47
48 scoped_ptr<net::URLRequest> request(context.CreateRequest(
49 GURL("http://example.com"), net::DEFAULT_PRIORITY, &test_delegate));
mmenke 2015/09/03 16:02:59 What are we actually requesting a URL from, in thi
amohammadkhan 2015/09/03 23:10:36 Done.
50 if (from_user) {
51 content::ResourceRequestInfo::AllocateForTesting(
52 request.get(), content::RESOURCE_TYPE_MAIN_FRAME, NULL, -2, -2, -2,
mmenke 2015/09/03 16:02:58 nullptr
amohammadkhan 2015/09/03 23:10:36 Done.
53 true, false, true, true);
54 } else {
55 request->SetUserData(
56 data_use_measurement::DataUseUserData::kUserDataKey,
57 new data_use_measurement::DataUseUserData(
58 data_use_measurement::DataUseUserData::SUGGESTIONS));
59 }
60 request->Start();
61 base::RunLoop().Run();
42 } 62 }
43 63
44 TEST(ChromeNetworkDelegateTest, EnableFirstPartyOnlyCookiesIffFlagEnabled) { 64 } // namespace
65
66 class ChromeNetworkDelegateTest : public testing::Test {
67 public:
68 ChromeNetworkDelegateTest()
69 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
70 context_(true) {
71 #if defined(ENABLE_EXTENSIONS)
72 forwarder_ = new extensions::EventRouterForwarder();
73 #endif
74 }
75
76 void SetUp() override {
77 ChromeNetworkDelegate::InitializePrefsOnUIThread(
78 &enable_referrers_, NULL, NULL, NULL, profile_.GetTestingPrefService());
mmenke 2015/09/03 16:02:59 nullptr
amohammadkhan 2015/09/03 23:10:36 Done.
79 }
80
81 protected:
82 scoped_ptr<net::NetworkDelegate> CreateNetworkDelegate() {
83 scoped_ptr<ChromeNetworkDelegate> network_delegate(
84 new ChromeNetworkDelegate(forwarder(), &enable_referrers_));
85 return network_delegate.Pass();
86 }
87
88 void SetDelegate(net::NetworkDelegate* delegate) {
89 network_delegate_ = delegate;
90 context_.set_network_delegate(network_delegate_);
91 context_.Init();
mmenke 2015/09/03 16:02:59 Each test is run in a separate copy of the test fi
amohammadkhan 2015/09/03 23:10:36 I tried it. It didn't work. One of the tests fails
92 }
93
94 extensions::EventRouterForwarder* forwarder() {
95 #if defined(ENABLE_EXTENSIONS)
96 return forwarder_.get();
97 #else
98 return nullptr;
99 #endif
100 }
101
102 private:
103 content::TestBrowserThreadBundle thread_bundle_;
104
105 protected:
106 net::TestURLRequestContext context_;
107 net::NetworkDelegate* network_delegate_;
108
109 private:
mmenke 2015/09/03 16:02:59 Shouldn't have more than one private section. Sug
amohammadkhan 2015/09/03 23:10:36 Done.
110 #if defined(ENABLE_EXTENSIONS)
111 scoped_refptr<extensions::EventRouterForwarder> forwarder_;
112 #endif
113 TestingProfile profile_;
114 BooleanPrefMember enable_referrers_;
115 };
116
117 // This function tests data use measurement for requests by services. it makes a
118 // query which is similar to a query of a service, so it should affect
119 // DataUse.TrafficSize.System.Dimensions and DataUse.MessageSize.ServiceName
120 // histograms. AppState and ConnectionType dimensions are always Foreground and
121 // NotCellular respectively.
122 #if !defined(OS_IOS)
123 TEST_F(ChromeNetworkDelegateTest, DataUseMeasurementServiceTest) {
124 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate());
125 SetDelegate(delegate.get());
126 base::HistogramTester histogram_tester;
127
128 QueryURLDataUse(context_, 0); // A query from a service
129 histogram_tester.ExpectTotalCount(
130 "DataUse.TrafficSize.System.Downstream.Foreground.NotCellular", 1);
131 histogram_tester.ExpectTotalCount(
132 "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 1);
133 // One upload and one download message, so totalCount should be 2.
134 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 2);
135 histogram_tester.ExpectTotalCount(
136 "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 0);
137 histogram_tester.ExpectTotalCount(
138 "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 0);
139 }
140
141 // This function tests data use measurement for requests by user.The query from
142 // a user should affect DataUse.TrafficSize.User.Dimensions histogram. AppState
143 // and ConnectionType dimensions are always Foreground and NotCellular
144 // respectively.
145 TEST_F(ChromeNetworkDelegateTest, DataUseMeasurementUserTest) {
146 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate());
147 SetDelegate(delegate.get());
148 base::HistogramTester histogram_tester;
149
150 QueryURLDataUse(context_, 1); // A query from user
151 histogram_tester.ExpectTotalCount(
152 "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 1);
153 histogram_tester.ExpectTotalCount(
154 "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 1);
155 histogram_tester.ExpectTotalCount(
156 "DataUse.TrafficSize.System.Downstream.Foreground.NotCellular", 0);
157 histogram_tester.ExpectTotalCount(
158 "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 0);
159 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 0);
160 }
161
162 #endif
163
164 TEST_F(ChromeNetworkDelegateTest, DisableFirstPartyOnlyCookiesIffFlagDisabled) {
165 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate());
166 SetDelegate(delegate.get());
167 EXPECT_FALSE(network_delegate_->FirstPartyOnlyCookieExperimentEnabled());
168 }
169
170 TEST_F(ChromeNetworkDelegateTest, EnableFirstPartyOnlyCookiesIffFlagEnabled) {
45 base::CommandLine::ForCurrentProcess()->AppendSwitch( 171 base::CommandLine::ForCurrentProcess()->AppendSwitch(
46 switches::kEnableExperimentalWebPlatformFeatures); 172 switches::kEnableExperimentalWebPlatformFeatures);
47 BooleanPrefMember pref_member_; 173 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate());
48 scoped_ptr<ChromeNetworkDelegate> delegate; 174 SetDelegate(delegate.get());
49
50 #if defined(ENABLE_EXTENSIONS)
51 scoped_refptr<extensions::EventRouterForwarder> forwarder =
52 new extensions::EventRouterForwarder();
53 delegate.reset(new ChromeNetworkDelegate(forwarder.get(), &pref_member_));
54 #else
55 delegate.reset(new ChromeNetworkDelegate(nullptr, &pref_member_));
56 #endif
57 EXPECT_TRUE(delegate->FirstPartyOnlyCookieExperimentEnabled()); 175 EXPECT_TRUE(delegate->FirstPartyOnlyCookieExperimentEnabled());
58 } 176 }
59 177
60 class ChromeNetworkDelegateSafeSearchTest : public testing::Test { 178 class ChromeNetworkDelegateSafeSearchTest : public testing::Test {
61 public: 179 public:
62 ChromeNetworkDelegateSafeSearchTest() 180 ChromeNetworkDelegateSafeSearchTest()
63 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { 181 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
64 #if defined(ENABLE_EXTENSIONS) 182 #if defined(ENABLE_EXTENSIONS)
65 forwarder_ = new extensions::EventRouterForwarder(); 183 forwarder_ = new extensions::EventRouterForwarder();
66 #endif 184 #endif
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 kBlockedFirstPartySite)); 379 kBlockedFirstPartySite));
262 380
263 cookie_settings_->SetCookieSetting( 381 cookie_settings_->SetCookieSetting(
264 ContentSettingsPattern::FromURL(kBlockedFirstPartySite), 382 ContentSettingsPattern::FromURL(kBlockedFirstPartySite),
265 ContentSettingsPattern::Wildcard(), 383 ContentSettingsPattern::Wildcard(),
266 CONTENT_SETTING_BLOCK); 384 CONTENT_SETTING_BLOCK);
267 // Privacy mode is disabled as kAllowedSite is still getting cookies 385 // Privacy mode is disabled as kAllowedSite is still getting cookies
268 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite, 386 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite,
269 kBlockedFirstPartySite)); 387 kBlockedFirstPartySite));
270 } 388 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698