summaryrefslogtreecommitdiffstats
path: root/net/quic/network_connection_unittest.cc
blob: a63438dbcc36b35cea0dd880f79a0d8489dec3e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/quic/network_connection.h"

#include "net/base/network_change_notifier.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {
namespace test {

class NetworkConnectionPeer {
 public:
  static NetworkChangeNotifier::ConnectionType connection_type(
      const NetworkConnection& network_connection) {
    return network_connection.connection_type_;
  }
  static void set_connection_type(NetworkConnection& network_connection,
                                  NetworkChangeNotifier::ConnectionType type) {
    network_connection.connection_type_ = type;
  }

  static const char* connection_description(
      const NetworkConnection& network_connection) {
    return network_connection.connection_description_;
  }
  static void set_connection_description(NetworkConnection& network_connection,
                                         const char* description) {
    network_connection.connection_description_ = description;
  }
};

// Test NetworkConnection().
class NetworkConnectionTest : public testing::Test {
 protected:
  void CheckNetworkConnectionDescription() {
    NetworkChangeNotifier::ConnectionType type =
        NetworkChangeNotifier::GetConnectionType();
    const char* description = network_connection_.GetDescription();
    // Verify GetDescription() updated the cached data.
    EXPECT_EQ(NetworkConnectionPeer::connection_type(network_connection_),
              type);
    EXPECT_EQ(
        NetworkConnectionPeer::connection_description(network_connection_),
        description);

    if (type != NetworkChangeNotifier::CONNECTION_WIFI)
      EXPECT_EQ(description,
                NetworkChangeNotifier::ConnectionTypeToString(type));
    else
      EXPECT_NE(nullptr, network_connection_.GetDescription());
  }

  NetworkConnection network_connection_;
};

TEST_F(NetworkConnectionTest, GetDescription) {
  const char* description = network_connection_.GetDescription();

  // Set connection description to nullptr.
  NetworkConnectionPeer::set_connection_description(network_connection_,
                                                    nullptr);
  CheckNetworkConnectionDescription();

  // Set connection type to a junk value.
  NetworkConnectionPeer::set_connection_type(
      network_connection_, NetworkChangeNotifier::CONNECTION_LAST);
  CheckNetworkConnectionDescription();

  EXPECT_EQ(description, network_connection_.GetDescription());
}

TEST_F(NetworkConnectionTest, Clear) {
  CheckNetworkConnectionDescription();
  network_connection_.Clear();
  CheckNetworkConnectionDescription();
}

}  // namespace test
}  // namespace net