| 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_usage/core/data_use_aggregator.h" | 5 #include "components/data_usage/core/data_use_aggregator.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "components/data_usage/core/data_use.h" | 17 #include "components/data_usage/core/data_use.h" |
| 18 #include "components/data_usage/core/data_use_amortizer.h" |
| 18 #include "components/data_usage/core/data_use_annotator.h" | 19 #include "components/data_usage/core/data_use_annotator.h" |
| 19 #include "net/base/load_timing_info.h" | 20 #include "net/base/load_timing_info.h" |
| 20 #include "net/base/network_change_notifier.h" | 21 #include "net/base/network_change_notifier.h" |
| 21 #include "net/base/network_delegate_impl.h" | 22 #include "net/base/network_delegate_impl.h" |
| 22 #include "net/socket/socket_test_util.h" | 23 #include "net/socket/socket_test_util.h" |
| 23 #include "net/url_request/url_request.h" | 24 #include "net/url_request/url_request.h" |
| 24 #include "net/url_request/url_request_test_util.h" | 25 #include "net/url_request/url_request_test_util.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "url/gurl.h" | 27 #include "url/gurl.h" |
| 27 | 28 |
| 28 namespace data_usage { | 29 namespace data_usage { |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 base::TimeTicks GetRequestStart(const net::URLRequest& request) { | 33 base::TimeTicks GetRequestStart(const net::URLRequest& request) { |
| 33 net::LoadTimingInfo load_timing_info; | 34 net::LoadTimingInfo load_timing_info; |
| 34 request.GetLoadTimingInfo(&load_timing_info); | 35 request.GetLoadTimingInfo(&load_timing_info); |
| 35 return load_timing_info.request_start; | 36 return load_timing_info.request_start; |
| 36 } | 37 } |
| 37 | 38 |
| 38 // Test class that can set the network operator's MCCMNC. | 39 // Test class that can set the network operator's MCCMNC. |
| 39 class TestDataUseAggregator : public DataUseAggregator { | 40 class TestDataUseAggregator : public DataUseAggregator { |
| 40 public: | 41 public: |
| 41 TestDataUseAggregator(scoped_ptr<DataUseAnnotator> annotator) | 42 TestDataUseAggregator(scoped_ptr<DataUseAnnotator> annotator, |
| 42 : DataUseAggregator(annotator.Pass()) {} | 43 scoped_ptr<DataUseAmortizer> amortizer) |
| 44 : DataUseAggregator(annotator.Pass(), amortizer.Pass()) {} |
| 43 | 45 |
| 44 ~TestDataUseAggregator() override {} | 46 ~TestDataUseAggregator() override {} |
| 45 | 47 |
| 46 private: | 48 private: |
| 47 friend class TestNetworkChangeNotifier; | 49 friend class TestNetworkChangeNotifier; |
| 48 using DataUseAggregator::OnConnectionTypeChanged; | 50 using DataUseAggregator::OnConnectionTypeChanged; |
| 49 using DataUseAggregator::SetMccMncForTests; | 51 using DataUseAggregator::SetMccMncForTests; |
| 50 }; | 52 }; |
| 51 | 53 |
| 52 // Override NetworkChangeNotifier to simulate connection type changes for tests. | 54 // Override NetworkChangeNotifier to simulate connection type changes for tests. |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 int on_data_use_called_count_; | 209 int on_data_use_called_count_; |
| 208 | 210 |
| 209 DISALLOW_COPY_AND_ASSIGN(TestObserver); | 211 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| 210 }; | 212 }; |
| 211 | 213 |
| 212 class DataUseAggregatorTest : public testing::Test { | 214 class DataUseAggregatorTest : public testing::Test { |
| 213 public: | 215 public: |
| 214 DataUseAggregatorTest() | 216 DataUseAggregatorTest() |
| 215 : fake_data_use_annotator_(new FakeDataUseAnnotator()), | 217 : fake_data_use_annotator_(new FakeDataUseAnnotator()), |
| 216 data_use_aggregator_( | 218 data_use_aggregator_( |
| 217 scoped_ptr<DataUseAnnotator>(fake_data_use_annotator_)), | 219 scoped_ptr<DataUseAnnotator>(fake_data_use_annotator_), |
| 220 // TODO(sclittle): Add a test for a non-NULL DataUseAmortizer. |
| 221 scoped_ptr<DataUseAmortizer>()), |
| 218 test_network_change_notifier_(&data_use_aggregator_), | 222 test_network_change_notifier_(&data_use_aggregator_), |
| 219 reporting_network_delegate_(&data_use_aggregator_, | 223 reporting_network_delegate_(&data_use_aggregator_, |
| 220 fake_data_use_annotator_, | 224 fake_data_use_annotator_, |
| 221 &test_network_change_notifier_), | 225 &test_network_change_notifier_), |
| 222 context_(true), | 226 context_(true), |
| 223 test_observer_(&data_use_aggregator_) { | 227 test_observer_(&data_use_aggregator_) { |
| 224 context_.set_client_socket_factory(&mock_socket_factory_); | 228 context_.set_client_socket_factory(&mock_socket_factory_); |
| 225 context_.set_network_delegate(&reporting_network_delegate_); | 229 context_.set_network_delegate(&reporting_network_delegate_); |
| 226 context_.Init(); | 230 context_.Init(); |
| 227 } | 231 } |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 EXPECT_EQ(kBarMccMnc, data_use_it->mcc_mnc); | 339 EXPECT_EQ(kBarMccMnc, data_use_it->mcc_mnc); |
| 336 | 340 |
| 337 observed_bar_tx_bytes += data_use_it->tx_bytes; | 341 observed_bar_tx_bytes += data_use_it->tx_bytes; |
| 338 observed_bar_rx_bytes += data_use_it->rx_bytes; | 342 observed_bar_rx_bytes += data_use_it->rx_bytes; |
| 339 ++data_use_it; | 343 ++data_use_it; |
| 340 } | 344 } |
| 341 EXPECT_EQ(bar_request->GetTotalSentBytes(), observed_bar_tx_bytes); | 345 EXPECT_EQ(bar_request->GetTotalSentBytes(), observed_bar_tx_bytes); |
| 342 EXPECT_EQ(bar_request->GetTotalReceivedBytes(), observed_bar_rx_bytes); | 346 EXPECT_EQ(bar_request->GetTotalReceivedBytes(), observed_bar_rx_bytes); |
| 343 } | 347 } |
| 344 | 348 |
| 345 TEST_F(DataUseAggregatorTest, ReportCombinedDataUse) { | |
| 346 // Set up the |foo_request|. | |
| 347 net::MockRead foo_reads[] = { | |
| 348 net::MockRead(net::SYNCHRONOUS, "HTTP/1.1 200 OK\r\n\r\n"), | |
| 349 net::MockRead(net::SYNCHRONOUS, "hello world"), | |
| 350 net::MockRead(net::SYNCHRONOUS, net::OK), | |
| 351 }; | |
| 352 net::StaticSocketDataProvider foo_socket(foo_reads, arraysize(foo_reads), | |
| 353 nullptr, 0); | |
| 354 mock_socket_factory()->AddSocketDataProvider(&foo_socket); | |
| 355 | |
| 356 net::TestDelegate foo_delegate; | |
| 357 scoped_ptr<net::URLRequest> foo_request = context()->CreateRequest( | |
| 358 GURL("http://foo.com"), net::IDLE, &foo_delegate); | |
| 359 foo_request->set_first_party_for_cookies(GURL("http://foofirstparty.com")); | |
| 360 | |
| 361 // Set up the |bar_request|. | |
| 362 net::MockRead bar_reads[] = { | |
| 363 net::MockRead(net::SYNCHRONOUS, "HTTP/1.1 200 OK\r\n\r\n"), | |
| 364 net::MockRead(net::SYNCHRONOUS, "hello world"), | |
| 365 net::MockRead(net::SYNCHRONOUS, net::OK), | |
| 366 }; | |
| 367 net::StaticSocketDataProvider bar_socket(bar_reads, arraysize(bar_reads), | |
| 368 nullptr, 0); | |
| 369 mock_socket_factory()->AddSocketDataProvider(&bar_socket); | |
| 370 | |
| 371 net::TestDelegate bar_delegate; | |
| 372 scoped_ptr<net::URLRequest> bar_request = context()->CreateRequest( | |
| 373 GURL("http://bar.com"), net::IDLE, &bar_delegate); | |
| 374 bar_request->set_first_party_for_cookies(GURL("http://barfirstparty.com")); | |
| 375 | |
| 376 // Set up the network delegate to assign tab IDs and connection types for each | |
| 377 // request. | |
| 378 const int32_t kFooTabId = 10; | |
| 379 const net::NetworkChangeNotifier::ConnectionType kFooConnectionType = | |
| 380 net::NetworkChangeNotifier::CONNECTION_2G; | |
| 381 const std::string kFooMccMnc = "foo_mcc_mnc"; | |
| 382 const int32_t kBarTabId = 20; | |
| 383 const net::NetworkChangeNotifier::ConnectionType kBarConnectionType = | |
| 384 net::NetworkChangeNotifier::CONNECTION_WIFI; | |
| 385 const std::string kBarMccMnc = "bar_mcc_mnc"; | |
| 386 | |
| 387 ReportingNetworkDelegate::DataUseContextMap data_use_context_map; | |
| 388 data_use_context_map[foo_request.get()] = | |
| 389 ReportingNetworkDelegate::DataUseContext(kFooTabId, kFooConnectionType, | |
| 390 kFooMccMnc); | |
| 391 data_use_context_map[bar_request.get()] = | |
| 392 ReportingNetworkDelegate::DataUseContext(kBarTabId, kBarConnectionType, | |
| 393 kBarMccMnc); | |
| 394 reporting_network_delegate()->set_data_use_context_map(data_use_context_map); | |
| 395 | |
| 396 // Run the requests. | |
| 397 foo_request->Start(); | |
| 398 bar_request->Start(); | |
| 399 base::MessageLoop::current()->RunUntilIdle(); | |
| 400 | |
| 401 // The observer should have been notified once with a DataUse element for each | |
| 402 // request. | |
| 403 EXPECT_EQ(1, test_observer()->on_data_use_called_count()); | |
| 404 EXPECT_EQ(static_cast<size_t>(2), | |
| 405 test_observer()->observed_data_use().size()); | |
| 406 | |
| 407 // All of the |foo_request| DataUse should have been combined into a single | |
| 408 // DataUse element. | |
| 409 const DataUse& foo_data_use = test_observer()->observed_data_use().front(); | |
| 410 EXPECT_EQ(GURL("http://foo.com"), foo_data_use.url); | |
| 411 EXPECT_EQ(GetRequestStart(*foo_request), foo_data_use.request_start); | |
| 412 EXPECT_EQ(GURL("http://foofirstparty.com"), | |
| 413 foo_data_use.first_party_for_cookies); | |
| 414 EXPECT_EQ(kFooTabId, foo_data_use.tab_id); | |
| 415 EXPECT_EQ(kFooConnectionType, foo_data_use.connection_type); | |
| 416 EXPECT_EQ(kFooMccMnc, foo_data_use.mcc_mnc); | |
| 417 EXPECT_EQ(foo_request->GetTotalSentBytes(), foo_data_use.tx_bytes); | |
| 418 EXPECT_EQ(foo_request->GetTotalReceivedBytes(), foo_data_use.rx_bytes); | |
| 419 | |
| 420 // All of the |bar_request| DataUse should have been combined into a single | |
| 421 // DataUse element. | |
| 422 const DataUse& bar_data_use = test_observer()->observed_data_use().back(); | |
| 423 EXPECT_EQ(GURL("http://bar.com"), bar_data_use.url); | |
| 424 EXPECT_EQ(GetRequestStart(*bar_request), bar_data_use.request_start); | |
| 425 EXPECT_EQ(GURL("http://barfirstparty.com"), | |
| 426 bar_data_use.first_party_for_cookies); | |
| 427 EXPECT_EQ(kBarTabId, bar_data_use.tab_id); | |
| 428 EXPECT_EQ(kBarConnectionType, bar_data_use.connection_type); | |
| 429 EXPECT_EQ(kBarMccMnc, bar_data_use.mcc_mnc); | |
| 430 EXPECT_EQ(bar_request->GetTotalSentBytes(), bar_data_use.tx_bytes); | |
| 431 EXPECT_EQ(bar_request->GetTotalReceivedBytes(), bar_data_use.rx_bytes); | |
| 432 } | |
| 433 | |
| 434 TEST_F(DataUseAggregatorTest, ReportOffTheRecordDataUse) { | 349 TEST_F(DataUseAggregatorTest, ReportOffTheRecordDataUse) { |
| 435 // Off the record data use should not be reported to observers. | 350 // Off the record data use should not be reported to observers. |
| 436 data_use_aggregator()->ReportOffTheRecordDataUse(1000, 1000); | 351 data_use_aggregator()->ReportOffTheRecordDataUse(1000, 1000); |
| 437 base::MessageLoop::current()->RunUntilIdle(); | 352 base::MessageLoop::current()->RunUntilIdle(); |
| 438 EXPECT_EQ(static_cast<size_t>(0), | 353 EXPECT_EQ(static_cast<size_t>(0), |
| 439 test_observer()->observed_data_use().size()); | 354 test_observer()->observed_data_use().size()); |
| 440 } | 355 } |
| 441 | 356 |
| 442 } // namespace | 357 } // namespace |
| 443 | 358 |
| 444 } // namespace data_usage | 359 } // namespace data_usage |
| OLD | NEW |