OLD | NEW |
---|---|
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/content/data_use_user_data.h" | |
sclittle
2015/08/26 23:08:29
Isn't data_use_user_data under "core/"?
amohammadkhan
2015/08/29 01:00:59
Done.
| |
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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 | 112 |
109 request->Start(); | 113 request->Start(); |
110 base::MessageLoop::current()->RunUntilIdle(); | 114 base::MessageLoop::current()->RunUntilIdle(); |
111 | 115 |
112 EXPECT_EQ(expect_google_safe_search ? 1 : 0, | 116 EXPECT_EQ(expect_google_safe_search ? 1 : 0, |
113 safe_search_util::GetForceGoogleSafeSearchCountForTesting()); | 117 safe_search_util::GetForceGoogleSafeSearchCountForTesting()); |
114 EXPECT_EQ(expect_youtube_safety_mode ? 1 : 0, | 118 EXPECT_EQ(expect_youtube_safety_mode ? 1 : 0, |
115 safe_search_util::GetForceYouTubeSafetyModeCountForTesting()); | 119 safe_search_util::GetForceYouTubeSafetyModeCountForTesting()); |
116 } | 120 } |
117 | 121 |
122 // This function queries a URLRequest. If |from_user| is true, so it attaches | |
123 // a ResourceRequestInfo to the URLRequest, because requests from users have | |
124 // this info. If |from_user| is false, the request assumed to be from one of | |
125 // the services so it attaches a DataUseUserData. ServiceName is used as the | |
126 // name of the service owning this request. | |
127 void QueryURLDataUse(bool from_user) { | |
128 scoped_ptr<net::URLRequest> request(context_.CreateRequest( | |
129 GURL("http://example.com"), net::DEFAULT_PRIORITY, &delegate_)); | |
130 // The render_process_id should be negative, all the other parameters are | |
131 // arbitrary. | |
132 if (from_user) { | |
133 content::ResourceRequestInfo::AllocateForTesting( | |
134 request.get(), content::RESOURCE_TYPE_MAIN_FRAME, NULL, | |
135 -2, // render_process_id | |
136 -2, -2, true, false, true, true); | |
137 } else { | |
138 request->SetUserData( | |
139 measurement_component::DataUseUserData::kUserDataKey, | |
sclittle
2015/08/26 23:08:29
That namespace looks wrong, "measurement_component
amohammadkhan
2015/08/29 01:00:59
Done.
| |
140 new measurement_component::DataUseUserData("ServiceName")); | |
141 } | |
142 request->Start(); | |
143 base::MessageLoop::current()->RunUntilIdle(); | |
144 } | |
145 | |
118 private: | 146 private: |
119 extensions::EventRouterForwarder* forwarder() { | 147 extensions::EventRouterForwarder* forwarder() { |
120 #if defined(ENABLE_EXTENSIONS) | 148 #if defined(ENABLE_EXTENSIONS) |
121 return forwarder_.get(); | 149 return forwarder_.get(); |
122 #else | 150 #else |
123 return NULL; | 151 return NULL; |
124 #endif | 152 #endif |
125 } | 153 } |
126 | 154 |
127 content::TestBrowserThreadBundle thread_bundle_; | 155 content::TestBrowserThreadBundle thread_bundle_; |
(...skipping 17 matching lines...) Expand all Loading... | |
145 // Loop over all combinations of the two policies. | 173 // Loop over all combinations of the two policies. |
146 for (int i = 0; i < 4; i++) { | 174 for (int i = 0; i < 4; i++) { |
147 bool google_safe_search = i % 2; | 175 bool google_safe_search = i % 2; |
148 bool youtube_safety_mode = i / 2; | 176 bool youtube_safety_mode = i / 2; |
149 SetSafeSearch(google_safe_search, youtube_safety_mode); | 177 SetSafeSearch(google_safe_search, youtube_safety_mode); |
150 | 178 |
151 QueryURL(google_safe_search, youtube_safety_mode); | 179 QueryURL(google_safe_search, youtube_safety_mode); |
152 } | 180 } |
153 } | 181 } |
154 | 182 |
183 // This function tests data use measurement for different types of requests by | |
184 // making two queries. First query is similar to a query from a service, so it | |
185 // should affect DataUse.NotUser.Dimensions and DataUse.service.ServiceName | |
186 // histograms. The second query is similar to a query from a user, so it should | |
187 // affect DataUse.User.Dimensions. AppState and ConnectionType dimensions are | |
188 // always Foreground and NotCellular respectively. Because Background dimension | |
189 // is only defined for android platform and when Chrome is in background and | |
190 // Cellular ConnectionType is only exist on mobile devices. | |
191 TEST_F(ChromeNetworkDelegateSafeSearchTest, DataUseMeasurement) { | |
192 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate()); | |
193 SetDelegate(delegate.get()); | |
194 | |
195 base::HistogramTester histogram_tester; | |
196 | |
197 QueryURLDataUse(0); // A query from a service | |
198 histogram_tester.ExpectTotalCount( | |
199 "DataUse.NotUser.Download.Foreground.NotCellular", 1); | |
200 histogram_tester.ExpectTotalCount( | |
201 "DataUse.NotUser.Upload.Foreground.NotCellular", 1); | |
202 // One upload and one download message, so totalCount shoudl be 2. | |
203 histogram_tester.ExpectTotalCount("DataUse.Service.ServiceName", 2); | |
204 // The upload nad download messages are empty, so their size should be 0. | |
205 histogram_tester.ExpectUniqueSample("DataUse.Service.ServiceName", 0, 2); | |
206 | |
207 QueryURLDataUse(1); // A query from user | |
208 histogram_tester.ExpectTotalCount( | |
209 "DataUse.User.Download.Foreground.NotCellular", 1); | |
210 histogram_tester.ExpectTotalCount( | |
211 "DataUse.User.Upload.Foreground.NotCellular", 1); | |
212 } | |
213 | |
155 // Privacy Mode disables Channel Id if cookies are blocked (cr223191) | 214 // Privacy Mode disables Channel Id if cookies are blocked (cr223191) |
156 class ChromeNetworkDelegatePrivacyModeTest : public testing::Test { | 215 class ChromeNetworkDelegatePrivacyModeTest : public testing::Test { |
157 public: | 216 public: |
158 ChromeNetworkDelegatePrivacyModeTest() | 217 ChromeNetworkDelegatePrivacyModeTest() |
159 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 218 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
160 #if defined(ENABLE_EXTENSIONS) | 219 #if defined(ENABLE_EXTENSIONS) |
161 forwarder_(new extensions::EventRouterForwarder()), | 220 forwarder_(new extensions::EventRouterForwarder()), |
162 #endif | 221 #endif |
163 cookie_settings_(CookieSettingsFactory::GetForProfile(&profile_).get()), | 222 cookie_settings_(CookieSettingsFactory::GetForProfile(&profile_).get()), |
164 kBlockedSite("http://ads.thirdparty.com"), | 223 kBlockedSite("http://ads.thirdparty.com"), |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
261 kBlockedFirstPartySite)); | 320 kBlockedFirstPartySite)); |
262 | 321 |
263 cookie_settings_->SetCookieSetting( | 322 cookie_settings_->SetCookieSetting( |
264 ContentSettingsPattern::FromURL(kBlockedFirstPartySite), | 323 ContentSettingsPattern::FromURL(kBlockedFirstPartySite), |
265 ContentSettingsPattern::Wildcard(), | 324 ContentSettingsPattern::Wildcard(), |
266 CONTENT_SETTING_BLOCK); | 325 CONTENT_SETTING_BLOCK); |
267 // Privacy mode is disabled as kAllowedSite is still getting cookies | 326 // Privacy mode is disabled as kAllowedSite is still getting cookies |
268 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite, | 327 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite, |
269 kBlockedFirstPartySite)); | 328 kBlockedFirstPartySite)); |
270 } | 329 } |
OLD | NEW |