Move FakeNearbyShareClient into sharing/internal/api

PiperOrigin-RevId: 601149255
This commit is contained in:
Francis Tsui
2024-01-24 09:46:28 -08:00
committed by Copybara-Service
parent 6f58a4d111
commit 0905b2c3e0
4 changed files with 218 additions and 0 deletions
+6
View File
@@ -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",
@@ -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 <memory>
#include <utility>
#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<proto::UpdateDeviceResponse>& response) &&>
callback) {
update_device_requests_.emplace_back(request);
std::move(callback)(update_device_response_);
}
void FakeNearbyShareClient::ListContactPeople(
const proto::ListContactPeopleRequest& request,
absl::AnyInvocable<void(const absl::StatusOr<
proto::ListContactPeopleResponse>& 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<proto::ListPublicCertificatesResponse>&
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<nearby::sharing::api::SharingRpcClient>
FakeNearbyShareClientFactory::CreateInstance() {
auto instance = std::make_unique<FakeNearbyShareClient>();
instances_.push_back(instance.get());
return instance;
}
} // namespace sharing
} // namespace nearby
@@ -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 <memory>
#include <vector>
#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<nearby::sharing::proto::UpdateDeviceRequest>&
update_device_requests() {
return update_device_requests_;
}
std::vector<nearby::sharing::proto::ListContactPeopleRequest>&
list_contact_people_requests() {
return list_contact_people_requests_;
}
std::vector<nearby::sharing::proto::ListPublicCertificatesRequest>&
list_public_certificates_requests() {
return list_public_certificates_requests_;
}
absl::StatusOr<proto::UpdateDeviceResponse>& update_device_response() {
return update_device_response_;
}
void SetUpdateDeviceResponse(
absl::StatusOr<proto::UpdateDeviceResponse> response) {
update_device_response_ = response;
}
void SetListContactPeopleResponses(
std::vector<absl::StatusOr<proto::ListContactPeopleResponse>> responses) {
list_contact_people_responses_ = responses;
}
void SetListPublicCertificatesResponses(
std::vector<absl::StatusOr<proto::ListPublicCertificatesResponse>>
responses) {
list_public_certificates_responses_ = responses;
}
private:
void UpdateDevice(
const proto::UpdateDeviceRequest& request,
absl::AnyInvocable<
void(const absl::StatusOr<proto::UpdateDeviceResponse>& response) &&>
callback) override;
void ListContactPeople(
const proto::ListContactPeopleRequest& request,
absl::AnyInvocable<void(const absl::StatusOr<
proto::ListContactPeopleResponse>& response) &&>
callback) override;
void ListPublicCertificates(
const proto::ListPublicCertificatesRequest& request,
absl::AnyInvocable<
void(const absl::StatusOr<proto::ListPublicCertificatesResponse>&
response) &&>
callback) override;
std::vector<nearby::sharing::proto::UpdateDeviceRequest>
update_device_requests_;
std::vector<nearby::sharing::proto::ListContactPeopleRequest>
list_contact_people_requests_;
std::vector<nearby::sharing::proto::ListPublicCertificatesRequest>
list_public_certificates_requests_;
absl::StatusOr<proto::UpdateDeviceResponse> update_device_response_;
std::vector<absl::StatusOr<proto::ListContactPeopleResponse>>
list_contact_people_responses_;
std::vector<absl::StatusOr<proto::ListPublicCertificatesResponse>>
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<FakeNearbyShareClient*>& instances() { return instances_; }
nearby::sharing::api::SharingRpcNotifier* GetRpcNotifier() const override {
return nullptr;
}
private:
// SharingRpcClientFactory:
std::unique_ptr<nearby::sharing::api::SharingRpcClient> CreateInstance()
override;
std::vector<FakeNearbyShareClient*> instances_;
};
} // namespace sharing
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_SHARING_INTERNAL_API_FAKE_NEARBY_SHARE_CLIENT_H_
+1
View File
@@ -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))