Index: components/data_use_measurement/content/data_use_measurement_unittest.cc |
diff --git a/components/data_use_measurement/content/data_use_measurement_unittest.cc b/components/data_use_measurement/content/data_use_measurement_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0995f09e733be49892edabf32bdf27ceee8dcae1 |
--- /dev/null |
+++ b/components/data_use_measurement/content/data_use_measurement_unittest.cc |
@@ -0,0 +1,120 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/data_use_measurement/content/data_use_measurement.h" |
+ |
+#include "base/test/histogram_tester.h" |
+#include "content/public/browser/resource_request_info.h" |
+#include "net/base/network_change_notifier.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/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+#if defined(OS_ANDROID) |
+#include "base/android/application_status_listener.h" |
+#endif |
+ |
+namespace data_use_measurement { |
+ |
+class DataUseMeasurementTest : public testing::Test { |
+ public: |
+ DataUseMeasurementTest() : context_(new net::TestURLRequestContext(true)) { |
+ context_.set_client_socket_factory(&socket_factory_); |
+ context_.set_network_delegate(&test_network_delegate_); |
+ context_.Init(); |
+ |
+ UpdateConnectionType(); |
+ } |
+ |
+ protected: |
+ // This function makes one user request and one service request and confirms |
+ // that their effect is reflected in proper histograms. |
+ void TestForAUserAndAServicePacket(const std::string& target_dimension) { |
+ net::MockRead reads[] = {net::MockRead("HTTP/1.1 200 OK\r\n" |
+ "Content-Length: 12\r\n\r\n"), |
+ net::MockRead("Test Content"), |
+ net::MockRead("HTTP/1.1 200 OK\r\n" |
+ "Content-Length: 13\r\n\r\n"), |
+ net::MockRead("Test Content2")}; |
+ |
+ net::StaticSocketDataProvider socket_data(reads, arraysize(reads), nullptr, |
+ 0); |
+ socket_factory_.AddSocketDataProvider(&socket_data); |
+ base::HistogramTester histogram_tester; |
+ scoped_ptr<net::URLRequest> request(context_.CreateRequest( |
+ GURL("http://foo.com"), net::DEFAULT_PRIORITY, &test_delegate_)); |
+ request->SetUserData( |
+ data_use_measurement::DataUseUserData::kUserDataKey, |
+ new data_use_measurement::DataUseUserData( |
+ data_use_measurement::DataUseUserData::SUGGESTIONS)); |
+ request->Start(); |
+ loop_.Run(); |
+ data_use_measurement_.ReportDataUseUMA(request.get()); |
+ histogram_tester.ExpectTotalCount( |
+ "DataUse.NotUser.Downstream." + target_dimension + connection_type_, 1); |
+ histogram_tester.ExpectTotalCount( |
+ "DataUse.NotUser.Upstream." + target_dimension + connection_type_, 1); |
+ // One upload and one download message, so total count should be 2. |
+ histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 2); |
+ |
+ scoped_ptr<net::URLRequest> request2(context_.CreateRequest( |
+ GURL("http://foo.com"), net::DEFAULT_PRIORITY, &test_delegate_)); |
+ content::ResourceRequestInfo::AllocateForTesting( |
+ request2.get(), content::RESOURCE_TYPE_MAIN_FRAME, NULL, |
+ -2, // render_process_id |
+ -2, -2, true, false, true, true); |
bengr
2015/09/01 20:16:01
What are these -2's and other parameters? I.e., wh
amohammadkhan
2015/09/01 23:02:43
I was using negative process id to prevent an erro
|
+ request2->Start(); |
+ loop_.Run(); |
+ data_use_measurement_.ReportDataUseUMA(request2.get()); |
+ histogram_tester.ExpectTotalCount( |
+ "DataUse.User.Downstream." + target_dimension + connection_type_, 1); |
+ histogram_tester.ExpectTotalCount( |
+ "DataUse.User.Upstream." + target_dimension + connection_type_, 1); |
+ } |
+ |
+ void UpdateConnectionType() { |
+ if (net::NetworkChangeNotifier::IsConnectionCellular( |
+ net::NetworkChangeNotifier::GetConnectionType())) { |
+ connection_type_ = "Cellular"; |
+ } else { |
+ connection_type_ = "NotCellular"; |
+ } |
+ } |
+ |
+ base::MessageLoopForIO loop_; |
+ net::TestURLRequestContext context_; |
+ net::TestNetworkDelegate test_network_delegate_; |
+ DataUseMeasurement data_use_measurement_; |
+ net::TestDelegate test_delegate_; |
+ std::string connection_type_; |
+ net::MockClientSocketFactory socket_factory_; |
+}; |
+ |
+// This test function tests recording of data use information in UMA histogram |
+// when packet is originated from user or services. |
+// TODO(amohammadkhan): Add tests for Cellular/non-cellular connection types |
+// when support for testing is provided in its class. |
+TEST_F(DataUseMeasurementTest, UserNotUserTest) { |
+#if defined(OS_ANDROID) |
+ data_use_measurement_.OnApplicationStateChangeForTesting( |
+ base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES); |
+#endif |
+ UpdateConnectionType(); |
+ TestForAUserAndAServicePacket("Foreground."); |
+} |
+ |
+#if defined(OS_ANDROID) |
+TEST_F(DataUseMeasurementTest, ApplicationStateTest) { |
+#if defined(OS_ANDROID) |
+ data_use_measurement_.OnApplicationStateChangeForTesting( |
+ base::android::APPLICATION_STATE_HAS_STOPPED_ACTIVITIES); |
+#endif |
+ UpdateConnectionType(); |
+ TestForAUserAndAServicePacket("Background."); |
+} |
+#endif |
+ |
+} // namespace data_use_measurement |