DeviceProvider: refactor getdevice interface once again

PiperOrigin-RevId: 531659446
This commit is contained in:
Anay Wadhera
2023-05-12 19:37:54 -07:00
committed by Copybara-Service
parent 013bf1f420
commit b698c00b5d
9 changed files with 32 additions and 35 deletions
+2 -5
View File
@@ -15,9 +15,8 @@
#ifndef CORE_CORE_H_
#define CORE_CORE_H_
#include <functional>
#include <memory>
#include <string>
#include <type_traits>
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
@@ -508,9 +507,7 @@ class Core {
// Registers a DeviceProvider to provide functionality for Nearby Connections
// to interact with the DeviceProvider for retrieving the local device.
template <typename T, typename std::enable_if<std::is_base_of<
NearbyDevice, T>::value>::type* = nullptr>
void RegisterDeviceProvider(NearbyDeviceProvider<T>* provider);
void RegisterDeviceProvider(std::unique_ptr<NearbyDeviceProvider> provider);
private:
ClientProxy client_;
+2 -3
View File
@@ -24,8 +24,7 @@ namespace nearby {
namespace connections {
namespace v3 {
class ConnectionsDeviceProvider
: public NearbyDeviceProvider<ConnectionsDevice> {
class ConnectionsDeviceProvider : public NearbyDeviceProvider {
public:
ConnectionsDeviceProvider(
absl::string_view endpoint_info,
@@ -36,7 +35,7 @@ class ConnectionsDeviceProvider
const std::vector<ConnectionInfoVariant> connection_infos)
: device_{endpoint_id, endpoint_info, connection_infos} {}
const ConnectionsDevice& GetLocalDevice() override { return device_; }
const NearbyDevice* GetLocalDevice() override { return &device_; }
private:
ConnectionsDevice device_;
@@ -28,21 +28,22 @@ constexpr absl::string_view kEndpointInfo = "NC endpoint";
TEST(ConnectionsDeviceProviderTest, TestProviderWorksTwoArgs) {
ConnectionsDeviceProvider provider(kEndpointInfo, {});
EXPECT_EQ(provider.GetLocalDevice().GetEndpointInfo(), kEndpointInfo);
EXPECT_EQ(provider.GetLocalDevice().GetType(),
NearbyDevice::Type::kConnectionsDevice);
EXPECT_EQ(provider.GetLocalDevice().GetConnectionInfos().size(), 0);
EXPECT_EQ(provider.GetLocalDevice().GetEndpointId().size(),
kEndpointIdLength);
auto device = provider.GetLocalDevice();
ASSERT_EQ(device->GetType(), NearbyDevice::Type::kConnectionsDevice);
auto connections_device = static_cast<const ConnectionsDevice*>(device);
EXPECT_EQ(connections_device->GetEndpointInfo(), kEndpointInfo);
EXPECT_EQ(connections_device->GetConnectionInfos().size(), 0);
EXPECT_EQ(connections_device->GetEndpointId().size(), kEndpointIdLength);
}
TEST(ConnectionsDeviceProviderTest, TestProviderWorksThreeArgs) {
ConnectionsDeviceProvider provider(kEndpointId, kEndpointInfo, {});
EXPECT_EQ(provider.GetLocalDevice().GetEndpointInfo(), kEndpointInfo);
EXPECT_EQ(provider.GetLocalDevice().GetType(),
NearbyDevice::Type::kConnectionsDevice);
EXPECT_EQ(provider.GetLocalDevice().GetConnectionInfos().size(), 0);
EXPECT_EQ(provider.GetLocalDevice().GetEndpointId(), kEndpointId);
auto device = provider.GetLocalDevice();
ASSERT_EQ(device->GetType(), NearbyDevice::Type::kConnectionsDevice);
auto connections_device = static_cast<const ConnectionsDevice*>(device);
EXPECT_EQ(connections_device->GetEndpointInfo(), kEndpointInfo);
EXPECT_EQ(connections_device->GetConnectionInfos().size(), 0);
EXPECT_EQ(connections_device->GetEndpointId(), kEndpointId);
}
} // namespace
+1 -3
View File
@@ -22,13 +22,11 @@ namespace nearby {
// The base device provider class for use with the Nearby Connections V3 APIs.
// This class currently provides a function to get the local device for whatever
// client implements it.
template <typename T, typename std::enable_if<std::is_base_of<
::nearby::NearbyDevice, T>::value>::type* = nullptr>
class NearbyDeviceProvider {
public:
virtual ~NearbyDeviceProvider() = default;
const virtual T& GetLocalDevice() = 0;
const virtual NearbyDevice* GetLocalDevice() = 0;
};
} // namespace nearby
+3 -1
View File
@@ -65,7 +65,9 @@ void PresenceClient::StopBroadcast(BroadcastSessionId session_id) {
std::optional<PresenceDevice> PresenceClient::GetLocalDevice() {
::nearby::Borrowed<PresenceService*> borrowed = service_.Borrow();
if (borrowed) {
return (*borrowed)->GetLocalDeviceProvider()->GetLocalDevice();
const PresenceDevice* device = static_cast<const PresenceDevice*>(
(*borrowed)->GetLocalDeviceProvider()->GetLocalDevice());
return PresenceDevice(*device);
}
return std::nullopt;
}
+1 -3
View File
@@ -15,9 +15,7 @@
#ifndef THIRD_PARTY_NEARBY_PRESENCE_PRESENCE_CLIENT_H_
#define THIRD_PARTY_NEARBY_PRESENCE_PRESENCE_CLIENT_H_
#include <functional>
#include <memory>
#include <vector>
#include <optional>
#include "absl/status/statusor.h"
#include "internal/platform/borrowable.h"
+2 -2
View File
@@ -126,8 +126,8 @@ TEST_F(PresenceClientTest, GettingDeviceWorks) {
{}, 0, 0, {});
auto device = presence_client.GetLocalDevice();
ASSERT_NE(device, std::nullopt);
EXPECT_EQ(device->GetEndpointId().length(), kEndpointIdLength);
EXPECT_EQ(device->GetMetadata().SerializeAsString(),
EXPECT_EQ((*device).GetEndpointId().length(), kEndpointIdLength);
EXPECT_EQ((*device).GetMetadata().SerializeAsString(),
CreateTestMetadata().SerializeAsString());
}
+2 -2
View File
@@ -22,12 +22,12 @@
namespace nearby {
namespace presence {
class PresenceDeviceProvider : public NearbyDeviceProvider<PresenceDevice> {
class PresenceDeviceProvider : public NearbyDeviceProvider {
public:
explicit PresenceDeviceProvider(::nearby::internal::Metadata metadata)
: device_{metadata} {}
const PresenceDevice& GetLocalDevice() override { return device_; }
const NearbyDevice* GetLocalDevice() override { return &device_; }
void UpdateMetadata(const ::nearby::internal::Metadata& metadata) {
device_.SetMetadata(metadata);
+7 -5
View File
@@ -49,21 +49,23 @@ TEST(PresenceDeviceProviderTest, ProviderIsNotTriviallyConstructible) {
TEST(PresenceDeviceProviderTest, DeviceProviderWorks) {
PresenceDeviceProvider provider(CreateTestMetadata());
auto device = provider.GetLocalDevice();
EXPECT_EQ(device.GetMetadata().SerializeAsString(),
ASSERT_EQ(device->GetType(), NearbyDevice::Type::kPresenceDevice);
auto presence_device = static_cast<const PresenceDevice*>(device);
EXPECT_EQ(presence_device->GetMetadata().SerializeAsString(),
CreateTestMetadata().SerializeAsString());
}
TEST(PresenceDeviceProviderTest, DeviceProviderCanUpdateDevice) {
PresenceDeviceProvider provider(CreateTestMetadata());
auto device = provider.GetLocalDevice();
EXPECT_EQ(device.GetMetadata().SerializeAsString(),
ASSERT_EQ(device->GetType(), NearbyDevice::Type::kPresenceDevice);
auto presence_device = static_cast<const PresenceDevice*>(device);
EXPECT_EQ(presence_device->GetMetadata().SerializeAsString(),
CreateTestMetadata().SerializeAsString());
Metadata new_metadata = CreateTestMetadata();
new_metadata.set_device_name("NP interop device");
provider.UpdateMetadata(new_metadata);
EXPECT_NE(device.GetMetadata().SerializeAsString(),
new_metadata.SerializeAsString());
EXPECT_EQ(provider.GetLocalDevice().GetMetadata().SerializeAsString(),
EXPECT_EQ(presence_device->GetMetadata().SerializeAsString(),
new_metadata.SerializeAsString());
}