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

Unified Diff: components/data_use_measurement/content/data_use_measurement_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: Improving the naming of source_sets in gn files. 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
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..a497971400cc00e20a33c7373e2afd1844d0b9cf
--- /dev/null
+++ b/components/data_use_measurement/content/data_use_measurement_unittest.cc
@@ -0,0 +1,125 @@
+// 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:
mmenke 2015/09/04 20:51:46 No need for the protected section.
amohammadkhan 2015/09/05 00:58:02 Done.
+ // 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")};
mmenke 2015/09/04 20:51:46 Relying on reusing the socket is really ugly. I s
amohammadkhan 2015/09/05 00:58:02 Done.
+
+ 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.TrafficSize.System.Downstream." +
+ target_dimension + connection_type_,
+ 1);
+ histogram_tester.ExpectTotalCount("DataUse.TrafficSize.System.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(
mmenke 2015/09/04 20:51:46 include base/memory/scoped_ptr.h
amohammadkhan 2015/09/05 00:58:02 Done.
+ GURL("http://foo.com"), net::DEFAULT_PRIORITY, &test_delegate_));
mmenke 2015/09/04 20:51:46 include url/gurl.h and net/base/request_priority.h
amohammadkhan 2015/09/05 00:58:02 Done.
+ content::ResourceRequestInfo::AllocateForTesting(
+ request2.get(), content::RESOURCE_TYPE_MAIN_FRAME, nullptr, -2, -2, -2,
+ true, false, true, true);
+ request2->Start();
+ loop_.Run();
+ data_use_measurement_.ReportDataUseUMA(request2.get());
+ histogram_tester.ExpectTotalCount("DataUse.TrafficSize.User.Downstream." +
+ target_dimension + connection_type_,
+ 1);
+ histogram_tester.ExpectTotalCount("DataUse.TrafficSize.User.Upstream." +
+ target_dimension + connection_type_,
+ 1);
mmenke 2015/09/04 20:51:46 Check DataUse.MessageSize.AllServices, too?
amohammadkhan 2015/09/05 00:58:02 Done.
+ }
+
+ void UpdateConnectionType() {
+ if (net::NetworkChangeNotifier::IsConnectionCellular(
+ net::NetworkChangeNotifier::GetConnectionType())) {
mmenke 2015/09/04 20:51:46 This always returns CONNECTION_TYPE_UNKNOWN when w
amohammadkhan 2015/09/05 00:58:02 Done.
+ connection_type_ = "Cellular";
+ } else {
+ connection_type_ = "NotCellular";
+ }
+ }
+
+ DataUseMeasurement data_use_measurement_;
mmenke 2015/09/04 20:51:46 This can be private.
amohammadkhan 2015/09/05 00:58:02 Done.
+
+ private:
+ base::MessageLoopForIO loop_;
+ scoped_ptr<net::TestURLRequestContext> context_;
+ net::TestNetworkDelegate test_network_delegate_;
+ net::TestDelegate test_delegate_;
mmenke 2015/09/04 20:51:46 Suggest removing this as a member variable, and ju
amohammadkhan 2015/09/05 00:58:02 Done.
+ std::string connection_type_;
mmenke 2015/09/04 20:51:46 include <string>
amohammadkhan 2015/09/05 00:58:02 Done.
+ net::MockClientSocketFactory socket_factory_;
mmenke 2015/09/04 20:51:46 Order these based on dependencies: socket_factory
amohammadkhan 2015/09/05 00:58:02 Done.
+
+ DISALLOW_COPY_AND_ASSIGN(DataUseMeasurementTest);
+};
+
+// 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) {
+ data_use_measurement_.OnApplicationStateChangeForTesting(
+ base::android::APPLICATION_STATE_HAS_STOPPED_ACTIVITIES);
+ UpdateConnectionType();
+ TestForAUserAndAServicePacket("Background.");
+}
+#endif
+
+} // namespace data_use_measurement

Powered by Google App Engine
This is Rietveld 408576698