diff --git a/sharing/internal/api/BUILD b/sharing/internal/api/BUILD index f3d08026..5cc192aa 100644 --- a/sharing/internal/api/BUILD +++ b/sharing/internal/api/BUILD @@ -55,7 +55,11 @@ cc_library( cc_library( name = "mock_sharing_platform", testonly = True, + srcs = [ + "fake_nearby_share_client.cc", + ], hdrs = [ + "fake_nearby_share_client.h", "mock_app_info.h", "mock_bluetooth_adapter.h", "mock_network_monitor.h", @@ -69,8 +73,10 @@ cc_library( "//internal/analytics:event_logger", "//internal/platform:types", "//internal/platform/implementation:types", + "//sharing/proto:share_cc_proto", "@com_google_absl//absl/functional:any_invocable", "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", "@com_google_absl//absl/types:span", "@com_google_googletest//:gtest_for_library_testonly", diff --git a/sharing/internal/api/fake_nearby_share_client.cc b/sharing/internal/api/fake_nearby_share_client.cc new file mode 100644 index 00000000..ec9e3455 --- /dev/null +++ b/sharing/internal/api/fake_nearby_share_client.cc @@ -0,0 +1,81 @@ +// 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 "sharing/internal/api/fake_nearby_share_client.h" + +#include +#include + +#include "absl/functional/any_invocable.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "sharing/internal/api/sharing_rpc_client.h" +#include "sharing/proto/certificate_rpc.pb.h" +#include "sharing/proto/contact_rpc.pb.h" +#include "sharing/proto/device_rpc.pb.h" + +namespace nearby { +namespace sharing { + +void FakeNearbyShareClient::UpdateDevice( + const proto::UpdateDeviceRequest& request, + absl::AnyInvocable< + void(const absl::StatusOr& response) &&> + callback) { + update_device_requests_.emplace_back(request); + std::move(callback)(update_device_response_); +} + +void FakeNearbyShareClient::ListContactPeople( + const proto::ListContactPeopleRequest& request, + absl::AnyInvocable& response) &&> + callback) { + list_contact_people_requests_.emplace_back(request); + if (list_contact_people_responses_.empty()) { + std::move(callback)(absl::NotFoundError("")); + return; + } + auto response = list_contact_people_responses_[0]; + list_contact_people_responses_.erase(list_contact_people_responses_.begin()); + std::move(callback)(response); +} + +void FakeNearbyShareClient::ListPublicCertificates( + const proto::ListPublicCertificatesRequest& request, + absl::AnyInvocable< + void(const absl::StatusOr& + response) &&> + callback) { + list_public_certificates_requests_.emplace_back(request); + if (list_public_certificates_responses_.empty()) { + std::move(callback)(absl::NotFoundError("")); + return; + } + auto response = list_public_certificates_responses_[0]; + list_public_certificates_responses_.erase( + list_public_certificates_responses_.begin()); + std::move(callback)(response); +} + +std::unique_ptr +FakeNearbyShareClientFactory::CreateInstance() { + auto instance = std::make_unique(); + instances_.push_back(instance.get()); + + return instance; +} + +} // namespace sharing +} // namespace nearby diff --git a/sharing/internal/api/fake_nearby_share_client.h b/sharing/internal/api/fake_nearby_share_client.h new file mode 100644 index 00000000..22028a45 --- /dev/null +++ b/sharing/internal/api/fake_nearby_share_client.h @@ -0,0 +1,130 @@ +// 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_SHARING_INTERNAL_API_FAKE_NEARBY_SHARE_CLIENT_H_ +#define THIRD_PARTY_NEARBY_SHARING_INTERNAL_API_FAKE_NEARBY_SHARE_CLIENT_H_ + +#include +#include + +#include "absl/functional/any_invocable.h" +#include "absl/status/statusor.h" +#include "sharing/internal/api/sharing_rpc_client.h" +#include "sharing/internal/api/sharing_rpc_notifier.h" +#include "sharing/proto/certificate_rpc.pb.h" +#include "sharing/proto/contact_rpc.pb.h" +#include "sharing/proto/device_rpc.pb.h" + +namespace nearby { +namespace sharing { + +// A fake implementation of the Nearby Share HTTP client that stores all request +// data. Only use in unit tests. +class FakeNearbyShareClient : public nearby::sharing::api::SharingRpcClient { + public: + FakeNearbyShareClient() = default; + ~FakeNearbyShareClient() override = default; + + std::vector& + update_device_requests() { + return update_device_requests_; + } + + std::vector& + list_contact_people_requests() { + return list_contact_people_requests_; + } + + std::vector& + list_public_certificates_requests() { + return list_public_certificates_requests_; + } + + absl::StatusOr& update_device_response() { + return update_device_response_; + } + + void SetUpdateDeviceResponse( + absl::StatusOr response) { + update_device_response_ = response; + } + + void SetListContactPeopleResponses( + std::vector> responses) { + list_contact_people_responses_ = responses; + } + + void SetListPublicCertificatesResponses( + std::vector> + responses) { + list_public_certificates_responses_ = responses; + } + + private: + void UpdateDevice( + const proto::UpdateDeviceRequest& request, + absl::AnyInvocable< + void(const absl::StatusOr& response) &&> + callback) override; + void ListContactPeople( + const proto::ListContactPeopleRequest& request, + absl::AnyInvocable& response) &&> + callback) override; + void ListPublicCertificates( + const proto::ListPublicCertificatesRequest& request, + absl::AnyInvocable< + void(const absl::StatusOr& + response) &&> + callback) override; + + std::vector + update_device_requests_; + std::vector + list_contact_people_requests_; + std::vector + list_public_certificates_requests_; + + absl::StatusOr update_device_response_; + std::vector> + list_contact_people_responses_; + std::vector> + list_public_certificates_responses_; +}; + +class FakeNearbyShareClientFactory + : public nearby::sharing::api::SharingRpcClientFactory { + public: + FakeNearbyShareClientFactory() = default; + ~FakeNearbyShareClientFactory() override = default; + + public: + // Returns all FakeNearbyShareClient instances created by CreateInstance(). + std::vector& instances() { return instances_; } + nearby::sharing::api::SharingRpcNotifier* GetRpcNotifier() const override { + return nullptr; + } + + private: + // SharingRpcClientFactory: + std::unique_ptr CreateInstance() + override; + + std::vector instances_; +}; + +} // namespace sharing +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_SHARING_INTERNAL_API_FAKE_NEARBY_SHARE_CLIENT_H_ diff --git a/sharing/internal/public/logging.h b/sharing/internal/public/logging.h index 35261248..c1f92acb 100644 --- a/sharing/internal/public/logging.h +++ b/sharing/internal/public/logging.h @@ -24,6 +24,7 @@ #define NL_LOG(severity) LOG(severity) #define NL_DLOG(severity) DLOG(severity) +#define NL_DVLOG(severity) DVLOG(severity) #define NL_CHECK(expr) CHECK(expr) #define NL_CHECK_EQ(a, b) CHECK_EQ((a), (b))