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..17381c430e9438d47c21d39843dd4dbdb8fe1c64 100644 |
| --- a/chrome/browser/net/chrome_network_delegate_unittest.cc |
| +++ b/chrome/browser/net/chrome_network_delegate_unittest.cc |
| @@ -9,6 +9,8 @@ |
| #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/content_settings/cookie_settings_factory.h" |
| #include "chrome/browser/net/safe_search_util.h" |
| #include "chrome/common/pref_names.h" |
| @@ -16,7 +18,9 @@ |
| #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 "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" |
| @@ -27,33 +31,147 @@ |
| #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 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(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.
|
| + 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)); |
|
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.
|
| + if (from_user) { |
| + content::ResourceRequestInfo::AllocateForTesting( |
| + 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.
|
| + 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().Run(); |
| +} |
| + |
| +} // namespace |
| +class ChromeNetworkDelegateTest : public testing::Test { |
| + public: |
| + ChromeNetworkDelegateTest() |
| + : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| + context_(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_, NULL, NULL, NULL, profile_.GetTestingPrefService()); |
|
mmenke
2015/09/03 16:02:59
nullptr
amohammadkhan
2015/09/03 23:10:36
Done.
|
| + } |
| + 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_); |
| + 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
|
| + } |
| + |
| + 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: |
| + content::TestBrowserThreadBundle thread_bundle_; |
| + |
| + protected: |
| + net::TestURLRequestContext context_; |
| + net::NetworkDelegate* network_delegate_; |
| + |
| + 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.
|
| +#if defined(ENABLE_EXTENSIONS) |
| + scoped_refptr<extensions::EventRouterForwarder> forwarder_; |
| +#endif |
| + TestingProfile profile_; |
| + BooleanPrefMember enable_referrers_; |
| +}; |
| + |
| +// 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) { |
| + scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate()); |
| + SetDelegate(delegate.get()); |
| + base::HistogramTester histogram_tester; |
| + |
| + QueryURLDataUse(context_, 0); // A query from a service |
| + 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) { |
| + scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate()); |
| + SetDelegate(delegate.get()); |
| + base::HistogramTester histogram_tester; |
| + |
| + QueryURLDataUse(context_, 1); // A query from user |
| + 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); |
| +} |
| + |
| +#endif |
| + |
| +TEST_F(ChromeNetworkDelegateTest, DisableFirstPartyOnlyCookiesIffFlagDisabled) { |
| + scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate()); |
| + SetDelegate(delegate.get()); |
| + EXPECT_FALSE(network_delegate_->FirstPartyOnlyCookieExperimentEnabled()); |
| +} |
| + |
| +TEST_F(ChromeNetworkDelegateTest, EnableFirstPartyOnlyCookiesIffFlagEnabled) { |
| + base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| + switches::kEnableExperimentalWebPlatformFeatures); |
| + scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate()); |
| + SetDelegate(delegate.get()); |
| EXPECT_TRUE(delegate->FirstPartyOnlyCookieExperimentEnabled()); |
| } |