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