Chromium Code Reviews| Index: chrome/browser/net/chrome_network_delegate_unittest.cc |
| diff --git a/chrome/browser/net/chrome_network_delegate_unittest.cc b/chrome/browser/net/chrome_network_delegate_unittest.cc |
| index 3ca7bbe09a078acaab591f43fdcdc47c065fa020..46ae264f7bffa6298aa68a53ebbe0fbc925b21ad 100644 |
| --- a/chrome/browser/net/chrome_network_delegate_unittest.cc |
| +++ b/chrome/browser/net/chrome_network_delegate_unittest.cc |
| @@ -9,6 +9,7 @@ |
| #include "base/memory/scoped_ptr.h" |
| #include "base/message_loop/message_loop.h" |
| #include "base/prefs/pref_member.h" |
| +#include "base/test/histogram_tester.h" |
| #include "chrome/browser/content_settings/cookie_settings_factory.h" |
| #include "chrome/browser/net/safe_search_util.h" |
| #include "chrome/common/pref_names.h" |
| @@ -16,7 +17,10 @@ |
| #include "chrome/test/base/testing_profile.h" |
| #include "components/content_settings/core/browser/cookie_settings.h" |
| #include "components/content_settings/core/common/pref_names.h" |
| +#include "components/data_use_measurement/core/data_use_user_data.h" |
| +#include "content/public/browser/resource_request_info.h" |
| #include "content/public/common/content_switches.h" |
| +#include "content/public/common/resource_type.h" |
| #include "content/public/test/test_browser_thread_bundle.h" |
| #include "net/base/request_priority.h" |
| #include "net/url_request/url_request.h" |
| @@ -152,6 +156,104 @@ TEST_F(ChromeNetworkDelegateSafeSearchTest, SafeSearch) { |
| } |
| } |
| +// This class tests recoding data use of user and services in histograms. |
| +class ChromeNetworkDelegateDataUseMeasurementTest : public testing::Test { |
| + public: |
| + ChromeNetworkDelegateDataUseMeasurementTest() |
| + : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
| +#if defined(ENABLE_EXTENSIONS) |
| + forwarder_ = new extensions::EventRouterForwarder(); |
| +#endif |
| + } |
| + |
| + void SetUp() override { |
| + ChromeNetworkDelegate::InitializePrefsOnUIThread( |
| + &enable_referrers_, NULL, NULL, NULL, profile_.GetTestingPrefService()); |
| + } |
| + |
| + protected: |
| + scoped_ptr<net::NetworkDelegate> CreateNetworkDelegate() { |
| + scoped_ptr<ChromeNetworkDelegate> network_delegate( |
| + new ChromeNetworkDelegate(forwarder(), &enable_referrers_)); |
| + return network_delegate.Pass(); |
| + } |
| + |
| + void SetDelegate(net::NetworkDelegate* delegate) { |
| + network_delegate_ = delegate; |
| + context_.set_network_delegate(network_delegate_); |
| + } |
| + |
| + // 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.
|
| + // a ResourceRequestInfo to the URLRequest, because requests from users have |
| + // this info. If |from_user| is false, the request assumed to be from one of |
| + // the services so it attaches a DataUseUserData. ServiceName is used as the |
| + // name of the service owning this request. |
| + void QueryURLDataUse(bool from_user) { |
| + scoped_ptr<net::URLRequest> request(context_.CreateRequest( |
| + GURL("http://example.com"), net::DEFAULT_PRIORITY, &delegate_)); |
| + if (from_user) { |
| + content::ResourceRequestInfo::AllocateForTesting( |
| + request.get(), content::RESOURCE_TYPE_MAIN_FRAME, NULL, |
| + -2, -2, -2, true, false, true, true); |
| + } else { |
| + request->SetUserData( |
| + data_use_measurement::DataUseUserData::kUserDataKey, |
| + new data_use_measurement::DataUseUserData( |
| + data_use_measurement::DataUseUserData::SUGGESTIONS)); |
| + } |
| + request->Start(); |
| + base::MessageLoop::current()->RunUntilIdle(); |
| + } |
| + |
| + private: |
| + extensions::EventRouterForwarder* forwarder() { |
| +#if defined(ENABLE_EXTENSIONS) |
| + return forwarder_.get(); |
| +#else |
| + return NULL; |
| +#endif |
| + } |
| + |
| + content::TestBrowserThreadBundle thread_bundle_; |
| +#if defined(ENABLE_EXTENSIONS) |
| + scoped_refptr<extensions::EventRouterForwarder> forwarder_; |
| +#endif |
| + TestingProfile profile_; |
| + BooleanPrefMember enable_referrers_; |
| + scoped_ptr<net::URLRequest> request_; |
| + net::TestURLRequestContext context_; |
| + net::NetworkDelegate* network_delegate_; |
| + net::TestDelegate delegate_; |
| +}; |
| + |
| +// This function tests data use measurement for different types of requests by |
| +// making two queries. First query is similar to a query from a service, so it |
| +// should affect DataUse.NotUser.Dimensions and DataUse.MessageSize.ServiceName |
| +// histograms. The second query is similar to a query from a user, so it should |
| +// affect DataUse.User.Dimensions. AppState and ConnectionType dimensions are |
| +// always Foreground and NotCellular respectively. |
| +TEST_F(ChromeNetworkDelegateDataUseMeasurementTest, MainTest) { |
| + scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate()); |
| + SetDelegate(delegate.get()); |
| + base::HistogramTester histogram_tester; |
| + |
| + QueryURLDataUse(0); // A query from a service |
| + histogram_tester.ExpectTotalCount( |
| + "DataUse.NotUser.Downstream.Foreground.NotCellular", 1); |
| + histogram_tester.ExpectTotalCount( |
| + "DataUse.NotUser.Upstream.Foreground.NotCellular", 1); |
| + // One upload and one download message, so totalCount shoudl be 2. |
| + histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 2); |
| + // The upload nad download messages are empty, so their size should be 0. |
| + histogram_tester.ExpectUniqueSample("DataUse.MessageSize.Suggestions", 0, 2); |
| + |
| + QueryURLDataUse(1); // A query from user |
| + histogram_tester.ExpectTotalCount( |
| + "DataUse.User.Downstream.Foreground.NotCellular", 1); |
| + histogram_tester.ExpectTotalCount( |
| + "DataUse.User.Upstream.Foreground.NotCellular", 1); |
| +} |
| + |
| // Privacy Mode disables Channel Id if cookies are blocked (cr223191) |
| class ChromeNetworkDelegatePrivacyModeTest : public testing::Test { |
| public: |