PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/components/data_use_measurement/content/data_use_measurement_unittest.cc

https://gitlab.com/jonnialva90/iridium-browser
C++ | 151 lines | 114 code | 20 blank | 17 comment | 0 complexity | 8f40137c8b553dc32f46532bdf577357 MD5 | raw file
  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
  3. // found in the LICENSE file.
  4. #include "components/data_use_measurement/content/data_use_measurement.h"
  5. #include <string>
  6. #include "base/memory/scoped_ptr.h"
  7. #include "base/test/histogram_tester.h"
  8. #include "content/public/browser/resource_request_info.h"
  9. #include "net/base/network_change_notifier.h"
  10. #include "net/base/request_priority.h"
  11. #include "net/socket/socket_test_util.h"
  12. #include "net/url_request/url_request.h"
  13. #include "net/url_request/url_request_test_util.h"
  14. #include "testing/gtest/include/gtest/gtest.h"
  15. #include "url/gurl.h"
  16. #if defined(OS_ANDROID)
  17. #include "base/android/application_status_listener.h"
  18. #endif
  19. namespace data_use_measurement {
  20. class DataUseMeasurementTest : public testing::Test {
  21. public:
  22. DataUseMeasurementTest() {
  23. // During the test it is expected to not have cellular connection.
  24. DCHECK(!net::NetworkChangeNotifier::IsConnectionCellular(
  25. net::NetworkChangeNotifier::GetConnectionType()));
  26. }
  27. // This function makes a user request and confirms that its effect is
  28. // reflected in proper histograms.
  29. void TestForAUserRequest(const std::string& target_dimension) {
  30. net::TestDelegate test_delegate;
  31. InitializeContext();
  32. base::HistogramTester histogram_tester;
  33. net::MockRead reads[] = {net::MockRead("HTTP/1.1 200 OK\r\n"
  34. "Content-Length: 12\r\n\r\n"),
  35. net::MockRead("Test Content")};
  36. net::StaticSocketDataProvider socket_data(reads, arraysize(reads), nullptr,
  37. 0);
  38. socket_factory_->AddSocketDataProvider(&socket_data);
  39. scoped_ptr<net::URLRequest> request(context_->CreateRequest(
  40. GURL("http://foo.com"), net::DEFAULT_PRIORITY, &test_delegate));
  41. request->SetUserData(
  42. data_use_measurement::DataUseUserData::kUserDataKey,
  43. new data_use_measurement::DataUseUserData(
  44. data_use_measurement::DataUseUserData::SUGGESTIONS));
  45. request->Start();
  46. loop_.RunUntilIdle();
  47. data_use_measurement_.ReportDataUseUMA(request.get());
  48. histogram_tester.ExpectTotalCount("DataUse.TrafficSize.System.Downstream." +
  49. target_dimension + kConnectionType,
  50. 1);
  51. histogram_tester.ExpectTotalCount("DataUse.TrafficSize.System.Upstream." +
  52. target_dimension + kConnectionType,
  53. 1);
  54. // One upload and one download message, so total count should be 2.
  55. histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 2);
  56. }
  57. // This function makes a service request and confirms that its effect is
  58. // reflected in proper histograms.
  59. void TestForAServiceRequest(const std::string& target_dimension) {
  60. net::TestDelegate test_delegate;
  61. InitializeContext();
  62. base::HistogramTester histogram_tester;
  63. net::MockRead reads[] = {net::MockRead("HTTP/1.1 200 OK\r\n"
  64. "Content-Length: 12\r\n\r\n"),
  65. net::MockRead("Test Content")};
  66. net::StaticSocketDataProvider socket_data(reads, arraysize(reads), nullptr,
  67. 0);
  68. socket_factory_->AddSocketDataProvider(&socket_data);
  69. scoped_ptr<net::URLRequest> request(context_->CreateRequest(
  70. GURL("http://foo.com"), net::DEFAULT_PRIORITY, &test_delegate));
  71. content::ResourceRequestInfo::AllocateForTesting(
  72. request.get(), content::RESOURCE_TYPE_MAIN_FRAME, nullptr, -2, -2, -2,
  73. true, false, true, true);
  74. request->Start();
  75. loop_.RunUntilIdle();
  76. data_use_measurement_.ReportDataUseUMA(request.get());
  77. histogram_tester.ExpectTotalCount("DataUse.TrafficSize.User.Downstream." +
  78. target_dimension + kConnectionType,
  79. 1);
  80. histogram_tester.ExpectTotalCount("DataUse.TrafficSize.User.Upstream." +
  81. target_dimension + kConnectionType,
  82. 1);
  83. histogram_tester.ExpectTotalCount(
  84. "DataUse.MessageSize.AllServices.Upstream." + target_dimension +
  85. kConnectionType,
  86. 0);
  87. histogram_tester.ExpectTotalCount(
  88. "DataUse.MessageSize.AllServices.Downstream." + target_dimension +
  89. kConnectionType,
  90. 0);
  91. }
  92. DataUseMeasurement* data_use_measurement() { return &data_use_measurement_; }
  93. private:
  94. void InitializeContext() {
  95. context_.reset(new net::TestURLRequestContext(true));
  96. socket_factory_.reset(new net::MockClientSocketFactory());
  97. context_->set_client_socket_factory(socket_factory_.get());
  98. context_->Init();
  99. }
  100. base::MessageLoopForIO loop_;
  101. DataUseMeasurement data_use_measurement_;
  102. scoped_ptr<net::MockClientSocketFactory> socket_factory_;
  103. scoped_ptr<net::TestURLRequestContext> context_;
  104. const std::string kConnectionType = "NotCellular";
  105. DISALLOW_COPY_AND_ASSIGN(DataUseMeasurementTest);
  106. };
  107. // This test function tests recording of data use information in UMA histogram
  108. // when packet is originated from user or services when the app is in the
  109. // foreground or the OS is not Android.
  110. // TODO(amohammadkhan): Add tests for Cellular/non-cellular connection types
  111. // when support for testing is provided in its class.
  112. TEST_F(DataUseMeasurementTest, UserNotUserTest) {
  113. #if defined(OS_ANDROID)
  114. data_use_measurement()->OnApplicationStateChangeForTesting(
  115. base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES);
  116. #endif
  117. TestForAServiceRequest("Foreground.");
  118. TestForAUserRequest("Foreground.");
  119. }
  120. #if defined(OS_ANDROID)
  121. // This test function tests recording of data use information in UMA histogram
  122. // when packet is originated from user or services when the app is in the
  123. // background and OS is Android.
  124. TEST_F(DataUseMeasurementTest, ApplicationStateTest) {
  125. data_use_measurement()->OnApplicationStateChangeForTesting(
  126. base::android::APPLICATION_STATE_HAS_STOPPED_ACTIVITIES);
  127. TestForAServiceRequest("Background.");
  128. TestForAUserRequest("Background.");
  129. }
  130. #endif
  131. } // namespace data_use_measurement