xref: /aosp_15_r20/external/cronet/components/metrics/net/network_metrics_provider_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2017 The Chromium Authors
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 <memory>
8 
9 #include "base/functional/callback.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/run_loop.h"
12 #include "build/build_config.h"
13 #include "build/chromeos_buildflags.h"
14 #include "services/network/test/test_network_connection_tracker.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/metrics_proto/system_profile.pb.h"
17 
18 #if BUILDFLAG(IS_CHROMEOS_ASH)
19 #include "chromeos/ash/components/network/network_handler_test_helper.h"
20 #endif  // BUILDFLAG(IS_CHROMEOS_ASH)
21 
22 #if BUILDFLAG(IS_IOS)
23 #include "ios/web/public/test/web_task_environment.h"
24 using MetricsTaskEnvironment = web::WebTaskEnvironment;
25 #else  // !BUILDFLAG(IS_IOS)
26 #include "content/public/test/browser_task_environment.h"
27 using MetricsTaskEnvironment = content::BrowserTaskEnvironment;
28 #endif  // BUILDFLAG(IS_IOS)
29 
30 namespace metrics {
31 
32 class NetworkMetricsProviderTest : public testing::Test {
33  public:
34   NetworkMetricsProviderTest(const NetworkMetricsProviderTest&) = delete;
35   NetworkMetricsProviderTest& operator=(const NetworkMetricsProviderTest&) =
36       delete;
37 
38  protected:
NetworkMetricsProviderTest()39   NetworkMetricsProviderTest()
40       : task_environment_(MetricsTaskEnvironment::IO_MAINLOOP) {}
~NetworkMetricsProviderTest()41   ~NetworkMetricsProviderTest() override {}
42 
43  private:
44   MetricsTaskEnvironment task_environment_;
45 #if BUILDFLAG(IS_CHROMEOS_ASH)
46   ash::NetworkHandlerTestHelper network_handler_test_helper_;
47 #endif  // BUILDFLAG(IS_CHROMEOS_ASH)
48 };
49 
50 // Verifies that the effective connection type is correctly set.
TEST_F(NetworkMetricsProviderTest,EffectiveConnectionType)51 TEST_F(NetworkMetricsProviderTest, EffectiveConnectionType) {
52   NetworkMetricsProvider network_metrics_provider(
53       network::TestNetworkConnectionTracker::CreateAsyncGetter());
54   base::RunLoop().RunUntilIdle();
55 
56   EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN,
57             network_metrics_provider.effective_connection_type_);
58   EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN,
59             network_metrics_provider.min_effective_connection_type_);
60   EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN,
61             network_metrics_provider.max_effective_connection_type_);
62   SystemProfileProto system_profile;
63   network_metrics_provider.ProvideSystemProfileMetrics(&system_profile);
64   EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_UNKNOWN,
65             system_profile.network().min_effective_connection_type());
66   EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_UNKNOWN,
67             system_profile.network().max_effective_connection_type());
68 
69   network_metrics_provider.OnEffectiveConnectionTypeChanged(
70       net::EFFECTIVE_CONNECTION_TYPE_2G);
71   EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_2G,
72             network_metrics_provider.effective_connection_type_);
73   EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_2G,
74             network_metrics_provider.min_effective_connection_type_);
75   EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_2G,
76             network_metrics_provider.max_effective_connection_type_);
77   network_metrics_provider.ProvideSystemProfileMetrics(&system_profile);
78   EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_2G,
79             system_profile.network().min_effective_connection_type());
80   EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_2G,
81             system_profile.network().max_effective_connection_type());
82 
83   network_metrics_provider.OnEffectiveConnectionTypeChanged(
84       net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
85   EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G,
86             network_metrics_provider.effective_connection_type_);
87   EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G,
88             network_metrics_provider.min_effective_connection_type_);
89   EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_2G,
90             network_metrics_provider.max_effective_connection_type_);
91   network_metrics_provider.ProvideSystemProfileMetrics(&system_profile);
92   // Effective connection type changed from 2G to SLOW_2G during the lifetime of
93   // the log. Minimum value of ECT must be different from the maximum value.
94   EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_SLOW_2G,
95             system_profile.network().min_effective_connection_type());
96   EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_2G,
97             system_profile.network().max_effective_connection_type());
98 
99   // Getting the system profile again should return the current effective
100   // connection type.
101   network_metrics_provider.ProvideSystemProfileMetrics(&system_profile);
102   EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_SLOW_2G,
103             system_profile.network().min_effective_connection_type());
104   EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_SLOW_2G,
105             system_profile.network().max_effective_connection_type());
106 }
107 
108 // Verifies that the effective connection type is not set to UNKNOWN when there
109 // is a change in the connection type.
TEST_F(NetworkMetricsProviderTest,ECTAmbiguousOnConnectionTypeChange)110 TEST_F(NetworkMetricsProviderTest, ECTAmbiguousOnConnectionTypeChange) {
111   NetworkMetricsProvider network_metrics_provider(
112       network::TestNetworkConnectionTracker::CreateAsyncGetter());
113   base::RunLoop().RunUntilIdle();
114 
115   EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN,
116             network_metrics_provider.effective_connection_type_);
117   EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN,
118             network_metrics_provider.min_effective_connection_type_);
119   EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN,
120             network_metrics_provider.max_effective_connection_type_);
121 
122   network_metrics_provider.OnEffectiveConnectionTypeChanged(
123       net::EFFECTIVE_CONNECTION_TYPE_2G);
124   EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_2G,
125             network_metrics_provider.effective_connection_type_);
126   EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_2G,
127             network_metrics_provider.min_effective_connection_type_);
128   EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_2G,
129             network_metrics_provider.max_effective_connection_type_);
130 
131   // There is no change in the connection type. Effective connection types
132   // should be reported as 2G.
133   SystemProfileProto system_profile;
134   network_metrics_provider.ProvideSystemProfileMetrics(&system_profile);
135   EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_2G,
136             system_profile.network().min_effective_connection_type());
137   EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_2G,
138             system_profile.network().max_effective_connection_type());
139 
140   // Even with change in the connection type, effective connection types
141   // should be reported as 2G.
142   network_metrics_provider.OnConnectionChanged(
143       network::mojom::ConnectionType::CONNECTION_2G);
144   network_metrics_provider.ProvideSystemProfileMetrics(&system_profile);
145   EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_2G,
146             system_profile.network().min_effective_connection_type());
147   EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_2G,
148             system_profile.network().max_effective_connection_type());
149 }
150 
151 // Verifies that the effective connection type is not set to UNKNOWN when the
152 // connection type is OFFLINE.
TEST_F(NetworkMetricsProviderTest,ECTNotAmbiguousOnUnknownOrOffline)153 TEST_F(NetworkMetricsProviderTest, ECTNotAmbiguousOnUnknownOrOffline) {
154   for (net::EffectiveConnectionType force_ect :
155        {net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN,
156         net::EFFECTIVE_CONNECTION_TYPE_OFFLINE}) {
157     NetworkMetricsProvider network_metrics_provider(
158         network::TestNetworkConnectionTracker::CreateAsyncGetter());
159     base::RunLoop().RunUntilIdle();
160 
161     network_metrics_provider.OnEffectiveConnectionTypeChanged(
162         net::EFFECTIVE_CONNECTION_TYPE_2G);
163 
164     SystemProfileProto system_profile;
165     network_metrics_provider.ProvideSystemProfileMetrics(&system_profile);
166 
167     network_metrics_provider.OnEffectiveConnectionTypeChanged(force_ect);
168 
169     network_metrics_provider.ProvideSystemProfileMetrics(&system_profile);
170     EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_2G,
171               system_profile.network().min_effective_connection_type());
172     EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_2G,
173               system_profile.network().max_effective_connection_type());
174 
175     network_metrics_provider.OnEffectiveConnectionTypeChanged(
176         net::EFFECTIVE_CONNECTION_TYPE_4G);
177     network_metrics_provider.ProvideSystemProfileMetrics(&system_profile);
178     EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_4G,
179               system_profile.network().min_effective_connection_type());
180     EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_4G,
181               system_profile.network().max_effective_connection_type());
182   }
183 }
184 
185 // Verifies that the connection type is ambiguous boolean is correctly set.
TEST_F(NetworkMetricsProviderTest,ConnectionTypeIsAmbiguous)186 TEST_F(NetworkMetricsProviderTest, ConnectionTypeIsAmbiguous) {
187   NetworkMetricsProvider network_metrics_provider(
188       network::TestNetworkConnectionTracker::CreateAsyncGetter());
189 
190   EXPECT_EQ(network::mojom::ConnectionType::CONNECTION_UNKNOWN,
191             network_metrics_provider.connection_type_);
192   EXPECT_FALSE(network_metrics_provider.connection_type_is_ambiguous_);
193   EXPECT_FALSE(
194       network_metrics_provider.network_connection_tracker_initialized_);
195 
196   // When a connection type change callback is received, network change notifier
197   // should be marked as initialized.
198   network_metrics_provider.OnConnectionChanged(
199       network::mojom::ConnectionType::CONNECTION_2G);
200   EXPECT_EQ(network::mojom::ConnectionType::CONNECTION_2G,
201             network_metrics_provider.connection_type_);
202   // Connection type should not be marked as ambiguous when a delayed connection
203   // type change callback is received due to delayed initialization of the
204   // network change notifier.
205   EXPECT_FALSE(network_metrics_provider.connection_type_is_ambiguous_);
206   EXPECT_TRUE(network_metrics_provider.network_connection_tracker_initialized_);
207 
208   // On collection of the system profile, |connection_type_is_ambiguous_| should
209   // stay false, and |network_connection_tracker_initialized_| should remain
210   // true.
211   SystemProfileProto system_profile;
212   network_metrics_provider.ProvideSystemProfileMetrics(&system_profile);
213   EXPECT_FALSE(network_metrics_provider.connection_type_is_ambiguous_);
214   EXPECT_TRUE(network_metrics_provider.network_connection_tracker_initialized_);
215   EXPECT_FALSE(system_profile.network().connection_type_is_ambiguous());
216   EXPECT_EQ(SystemProfileProto::Network::CONNECTION_2G,
217             system_profile.network().connection_type());
218 
219   network_metrics_provider.OnConnectionChanged(
220       network::mojom::ConnectionType::CONNECTION_3G);
221   EXPECT_TRUE(network_metrics_provider.connection_type_is_ambiguous_);
222   EXPECT_TRUE(network_metrics_provider.network_connection_tracker_initialized_);
223 
224   // On collection of the system profile, |connection_type_is_ambiguous_| should
225   // be reset to false, and |network_connection_tracker_initialized_| should
226   // remain true.
227   network_metrics_provider.ProvideSystemProfileMetrics(&system_profile);
228   EXPECT_FALSE(network_metrics_provider.connection_type_is_ambiguous_);
229   EXPECT_TRUE(network_metrics_provider.network_connection_tracker_initialized_);
230   EXPECT_TRUE(system_profile.network().connection_type_is_ambiguous());
231   EXPECT_EQ(SystemProfileProto::Network::CONNECTION_3G,
232             system_profile.network().connection_type());
233 }
234 
235 }  // namespace metrics
236