xref: /aosp_15_r20/external/cronet/net/nqe/network_quality_estimator_params_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 "net/nqe/network_quality_estimator_params.h"
6 
7 #include <map>
8 #include <string>
9 
10 #include "net/base/network_change_notifier.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 
13 namespace net::nqe::internal {
14 
15 namespace {
16 
17 // Tests if |weight_multiplier_per_second()| returns correct value for various
18 // values of half life parameter.
TEST(NetworkQualityEstimatorParamsTest,HalfLifeParam)19 TEST(NetworkQualityEstimatorParamsTest, HalfLifeParam) {
20   std::map<std::string, std::string> variation_params;
21 
22   const struct {
23     std::string description;
24     std::string variation_params_value;
25     double expected_weight_multiplier;
26   } tests[] = {
27       {"Half life parameter is not set, default value should be used",
28        std::string(), 0.988},
29       {"Half life parameter is set to negative, default value should be used",
30        "-100", 0.988},
31       {"Half life parameter is set to zero, default value should be used", "0",
32        0.988},
33       {"Half life parameter is set correctly", "10", 0.933},
34   };
35 
36   for (const auto& test : tests) {
37     variation_params["HalfLifeSeconds"] = test.variation_params_value;
38     NetworkQualityEstimatorParams params(variation_params);
39     EXPECT_NEAR(test.expected_weight_multiplier,
40                 params.weight_multiplier_per_second(), 0.001)
41         << test.description;
42   }
43 }
44 
45 // Test that the typical network qualities are set correctly.
TEST(NetworkQualityEstimatorParamsTest,TypicalNetworkQualities)46 TEST(NetworkQualityEstimatorParamsTest, TypicalNetworkQualities) {
47   std::map<std::string, std::string> variation_params;
48   NetworkQualityEstimatorParams params(variation_params);
49 
50   // Typical network quality should not be set for Unknown and Offline.
51   for (size_t i = EFFECTIVE_CONNECTION_TYPE_UNKNOWN;
52        i <= EFFECTIVE_CONNECTION_TYPE_OFFLINE; ++i) {
53     EffectiveConnectionType ect = static_cast<EffectiveConnectionType>(i);
54     EXPECT_EQ(nqe::internal::InvalidRTT(),
55               params.TypicalNetworkQuality(ect).http_rtt());
56 
57     EXPECT_EQ(nqe::internal::InvalidRTT(),
58               params.TypicalNetworkQuality(ect).transport_rtt());
59   }
60 
61   // Typical network quality should be set for other effective connection
62   // types.
63   for (size_t i = EFFECTIVE_CONNECTION_TYPE_SLOW_2G;
64        i <= EFFECTIVE_CONNECTION_TYPE_3G; ++i) {
65     EffectiveConnectionType ect = static_cast<EffectiveConnectionType>(i);
66     // The typical RTT for an effective connection type should be at least as
67     // much as the threshold RTT.
68     EXPECT_NE(nqe::internal::InvalidRTT(),
69               params.TypicalNetworkQuality(ect).http_rtt());
70     EXPECT_GT(params.TypicalNetworkQuality(ect).http_rtt(),
71               params.ConnectionThreshold(ect).http_rtt());
72 
73     EXPECT_NE(nqe::internal::InvalidRTT(),
74               params.TypicalNetworkQuality(ect).transport_rtt());
75     EXPECT_EQ(nqe::internal::InvalidRTT(),
76               params.ConnectionThreshold(ect).transport_rtt());
77 
78     EXPECT_NE(nqe::internal::INVALID_RTT_THROUGHPUT,
79               params.TypicalNetworkQuality(ect).downstream_throughput_kbps());
80     EXPECT_EQ(nqe::internal::INVALID_RTT_THROUGHPUT,
81               params.ConnectionThreshold(ect).downstream_throughput_kbps());
82 
83     EXPECT_EQ(params.TypicalNetworkQuality(ect).http_rtt(),
84               NetworkQualityEstimatorParams::GetDefaultTypicalHttpRtt(ect));
85     EXPECT_EQ(
86         params.TypicalNetworkQuality(ect).downstream_throughput_kbps(),
87         NetworkQualityEstimatorParams::GetDefaultTypicalDownlinkKbps(ect));
88   }
89 
90   // The typical network quality of 4G connection should be at least as fast
91   // as the threshold for 3G connection.
92   EXPECT_LT(
93       params.TypicalNetworkQuality(EFFECTIVE_CONNECTION_TYPE_4G).http_rtt(),
94       params.ConnectionThreshold(EFFECTIVE_CONNECTION_TYPE_3G).http_rtt());
95 
96   EXPECT_NE(nqe::internal::InvalidRTT(),
97             params.TypicalNetworkQuality(EFFECTIVE_CONNECTION_TYPE_4G)
98                 .transport_rtt());
99   EXPECT_EQ(
100       nqe::internal::InvalidRTT(),
101       params.ConnectionThreshold(EFFECTIVE_CONNECTION_TYPE_4G).transport_rtt());
102 
103   EXPECT_NE(nqe::internal::INVALID_RTT_THROUGHPUT,
104             params.TypicalNetworkQuality(EFFECTIVE_CONNECTION_TYPE_4G)
105                 .downstream_throughput_kbps());
106 
107   EXPECT_EQ(nqe::internal::INVALID_RTT_THROUGHPUT,
108             params.ConnectionThreshold(EFFECTIVE_CONNECTION_TYPE_4G)
109                 .downstream_throughput_kbps());
110 }
111 
112 // Verify ECT when forced ECT is Slow-2G-On-Cellular.
TEST(NetworkQualityEstimatorParamsTest,GetForcedECTCellularOnly)113 TEST(NetworkQualityEstimatorParamsTest, GetForcedECTCellularOnly) {
114   std::map<std::string, std::string> variation_params;
115   // Set force-effective-connection-type to Slow-2G-On-Cellular.
116   variation_params[kForceEffectiveConnectionType] =
117       kEffectiveConnectionTypeSlow2GOnCellular;
118 
119   NetworkQualityEstimatorParams params(variation_params);
120 
121   for (size_t i = 0; i < NetworkChangeNotifier::ConnectionType::CONNECTION_LAST;
122        ++i) {
123     NetworkChangeNotifier::ConnectionType connection_type =
124         static_cast<NetworkChangeNotifier::ConnectionType>(i);
125     std::optional<EffectiveConnectionType> ect =
126         params.GetForcedEffectiveConnectionType(connection_type);
127 
128     if (net::NetworkChangeNotifier::IsConnectionCellular(connection_type)) {
129       // Test for cellular connection types. Make sure that ECT is Slow-2G.
130       EXPECT_EQ(EFFECTIVE_CONNECTION_TYPE_SLOW_2G, ect);
131     } else {
132       // Test for non-cellular connection types. Make sure that there is no
133       // forced ect.
134       EXPECT_EQ(std::nullopt, ect);
135     }
136   }
137 }
138 
139 }  // namespace
140 
141 }  // namespace net::nqe::internal
142