network_qualities_pref_delegate_unittest.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2018 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 "services/network/network_qualities_pref_delegate.h"
  5. #include <map>
  6. #include <string>
  7. #include "base/bind.h"
  8. #include "base/test/metrics/histogram_tester.h"
  9. #include "base/test/task_environment.h"
  10. #include "components/prefs/testing_pref_service.h"
  11. #include "net/base/network_change_notifier.h"
  12. #include "net/nqe/cached_network_quality.h"
  13. #include "net/nqe/effective_connection_type.h"
  14. #include "net/nqe/network_id.h"
  15. #include "net/nqe/network_quality_estimator_test_util.h"
  16. #include "testing/gtest/include/gtest/gtest.h"
  17. namespace network {
  18. namespace {
  19. class NetworkQualitiesPrefDelegateTest : public testing::Test {
  20. public:
  21. NetworkQualitiesPrefDelegateTest()
  22. : task_environment_(base::test::TaskEnvironment::MainThreadType::IO) {}
  23. NetworkQualitiesPrefDelegateTest(const NetworkQualitiesPrefDelegateTest&) =
  24. delete;
  25. NetworkQualitiesPrefDelegateTest& operator=(
  26. const NetworkQualitiesPrefDelegateTest&) = delete;
  27. ~NetworkQualitiesPrefDelegateTest() override = default;
  28. private:
  29. base::test::TaskEnvironment task_environment_;
  30. };
  31. // Verify that prefs are writen and read correctly.
  32. TEST_F(NetworkQualitiesPrefDelegateTest, WritingReadingToPrefsEnabled) {
  33. TestingPrefServiceSimple pref_service_simple;
  34. net::TestNetworkQualityEstimator estimator;
  35. NetworkQualitiesPrefDelegate::RegisterPrefs(pref_service_simple.registry());
  36. base::HistogramTester initial_histogram_tester;
  37. NetworkQualitiesPrefDelegate pref_delegate(&pref_service_simple, &estimator);
  38. // NetworkQualityEstimator must be notified of the read prefs at startup.
  39. EXPECT_FALSE(
  40. initial_histogram_tester.GetAllSamples("NQE.Prefs.ReadSize").empty());
  41. {
  42. base::HistogramTester histogram_tester;
  43. estimator.set_effective_connection_type(
  44. net::EFFECTIVE_CONNECTION_TYPE_OFFLINE);
  45. estimator.set_recent_effective_connection_type(
  46. net::EFFECTIVE_CONNECTION_TYPE_OFFLINE);
  47. estimator.RunOneRequest();
  48. // Prefs are written only if persistent caching was enabled.
  49. EXPECT_FALSE(
  50. histogram_tester.GetAllSamples("NQE.Prefs.WriteCount").empty());
  51. histogram_tester.ExpectTotalCount("NQE.Prefs.ReadCount", 0);
  52. // NetworkQualityEstimator should not be notified of change in prefs.
  53. histogram_tester.ExpectTotalCount("NQE.Prefs.ReadSize", 0);
  54. }
  55. {
  56. base::HistogramTester histogram_tester;
  57. estimator.set_effective_connection_type(
  58. net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
  59. estimator.set_recent_effective_connection_type(
  60. net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
  61. estimator.RunOneRequest();
  62. // Prefs are written even if the network id was unavailable.
  63. EXPECT_FALSE(
  64. histogram_tester.GetAllSamples("NQE.Prefs.WriteCount").empty());
  65. histogram_tester.ExpectTotalCount("NQE.Prefs.ReadCount", 0);
  66. // NetworkQualityEstimator should not be notified of change in prefs.
  67. histogram_tester.ExpectTotalCount("NQE.Prefs.ReadSize", 0);
  68. }
  69. // Verify the contents of the prefs by reading them again.
  70. std::map<net::nqe::internal::NetworkID,
  71. net::nqe::internal::CachedNetworkQuality>
  72. read_prefs = pref_delegate.ForceReadPrefsForTesting();
  73. // Number of entries must be between 1 and 2. It's possible that 2 entries
  74. // are added if the connection type is unknown to network quality estimator
  75. // at the time of startup, and shortly after it receives a notification
  76. // about the change in the connection type.
  77. EXPECT_LE(1u, read_prefs.size());
  78. EXPECT_GE(2u, read_prefs.size());
  79. // Verify that the cached network quality was written correctly.
  80. EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G,
  81. read_prefs.begin()->second.effective_connection_type());
  82. if (net::NetworkChangeNotifier::GetConnectionType() ==
  83. net::NetworkChangeNotifier::CONNECTION_ETHERNET) {
  84. // Verify that the network ID was written correctly.
  85. net::nqe::internal::NetworkID ethernet_network_id(
  86. net::NetworkChangeNotifier::CONNECTION_ETHERNET, std::string(),
  87. INT32_MIN);
  88. EXPECT_EQ(ethernet_network_id, read_prefs.begin()->first);
  89. }
  90. }
  91. } // namespace
  92. } // namespace network