effective_connection_type_unittest.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2016 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. #include "net/nqe/effective_connection_type.h"
  5. #include <string>
  6. #include "testing/gtest/include/gtest/gtest.h"
  7. #include "third_party/abseil-cpp/absl/types/optional.h"
  8. namespace net {
  9. namespace {
  10. // Tests that the effective connection type is converted correctly to a
  11. // descriptive string name, and vice-versa.
  12. TEST(EffectiveConnectionTypeTest, NameConnectionTypeConversion) {
  13. // Verify GetEffectiveConnectionTypeForName() returns an unset value when an
  14. // invalid effective connection type name is provided.
  15. EXPECT_FALSE(
  16. GetEffectiveConnectionTypeForName("InvalidEffectiveConnectionTypeName"));
  17. EXPECT_FALSE(GetEffectiveConnectionTypeForName(std::string()));
  18. for (size_t i = 0; i < EFFECTIVE_CONNECTION_TYPE_LAST; ++i) {
  19. const EffectiveConnectionType effective_connection_type =
  20. static_cast<EffectiveConnectionType>(i);
  21. std::string connection_type_name = std::string(
  22. GetNameForEffectiveConnectionType(effective_connection_type));
  23. EXPECT_FALSE(connection_type_name.empty());
  24. if (effective_connection_type != EFFECTIVE_CONNECTION_TYPE_SLOW_2G) {
  25. // For all effective connection types except Slow2G,
  26. // DeprecatedGetNameForEffectiveConnectionType should return the same
  27. // name as GetNameForEffectiveConnectionType.
  28. EXPECT_EQ(connection_type_name,
  29. DeprecatedGetNameForEffectiveConnectionType(
  30. effective_connection_type));
  31. }
  32. EXPECT_EQ(effective_connection_type,
  33. GetEffectiveConnectionTypeForName(connection_type_name));
  34. }
  35. }
  36. // Tests that the Slow 2G effective connection type is converted correctly to a
  37. // descriptive string name, and vice-versa.
  38. TEST(EffectiveConnectionTypeTest, Slow2GTypeConversion) {
  39. // GetEffectiveConnectionTypeForName should return Slow2G as effective
  40. // connection type for both the deprecated and the current string
  41. // representation.
  42. absl::optional<EffectiveConnectionType> type =
  43. GetEffectiveConnectionTypeForName("Slow2G");
  44. EXPECT_EQ(EFFECTIVE_CONNECTION_TYPE_SLOW_2G, type.value());
  45. type = GetEffectiveConnectionTypeForName("Slow-2G");
  46. EXPECT_EQ(EFFECTIVE_CONNECTION_TYPE_SLOW_2G, type.value());
  47. EXPECT_EQ("Slow-2G", std::string(GetNameForEffectiveConnectionType(
  48. EFFECTIVE_CONNECTION_TYPE_SLOW_2G)));
  49. EXPECT_EQ("Slow2G", std::string(DeprecatedGetNameForEffectiveConnectionType(
  50. EFFECTIVE_CONNECTION_TYPE_SLOW_2G)));
  51. }
  52. } // namespace
  53. } // namespace net