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

Unified 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: Addressed comments and rebased. 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/net/chrome_network_delegate.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..ea7b8ca4e38dcb33b9cde26d1fce27117dd96c9a 100644
--- a/chrome/browser/net/chrome_network_delegate_unittest.cc
+++ b/chrome/browser/net/chrome_network_delegate_unittest.cc
@@ -9,52 +9,242 @@
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/prefs/pref_member.h"
+#include "base/run_loop.h"
+#include "base/test/histogram_tester.h"
+#include "chrome/browser/browser_process.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/net/safe_search_util.h"
#include "chrome/common/pref_names.h"
+#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_pref_service_syncable.h"
#include "chrome/test/base/testing_profile.h"
+#include "chrome/test/base/testing_profile_manager.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/content_settings/core/common/pref_names.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/socket/socket_test_util.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
+#include "url/gurl.h"
#if defined(ENABLE_EXTENSIONS)
#include "chrome/browser/extensions/event_router_forwarder.h"
#endif
-TEST(ChromeNetworkDelegateTest, DisableFirstPartyOnlyCookiesIffFlagDisabled) {
- BooleanPrefMember pref_member_;
- scoped_ptr<ChromeNetworkDelegate> delegate;
+#if !defined(OS_IOS)
+#include "components/data_use_measurement/core/data_use_user_data.h"
+#endif
+
+namespace {
+
+// This function requests a URL, and makes it return a known response. 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. (As an example suggestions service tag is attached). if
+// |redirect| is true, it adds necessary socket data to have it follow redirect
+// before getting the final response.
+void RequestURL(net::URLRequestContext* context,
+ net::MockClientSocketFactory* socket_factory,
+ bool from_user,
+ bool redirect) {
+ net::MockRead redirect_mock_reads[] = {
+ net::MockRead("HTTP/1.1 302 Found\r\n"
+ "Location: http://bar.com/\r\n\r\n"),
+ net::MockRead(net::SYNCHRONOUS, net::OK),
+ };
+ net::StaticSocketDataProvider redirect_socket_data_provider(
+ redirect_mock_reads, arraysize(redirect_mock_reads), nullptr, 0);
+
+ if (redirect)
+ socket_factory->AddSocketDataProvider(&redirect_socket_data_provider);
+ net::MockRead response_mock_reads[] = {
+ net::MockRead("HTTP/1.1 200 OK\r\n\r\n"), net::MockRead("response body"),
+ net::MockRead(net::SYNCHRONOUS, net::OK),
+ };
+ net::StaticSocketDataProvider response_socket_data_provider(
+ response_mock_reads, arraysize(response_mock_reads), nullptr, 0);
+ socket_factory->AddSocketDataProvider(&response_socket_data_provider);
+ net::TestDelegate test_delegate;
+ test_delegate.set_quit_on_complete(true);
+ scoped_ptr<net::URLRequest> request(context->CreateRequest(
+ GURL("http://example.com"), net::DEFAULT_PRIORITY, &test_delegate));
+
+ if (from_user) {
+ content::ResourceRequestInfo::AllocateForTesting(
+ request.get(), content::RESOURCE_TYPE_MAIN_FRAME, nullptr, -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::RunLoop().RunUntilIdle();
+}
+
+} // namespace
+class ChromeNetworkDelegateTest : public testing::Test {
+ public:
+ ChromeNetworkDelegateTest()
+ : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
+ context_(new net::TestURLRequestContext(true)) {
#if defined(ENABLE_EXTENSIONS)
- scoped_refptr<extensions::EventRouterForwarder> forwarder =
- new extensions::EventRouterForwarder();
- delegate.reset(new ChromeNetworkDelegate(forwarder.get(), &pref_member_));
-#else
- delegate.reset(new ChromeNetworkDelegate(nullptr, &pref_member_));
+ forwarder_ = new extensions::EventRouterForwarder();
#endif
- EXPECT_FALSE(delegate->FirstPartyOnlyCookieExperimentEnabled());
-}
+ }
-TEST(ChromeNetworkDelegateTest, EnableFirstPartyOnlyCookiesIffFlagEnabled) {
- base::CommandLine::ForCurrentProcess()->AppendSwitch(
- switches::kEnableExperimentalWebPlatformFeatures);
- BooleanPrefMember pref_member_;
- scoped_ptr<ChromeNetworkDelegate> delegate;
+ void SetUp() override {
+ ChromeNetworkDelegate::InitializePrefsOnUIThread(
+ &enable_referrers_, nullptr, nullptr, nullptr,
+ profile_.GetTestingPrefService());
+ profile_manager_.reset(
+ new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
+ ASSERT_TRUE(profile_manager_->SetUp());
+ }
+
+ void Initialize() {
+ network_delegate_.reset(
+ new ChromeNetworkDelegate(forwarder(), &enable_referrers_));
+ context_->set_client_socket_factory(&socket_factory_);
+ context_->set_network_delegate(network_delegate_.get());
+ context_->Init();
+ }
+
+ net::TestURLRequestContext* context() { return context_.get(); }
+ net::NetworkDelegate* network_delegate() { return network_delegate_.get(); }
+ net::MockClientSocketFactory* socket_factory() { return &socket_factory_; }
+ extensions::EventRouterForwarder* forwarder() {
#if defined(ENABLE_EXTENSIONS)
- scoped_refptr<extensions::EventRouterForwarder> forwarder =
- new extensions::EventRouterForwarder();
- delegate.reset(new ChromeNetworkDelegate(forwarder.get(), &pref_member_));
+ return forwarder_.get();
#else
- delegate.reset(new ChromeNetworkDelegate(nullptr, &pref_member_));
+ return nullptr;
+#endif
+ }
+
+ private:
+ scoped_ptr<TestingProfileManager> profile_manager_;
+ content::TestBrowserThreadBundle thread_bundle_;
+#if defined(ENABLE_EXTENSIONS)
+ scoped_refptr<extensions::EventRouterForwarder> forwarder_;
+#endif
+ TestingProfile profile_;
+ BooleanPrefMember enable_referrers_;
+ scoped_ptr<net::NetworkDelegate> network_delegate_;
+ net::MockClientSocketFactory socket_factory_;
+ scoped_ptr<net::TestURLRequestContext> context_;
+};
+
+// This function tests data use measurement for requests by services. it makes a
+// query which is similar to a query of a service, so it should affect
+// DataUse.TrafficSize.System.Dimensions and DataUse.MessageSize.ServiceName
+// histograms. AppState and ConnectionType dimensions are always Foreground and
+// NotCellular respectively.
+#if !defined(OS_IOS)
+TEST_F(ChromeNetworkDelegateTest, DataUseMeasurementServiceTest) {
+ Initialize();
+ base::HistogramTester histogram_tester;
+
+ // A query from a service without redirection.
+ RequestURL(context(), socket_factory(), false, false);
+ histogram_tester.ExpectTotalCount(
+ "DataUse.TrafficSize.System.Downstream.Foreground.NotCellular", 1);
+ histogram_tester.ExpectTotalCount(
+ "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 1);
+ // One upload and one download message, so totalCount should be 2.
+ histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 2);
+ histogram_tester.ExpectTotalCount(
+ "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 0);
+ histogram_tester.ExpectTotalCount(
+ "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 0);
+}
+
+// This function tests data use measurement for requests by user.The query from
+// a user should affect DataUse.TrafficSize.User.Dimensions histogram. AppState
+// and ConnectionType dimensions are always Foreground and NotCellular
+// respectively.
+TEST_F(ChromeNetworkDelegateTest, DataUseMeasurementUserTest) {
+ Initialize();
+ base::HistogramTester histogram_tester;
+
+ // A query from user without redirection.
+ RequestURL(context(), socket_factory(), true, false);
+ histogram_tester.ExpectTotalCount(
+ "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 1);
+ histogram_tester.ExpectTotalCount(
+ "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 1);
+ histogram_tester.ExpectTotalCount(
+ "DataUse.TrafficSize.System.Downstream.Foreground.NotCellular", 0);
+ histogram_tester.ExpectTotalCount(
+ "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 0);
+ histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 0);
+}
+
+// This function tests data use measurement for requests by services in case the
+// request is redirected once. it makes a query which is similar to a query of a
+// service, so it should affect DataUse.TrafficSize.System.Dimensions and
+// DataUse.MessageSize.ServiceName histograms. AppState and ConnectionType
+// dimensions are always Foreground and NotCellular respectively.
+TEST_F(ChromeNetworkDelegateTest, DataUseMeasurementServiceTestWithRedirect) {
+ Initialize();
+ base::HistogramTester histogram_tester;
+
+ // A query from user with one redirection.
+ RequestURL(context(), socket_factory(), false, true);
+ histogram_tester.ExpectTotalCount(
+ "DataUse.TrafficSize.System.Downstream.Foreground.NotCellular", 2);
+ histogram_tester.ExpectTotalCount(
+ "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 2);
+ // Two uploads and two downloads message, so totalCount should be 4.
+ histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 4);
+ histogram_tester.ExpectTotalCount(
+ "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 0);
+ histogram_tester.ExpectTotalCount(
+ "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 0);
+}
+
+// This function tests data use measurement for requests by user in case the
+// request is redirected once.The query from a user should affect
+// DataUse.TrafficSize.User.Dimensions histogram. AppState and ConnectionType
+// dimensions are always Foreground and NotCellular respectively.
+TEST_F(ChromeNetworkDelegateTest, DataUseMeasurementUserTestWithRedirect) {
+ Initialize();
+ base::HistogramTester histogram_tester;
+
+ // A query from user with one redirection.
+ RequestURL(context(), socket_factory(), true, true);
+
+ histogram_tester.ExpectTotalCount(
+ "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 2);
+ histogram_tester.ExpectTotalCount(
+ "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 2);
+ histogram_tester.ExpectTotalCount(
+ "DataUse.TrafficSize.System.Downstream.Foreground.NotCellular", 0);
+ histogram_tester.ExpectTotalCount(
+ "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 0);
+ histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 0);
+}
+
#endif
- EXPECT_TRUE(delegate->FirstPartyOnlyCookieExperimentEnabled());
+
+TEST_F(ChromeNetworkDelegateTest, DisableFirstPartyOnlyCookiesIffFlagDisabled) {
+ Initialize();
+ EXPECT_FALSE(network_delegate()->FirstPartyOnlyCookieExperimentEnabled());
+}
+
+TEST_F(ChromeNetworkDelegateTest, EnableFirstPartyOnlyCookiesIffFlagEnabled) {
+ base::CommandLine::ForCurrentProcess()->AppendSwitch(
+ switches::kEnableExperimentalWebPlatformFeatures);
+ Initialize();
+ EXPECT_TRUE(network_delegate()->FirstPartyOnlyCookieExperimentEnabled());
}
class ChromeNetworkDelegateSafeSearchTest : public testing::Test {
« no previous file with comments | « chrome/browser/net/chrome_network_delegate.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698