OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/components/tether/tether_network_disconnection_handler.h" | 5 #include "chromeos/components/tether/tether_network_disconnection_handler.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/test/scoped_task_environment.h" | 10 #include "base/test/scoped_task_environment.h" |
11 #include "chromeos/components/tether/fake_active_host.h" | 11 #include "chromeos/components/tether/fake_active_host.h" |
| 12 #include "chromeos/components/tether/network_configuration_remover.h" |
12 #include "chromeos/dbus/dbus_thread_manager.h" | 13 #include "chromeos/dbus/dbus_thread_manager.h" |
13 #include "chromeos/network/network_state.h" | 14 #include "chromeos/network/network_state.h" |
14 #include "chromeos/network/network_state_test.h" | 15 #include "chromeos/network/network_state_test.h" |
15 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "third_party/cros_system_api/dbus/shill/dbus-constants.h" | 18 #include "third_party/cros_system_api/dbus/shill/dbus-constants.h" |
18 | 19 |
| 20 using testing::_; |
| 21 using testing::NiceMock; |
| 22 |
19 namespace chromeos { | 23 namespace chromeos { |
20 | 24 |
21 namespace tether { | 25 namespace tether { |
22 | 26 |
23 namespace { | 27 namespace { |
24 | 28 |
25 const char kDeviceId[] = "deviceId"; | 29 const char kDeviceId[] = "deviceId"; |
26 const char kWifiNetworkGuid[] = "wifiNetworkGuid"; | 30 const char kWifiNetworkGuid[] = "wifiNetworkGuid"; |
27 const char kTetherNetworkGuid[] = "tetherNetworkGuid"; | 31 const char kTetherNetworkGuid[] = "tetherNetworkGuid"; |
28 | 32 |
29 std::string CreateConnectedWifiConfigurationJsonString() { | 33 std::string CreateConnectedWifiConfigurationJsonString() { |
30 std::stringstream ss; | 34 std::stringstream ss; |
31 ss << "{" | 35 ss << "{" |
32 << " \"GUID\": \"" << kWifiNetworkGuid << "\"," | 36 << " \"GUID\": \"" << kWifiNetworkGuid << "\"," |
33 << " \"Type\": \"" << shill::kTypeWifi << "\"," | 37 << " \"Type\": \"" << shill::kTypeWifi << "\"," |
34 << " \"State\": \"" << shill::kStateReady << "\"" | 38 << " \"State\": \"" << shill::kStateReady << "\"" |
35 << "}"; | 39 << "}"; |
36 return ss.str(); | 40 return ss.str(); |
37 } | 41 } |
38 | 42 |
| 43 class MockNetworkConfigurationRemover : public NetworkConfigurationRemover { |
| 44 public: |
| 45 MockNetworkConfigurationRemover() |
| 46 : NetworkConfigurationRemover(nullptr, nullptr) {} |
| 47 ~MockNetworkConfigurationRemover() override {} |
| 48 |
| 49 MOCK_METHOD1(RemoveNetworkConfiguration, void(const std::string&)); |
| 50 }; |
| 51 |
39 } // namespace | 52 } // namespace |
40 | 53 |
41 class TetherNetworkDisconnectionHandlerTest : public NetworkStateTest { | 54 class TetherNetworkDisconnectionHandlerTest : public NetworkStateTest { |
42 protected: | 55 protected: |
43 TetherNetworkDisconnectionHandlerTest() : NetworkStateTest() {} | 56 TetherNetworkDisconnectionHandlerTest() : NetworkStateTest() {} |
44 ~TetherNetworkDisconnectionHandlerTest() override {} | 57 ~TetherNetworkDisconnectionHandlerTest() override {} |
45 | 58 |
46 void SetUp() override { | 59 void SetUp() override { |
47 DBusThreadManager::Initialize(); | 60 DBusThreadManager::Initialize(); |
48 NetworkStateTest::SetUp(); | 61 NetworkStateTest::SetUp(); |
49 | 62 |
50 wifi_service_path_ = | 63 wifi_service_path_ = |
51 ConfigureService(CreateConnectedWifiConfigurationJsonString()); | 64 ConfigureService(CreateConnectedWifiConfigurationJsonString()); |
52 | 65 |
53 fake_active_host_ = base::MakeUnique<FakeActiveHost>(); | 66 fake_active_host_ = base::MakeUnique<FakeActiveHost>(); |
| 67 mock_network_configuration_remover_ = |
| 68 base::WrapUnique(new NiceMock<MockNetworkConfigurationRemover>); |
54 | 69 |
55 handler_ = base::WrapUnique(new TetherNetworkDisconnectionHandler( | 70 handler_ = base::WrapUnique(new TetherNetworkDisconnectionHandler( |
56 fake_active_host_.get(), network_state_handler())); | 71 fake_active_host_.get(), network_state_handler(), |
| 72 mock_network_configuration_remover_.get())); |
57 } | 73 } |
58 | 74 |
59 void TearDown() override { | 75 void TearDown() override { |
60 // Delete handler before the NetworkStateHandler and |fake_active_host_|. | 76 // Delete handler before the NetworkStateHandler and |fake_active_host_|. |
61 handler_.reset(); | 77 handler_.reset(); |
62 ShutdownNetworkState(); | 78 ShutdownNetworkState(); |
63 NetworkStateTest::TearDown(); | 79 NetworkStateTest::TearDown(); |
64 DBusThreadManager::Shutdown(); | 80 DBusThreadManager::Shutdown(); |
65 } | 81 } |
66 | 82 |
67 void NotifyDisconnected() { | 83 void NotifyDisconnected() { |
68 SetServiceProperty(wifi_service_path_, std::string(shill::kStateProperty), | 84 SetServiceProperty(wifi_service_path_, std::string(shill::kStateProperty), |
69 base::Value(shill::kStateIdle)); | 85 base::Value(shill::kStateIdle)); |
70 } | 86 } |
71 | 87 |
72 base::test::ScopedTaskEnvironment scoped_task_environment_; | 88 base::test::ScopedTaskEnvironment scoped_task_environment_; |
73 std::string wifi_service_path_; | 89 std::string wifi_service_path_; |
74 | 90 |
75 std::unique_ptr<FakeActiveHost> fake_active_host_; | 91 std::unique_ptr<FakeActiveHost> fake_active_host_; |
| 92 std::unique_ptr<MockNetworkConfigurationRemover> |
| 93 mock_network_configuration_remover_; |
76 | 94 |
77 std::unique_ptr<TetherNetworkDisconnectionHandler> handler_; | 95 std::unique_ptr<TetherNetworkDisconnectionHandler> handler_; |
78 | 96 |
79 private: | 97 private: |
80 DISALLOW_COPY_AND_ASSIGN(TetherNetworkDisconnectionHandlerTest); | 98 DISALLOW_COPY_AND_ASSIGN(TetherNetworkDisconnectionHandlerTest); |
81 }; | 99 }; |
82 | 100 |
83 TEST_F(TetherNetworkDisconnectionHandlerTest, TestConnectAndDisconnect) { | 101 TEST_F(TetherNetworkDisconnectionHandlerTest, TestConnectAndDisconnect) { |
| 102 EXPECT_CALL(*mock_network_configuration_remover_, |
| 103 RemoveNetworkConfiguration(kWifiNetworkGuid)) |
| 104 .Times(1); |
| 105 |
84 // Connect to the network. |handler_| should start tracking the connection. | 106 // Connect to the network. |handler_| should start tracking the connection. |
85 fake_active_host_->SetActiveHostConnecting(kDeviceId, kTetherNetworkGuid); | 107 fake_active_host_->SetActiveHostConnecting(kDeviceId, kTetherNetworkGuid); |
86 fake_active_host_->SetActiveHostConnected(kDeviceId, kTetherNetworkGuid, | 108 fake_active_host_->SetActiveHostConnected(kDeviceId, kTetherNetworkGuid, |
87 kWifiNetworkGuid); | 109 kWifiNetworkGuid); |
88 | 110 |
89 // Now, disconnect the Wi-Fi network. This should result in | 111 // Now, disconnect the Wi-Fi network. This should result in |
90 // |fake_active_host_| becoming disconnected. | 112 // |fake_active_host_| becoming disconnected. |
91 NotifyDisconnected(); | 113 NotifyDisconnected(); |
92 EXPECT_EQ(ActiveHost::ActiveHostStatus::DISCONNECTED, | 114 EXPECT_EQ(ActiveHost::ActiveHostStatus::DISCONNECTED, |
93 fake_active_host_->GetActiveHostStatus()); | 115 fake_active_host_->GetActiveHostStatus()); |
94 } | 116 } |
95 | 117 |
96 } // namespace tether | 118 } // namespace tether |
97 | 119 |
98 } // namespace cryptauth | 120 } // namespace chromeos |
OLD | NEW |