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/core/data_use_measurement.h" | 5 #include "components/data_use_measurement/core/data_use_measurement.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "base/test/histogram_tester.h" | 13 #include "base/test/histogram_tester.h" |
14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 15 #include "components/data_use_measurement/core/data_use_ascriber.h" |
| 16 #include "components/data_use_measurement/core/data_use_recorder.h" |
15 #include "components/data_use_measurement/core/url_request_classifier.h" | 17 #include "components/data_use_measurement/core/url_request_classifier.h" |
16 #include "net/base/network_change_notifier.h" | 18 #include "net/base/network_change_notifier.h" |
17 #include "net/base/request_priority.h" | 19 #include "net/base/request_priority.h" |
18 #include "net/socket/socket_test_util.h" | 20 #include "net/socket/socket_test_util.h" |
19 #include "net/url_request/url_request.h" | 21 #include "net/url_request/url_request.h" |
20 #include "net/url_request/url_request_test_util.h" | 22 #include "net/url_request/url_request_test_util.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "url/gurl.h" | 24 #include "url/gurl.h" |
23 | 25 |
24 #if defined(OS_ANDROID) | 26 #if defined(OS_ANDROID) |
25 #include "base/android/application_status_listener.h" | 27 #include "base/android/application_status_listener.h" |
26 #endif | 28 #endif |
27 | 29 |
28 namespace data_use_measurement { | 30 namespace data_use_measurement { |
29 | 31 |
30 class UserRequestUserDataForTesting : public base::SupportsUserData::Data, | 32 class UserRequestUserDataForTesting : public base::SupportsUserData::Data, |
31 public URLRequestClassifier { | 33 public URLRequestClassifier { |
32 public: | 34 public: |
33 static const void* const kUserDataKey; | 35 static const void* const kUserDataKey; |
34 | 36 |
35 bool IsUserRequest(const net::URLRequest& request) const override { | 37 bool IsUserRequest(const net::URLRequest& request) const override { |
36 return request.GetUserData(kUserDataKey) != nullptr; | 38 return request.GetUserData(kUserDataKey) != nullptr; |
37 } | 39 } |
38 | 40 |
39 static void MarkAsUserRequest(net::URLRequest* request) { | 41 static void MarkAsUserRequest(net::URLRequest* request) { |
40 request->SetUserData(kUserDataKey, new UserRequestUserDataForTesting()); | 42 request->SetUserData(kUserDataKey, new UserRequestUserDataForTesting()); |
41 } | 43 } |
42 }; | 44 }; |
43 | 45 |
| 46 class TestDataUseAscriber : public DataUseAscriber { |
| 47 public: |
| 48 TestDataUseAscriber() {} |
| 49 |
| 50 DataUseRecorder* GetOrCreateDataUseRecorder( |
| 51 net::URLRequest* request) override { |
| 52 return &recorder_; |
| 53 } |
| 54 |
| 55 DataUseRecorder* GetDataUseRecorder(const net::URLRequest& request) override { |
| 56 return &recorder_; |
| 57 } |
| 58 |
| 59 std::unique_ptr<URLRequestClassifier> CreateURLRequestClassifier() |
| 60 const override { |
| 61 return nullptr; |
| 62 } |
| 63 |
| 64 void SetTabVisibility(bool visible) { recorder_.set_is_visible(visible); } |
| 65 |
| 66 private: |
| 67 DataUseRecorder recorder_; |
| 68 }; |
| 69 |
44 // The more usual initialization of kUserDataKey would be along the lines of | 70 // The more usual initialization of kUserDataKey would be along the lines of |
45 // const void* const UserRequestUserDataForTesting::kUserDataKey = | 71 // const void* const UserRequestUserDataForTesting::kUserDataKey = |
46 // &UserRequestUserDataForTesting::kUserDataKey; | 72 // &UserRequestUserDataForTesting::kUserDataKey; |
47 // but lld's identical constant folding then folds that with | 73 // but lld's identical constant folding then folds that with |
48 // DataUseUserData::kUserDataKey which is initialized like that as well, and | 74 // DataUseUserData::kUserDataKey which is initialized like that as well, and |
49 // then UserRequestUserDataForTesting::IsUserRequest() starts classifying | 75 // then UserRequestUserDataForTesting::IsUserRequest() starts classifying |
50 // service requests as user requests. To work around this, make | 76 // service requests as user requests. To work around this, make |
51 // UserRequestUserDataForTesting::kUserDataKey point to an arbitrary integer | 77 // UserRequestUserDataForTesting::kUserDataKey point to an arbitrary integer |
52 // instead. | 78 // instead. |
53 // TODO(thakis): If we changed lld to only ICF over code and not over data, | 79 // TODO(thakis): If we changed lld to only ICF over code and not over data, |
54 // we could undo this again. | 80 // we could undo this again. |
55 const int kICFBuster = 12345634; | 81 const int kICFBuster = 12345634; |
56 | 82 |
57 // static | 83 // static |
58 const void* const UserRequestUserDataForTesting::kUserDataKey = &kICFBuster; | 84 const void* const UserRequestUserDataForTesting::kUserDataKey = &kICFBuster; |
59 | 85 |
60 class DataUseMeasurementTest : public testing::Test { | 86 class DataUseMeasurementTest : public testing::Test { |
61 public: | 87 public: |
62 DataUseMeasurementTest() | 88 DataUseMeasurementTest() |
63 : data_use_measurement_( | 89 : data_use_measurement_( |
64 base::MakeUnique<UserRequestUserDataForTesting>(), | 90 base::MakeUnique<UserRequestUserDataForTesting>(), |
65 base::Bind(&DataUseMeasurementTest::FakeDataUseforwarder, | 91 base::Bind(&DataUseMeasurementTest::FakeDataUseforwarder, |
66 base::Unretained(this))) { | 92 base::Unretained(this)), |
| 93 &ascriber_) { |
67 // During the test it is expected to not have cellular connection. | 94 // During the test it is expected to not have cellular connection. |
68 DCHECK(!net::NetworkChangeNotifier::IsConnectionCellular( | 95 DCHECK(!net::NetworkChangeNotifier::IsConnectionCellular( |
69 net::NetworkChangeNotifier::GetConnectionType())); | 96 net::NetworkChangeNotifier::GetConnectionType())); |
70 } | 97 } |
71 | 98 |
72 // Creates a test request. | 99 // Creates a test request. |
73 enum RequestKind { kServiceRequest, kUserRequest }; | 100 enum RequestKind { kServiceRequest, kUserRequest }; |
74 std::unique_ptr<net::URLRequest> CreateTestRequest( | 101 std::unique_ptr<net::URLRequest> CreateTestRequest( |
75 RequestKind is_user_request) { | 102 RequestKind is_user_request) { |
76 net::TestDelegate test_delegate; | 103 net::TestDelegate test_delegate; |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 context_->Init(); | 185 context_->Init(); |
159 } | 186 } |
160 | 187 |
161 void FakeDataUseforwarder(const std::string& service_name, | 188 void FakeDataUseforwarder(const std::string& service_name, |
162 int message_size, | 189 int message_size, |
163 bool is_celllular) { | 190 bool is_celllular) { |
164 is_data_use_forwarder_called_ = true; | 191 is_data_use_forwarder_called_ = true; |
165 } | 192 } |
166 | 193 |
167 base::MessageLoopForIO loop_; | 194 base::MessageLoopForIO loop_; |
| 195 |
| 196 TestDataUseAscriber ascriber_; |
168 DataUseMeasurement data_use_measurement_; | 197 DataUseMeasurement data_use_measurement_; |
| 198 |
169 std::unique_ptr<net::MockClientSocketFactory> socket_factory_; | 199 std::unique_ptr<net::MockClientSocketFactory> socket_factory_; |
170 std::unique_ptr<net::TestURLRequestContext> context_; | 200 std::unique_ptr<net::TestURLRequestContext> context_; |
171 const std::string kConnectionType = "NotCellular"; | 201 const std::string kConnectionType = "NotCellular"; |
172 bool is_data_use_forwarder_called_ = false; | 202 bool is_data_use_forwarder_called_ = false; |
173 | 203 |
174 DISALLOW_COPY_AND_ASSIGN(DataUseMeasurementTest); | 204 DISALLOW_COPY_AND_ASSIGN(DataUseMeasurementTest); |
175 }; | 205 }; |
176 | 206 |
177 // This test function tests recording of data use information in UMA histogram | 207 // This test function tests recording of data use information in UMA histogram |
178 // when packet is originated from user or services when the app is in the | 208 // when packet is originated from user or services when the app is in the |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 data_use_measurement_.OnNetworkBytesSent(*request, 100); | 365 data_use_measurement_.OnNetworkBytesSent(*request, 100); |
336 data_use_measurement_.OnNetworkBytesReceived(*request, 1000); | 366 data_use_measurement_.OnNetworkBytesReceived(*request, 1000); |
337 data_use_measurement_.OnNetworkBytesSent(*request, 200); | 367 data_use_measurement_.OnNetworkBytesSent(*request, 200); |
338 data_use_measurement_.OnNetworkBytesReceived(*request, 2000); | 368 data_use_measurement_.OnNetworkBytesReceived(*request, 2000); |
339 histogram_tester.ExpectTotalCount( | 369 histogram_tester.ExpectTotalCount( |
340 "DataUse.BackgroundToDataRecievedPerByte.User", 0); | 370 "DataUse.BackgroundToDataRecievedPerByte.User", 0); |
341 histogram_tester.ExpectTotalCount( | 371 histogram_tester.ExpectTotalCount( |
342 "DataUse.BackgroundToFirstDownstream.User", 0); | 372 "DataUse.BackgroundToFirstDownstream.User", 0); |
343 } | 373 } |
344 } | 374 } |
| 375 |
| 376 TEST_F(DataUseMeasurementTest, AppTabState) { |
| 377 base::HistogramTester histogram_tester; |
| 378 std::unique_ptr<net::URLRequest> request = CreateTestRequest(kUserRequest); |
| 379 |
| 380 // App in foreground, Tab in background. |
| 381 data_use_measurement_.OnBeforeURLRequest(request.get()); |
| 382 data_use_measurement_.OnNetworkBytesReceived(*request, 1000); |
| 383 data_use_measurement_.OnNetworkBytesSent(*request, 100); |
| 384 |
| 385 histogram_tester.ExpectTotalCount( |
| 386 "DataUse.AppTabState.Upstream.AppForeground.TabBackground", 1); |
| 387 histogram_tester.ExpectTotalCount( |
| 388 "DataUse.AppTabState.Downstream.AppForeground.TabBackground", 1); |
| 389 |
| 390 // App and Tab in foreground. |
| 391 ascriber_.SetTabVisibility(true); |
| 392 data_use_measurement_.OnBeforeURLRequest(request.get()); |
| 393 data_use_measurement_.OnNetworkBytesReceived(*request, 1000); |
| 394 data_use_measurement_.OnNetworkBytesSent(*request, 100); |
| 395 |
| 396 histogram_tester.ExpectTotalCount( |
| 397 "DataUse.AppTabState.Upstream.AppForeground.TabForeground", 1); |
| 398 histogram_tester.ExpectTotalCount( |
| 399 "DataUse.AppTabState.Downstream.AppForeground.TabForeground", 1); |
| 400 |
| 401 // App and Tab in background. |
| 402 data_use_measurement()->OnApplicationStateChangeForTesting( |
| 403 base::android::APPLICATION_STATE_HAS_STOPPED_ACTIVITIES); |
| 404 ascriber_.SetTabVisibility(false); |
| 405 data_use_measurement_.OnBeforeURLRequest(request.get()); |
| 406 // First network access changes the app state to UNKNOWN and the next nextwork |
| 407 // access changes to BACKGROUND. |
| 408 data_use_measurement_.OnNetworkBytesReceived(*request, 1000); |
| 409 data_use_measurement_.OnNetworkBytesReceived(*request, 1000); |
| 410 data_use_measurement_.OnNetworkBytesSent(*request, 100); |
| 411 |
| 412 histogram_tester.ExpectTotalCount( |
| 413 "DataUse.AppTabState.Upstream.AppBackground", 1); |
| 414 histogram_tester.ExpectTotalCount( |
| 415 "DataUse.AppTabState.Downstream.AppBackground", 1); |
| 416 } |
345 #endif | 417 #endif |
346 | 418 |
347 } // namespace data_use_measurement | 419 } // namespace data_use_measurement |
OLD | NEW |