diff --git a/sharing/certificates/nearby_share_certificate_manager_impl_test.cc b/sharing/certificates/nearby_share_certificate_manager_impl_test.cc index 513f5b79..feb6fcd4 100644 --- a/sharing/certificates/nearby_share_certificate_manager_impl_test.cc +++ b/sharing/certificates/nearby_share_certificate_manager_impl_test.cc @@ -208,16 +208,14 @@ class NearbyShareCertificateManagerImplTest void SetMockBluetoothAddress(absl::string_view bluetooth_mac_address) { FakeBluetoothAdapter& bluetooth_adapter = - dynamic_cast( - fake_context_.GetBluetoothAdapter()); + *fake_context_.fake_bluetooth_adapter(); bluetooth_adapter.SetAddress(bluetooth_mac_address); } void SetBluetoothAdapterIsPresent(bool is_present) { if (!is_present) { FakeBluetoothAdapter& bluetooth_adapter = - dynamic_cast( - fake_context_.GetBluetoothAdapter()); + *fake_context_.fake_bluetooth_adapter(); bluetooth_adapter.SetAddress(""); } } diff --git a/sharing/fast_initiation/nearby_fast_initiation_impl_test.cc b/sharing/fast_initiation/nearby_fast_initiation_impl_test.cc index 0f14f7d2..01bb36bc 100644 --- a/sharing/fast_initiation/nearby_fast_initiation_impl_test.cc +++ b/sharing/fast_initiation/nearby_fast_initiation_impl_test.cc @@ -16,7 +16,6 @@ #include #include -#include #include "gtest/gtest.h" #include "sharing/fast_initiation/fake_nearby_fast_initiation_observer.h" @@ -88,8 +87,7 @@ TEST(NearbyFastInitiationImpl, StartAdvertising) { NearbyFastInitiation::FastInitType::kNotify, success_callback, error_callback); FakeFastInitiationManager& fake_fast_initiation_manager = - dynamic_cast( - fake_context->GetFastInitiationManager()); + *fake_context->fake_fast_initiation_manager(); fake_fast_initiation_manager.SetAdvertisingStarted(); EXPECT_TRUE(success_callback_called); } @@ -113,8 +111,7 @@ TEST(NearbyFastInitiationImpl, StartAdvertisingAndGetHardwareError) { error_callback); FakeFastInitiationManager& fake_fast_initiation_manager = - dynamic_cast( - fake_context->GetFastInitiationManager()); + *fake_context->fake_fast_initiation_manager(); // Mocking OS hardware error event on Windows fake_fast_initiation_manager.SetAdvertisingError( @@ -141,8 +138,7 @@ TEST(NearbyFastInitiationImpl, StopStartedAdvertising) { bool stop_callback_called = false; auto fake_context = std::make_unique(); FakeFastInitiationManager& fake_fast_initiation_manager = - dynamic_cast( - fake_context->GetFastInitiationManager()); + *fake_context->fake_fast_initiation_manager(); NearbyFastInitiationImpl nearby_fast_initiation_impl(fake_context.get()); nearby_fast_initiation_impl.StartAdvertising( @@ -172,8 +168,7 @@ TEST(NearbyFastInitiationImpl, StartScanningSucceed) { devices_not_discovered_callback, error_callback); FakeFastInitiationManager& fake_fast_initiation_manager = - dynamic_cast( - fake_context->GetFastInitiationManager()); + *fake_context->fake_fast_initiation_manager(); fake_fast_initiation_manager.SetScanningDiscovered(); EXPECT_TRUE(devices_discovered); } @@ -198,8 +193,7 @@ TEST(NearbyFastInitiationImpl, StartScanningAndGetHardwareError) { error_callback); FakeFastInitiationManager& fake_fast_initiation_manager = - dynamic_cast( - fake_context->GetFastInitiationManager()); + *fake_context->fake_fast_initiation_manager(); // Mocking OS hardware error event on Windows fake_fast_initiation_manager.SetScanningError( @@ -226,8 +220,7 @@ TEST(NearbyFastInitiationImpl, StopStartedScanning) { auto fake_context = std::make_unique(); NearbyFastInitiationImpl nearby_fast_initiation_impl(fake_context.get()); FakeFastInitiationManager& fake_fast_initiation_manager = - dynamic_cast( - fake_context->GetFastInitiationManager()); + *fake_context->fake_fast_initiation_manager(); nearby_fast_initiation_impl.StartScanning([]() {}, []() {}, []() {}); nearby_fast_initiation_impl.StopScanning( [&]() { stop_callback_called = true; }); diff --git a/sharing/internal/test/fake_context.cc b/sharing/internal/test/fake_context.cc index 53887f2a..ab621a79 100644 --- a/sharing/internal/test/fake_context.cc +++ b/sharing/internal/test/fake_context.cc @@ -44,13 +44,13 @@ namespace nearby { FakeContext::FakeContext() : fake_clock_(std::make_unique()), - connectivity_manager_(std::make_unique()), - bluetooth_adapter_(std::make_unique()), - wifi_adapter_(std::make_unique()), - fast_initiation_manager_(std::make_unique()), - shell_(std::make_unique()), - executor_(std::make_unique( - dynamic_cast(GetClock()), 5)) {} + fake_connectivity_manager_(std::make_unique()), + fake_bluetooth_adapter_(std::make_unique()), + fake_wifi_adapter_(std::make_unique()), + fake_fast_initiation_manager_( + std::make_unique()), + fake_shell_(std::make_unique()), + executor_(std::make_unique(fake_clock_.get(), 5)) {} Clock* FakeContext::GetClock() const { return fake_clock_.get(); } @@ -73,35 +73,35 @@ void FakeContext::CopyText(const absl::string_view text, } ConnectivityManager* FakeContext::GetConnectivityManager() const { - return connectivity_manager_.get(); + return fake_connectivity_manager_.get(); } sharing::api::BluetoothAdapter& FakeContext::GetBluetoothAdapter() const { - return *bluetooth_adapter_; + return *fake_bluetooth_adapter_; } sharing::api::WifiAdapter& FakeContext::GetWifiAdapter() const { - return *wifi_adapter_; + return *fake_wifi_adapter_; } api::FastInitiationManager& FakeContext::GetFastInitiationManager() const { - return *fast_initiation_manager_; + return *fake_fast_initiation_manager_; } std::unique_ptr FakeContext::CreateSequencedTaskRunner() const { std::unique_ptr task_runner = - std::make_unique(dynamic_cast(GetClock()), 1); + std::make_unique(fake_clock_.get(), 1); return task_runner; } std::unique_ptr FakeContext::CreateConcurrentTaskRunner( uint32_t concurrent_count) const { - std::unique_ptr task_runner = std::make_unique( - dynamic_cast(GetClock()), concurrent_count); + std::unique_ptr task_runner = + std::make_unique(fake_clock_.get(), concurrent_count); return task_runner; } -api::Shell& FakeContext::GetShell() const { return *shell_; } +api::Shell& FakeContext::GetShell() const { return *fake_shell_; } TaskRunner* FakeContext::GetTaskRunner() { return executor_.get(); } diff --git a/sharing/internal/test/fake_context.h b/sharing/internal/test/fake_context.h index 305f46cd..a5b5201a 100644 --- a/sharing/internal/test/fake_context.h +++ b/sharing/internal/test/fake_context.h @@ -33,6 +33,11 @@ #include "sharing/internal/api/wifi_adapter.h" #include "sharing/internal/public/connectivity_manager.h" #include "sharing/internal/public/context.h" +#include "sharing/internal/test/fake_bluetooth_adapter.h" +#include "sharing/internal/test/fake_connectivity_manager.h" +#include "sharing/internal/test/fake_fast_initiation_manager.h" +#include "sharing/internal/test/fake_shell.h" +#include "sharing/internal/test/fake_wifi_adapter.h" namespace nearby { @@ -58,14 +63,27 @@ class FakeContext : public Context { TaskRunner* GetTaskRunner() override; FakeClock* fake_clock() const { return fake_clock_.get(); } + FakeConnectivityManager* fake_connectivity_manager() const { + return fake_connectivity_manager_.get(); + } + FakeBluetoothAdapter* fake_bluetooth_adapter() const { + return fake_bluetooth_adapter_.get(); + } + FakeWifiAdapter* fake_wifi_adapter() const { + return fake_wifi_adapter_.get(); + } + FakeFastInitiationManager* fake_fast_initiation_manager() const { + return fake_fast_initiation_manager_.get(); + } + FakeShell* fake_shell() const { return fake_shell_.get(); } private: std::unique_ptr fake_clock_; - std::unique_ptr connectivity_manager_; - std::unique_ptr bluetooth_adapter_; - std::unique_ptr wifi_adapter_; - std::unique_ptr fast_initiation_manager_; - std::unique_ptr shell_; + std::unique_ptr fake_connectivity_manager_; + std::unique_ptr fake_bluetooth_adapter_; + std::unique_ptr fake_wifi_adapter_; + std::unique_ptr fake_fast_initiation_manager_; + std::unique_ptr fake_shell_; std::unique_ptr executor_; }; diff --git a/sharing/nearby_connections_manager_impl_test.cc b/sharing/nearby_connections_manager_impl_test.cc index 8a799147..6d469cf7 100644 --- a/sharing/nearby_connections_manager_impl_test.cc +++ b/sharing/nearby_connections_manager_impl_test.cc @@ -138,12 +138,10 @@ class NearbyConnectionsManagerImplTest : public testing::Test { NearbyFlags::GetInstance().OverrideBoolFlagValue( config_package_nearby::nearby_sharing_feature::kEnableMediumWifiLan, true); - std::unique_ptr nearby_connections_service = + auto nearby_connections_service = std::make_unique>(); SetConnectionType(ConnectivityManager::ConnectionType::kWifi); - nearby_connections_ = - dynamic_cast*>( - nearby_connections_service.get()); + nearby_connections_ = nearby_connections_service.get(); nearby_connections_manager_ = std::make_unique( diff --git a/sharing/nearby_sharing_service_extension_test.cc b/sharing/nearby_sharing_service_extension_test.cc index fa0681fc..d71a25ff 100644 --- a/sharing/nearby_sharing_service_extension_test.cc +++ b/sharing/nearby_sharing_service_extension_test.cc @@ -156,7 +156,7 @@ TEST_F(NearbySharingServiceExtensionTest, OpenSharedTargetUseDownloadFolder) { FileAttachment(std::filesystem::temp_directory_path() / "test.g2")}; StatusCodes status_codes = service_extension()->Open(share_target); EXPECT_EQ(status_codes, StatusCodes::kOk); - auto& shell = dynamic_cast(context()->GetShell()); + FakeShell& shell = *context()->fake_shell(); shell.set_return_error(true); status_codes = service_extension()->Open(share_target); EXPECT_NE(status_codes, StatusCodes::kOk); diff --git a/sharing/payload_tracker_test.cc b/sharing/payload_tracker_test.cc index 6f71e4de..31efa2ad 100644 --- a/sharing/payload_tracker_test.cc +++ b/sharing/payload_tracker_test.cc @@ -28,7 +28,6 @@ #include "internal/test/fake_clock.h" #include "sharing/attachment_info.h" #include "sharing/file_attachment.h" -#include "sharing/internal/public/context.h" #include "sharing/internal/test/fake_context.h" #include "sharing/nearby_connections_types.h" #include "sharing/proto/wire_format.pb.h" @@ -66,8 +65,7 @@ class PayloadTrackerTest : public ::testing::Test { float percentage() const { return current_percentage_; } void FastForward(absl::Duration duration) { - FakeClock* clock = dynamic_cast(context()->GetClock()); - clock->FastForward(duration); + context()->fake_clock()->FastForward(duration); } void PayloadUpdate(int bytes_transferred) { @@ -78,7 +76,7 @@ class PayloadTrackerTest : public ::testing::Test { } private: - Context* context() { + FakeContext* context() { static FakeContext* context = new FakeContext(); return context; }