Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/data_use_measurement/content/data_use_measurement.h" | 5 #include "components/data_use_measurement/content/data_use_measurement.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/test/histogram_tester.h" | 11 #include "base/test/histogram_tester.h" |
| 12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "content/public/browser/resource_request_info.h" | 13 #include "content/public/browser/resource_request_info.h" |
| 14 #include "net/base/network_change_notifier.h" | 14 #include "net/base/network_change_notifier.h" |
| 15 #include "net/base/request_priority.h" | 15 #include "net/base/request_priority.h" |
| 16 #include "net/socket/socket_test_util.h" | 16 #include "net/socket/socket_test_util.h" |
| 17 #include "net/url_request/url_request.h" | 17 #include "net/url_request/url_request.h" |
| 18 #include "net/url_request/url_request_test_util.h" | 18 #include "net/url_request/url_request_test_util.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 21 | 21 |
| 22 #if defined(OS_ANDROID) | 22 #if defined(OS_ANDROID) |
| 23 #include "base/android/application_status_listener.h" | 23 #include "base/android/application_status_listener.h" |
| 24 #endif | 24 #endif |
| 25 | 25 |
| 26 namespace data_use_measurement { | 26 namespace data_use_measurement { |
| 27 | 27 |
| 28 class DataUseMeasurementTest : public testing::Test { | 28 class DataUseMeasurementTest : public testing::Test { |
| 29 public: | 29 public: |
| 30 DataUseMeasurementTest() { | 30 DataUseMeasurementTest() |
| 31 : data_use_measurement_( | |
| 32 base::Bind(&DataUseMeasurementTest::FakeDataUseforwarder, | |
| 33 base::Unretained(this))) { | |
| 31 // During the test it is expected to not have cellular connection. | 34 // During the test it is expected to not have cellular connection. |
| 32 DCHECK(!net::NetworkChangeNotifier::IsConnectionCellular( | 35 DCHECK(!net::NetworkChangeNotifier::IsConnectionCellular( |
| 33 net::NetworkChangeNotifier::GetConnectionType())); | 36 net::NetworkChangeNotifier::GetConnectionType())); |
| 34 } | 37 } |
| 35 | 38 |
| 36 // This function makes a user request and confirms that its effect is | 39 void SendRequest(bool is_user_request) { |
|
Alexei Svitkine (slow)
2016/04/07 15:43:29
Add a comment and mention what the param means.
gayane -on leave until 09-2017
2016/04/07 18:53:49
Done.
| |
| 37 // reflected in proper histograms. | |
| 38 void TestForAUserRequest(const std::string& target_dimension) { | |
| 39 net::TestDelegate test_delegate; | 40 net::TestDelegate test_delegate; |
| 40 InitializeContext(); | 41 InitializeContext(); |
| 41 base::HistogramTester histogram_tester; | |
| 42 net::MockRead reads[] = {net::MockRead("HTTP/1.1 200 OK\r\n" | 42 net::MockRead reads[] = {net::MockRead("HTTP/1.1 200 OK\r\n" |
| 43 "Content-Length: 12\r\n\r\n"), | 43 "Content-Length: 12\r\n\r\n"), |
| 44 net::MockRead("Test Content")}; | 44 net::MockRead("Test Content")}; |
| 45 net::StaticSocketDataProvider socket_data(reads, arraysize(reads), nullptr, | 45 net::StaticSocketDataProvider socket_data(reads, arraysize(reads), nullptr, |
| 46 0); | 46 0); |
| 47 socket_factory_->AddSocketDataProvider(&socket_data); | 47 socket_factory_->AddSocketDataProvider(&socket_data); |
| 48 | 48 |
| 49 scoped_ptr<net::URLRequest> request(context_->CreateRequest( | 49 scoped_ptr<net::URLRequest> request(context_->CreateRequest( |
| 50 GURL("http://foo.com"), net::DEFAULT_PRIORITY, &test_delegate)); | 50 GURL("http://foo.com"), net::DEFAULT_PRIORITY, &test_delegate)); |
| 51 request->SetUserData( | 51 if (is_user_request) { |
| 52 data_use_measurement::DataUseUserData::kUserDataKey, | 52 request->SetUserData( |
| 53 new data_use_measurement::DataUseUserData( | 53 data_use_measurement::DataUseUserData::kUserDataKey, |
| 54 data_use_measurement::DataUseUserData::SUGGESTIONS)); | 54 new data_use_measurement::DataUseUserData( |
| 55 data_use_measurement::DataUseUserData::SUGGESTIONS)); | |
| 56 } else { | |
| 57 content::ResourceRequestInfo::AllocateForTesting( | |
| 58 request.get(), content::RESOURCE_TYPE_MAIN_FRAME, nullptr, -2, -2, -2, | |
| 59 true, false, true, true, false); | |
| 60 } | |
| 61 | |
| 55 request->Start(); | 62 request->Start(); |
| 56 loop_.RunUntilIdle(); | 63 loop_.RunUntilIdle(); |
| 57 | 64 |
| 58 data_use_measurement_.ReportDataUseUMA(request.get()); | 65 data_use_measurement_.ReportDataUseUMA(request.get()); |
| 66 } | |
| 67 | |
| 68 // This function makes a user request and confirms that its effect is | |
| 69 // reflected in proper histograms. | |
| 70 void TestForAUserRequest(const std::string& target_dimension) { | |
| 71 base::HistogramTester histogram_tester; | |
| 72 SendRequest(true); | |
| 59 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.System.Downstream." + | 73 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.System.Downstream." + |
| 60 target_dimension + kConnectionType, | 74 target_dimension + kConnectionType, |
| 61 1); | 75 1); |
| 62 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.System.Upstream." + | 76 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.System.Upstream." + |
| 63 target_dimension + kConnectionType, | 77 target_dimension + kConnectionType, |
| 64 1); | 78 1); |
| 65 // One upload and one download message, so total count should be 2. | 79 // One upload and one download message, so total count should be 2. |
| 66 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 2); | 80 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 2); |
| 67 } | 81 } |
| 68 | 82 |
| 69 // This function makes a service request and confirms that its effect is | 83 // This function makes a service request and confirms that its effect is |
| 70 // reflected in proper histograms. | 84 // reflected in proper histograms. |
| 71 void TestForAServiceRequest(const std::string& target_dimension) { | 85 void TestForAServiceRequest(const std::string& target_dimension) { |
| 72 net::TestDelegate test_delegate; | |
| 73 InitializeContext(); | |
| 74 base::HistogramTester histogram_tester; | 86 base::HistogramTester histogram_tester; |
| 75 | 87 SendRequest(false); |
| 76 net::MockRead reads[] = {net::MockRead("HTTP/1.1 200 OK\r\n" | |
| 77 "Content-Length: 12\r\n\r\n"), | |
| 78 net::MockRead("Test Content")}; | |
| 79 net::StaticSocketDataProvider socket_data(reads, arraysize(reads), nullptr, | |
| 80 0); | |
| 81 socket_factory_->AddSocketDataProvider(&socket_data); | |
| 82 | |
| 83 scoped_ptr<net::URLRequest> request(context_->CreateRequest( | |
| 84 GURL("http://foo.com"), net::DEFAULT_PRIORITY, &test_delegate)); | |
| 85 content::ResourceRequestInfo::AllocateForTesting( | |
| 86 request.get(), content::RESOURCE_TYPE_MAIN_FRAME, nullptr, -2, -2, -2, | |
| 87 true, false, true, true, false); | |
| 88 request->Start(); | |
| 89 loop_.RunUntilIdle(); | |
| 90 | |
| 91 data_use_measurement_.ReportDataUseUMA(request.get()); | |
| 92 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.User.Downstream." + | 88 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.User.Downstream." + |
| 93 target_dimension + kConnectionType, | 89 target_dimension + kConnectionType, |
| 94 1); | 90 1); |
| 95 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.User.Upstream." + | 91 histogram_tester.ExpectTotalCount("DataUse.TrafficSize.User.Upstream." + |
| 96 target_dimension + kConnectionType, | 92 target_dimension + kConnectionType, |
| 97 1); | 93 1); |
| 98 histogram_tester.ExpectTotalCount( | 94 histogram_tester.ExpectTotalCount( |
| 99 "DataUse.MessageSize.AllServices.Upstream." + target_dimension + | 95 "DataUse.MessageSize.AllServices.Upstream." + target_dimension + |
| 100 kConnectionType, | 96 kConnectionType, |
| 101 0); | 97 0); |
| 102 histogram_tester.ExpectTotalCount( | 98 histogram_tester.ExpectTotalCount( |
| 103 "DataUse.MessageSize.AllServices.Downstream." + target_dimension + | 99 "DataUse.MessageSize.AllServices.Downstream." + target_dimension + |
| 104 kConnectionType, | 100 kConnectionType, |
| 105 0); | 101 0); |
| 106 } | 102 } |
| 107 | 103 |
| 108 DataUseMeasurement* data_use_measurement() { return &data_use_measurement_; } | 104 DataUseMeasurement* data_use_measurement() { return &data_use_measurement_; } |
| 109 | 105 |
| 106 bool IsDataUseForwarderCalled() { return is_data_use_forwarder_called_; } | |
| 107 | |
| 110 private: | 108 private: |
| 111 void InitializeContext() { | 109 void InitializeContext() { |
| 112 context_.reset(new net::TestURLRequestContext(true)); | 110 context_.reset(new net::TestURLRequestContext(true)); |
| 113 socket_factory_.reset(new net::MockClientSocketFactory()); | 111 socket_factory_.reset(new net::MockClientSocketFactory()); |
| 114 context_->set_client_socket_factory(socket_factory_.get()); | 112 context_->set_client_socket_factory(socket_factory_.get()); |
| 115 context_->Init(); | 113 context_->Init(); |
| 116 } | 114 } |
| 117 | 115 |
| 116 void FakeDataUseforwarder(const std::string& service_name, | |
| 117 int message_size, | |
| 118 bool is_celllular) { | |
| 119 is_data_use_forwarder_called_ = true; | |
| 120 } | |
| 121 | |
| 118 base::MessageLoopForIO loop_; | 122 base::MessageLoopForIO loop_; |
| 119 DataUseMeasurement data_use_measurement_; | 123 DataUseMeasurement data_use_measurement_; |
| 120 scoped_ptr<net::MockClientSocketFactory> socket_factory_; | 124 scoped_ptr<net::MockClientSocketFactory> socket_factory_; |
| 121 scoped_ptr<net::TestURLRequestContext> context_; | 125 scoped_ptr<net::TestURLRequestContext> context_; |
| 122 const std::string kConnectionType = "NotCellular"; | 126 const std::string kConnectionType = "NotCellular"; |
| 127 bool is_data_use_forwarder_called_ = false; | |
| 123 | 128 |
| 124 DISALLOW_COPY_AND_ASSIGN(DataUseMeasurementTest); | 129 DISALLOW_COPY_AND_ASSIGN(DataUseMeasurementTest); |
| 125 }; | 130 }; |
| 126 | 131 |
| 127 // This test function tests recording of data use information in UMA histogram | 132 // This test function tests recording of data use information in UMA histogram |
| 128 // when packet is originated from user or services when the app is in the | 133 // when packet is originated from user or services when the app is in the |
| 129 // foreground or the OS is not Android. | 134 // foreground or the OS is not Android. |
| 130 // TODO(amohammadkhan): Add tests for Cellular/non-cellular connection types | 135 // TODO(amohammadkhan): Add tests for Cellular/non-cellular connection types |
| 131 // when support for testing is provided in its class. | 136 // when support for testing is provided in its class. |
| 132 TEST_F(DataUseMeasurementTest, UserNotUserTest) { | 137 TEST_F(DataUseMeasurementTest, UserNotUserTest) { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 143 // when packet is originated from user or services when the app is in the | 148 // when packet is originated from user or services when the app is in the |
| 144 // background and OS is Android. | 149 // background and OS is Android. |
| 145 TEST_F(DataUseMeasurementTest, ApplicationStateTest) { | 150 TEST_F(DataUseMeasurementTest, ApplicationStateTest) { |
| 146 data_use_measurement()->OnApplicationStateChangeForTesting( | 151 data_use_measurement()->OnApplicationStateChangeForTesting( |
| 147 base::android::APPLICATION_STATE_HAS_STOPPED_ACTIVITIES); | 152 base::android::APPLICATION_STATE_HAS_STOPPED_ACTIVITIES); |
| 148 TestForAServiceRequest("Background."); | 153 TestForAServiceRequest("Background."); |
| 149 TestForAUserRequest("Background."); | 154 TestForAUserRequest("Background."); |
| 150 } | 155 } |
| 151 #endif | 156 #endif |
| 152 | 157 |
| 158 TEST_F(DataUseMeasurementTest, DataUseForwarderIsCalled) { | |
| 159 EXPECT_FALSE(IsDataUseForwarderCalled()); | |
| 160 SendRequest(true); | |
| 161 EXPECT_TRUE(IsDataUseForwarderCalled()); | |
| 162 } | |
| 163 | |
| 153 } // namespace data_use_measurement | 164 } // namespace data_use_measurement |
| OLD | NEW |