| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chromeos/components/tether/initializer.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/test/scoped_task_environment.h" |
| 12 #include "chromeos/components/tether/active_host.h" |
| 13 #include "chromeos/components/tether/fake_notification_presenter.h" |
| 14 #include "chromeos/components/tether/host_scan_device_prioritizer.h" |
| 15 #include "chromeos/dbus/dbus_thread_manager.h" |
| 16 #include "chromeos/network/network_connect.h" |
| 17 #include "chromeos/network/network_handler.h" |
| 18 #include "chromeos/network/network_state_test.h" |
| 19 #include "components/cryptauth/cryptauth_device_manager.h" |
| 20 #include "components/cryptauth/cryptauth_enroller.h" |
| 21 #include "components/cryptauth/cryptauth_enrollment_manager.h" |
| 22 #include "components/cryptauth/fake_cryptauth_gcm_manager.h" |
| 23 #include "components/cryptauth/fake_cryptauth_service.h" |
| 24 #include "components/cryptauth/proto/cryptauth_api.pb.h" |
| 25 #include "components/cryptauth/secure_message_delegate.h" |
| 26 #include "components/prefs/testing_pref_service.h" |
| 27 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
| 28 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
| 29 #include "testing/gmock/include/gmock/gmock.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" |
| 31 #include "third_party/cros_system_api/dbus/shill/dbus-constants.h" |
| 32 |
| 33 using testing::NiceMock; |
| 34 |
| 35 namespace chromeos { |
| 36 |
| 37 namespace tether { |
| 38 |
| 39 namespace { |
| 40 |
| 41 class MockCryptAuthDeviceManager : public cryptauth::CryptAuthDeviceManager { |
| 42 public: |
| 43 ~MockCryptAuthDeviceManager() override {} |
| 44 |
| 45 MOCK_CONST_METHOD0(GetTetherHosts, |
| 46 std::vector<cryptauth::ExternalDeviceInfo>()); |
| 47 }; |
| 48 |
| 49 class MockCryptAuthEnrollmentManager |
| 50 : public cryptauth::CryptAuthEnrollmentManager { |
| 51 public: |
| 52 explicit MockCryptAuthEnrollmentManager( |
| 53 cryptauth::FakeCryptAuthGCMManager* fake_cryptauth_gcm_manager) |
| 54 : cryptauth::CryptAuthEnrollmentManager( |
| 55 nullptr /* clock */, |
| 56 nullptr /* enroller_factory */, |
| 57 nullptr /* secure_message_delegate */, |
| 58 cryptauth::GcmDeviceInfo(), |
| 59 fake_cryptauth_gcm_manager, |
| 60 nullptr /* pref_service */) {} |
| 61 ~MockCryptAuthEnrollmentManager() override {} |
| 62 |
| 63 MOCK_CONST_METHOD0(GetUserPrivateKey, std::string()); |
| 64 }; |
| 65 |
| 66 class MockNetworkConnect : public NetworkConnect { |
| 67 public: |
| 68 MockNetworkConnect() : NetworkConnect() {} |
| 69 ~MockNetworkConnect() override {} |
| 70 |
| 71 MOCK_METHOD1(ConnectToNetworkId, void(const std::string&)); |
| 72 MOCK_METHOD1(DisconnectFromNetworkId, void(const std::string&)); |
| 73 MOCK_METHOD2(MaybeShowConfigureUI, |
| 74 bool(const std::string&, const std::string&)); |
| 75 MOCK_METHOD2(SetTechnologyEnabled, |
| 76 void(const chromeos::NetworkTypePattern&, bool)); |
| 77 MOCK_METHOD1(ShowMobileSetup, void(const std::string&)); |
| 78 MOCK_METHOD3(ConfigureNetworkIdAndConnect, |
| 79 void(const std::string&, const base::DictionaryValue&, bool)); |
| 80 MOCK_METHOD2(CreateConfigurationAndConnect, |
| 81 void(base::DictionaryValue*, bool)); |
| 82 MOCK_METHOD2(CreateConfiguration, void(base::DictionaryValue*, bool)); |
| 83 MOCK_METHOD1(SetTetherDelegate, void(NetworkConnect::TetherDelegate*)); |
| 84 }; |
| 85 |
| 86 } // namespace |
| 87 |
| 88 class InitializerTest : public NetworkStateTest { |
| 89 protected: |
| 90 InitializerTest() : NetworkStateTest() {} |
| 91 ~InitializerTest() override {} |
| 92 |
| 93 void SetUp() override { |
| 94 DBusThreadManager::Initialize(); |
| 95 NetworkHandler::Initialize(); |
| 96 NetworkStateTest::SetUp(); |
| 97 |
| 98 test_pref_service_ = base::MakeUnique<TestingPrefServiceSimple>(); |
| 99 HostScanDevicePrioritizer::RegisterPrefs(test_pref_service_->registry()); |
| 100 ActiveHost::RegisterPrefs(test_pref_service_->registry()); |
| 101 } |
| 102 |
| 103 void TearDown() override { |
| 104 ShutdownNetworkState(); |
| 105 NetworkStateTest::TearDown(); |
| 106 DBusThreadManager::Shutdown(); |
| 107 } |
| 108 |
| 109 void InitializeAndDestroy( |
| 110 cryptauth::CryptAuthService* cryptauth_service, |
| 111 std::unique_ptr<NotificationPresenter> notification_presenter, |
| 112 PrefService* pref_service, |
| 113 ProfileOAuth2TokenService* token_service, |
| 114 NetworkStateHandler* network_state_handler, |
| 115 NetworkConnect* network_connect, |
| 116 scoped_refptr<device::BluetoothAdapter> adapter) { |
| 117 Initializer* initializer = new Initializer( |
| 118 cryptauth_service, std::move(notification_presenter), pref_service, |
| 119 token_service, network_state_handler, network_connect); |
| 120 initializer->OnBluetoothAdapterAdvertisingIntervalSet(adapter); |
| 121 delete initializer; |
| 122 } |
| 123 |
| 124 base::test::ScopedTaskEnvironment scoped_task_environment_; |
| 125 |
| 126 std::unique_ptr<TestingPrefServiceSimple> test_pref_service_; |
| 127 |
| 128 private: |
| 129 DISALLOW_COPY_AND_ASSIGN(InitializerTest); |
| 130 }; |
| 131 |
| 132 // This test ensures that Initializer's destructor runs in the correct order and |
| 133 // results in a correct clean-up of all created components. If the destructor |
| 134 // were to result in an error being thrown, this test would fail. |
| 135 TEST_F(InitializerTest, TestCreateAndDestroy) { |
| 136 std::unique_ptr<NiceMock<MockCryptAuthDeviceManager>> mock_device_manager = |
| 137 base::WrapUnique(new NiceMock<MockCryptAuthDeviceManager>()); |
| 138 |
| 139 std::unique_ptr<cryptauth::FakeCryptAuthGCMManager> |
| 140 fake_cryptauth_gcm_manager = |
| 141 base::MakeUnique<cryptauth::FakeCryptAuthGCMManager>( |
| 142 "registrationId"); |
| 143 |
| 144 std::unique_ptr<NiceMock<MockCryptAuthEnrollmentManager>> |
| 145 mock_enrollment_manager = |
| 146 base::WrapUnique(new NiceMock<MockCryptAuthEnrollmentManager>( |
| 147 fake_cryptauth_gcm_manager.get())); |
| 148 |
| 149 std::unique_ptr<cryptauth::FakeCryptAuthService> fake_cryptauth_service = |
| 150 base::MakeUnique<cryptauth::FakeCryptAuthService>(); |
| 151 fake_cryptauth_service->set_cryptauth_device_manager( |
| 152 mock_device_manager.get()); |
| 153 fake_cryptauth_service->set_cryptauth_enrollment_manager( |
| 154 mock_enrollment_manager.get()); |
| 155 |
| 156 std::unique_ptr<TestingPrefServiceSimple> test_pref_service = |
| 157 base::MakeUnique<TestingPrefServiceSimple>(); |
| 158 |
| 159 std::unique_ptr<FakeProfileOAuth2TokenService> fake_token_service = |
| 160 base::MakeUnique<FakeProfileOAuth2TokenService>(); |
| 161 |
| 162 std::unique_ptr<MockNetworkConnect> mock_network_connect = |
| 163 base::WrapUnique(new NiceMock<MockNetworkConnect>); |
| 164 |
| 165 scoped_refptr<NiceMock<device::MockBluetoothAdapter>> mock_adapter = |
| 166 make_scoped_refptr(new NiceMock<device::MockBluetoothAdapter>()); |
| 167 |
| 168 // Call an instance method of the test instead of initializing and destroying |
| 169 // here because the friend relationship between Initializer and |
| 170 // InitializerTest only applies to the class itself, not these test functions. |
| 171 InitializeAndDestroy(fake_cryptauth_service.get(), |
| 172 base::MakeUnique<FakeNotificationPresenter>(), |
| 173 test_pref_service_.get(), fake_token_service.get(), |
| 174 network_state_handler(), mock_network_connect.get(), |
| 175 mock_adapter); |
| 176 } |
| 177 |
| 178 } // namespace tether |
| 179 |
| 180 } // namespace chromeos |
| OLD | NEW |