OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/metrics/net/network_metrics_provider.h" |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "base/macros.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "components/metrics/proto/system_profile.pb.h" |
| 13 #include "net/base/network_change_notifier.h" |
| 14 #include "net/nqe/network_quality_estimator_test_util.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 #if defined(OS_CHROMEOS) |
| 18 #include "chromeos/dbus/dbus_thread_manager.h" |
| 19 #include "chromeos/network/network_handler.h" |
| 20 #endif // OS_CHROMEOS |
| 21 |
| 22 namespace metrics { |
| 23 |
| 24 namespace { |
| 25 |
| 26 class TestNetworkQualityEstimatorProvider |
| 27 : public NetworkMetricsProvider::NetworkQualityEstimatorProvider { |
| 28 public: |
| 29 explicit TestNetworkQualityEstimatorProvider( |
| 30 net::TestNetworkQualityEstimator* estimator) |
| 31 : estimator_(estimator) {} |
| 32 ~TestNetworkQualityEstimatorProvider() override {} |
| 33 |
| 34 private: |
| 35 // NetworkMetricsProvider::NetworkQualityEstimatorProvider implementation: |
| 36 scoped_refptr<base::SequencedTaskRunner> GetTaskRunner() override { |
| 37 return base::ThreadTaskRunnerHandle::Get(); |
| 38 } |
| 39 |
| 40 void ProvideEstimator( |
| 41 const base::Callback<void(net::NetworkQualityEstimator*)>& callback) |
| 42 override { |
| 43 callback.Run(estimator_); |
| 44 } |
| 45 |
| 46 net::TestNetworkQualityEstimator* estimator_; |
| 47 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimatorProvider); |
| 48 }; |
| 49 |
| 50 } // namespace |
| 51 |
| 52 // Verifies that the effective connection type is correctly set. |
| 53 TEST(NetworkMetricsProviderTest, EffectiveConnectionType) { |
| 54 base::MessageLoop loop(base::MessageLoop::TYPE_IO); |
| 55 |
| 56 #if defined(OS_CHROMEOS) |
| 57 chromeos::DBusThreadManager::Initialize(); |
| 58 chromeos::NetworkHandler::Initialize(); |
| 59 #endif // OS_CHROMEOS |
| 60 |
| 61 net::TestNetworkQualityEstimator estimator; |
| 62 std::unique_ptr<NetworkMetricsProvider::NetworkQualityEstimatorProvider> |
| 63 estimator_provider(base::WrapUnique( |
| 64 new TestNetworkQualityEstimatorProvider(&estimator))); |
| 65 SystemProfileProto system_profile; |
| 66 NetworkMetricsProvider network_metrics_provider( |
| 67 std::move(estimator_provider), base::ThreadTaskRunnerHandle::Get().get()); |
| 68 |
| 69 EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN, |
| 70 network_metrics_provider.effective_connection_type_); |
| 71 network_metrics_provider.ProvideSystemProfileMetrics(&system_profile); |
| 72 EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_UNKNOWN, |
| 73 system_profile.network().effective_connection_type()); |
| 74 |
| 75 // Set RTT so that the effective connection type is computed as 2G. |
| 76 estimator.set_recent_http_rtt(base::TimeDelta::FromMilliseconds(1500)); |
| 77 estimator.set_start_time_null_http_rtt( |
| 78 base::TimeDelta::FromMilliseconds(1500)); |
| 79 |
| 80 EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN, |
| 81 network_metrics_provider.effective_connection_type_); |
| 82 // Running a request would cause the effective connection type to be computed |
| 83 // as 2G, and observers to be notified. |
| 84 estimator.RunOneRequest(); |
| 85 base::RunLoop().RunUntilIdle(); |
| 86 EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_2G, |
| 87 network_metrics_provider.effective_connection_type_); |
| 88 network_metrics_provider.ProvideSystemProfileMetrics(&system_profile); |
| 89 EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_2G, |
| 90 system_profile.network().effective_connection_type()); |
| 91 |
| 92 // Set RTT so that the effective connection type is computed as SLOW_2G. |
| 93 estimator.set_recent_http_rtt(base::TimeDelta::FromMilliseconds(3000)); |
| 94 estimator.set_start_time_null_http_rtt( |
| 95 base::TimeDelta::FromMilliseconds(3000)); |
| 96 // Running a request would cause the effective connection type to be computed |
| 97 // as SLOW_2G, and observers to be notified. |
| 98 estimator.RunOneRequest(); |
| 99 base::RunLoop().RunUntilIdle(); |
| 100 EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G, |
| 101 network_metrics_provider.effective_connection_type_); |
| 102 network_metrics_provider.ProvideSystemProfileMetrics(&system_profile); |
| 103 // Effective connection type is set to ambiguous since the effective |
| 104 // connection type changed from 2G to SLOW_2G during the lifetime of the log. |
| 105 EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_AMBIGUOUS, |
| 106 system_profile.network().effective_connection_type()); |
| 107 |
| 108 // Getting the system profile again should return the actual effective |
| 109 // connection type since the effective connection type did not change during |
| 110 // the lifetime of the log. |
| 111 network_metrics_provider.ProvideSystemProfileMetrics(&system_profile); |
| 112 EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_SLOW_2G, |
| 113 system_profile.network().effective_connection_type()); |
| 114 } |
| 115 |
| 116 } // namespace metrics |
OLD | NEW |