Remove use of dynamic_cast from nearby/sharing.

PiperOrigin-RevId: 634519160
This commit is contained in:
Francis Tsui
2024-05-16 13:57:01 -07:00
committed by Copybara-Service
parent 20c0c5aac3
commit 16f820471e
7 changed files with 51 additions and 46 deletions
@@ -208,16 +208,14 @@ class NearbyShareCertificateManagerImplTest
void SetMockBluetoothAddress(absl::string_view bluetooth_mac_address) {
FakeBluetoothAdapter& bluetooth_adapter =
dynamic_cast<FakeBluetoothAdapter&>(
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<FakeBluetoothAdapter&>(
fake_context_.GetBluetoothAdapter());
*fake_context_.fake_bluetooth_adapter();
bluetooth_adapter.SetAddress("");
}
}
@@ -16,7 +16,6 @@
#include <functional>
#include <memory>
#include <string>
#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<FakeFastInitiationManager&>(
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<FakeFastInitiationManager&>(
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<FakeContext>();
FakeFastInitiationManager& fake_fast_initiation_manager =
dynamic_cast<FakeFastInitiationManager&>(
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<FakeFastInitiationManager&>(
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<FakeFastInitiationManager&>(
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<FakeContext>();
NearbyFastInitiationImpl nearby_fast_initiation_impl(fake_context.get());
FakeFastInitiationManager& fake_fast_initiation_manager =
dynamic_cast<FakeFastInitiationManager&>(
fake_context->GetFastInitiationManager());
*fake_context->fake_fast_initiation_manager();
nearby_fast_initiation_impl.StartScanning([]() {}, []() {}, []() {});
nearby_fast_initiation_impl.StopScanning(
[&]() { stop_callback_called = true; });
+15 -15
View File
@@ -44,13 +44,13 @@ namespace nearby {
FakeContext::FakeContext()
: fake_clock_(std::make_unique<FakeClock>()),
connectivity_manager_(std::make_unique<FakeConnectivityManager>()),
bluetooth_adapter_(std::make_unique<FakeBluetoothAdapter>()),
wifi_adapter_(std::make_unique<FakeWifiAdapter>()),
fast_initiation_manager_(std::make_unique<FakeFastInitiationManager>()),
shell_(std::make_unique<FakeShell>()),
executor_(std::make_unique<FakeTaskRunner>(
dynamic_cast<FakeClock*>(GetClock()), 5)) {}
fake_connectivity_manager_(std::make_unique<FakeConnectivityManager>()),
fake_bluetooth_adapter_(std::make_unique<FakeBluetoothAdapter>()),
fake_wifi_adapter_(std::make_unique<FakeWifiAdapter>()),
fake_fast_initiation_manager_(
std::make_unique<FakeFastInitiationManager>()),
fake_shell_(std::make_unique<FakeShell>()),
executor_(std::make_unique<FakeTaskRunner>(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<TaskRunner> FakeContext::CreateSequencedTaskRunner() const {
std::unique_ptr<TaskRunner> task_runner =
std::make_unique<FakeTaskRunner>(dynamic_cast<FakeClock*>(GetClock()), 1);
std::make_unique<FakeTaskRunner>(fake_clock_.get(), 1);
return task_runner;
}
std::unique_ptr<TaskRunner> FakeContext::CreateConcurrentTaskRunner(
uint32_t concurrent_count) const {
std::unique_ptr<TaskRunner> task_runner = std::make_unique<FakeTaskRunner>(
dynamic_cast<FakeClock*>(GetClock()), concurrent_count);
std::unique_ptr<TaskRunner> task_runner =
std::make_unique<FakeTaskRunner>(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(); }
+23 -5
View File
@@ -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<FakeClock> fake_clock_;
std::unique_ptr<ConnectivityManager> connectivity_manager_;
std::unique_ptr<sharing::api::BluetoothAdapter> bluetooth_adapter_;
std::unique_ptr<sharing::api::WifiAdapter> wifi_adapter_;
std::unique_ptr<api::FastInitiationManager> fast_initiation_manager_;
std::unique_ptr<api::Shell> shell_;
std::unique_ptr<FakeConnectivityManager> fake_connectivity_manager_;
std::unique_ptr<FakeBluetoothAdapter> fake_bluetooth_adapter_;
std::unique_ptr<FakeWifiAdapter> fake_wifi_adapter_;
std::unique_ptr<FakeFastInitiationManager> fake_fast_initiation_manager_;
std::unique_ptr<FakeShell> fake_shell_;
std::unique_ptr<TaskRunner> executor_;
};
@@ -138,12 +138,10 @@ class NearbyConnectionsManagerImplTest : public testing::Test {
NearbyFlags::GetInstance().OverrideBoolFlagValue(
config_package_nearby::nearby_sharing_feature::kEnableMediumWifiLan,
true);
std::unique_ptr<NearbyConnectionsService> nearby_connections_service =
auto nearby_connections_service =
std::make_unique<testing::NiceMock<FakeNearbyConnectionsService>>();
SetConnectionType(ConnectivityManager::ConnectionType::kWifi);
nearby_connections_ =
dynamic_cast<testing::NiceMock<FakeNearbyConnectionsService>*>(
nearby_connections_service.get());
nearby_connections_ = nearby_connections_service.get();
nearby_connections_manager_ =
std::make_unique<NearbyConnectionsManagerImpl>(
@@ -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<FakeShell&>(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);
+2 -4
View File
@@ -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<FakeClock*>(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;
}