OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
Alexei Svitkine (slow)
2017/01/04 15:18:59
2017
tbansal1
2017/01/04 20:47:54
Done.
| |
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 void ProvideEstimator( | |
37 const base::Callback<void(net::NetworkQualityEstimator*)>& callback) | |
38 override { | |
39 callback.Run(estimator_); | |
40 } | |
41 | |
42 net::TestNetworkQualityEstimator* estimator_; | |
43 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimatorProvider); | |
44 }; | |
45 | |
46 } // namespace | |
47 | |
48 // Verifies that the effective connection type is correctly set. | |
49 TEST(NetworkMetricsProviderTest, EffectiveConnectionType) { | |
50 base::MessageLoop loop(base::MessageLoop::TYPE_IO); | |
51 | |
52 #if defined(OS_CHROMEOS) | |
53 chromeos::DBusThreadManager::Initialize(); | |
54 chromeos::NetworkHandler::Initialize(); | |
55 #endif // OS_CHROMEOS | |
56 | |
57 net::TestNetworkQualityEstimator estimator; | |
58 std::unique_ptr<NetworkMetricsProvider::NetworkQualityEstimatorProvider> | |
59 estimator_provider(base::WrapUnique( | |
60 new TestNetworkQualityEstimatorProvider(&estimator))); | |
61 SystemProfileProto system_profile; | |
62 NetworkMetricsProvider network_metrics_provider( | |
63 base::ThreadTaskRunnerHandle::Get(), std::move(estimator_provider), | |
64 base::ThreadTaskRunnerHandle::Get().get()); | |
65 | |
66 EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN, | |
67 network_metrics_provider.effective_connection_type_); | |
68 network_metrics_provider.ProvideSystemProfileMetrics(&system_profile); | |
69 EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_UNKNOWN, | |
70 system_profile.network().effective_connection_type()); | |
71 | |
72 // Set RTT so that the effective connection type is computed as 2G. | |
73 estimator.set_recent_http_rtt(base::TimeDelta::FromMilliseconds(1500)); | |
74 estimator.set_start_time_null_http_rtt( | |
75 base::TimeDelta::FromMilliseconds(1500)); | |
76 | |
77 EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN, | |
78 network_metrics_provider.effective_connection_type_); | |
79 // Running a request would cause the effective connection type to be computed | |
80 // as 2G, and observers to be notified. | |
81 estimator.RunOneRequest(); | |
82 base::RunLoop().RunUntilIdle(); | |
83 EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_2G, | |
84 network_metrics_provider.effective_connection_type_); | |
85 network_metrics_provider.ProvideSystemProfileMetrics(&system_profile); | |
86 EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_2G, | |
87 system_profile.network().effective_connection_type()); | |
88 | |
89 // Set RTT so that the effective connection type is computed as SLOW_2G. | |
90 estimator.set_recent_http_rtt(base::TimeDelta::FromMilliseconds(3000)); | |
91 estimator.set_start_time_null_http_rtt( | |
92 base::TimeDelta::FromMilliseconds(3000)); | |
93 // Running a request would cause the effective connection type to be computed | |
94 // as SLOW_2G, and observers to be notified. | |
95 estimator.RunOneRequest(); | |
96 base::RunLoop().RunUntilIdle(); | |
97 EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G, | |
98 network_metrics_provider.effective_connection_type_); | |
99 network_metrics_provider.ProvideSystemProfileMetrics(&system_profile); | |
100 // Effective connection type is set to ambiguous since the effective | |
101 // connection type changed from 2G to SLOW_2G during the lifetime of the log. | |
102 EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_AMBIGUOUS, | |
103 system_profile.network().effective_connection_type()); | |
104 | |
105 // Getting the system profile again should return the actual effective | |
106 // connection type since the effective connection type did not change during | |
107 // the lifetime of the log. | |
108 network_metrics_provider.ProvideSystemProfileMetrics(&system_profile); | |
109 EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_SLOW_2G, | |
110 system_profile.network().effective_connection_type()); | |
111 } | |
112 | |
113 } // namespace metrics | |
OLD | NEW |