From 630b2dd3db76c4695690bc24d430bc0ad2cc117a Mon Sep 17 00:00:00 2001 From: Anay Wadhera Date: Fri, 5 May 2023 12:12:02 -0700 Subject: [PATCH] Create ConnectionsDeviceProvider PiperOrigin-RevId: 529781099 --- Package.swift | 1 + connections/v3/BUILD | 2 + connections/v3/connections_device_provider.h | 49 ++++++++++++++++++ .../v3/connections_device_provider_test.cc | 51 +++++++++++++++++++ connections/v3/connections_device_test.cc | 10 ++++ 5 files changed, 113 insertions(+) create mode 100644 connections/v3/connections_device_provider.h create mode 100644 connections/v3/connections_device_provider_test.cc diff --git a/Package.swift b/Package.swift index f3d140d6..d7e1ed10 100644 --- a/Package.swift +++ b/Package.swift @@ -496,6 +496,7 @@ let package = Package( "connections/implementation/ble_advertisement_test.cc", "connections/implementation/base_endpoint_channel_test.cc", "connections/v3/connections_device_test.cc", + "connections/v3/connections_device_provider_test.cc", "connections/core_test.cc", "connections/status_test.cc", "connections/payload_test.cc", diff --git a/connections/v3/BUILD b/connections/v3/BUILD index 56ddeba1..26afa091 100644 --- a/connections/v3/BUILD +++ b/connections/v3/BUILD @@ -5,6 +5,7 @@ cc_library( "connection_listening_options.h", "connection_resolution.h", "connections_device.h", + "connections_device_provider.h", "listeners.h", ], visibility = [ @@ -23,6 +24,7 @@ cc_library( cc_test( name = "v3_types_test", srcs = [ + "connections_device_provider_test.cc", "connections_device_test.cc", ], deps = [ diff --git a/connections/v3/connections_device_provider.h b/connections/v3/connections_device_provider.h new file mode 100644 index 00000000..13b2c14d --- /dev/null +++ b/connections/v3/connections_device_provider.h @@ -0,0 +1,49 @@ +// Copyright 2023 Google LLC +// +// 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 +// +// https://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 THIRD_PARTY_NEARBY_CONNECTIONS_V3_CONNECTIONS_DEVICE_PROVIDER_H_ +#define THIRD_PARTY_NEARBY_CONNECTIONS_V3_CONNECTIONS_DEVICE_PROVIDER_H_ + +#include + +#include "connections/v3/connections_device.h" +#include "internal/interop/device_provider.h" + +namespace nearby { +namespace connections { +namespace v3 { + +class ConnectionsDeviceProvider + : public NearbyDeviceProvider { + public: + ConnectionsDeviceProvider( + absl::string_view endpoint_info, + const std::vector connection_infos) + : device_{endpoint_info, connection_infos} {} + ConnectionsDeviceProvider( + absl::string_view endpoint_id, absl::string_view endpoint_info, + const std::vector connection_infos) + : device_{endpoint_id, endpoint_info, connection_infos} {} + + const ConnectionsDevice& GetLocalDevice() override { return device_; } + + private: + ConnectionsDevice device_; +}; + +} // namespace v3 +} // namespace connections +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_CONNECTIONS_V3_CONNECTIONS_DEVICE_PROVIDER_H_ diff --git a/connections/v3/connections_device_provider_test.cc b/connections/v3/connections_device_provider_test.cc new file mode 100644 index 00000000..382b2aa0 --- /dev/null +++ b/connections/v3/connections_device_provider_test.cc @@ -0,0 +1,51 @@ +// Copyright 2023 Google LLC +// +// 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 +// +// https://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 "connections/v3/connections_device_provider.h" + +#include "gmock/gmock.h" +#include "protobuf-matchers/protocol-buffer-matchers.h" +#include "gtest/gtest.h" + +namespace nearby { +namespace connections { +namespace v3 { +namespace { + +constexpr absl::string_view kEndpointId = "ABCD"; +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); +} + +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); +} + +} // namespace +} // namespace v3 +} // namespace connections +} // namespace nearby diff --git a/connections/v3/connections_device_test.cc b/connections/v3/connections_device_test.cc index 4952b577..b157d54d 100644 --- a/connections/v3/connections_device_test.cc +++ b/connections/v3/connections_device_test.cc @@ -54,6 +54,16 @@ TEST(ConnectionsDeviceTest, TestThreeArgumentConstructor) { EXPECT_EQ(device.GetEndpointInfo(), "connections endpoint"); } +TEST(ConnectionsDeviceTest, TestLongEndpointId) { + auto connection_infos = CreateDefaultConnectionInfos(); + ConnectionsDevice device("ABCDEF", "connections endpoint", connection_infos); + EXPECT_EQ(device.GetConnectionInfos(), connection_infos); + EXPECT_EQ(device.GetType(), NearbyDevice::Type::kConnectionsDevice); + EXPECT_EQ(device.GetEndpointId().length(), 4); + EXPECT_NE(device.GetEndpointId(), "ABCDEF"); + EXPECT_EQ(device.GetEndpointInfo(), "connections endpoint"); +} + } // namespace } // namespace v3 } // namespace connections