Chromium Code Reviews| 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 "net/base/network_quality_estimator.h" | 5 #include "net/base/network_quality_estimator.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <map> | 10 #include <map> |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 #include "net/url_request/url_request_test_util.h" | 25 #include "net/url_request/url_request_test_util.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 27 #include "url/gurl.h" | 27 #include "url/gurl.h" |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 // Helps in setting the current network type and id. | 31 // Helps in setting the current network type and id. |
| 32 class TestNetworkQualityEstimator : public net::NetworkQualityEstimator { | 32 class TestNetworkQualityEstimator : public net::NetworkQualityEstimator { |
| 33 public: | 33 public: |
| 34 TestNetworkQualityEstimator( | 34 TestNetworkQualityEstimator( |
| 35 const std::map<std::string, std::string>& variation_params) | 35 const std::map<std::string, std::string>& variation_params, |
| 36 : NetworkQualityEstimator(scoped_ptr<net::ExternalEstimateProvider>(), | 36 scoped_ptr<net::ExternalEstimateProvider> external_estimate_provider) |
| 37 : NetworkQualityEstimator(external_estimate_provider.Pass(), | |
| 37 variation_params, | 38 variation_params, |
| 38 true, | 39 true, |
| 39 true) {} | 40 true) {} |
| 40 | 41 |
| 42 explicit TestNetworkQualityEstimator( | |
| 43 const std::map<std::string, std::string>& variation_params) | |
| 44 : TestNetworkQualityEstimator( | |
| 45 variation_params, | |
| 46 scoped_ptr<net::ExternalEstimateProvider>()) {} | |
| 47 | |
| 41 ~TestNetworkQualityEstimator() override {} | 48 ~TestNetworkQualityEstimator() override {} |
| 42 | 49 |
| 43 // Overrides the current network type and id. | 50 // Overrides the current network type and id. |
| 44 // Notifies network quality estimator of change in connection. | 51 // Notifies network quality estimator of change in connection. |
| 45 void SimulateNetworkChangeTo(net::NetworkChangeNotifier::ConnectionType type, | 52 void SimulateNetworkChangeTo(net::NetworkChangeNotifier::ConnectionType type, |
| 46 std::string network_id) { | 53 std::string network_id) { |
| 47 current_network_type_ = type; | 54 current_network_type_ = type; |
| 48 current_network_id_ = network_id; | 55 current_network_id_ = network_id; |
| 49 OnConnectionTypeChanged(type); | 56 OnConnectionTypeChanged(type); |
| 50 } | 57 } |
| (...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 647 EXPECT_EQ(100, rtt.InMilliseconds()); | 654 EXPECT_EQ(100, rtt.InMilliseconds()); |
| 648 | 655 |
| 649 int32_t downstream_throughput_kbps; | 656 int32_t downstream_throughput_kbps; |
| 650 EXPECT_FALSE(estimator.GetRecentMedianDownlinkThroughputKbps( | 657 EXPECT_FALSE(estimator.GetRecentMedianDownlinkThroughputKbps( |
| 651 now + base::TimeDelta::FromSeconds(10), &downstream_throughput_kbps)); | 658 now + base::TimeDelta::FromSeconds(10), &downstream_throughput_kbps)); |
| 652 EXPECT_TRUE(estimator.GetRecentMedianDownlinkThroughputKbps( | 659 EXPECT_TRUE(estimator.GetRecentMedianDownlinkThroughputKbps( |
| 653 now, &downstream_throughput_kbps)); | 660 now, &downstream_throughput_kbps)); |
| 654 EXPECT_EQ(100, downstream_throughput_kbps); | 661 EXPECT_EQ(100, downstream_throughput_kbps); |
| 655 } | 662 } |
| 656 | 663 |
| 664 class TestExternalEstimateProvider : public ExternalEstimateProvider { | |
| 665 public: | |
| 666 TestExternalEstimateProvider() | |
| 667 : time_since_last_update_(base::TimeDelta::FromSeconds(1)), | |
| 668 time_since_last_update_count_(0), | |
| 669 rtt_calls_count_(0), | |
| 670 downstream_throughput_kbps_count_(0), | |
| 671 request_update_count_(0) {} | |
| 672 ~TestExternalEstimateProvider() override {} | |
| 673 | |
|
mmenke
2015/09/14 20:07:06
nit: Add something along the lines of:
// Extern
tbansal1
2015/09/14 23:46:56
Done.
| |
| 674 bool GetRTT(base::TimeDelta* rtt) const override { | |
| 675 *rtt = base::TimeDelta::FromMilliseconds(1); | |
| 676 rtt_calls_count_++; | |
| 677 return true; | |
| 678 } | |
| 679 | |
| 680 bool GetDownstreamThroughputKbps( | |
| 681 int32_t* downstream_throughput_kbps) const override { | |
| 682 *downstream_throughput_kbps = 100; | |
| 683 downstream_throughput_kbps_count_++; | |
| 684 return false; | |
| 685 } | |
| 686 | |
| 687 bool GetUpstreamThroughputKbps( | |
| 688 int32_t* upstream_throughput_kbps) const override { | |
| 689 // NetworkQualityEstimator does not support upstream throughput. | |
| 690 NOTIMPLEMENTED(); | |
|
mmenke
2015/09/14 20:07:06
This should probably be ADD_FAILURE
tbansal1
2015/09/14 23:46:57
Done.
| |
| 691 return false; | |
| 692 } | |
| 693 | |
| 694 bool GetTimeSinceLastUpdate( | |
| 695 base::TimeDelta* time_since_last_update) const override { | |
| 696 *time_since_last_update = time_since_last_update_; | |
| 697 time_since_last_update_count_++; | |
| 698 return true; | |
| 699 } | |
| 700 | |
| 701 void SetTimeSinceLastUpdate(const base::TimeDelta& time_since_last_update) { | |
|
mmenke
2015/09/14 20:07:06
nit: This should not go in the middle of the over
mmenke
2015/09/14 20:07:06
nit: set_time_since_last_update(base::TimeDelta t
tbansal1
2015/09/14 23:46:56
Done.
tbansal1
2015/09/14 23:46:57
Done.
| |
| 702 time_since_last_update_ = time_since_last_update; | |
| 703 } | |
| 704 | |
| 705 void SetUpdatedEstimateDelegate(UpdatedEstimateDelegate* delegate) override {} | |
| 706 | |
| 707 void RequestUpdate() const override { request_update_count_++; } | |
| 708 | |
| 709 size_t time_since_last_update_count() const { | |
| 710 return time_since_last_update_count_; | |
| 711 } | |
| 712 size_t rtt_calls_count() const { return rtt_calls_count_; } | |
| 713 size_t downstream_throughput_kbps_count() const { | |
| 714 return downstream_throughput_kbps_count_; | |
| 715 } | |
| 716 size_t request_update_count() const { return request_update_count_; } | |
| 717 | |
| 718 private: | |
| 719 base::TimeDelta time_since_last_update_; | |
| 720 | |
| 721 mutable size_t time_since_last_update_count_; | |
| 722 mutable size_t rtt_calls_count_; | |
|
mmenke
2015/09/14 20:07:06
nit: get_rtt_count_, to match request_update_coun
tbansal1
2015/09/14 23:46:57
Did you mean rtt_count_?
mmenke
2015/09/15 15:19:53
No, I meant get_rtt_count_. Name of the method it
tbansal1
2015/09/15 16:48:30
That makes sense. Changed the metod names to have
| |
| 723 mutable size_t downstream_throughput_kbps_count_; | |
| 724 mutable size_t request_update_count_; | |
| 725 }; | |
| 726 | |
| 727 // Tests if the external estimate provider is called in the constructor and | |
| 728 // on network change notification. | |
| 729 TEST(NetworkQualityEstimatorTest, TestExternalEstimateProvider) { | |
| 730 TestExternalEstimateProvider* test_external_estimate_provider = | |
| 731 new TestExternalEstimateProvider(); | |
| 732 scoped_ptr<ExternalEstimateProvider> external_estimate_provider( | |
| 733 test_external_estimate_provider); | |
| 734 std::map<std::string, std::string> variation_params; | |
| 735 TestNetworkQualityEstimator estimator(variation_params, | |
| 736 external_estimate_provider.Pass()); | |
| 737 EXPECT_EQ(1U, | |
| 738 test_external_estimate_provider->time_since_last_update_count()); | |
| 739 EXPECT_EQ(1U, test_external_estimate_provider->rtt_calls_count()); | |
| 740 EXPECT_EQ( | |
| 741 1U, test_external_estimate_provider->downstream_throughput_kbps_count()); | |
| 742 | |
| 743 // Change network type to WiFi. Number of queries to External estimate | |
| 744 // provider must increment. | |
| 745 estimator.SimulateNetworkChangeTo( | |
| 746 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, "test-1"); | |
| 747 EXPECT_EQ(2U, | |
| 748 test_external_estimate_provider->time_since_last_update_count()); | |
| 749 EXPECT_EQ(2U, test_external_estimate_provider->rtt_calls_count()); | |
| 750 EXPECT_EQ( | |
| 751 2U, test_external_estimate_provider->downstream_throughput_kbps_count()); | |
| 752 | |
| 753 // Change network type to 2G. Number of queries to External estimate provider | |
| 754 // must increment. | |
| 755 estimator.SimulateNetworkChangeTo( | |
| 756 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1"); | |
| 757 EXPECT_EQ(3U, | |
| 758 test_external_estimate_provider->time_since_last_update_count()); | |
| 759 EXPECT_EQ(3U, test_external_estimate_provider->rtt_calls_count()); | |
| 760 EXPECT_EQ( | |
| 761 3U, test_external_estimate_provider->downstream_throughput_kbps_count()); | |
| 762 | |
| 763 // Set the external estimate as old. Network Quality estimator should request | |
| 764 // an update on connection type change. | |
| 765 EXPECT_EQ(0U, test_external_estimate_provider->request_update_count()); | |
| 766 test_external_estimate_provider->SetTimeSinceLastUpdate( | |
| 767 base::TimeDelta::Max()); | |
| 768 estimator.SimulateNetworkChangeTo( | |
| 769 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1"); | |
| 770 EXPECT_EQ(4U, | |
| 771 test_external_estimate_provider->time_since_last_update_count()); | |
| 772 EXPECT_EQ(3U, test_external_estimate_provider->rtt_calls_count()); | |
| 773 EXPECT_EQ( | |
| 774 3U, test_external_estimate_provider->downstream_throughput_kbps_count()); | |
| 775 EXPECT_EQ(1U, test_external_estimate_provider->request_update_count()); | |
| 776 } | |
| 777 | |
| 657 } // namespace net | 778 } // namespace net |
| OLD | NEW |