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

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: Adding sclittle to OWNERs and little fix in ChromeNetworkDelegate test. Created 5 years, 4 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..d1327f61e7328283a37bc0c9dbe22eb082e1b13f
--- /dev/null
+++ b/components/data_use_measurement/content/data_use_measurement_unittest.cc
@@ -0,0 +1,115 @@
+// 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.Download." + target_dimension + connection_type_, 1);
+ histogram_tester.ExpectTotalCount(
+ "DataUse.NotUser.Upload." + target_dimension + connection_type_, 1);
+ // One upload and one download message, so total count should be 2.
+ histogram_tester.ExpectTotalCount("DataUse.Service.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);
+ request2->Start();
+ loop_.Run();
+ data_use_measurement_.ReportDataUseUMA(request2.get());
+ histogram_tester.ExpectTotalCount(
+ "DataUse.User.Download." + target_dimension + connection_type_, 1);
+ histogram_tester.ExpectTotalCount(
+ "DataUse.User.Upload." + target_dimension + connection_type_, 1);
+ }
+
+ void UpdateConnectionType() {
+ if (net::NetworkChangeNotifier::IsConnectionCellular(
+ net::NetworkChangeNotifier::GetConnectionType()))
Alexei Svitkine (slow) 2015/08/31 17:26:31 Nit: {}'s
amohammadkhan 2015/09/01 04:52:49 Done.
+ 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) {
+ data_use_measurement_.OnApplicationStateChange(
+ base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES);
+ UpdateConnectionType();
+ TestForAUserAndAServicePacket("Foreground.");
+}
+
+#if defined(OS_ANDROID)
+TEST_F(DataUseMeasurementTest, ApplicationStateTest) {
+ data_use_measurement_.OnApplicationStateChange(
+ base::android::APPLICATION_STATE_HAS_STOPPED_ACTIVITIES);
+ UpdateConnectionType();
+ TestForAUserAndAServicePacket("Background.");
+}
+#endif
+
+} // namespace data_use_measurement

Powered by Google App Engine
This is Rietveld 408576698