From 69f61f7fd15d44dd32a107d850f05f0eb933ab2c Mon Sep 17 00:00:00 2001 From: Sohani Rao Date: Tue, 25 Jul 2017 18:09:57 -0700 Subject: Wificond: Create mocks of Offload Scan objects Create objects for Offload HAL to enable testing of Scanner Impl Test: Unit tests Bug: 32842314 Change-Id: Iea881dbbef0f6dc53b87f8ea24acd3c3e347f902 --- Android.mk | 2 + scanning/offload/offload_scan_manager.h | 7 +-- scanning/offload/offload_service_utils.cpp | 12 +++++ scanning/offload/offload_service_utils.h | 9 ++++ scanning/scanner_impl.cpp | 7 ++- scanning/scanner_impl.h | 6 +-- tests/mock_offload_scan_callback_interface.h | 6 +-- .../mock_offload_scan_callback_interface_impl.cpp | 28 +++++++++++ tests/mock_offload_scan_callback_interface_impl.h | 44 +++++++++++++++++ tests/mock_offload_scan_manager.cpp | 28 +++++++++++ tests/mock_offload_scan_manager.h | 56 ++++++++++++++++++++++ tests/mock_offload_service_utils.h | 9 ++++ tests/scanner_unittest.cpp | 13 +++++ 13 files changed, 214 insertions(+), 13 deletions(-) create mode 100644 tests/mock_offload_scan_callback_interface_impl.cpp create mode 100644 tests/mock_offload_scan_callback_interface_impl.h create mode 100644 tests/mock_offload_scan_manager.cpp create mode 100644 tests/mock_offload_scan_manager.h diff --git a/Android.mk b/Android.mk index fd42006..b25e39b 100644 --- a/Android.mk +++ b/Android.mk @@ -177,6 +177,8 @@ LOCAL_SRC_FILES := \ tests/mock_offload.cpp \ tests/mock_offload_callback_handlers.cpp \ tests/mock_offload_scan_callback_interface.cpp \ + tests/mock_offload_scan_callback_interface_impl.cpp \ + tests/mock_offload_scan_manager.cpp \ tests/mock_offload_service_utils.cpp \ tests/mock_scan_utils.cpp \ tests/netlink_manager_unittest.cpp \ diff --git a/scanning/offload/offload_scan_manager.h b/scanning/offload/offload_scan_manager.h index 9cc3d47..46f1bad 100644 --- a/scanning/offload/offload_scan_manager.h +++ b/scanning/offload/offload_scan_manager.h @@ -19,8 +19,7 @@ #include #include "wificond/scanning/offload/offload_callback.h" #include "wificond/scanning/offload/offload_callback_handlers.h" -#include "wificond/scanning/offload/offload_service_utils.h" -#include "wificond/scanning/offload_scan_callback_interface.h" +#include "wificond/scanning/offload_scan_callback_interface_impl.h" #include @@ -43,6 +42,8 @@ namespace android { namespace wificond { class OffloadScanManager; +class OffloadDeathRecipient; +class OffloadServiceUtils; // Provides callback interface implementation from Offload HAL class OffloadCallbackHandlersImpl : public OffloadCallbackHandlers { @@ -148,7 +149,7 @@ class OffloadScanManager { bool service_available_; const std::weak_ptr offload_service_utils_; - const std::unique_ptr offload_callback_handlers_; + const std::shared_ptr offload_callback_handlers_; std::shared_ptr event_callback_; friend class OffloadCallbackHandlersImpl; diff --git a/scanning/offload/offload_service_utils.cpp b/scanning/offload/offload_service_utils.cpp index a4a2474..e7b7be8 100644 --- a/scanning/offload/offload_service_utils.cpp +++ b/scanning/offload/offload_service_utils.cpp @@ -18,6 +18,7 @@ #include #include "wificond/scanning/offload/offload_scan_manager.h" +#include "wificond/scanning/scanner_impl.h" using ::android::hardware::wifi::offload::V1_0::IOffload; @@ -47,6 +48,17 @@ bool OffloadServiceUtils::IsOffloadScanSupported() const { return result; } +std::shared_ptr +OffloadServiceUtils::GetOffloadScanCallbackInterface(ScannerImpl* parent) { + return std::make_shared(parent); +} + +std::shared_ptr OffloadServiceUtils::GetOffloadScanManager( + std::weak_ptr service_utils, + std::shared_ptr callback_interface) { + return std::make_shared(service_utils, callback_interface); +} + OffloadDeathRecipient::OffloadDeathRecipient( OffloadDeathRecipientHandler handler) : handler_(handler) {} diff --git a/scanning/offload/offload_service_utils.h b/scanning/offload/offload_service_utils.h index cff5575..42268de 100644 --- a/scanning/offload/offload_service_utils.h +++ b/scanning/offload/offload_service_utils.h @@ -19,11 +19,15 @@ #include #include "wificond/scanning/offload/offload_callback.h" #include "wificond/scanning/offload/offload_callback_handlers.h" +#include "wificond/scanning/offload_scan_callback_interface_impl.h" namespace android { namespace wificond { typedef std::function OffloadDeathRecipientHandler; +class ScannerImpl; +class OffloadServiceUtils; +class OffloadScanManager; class OffloadDeathRecipient : public android::hardware::hidl_death_recipient { public: @@ -52,6 +56,11 @@ class OffloadServiceUtils { OffloadCallbackHandlers* handlers); virtual OffloadDeathRecipient* GetOffloadDeathRecipient( OffloadDeathRecipientHandler handler); + virtual std::shared_ptr + GetOffloadScanCallbackInterface(ScannerImpl* parent); + virtual std::shared_ptr GetOffloadScanManager( + std::weak_ptr service_utils, + std::shared_ptr callback_interface); }; } // namespace wificond diff --git a/scanning/scanner_impl.cpp b/scanning/scanner_impl.cpp index 308811a..5fa7093 100644 --- a/scanning/scanner_impl.cpp +++ b/scanning/scanner_impl.cpp @@ -80,10 +80,9 @@ ScannerImpl::ScannerImpl(uint32_t wiphy_index, uint32_t interface_index, _1, _2)); std::shared_ptr offload_scan_callback_interface = - std::make_shared( - *(new OffloadScanCallbackInterfaceImpl(this))); - offload_scan_manager_.reset(new OffloadScanManager( - offload_service_utils, offload_scan_callback_interface)); + offload_service_utils.lock()->GetOffloadScanCallbackInterface(this); + offload_scan_manager_ = offload_service_utils.lock()->GetOffloadScanManager( + offload_service_utils, offload_scan_callback_interface); offload_scan_supported_ = offload_service_utils.lock()->IsOffloadScanSupported(); } diff --git a/scanning/scanner_impl.h b/scanning/scanner_impl.h index ff1d50f..8e1f832 100644 --- a/scanning/scanner_impl.h +++ b/scanning/scanner_impl.h @@ -24,9 +24,8 @@ #include "android/net/wifi/BnWifiScannerImpl.h" #include "wificond/net/netlink_utils.h" -#include "wificond/scanning/offload/offload_scan_manager.h" +#include "wificond/scanning/offload_scan_callback_interface.h" #include "wificond/scanning/scan_utils.h" -#include "wificond/scanning/offload_scan_callback_interface_impl.h" namespace android { namespace wificond { @@ -35,6 +34,7 @@ class ClientInterfaceImpl; class OffloadServiceUtils; class ScanUtils; class OffloadScanCallbackInterfaceImpl; +class OffloadScanManager; class ScannerImpl : public android::net::wifi::BnWifiScannerImpl { public: @@ -128,7 +128,7 @@ class ScannerImpl : public android::net::wifi::BnWifiScannerImpl { ScanUtils* const scan_utils_; ::android::sp<::android::net::wifi::IPnoScanEvent> pno_scan_event_handler_; ::android::sp<::android::net::wifi::IScanEvent> scan_event_handler_; - std::unique_ptr offload_scan_manager_; + std::shared_ptr offload_scan_manager_; DISALLOW_COPY_AND_ASSIGN(ScannerImpl); }; diff --git a/tests/mock_offload_scan_callback_interface.h b/tests/mock_offload_scan_callback_interface.h index e7cdcc1..e4f8df9 100644 --- a/tests/mock_offload_scan_callback_interface.h +++ b/tests/mock_offload_scan_callback_interface.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef WIFICOND_TESTS_MOCK_OFFLOAD_CALLBACK_HANDLERS_H_ -#define WIFICOND_TESTS_MOCK_OFFLOAD_CALLBACK_HANDLERS_H_ +#ifndef WIFICOND_TESTS_MOCK_OFFLOAD_SCAN_CALLBACK_INTERFACE_H_ +#define WIFICOND_TESTS_MOCK_OFFLOAD_SCAN_CALLBACK_INTERFACE_H_ #include #include @@ -38,4 +38,4 @@ class MockOffloadScanCallbackInterface : public OffloadScanCallbackInterface { } // namespace wificond } // namespace android -#endif // WIFICOND_TESTS_MOCK_OFFLOAD_CALLBACK_HANDLERS_H_ +#endif // WIFICOND_TESTS_MOCK_OFFLOAD_SCAN_CALLBACK_INTERFACE_H_ diff --git a/tests/mock_offload_scan_callback_interface_impl.cpp b/tests/mock_offload_scan_callback_interface_impl.cpp new file mode 100644 index 0000000..b9dffc5 --- /dev/null +++ b/tests/mock_offload_scan_callback_interface_impl.cpp @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "wificond/tests/mock_offload_scan_callback_interface_impl.h" +#include "wificond/scanning/scanner_impl.h" + +namespace android { +namespace wificond { + +MockOffloadScanCallbackInterfaceImpl::MockOffloadScanCallbackInterfaceImpl( + ScannerImpl* parent) + : OffloadScanCallbackInterfaceImpl(parent) {} + +} // namespace wificond +} // namespace android diff --git a/tests/mock_offload_scan_callback_interface_impl.h b/tests/mock_offload_scan_callback_interface_impl.h new file mode 100644 index 0000000..8b76388 --- /dev/null +++ b/tests/mock_offload_scan_callback_interface_impl.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef WIFICOND_TESTS_MOCK_OFFLOAD_SCAN_CALLBACK_INTERFACE_IMPL_H__ +#define WIFICOND_TESTS_MOCK_OFFLOAD_SCAN_CALLBACK_INTERFACE_IMPL_H__ + +#include +#include + +#include "wificond/scanning/offload_scan_callback_interface_impl.h" + +namespace android { +namespace wificond { + +class ScannerImpl; + +class MockOffloadScanCallbackInterfaceImpl + : public OffloadScanCallbackInterfaceImpl { + public: + MockOffloadScanCallbackInterfaceImpl(ScannerImpl*); + ~MockOffloadScanCallbackInterfaceImpl() override = default; + + MOCK_METHOD0(OnOffloadScanResult, void()); + MOCK_METHOD1(OnOffloadError, + void(OffloadScanCallbackInterface::AsyncErrorReason)); +}; + +} // namespace wificond +} // namespace android + +#endif // WIFICOND_TESTS_MOCK_OFFLOAD_SCAN_CALLBACK_INTERFACE_IMPL_H__ diff --git a/tests/mock_offload_scan_manager.cpp b/tests/mock_offload_scan_manager.cpp new file mode 100644 index 0000000..8943524 --- /dev/null +++ b/tests/mock_offload_scan_manager.cpp @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "wificond/tests/mock_offload_scan_manager.h" + +namespace android { +namespace wificond { + +MockOffloadScanManager::MockOffloadScanManager( + std::weak_ptr service_utils, + std::shared_ptr callback_interface) + : OffloadScanManager(service_utils, callback_interface) {} + +} // namespace wificond +} // namespace android diff --git a/tests/mock_offload_scan_manager.h b/tests/mock_offload_scan_manager.h new file mode 100644 index 0000000..71d5e79 --- /dev/null +++ b/tests/mock_offload_scan_manager.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef WIFICOND_TESTS_MOCK_OFFLOAD_SCAN_MANAGER_H_ +#define WIFICOND_TESTS_MOCK_OFFLOAD_SCAN_MANAGER_H_ + +#include +#include + +#include "wificond/scanning/offload/offload_scan_manager.h" + +namespace android { +namespace wificond { + +class MockOffloadScanManager : public OffloadScanManager { + public: + MockOffloadScanManager( + std::weak_ptr service_utils, + std::shared_ptr callback_interface); + ~MockOffloadScanManager() override = default; + + MOCK_METHOD7(startScan, + bool(uint32_t interval_ms, int32_t rssi_threshold, + const std::vector>& scan_ssids, + const std::vector>& match_ssids, + const std::vector& match_security, + const std::vector& frequencies, + OffloadScanManager::ReasonCode* reason_code)); + MOCK_METHOD1(stopScan, bool(OffloadScanManager::ReasonCode* reason_code)); + MOCK_METHOD1(getScanStats, + bool(::com::android::server::wifi::wificond::NativeScanStats* + scan_stats)); + MOCK_CONST_METHOD0(getOffloadStatus, OffloadScanManager::StatusCode()); + MOCK_METHOD1( + getScanResults, + bool(std::vector< + ::com::android::server::wifi::wificond::NativeScanResult>*)); +}; + +} // namespace wificond +} // namespace android + +#endif // WIFICOND_TESTS_MOCK_OFFLOAD_SCAN_MANAGER_H_ diff --git a/tests/mock_offload_service_utils.h b/tests/mock_offload_service_utils.h index fd9fd34..76d022d 100644 --- a/tests/mock_offload_service_utils.h +++ b/tests/mock_offload_service_utils.h @@ -24,6 +24,7 @@ #include "wificond/scanning/offload/offload_callback.h" #include "wificond/scanning/offload/offload_callback_handlers.h" #include "wificond/scanning/offload/offload_service_utils.h" +#include "wificond/scanning/offload_scan_callback_interface_impl.h" namespace android { namespace wificond { @@ -40,6 +41,14 @@ class MockOffloadServiceUtils : public OffloadServiceUtils { MOCK_METHOD1(GetOffloadDeathRecipient, android::wificond::OffloadDeathRecipient*( OffloadDeathRecipientHandler handler)); + MOCK_METHOD1( + GetOffloadScanCallbackInterface, + std::shared_ptr(ScannerImpl* scanner)); + MOCK_METHOD2(GetOffloadScanManager, + std::shared_ptr( + std::weak_ptr service_utils, + std::shared_ptr + callback_interface)); }; } // namespace wificond diff --git a/tests/scanner_unittest.cpp b/tests/scanner_unittest.cpp index bbaeffd..1a93e31 100644 --- a/tests/scanner_unittest.cpp +++ b/tests/scanner_unittest.cpp @@ -25,6 +25,8 @@ #include "wificond/tests/mock_client_interface_impl.h" #include "wificond/tests/mock_netlink_manager.h" #include "wificond/tests/mock_netlink_utils.h" +#include "wificond/tests/mock_offload_scan_callback_interface_impl.h" +#include "wificond/tests/mock_offload_scan_manager.h" #include "wificond/tests/mock_offload_service_utils.h" #include "wificond/tests/mock_scan_utils.h" @@ -91,6 +93,10 @@ class ScannerTest : public ::testing::Test { void SetUp() override { ON_CALL(*offload_service_utils_, IsOffloadScanSupported()).WillByDefault( Return(false)); + ON_CALL(*offload_service_utils_, GetOffloadScanManager(_, _)) + .WillByDefault(Return(offload_scan_manager_)); + ON_CALL(*offload_service_utils_, GetOffloadScanCallbackInterface(_)) + .WillByDefault(Return(offload_scan_callback_interface_)); netlink_scanner_.reset(new ScannerImpl( kFakeWiphyIndex, kFakeInterfaceIndex, scan_capabilities_, wiphy_features_, @@ -108,6 +114,13 @@ class ScannerTest : public ::testing::Test { &if_tool_, &supplicant_manager_, &netlink_utils_, &scan_utils_}; shared_ptr> offload_service_utils_{ new NiceMock()}; + shared_ptr> + offload_scan_callback_interface_{ + new NiceMock( + netlink_scanner_.get())}; + std::shared_ptr> offload_scan_manager_{ + new NiceMock(offload_service_utils_, + offload_scan_callback_interface_)}; ScanCapabilities scan_capabilities_; WiphyFeatures wiphy_features_; }; -- cgit v1.2.3-54-g00ecf