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..d2f8b022e34aa36c3b6d515bd625e19a4f240340 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,101 @@ TEST_F(ChromeNetworkDelegateSafeSearchTest, SafeSearch) { |
} |
} |
+// This class tests recoding data use of user and services in histograms. |
+class ChromeNetworkDelegateDataUseMeasurementTest : public testing::Test { |
Alexei Svitkine (slow)
2015/09/01 20:04:04
A lot of this class is similar to ChromeNetworkDel
amohammadkhan
2015/09/01 23:02:43
You are right. But there are some difference here
|
+ 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, it attaches a |
+ // ResourceRequestInfo to the URLRequest, because requests from users have |
+ // this info. If |from_user| is false, the request is presumed to be from a |
+ // service, and the service name is set in the request's user data. |
+ 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); |
+ |
+ 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: |