diff --git a/sharing/BUILD b/sharing/BUILD index 69b3e50e..464580ac 100644 --- a/sharing/BUILD +++ b/sharing/BUILD @@ -862,3 +862,16 @@ cc_test( "@com_google_googletest//:gtest_main", ], ) + +cc_test( + name = "nearby_connections_service_test", + srcs = ["nearby_connections_service_test.cc"], + deps = [ + ":connection_types", + ":nearby_sharing_service", + "//internal/platform:types", + "//internal/platform/implementation/g3", # fixdeps: keep + "@com_github_protobuf_matchers//protobuf-matchers", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/sharing/nearby_connections_service.cc b/sharing/nearby_connections_service.cc index 9ae866ed..0a1b3d6e 100644 --- a/sharing/nearby_connections_service.cc +++ b/sharing/nearby_connections_service.cc @@ -43,8 +43,7 @@ Payload ConvertToPayload(NcPayload payload) { std::vector(data.begin(), data.end())); } case NcPayloadType::kFile: { - std::filesystem::path file_path = - std::filesystem::u8path(payload.AsFile()->GetFilePath()); + std::string file_path = payload.AsFile()->GetFilePath(); std::string parent_folder = payload.GetParentFolder(); NL_VLOG(1) << __func__ << ": Payload file_path=" << file_path << ", parent_folder = " << parent_folder; diff --git a/sharing/nearby_connections_service_test.cc b/sharing/nearby_connections_service_test.cc new file mode 100644 index 00000000..80bcdc6a --- /dev/null +++ b/sharing/nearby_connections_service_test.cc @@ -0,0 +1,42 @@ +// Copyright 2024 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 "sharing/nearby_connections_service.h" + +#include "gmock/gmock.h" +#include "protobuf-matchers/protocol-buffer-matchers.h" +#include "gtest/gtest.h" +#include "internal/platform/file.h" +#include "sharing/nearby_connections_types.h" + +namespace nearby::sharing { +namespace { + +using ::testing::Eq; + +TEST(NearbyConnectionSharingServicePayloadTest, ConvertBytesToPayload) { + Payload payload = ConvertToPayload(NcPayload(1234, NcByteArray("test"))); + EXPECT_THAT(payload.id, Eq(1234LL)); + EXPECT_THAT(payload.content.type, Eq(PayloadContent::Type::kBytes)); +} + +TEST(NearbyConnectionSharingServicePayloadTest, ConvertFileToPayload) { + Payload payload = ConvertToPayload( + NcPayload(1234, nearby::InputFile("/为甚么/tmp/test.txt", /*size=*/100))); + EXPECT_THAT(payload.id, Eq(1234LL)); + EXPECT_THAT(payload.content.type, Eq(PayloadContent::Type::kFile)); +} + +} // namespace +} // namespace nearby::sharing diff --git a/sharing/nearby_connections_types.h b/sharing/nearby_connections_types.h index 6b13694c..1119bd09 100644 --- a/sharing/nearby_connections_types.h +++ b/sharing/nearby_connections_types.h @@ -391,7 +391,9 @@ enum class DistanceInfo { struct InputFile { InputFile() = default; - explicit InputFile(std::filesystem::path path) { this->path = path; } + explicit InputFile(std::string path) { + this->path = std::filesystem::u8path(path); + } std::filesystem::path path; }; diff --git a/sharing/nearby_connections_types_payload_test.cc b/sharing/nearby_connections_types_payload_test.cc index ad61274d..abcaff57 100644 --- a/sharing/nearby_connections_types_payload_test.cc +++ b/sharing/nearby_connections_types_payload_test.cc @@ -14,8 +14,6 @@ #include "sharing/nearby_connections_types.h" -#include // NOLINT - #include "gmock/gmock.h" #include "protobuf-matchers/protocol-buffer-matchers.h" #include "gtest/gtest.h" @@ -24,21 +22,14 @@ namespace nearby::sharing { using ::testing::Eq; TEST(NearbyConnectionSharingTypesPayloadTest, FromInputFileUTF8) { - InputFile input_file(std::filesystem::u8path("/为甚么/tmp/test.txt")); - Payload payload(input_file); - EXPECT_THAT(payload.id, Eq(7724502655048749887LL)); - EXPECT_THAT(payload.content.type, Eq(PayloadContent::Type::kFile)); -} - -TEST(NearbyConnectionSharingTypesPayloadTest, FromInputFileUTF16) { - InputFile input_file(std::filesystem::path(L"/为甚么/tmp/test.txt")); + InputFile input_file("/为甚么/tmp/test.txt"); Payload payload(input_file); EXPECT_THAT(payload.id, Eq(7724502655048749887LL)); EXPECT_THAT(payload.content.type, Eq(PayloadContent::Type::kFile)); } TEST(NearbyConnectionSharingTypesPayloadTest, FromInputFileWithId) { - InputFile input_file(std::filesystem::u8path("/为甚么/tmp/test.txt")); + InputFile input_file("/为甚么/tmp/test.txt"); Payload payload(1234, input_file); EXPECT_THAT(payload.id, Eq(1234LL)); EXPECT_THAT(payload.content.type, Eq(PayloadContent::Type::kFile));