mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 14:46:12 -04:00
Remove unnecessary FileInfo struct.
PiperOrigin-RevId: 846781930
This commit is contained in:
committed by
Copybara-Service
parent
a590b2a3e2
commit
c1f24ce0ae
@@ -855,6 +855,7 @@ cc_test(
|
||||
":types",
|
||||
"//internal/analytics:mock_event_logger",
|
||||
"//internal/base:file_path",
|
||||
"//internal/base:files",
|
||||
"//internal/network:url",
|
||||
"//internal/platform/implementation:platform_impl",
|
||||
"//internal/test",
|
||||
|
||||
@@ -403,13 +403,13 @@ class NearbyConnectionsManagerImplTest : public testing::Test {
|
||||
EXPECT_EQ(payload_id, payload->id);
|
||||
|
||||
FilePayload file_payload = std::move(payload->content.file_payload);
|
||||
std::vector<uint8_t> payload_bytes(file_payload.size);
|
||||
std::vector<uint8_t> payload_bytes(expected_payload.size());
|
||||
std::ifstream payload_stream(file_payload.file_path.GetPath(),
|
||||
std::ios::in | std::ios::binary);
|
||||
ASSERT_TRUE(payload_stream.good());
|
||||
payload_stream.read(reinterpret_cast<char*>(payload_bytes.data()),
|
||||
file_payload.size);
|
||||
ASSERT_EQ(payload_stream.gcount(), file_payload.size);
|
||||
payload_bytes.size());
|
||||
ASSERT_EQ(payload_stream.gcount(), payload_bytes.size());
|
||||
EXPECT_EQ(expected_payload, payload_bytes);
|
||||
payload_stream.close();
|
||||
|
||||
@@ -1540,14 +1540,14 @@ TEST_F(NearbyConnectionsManagerImplTest, IncomingFilePayload) {
|
||||
nearby_connections_manager_->GetIncomingPayload(kPayloadId);
|
||||
ASSERT_NE(payload, nullptr);
|
||||
ASSERT_TRUE(payload->content.is_file());
|
||||
std::vector<uint8_t> payload_bytes(payload->content.file_payload.size);
|
||||
std::vector<uint8_t> payload_bytes(expected_payload.size());
|
||||
std::ifstream payload_stream(
|
||||
payload->content.file_payload.file_path.GetPath(),
|
||||
std::ios::in | std::ios::binary);
|
||||
ASSERT_TRUE(payload_stream.good());
|
||||
payload_stream.read(reinterpret_cast<char*>(payload_bytes.data()),
|
||||
payload->content.file_payload.size);
|
||||
ASSERT_EQ(payload_stream.gcount(), payload->content.file_payload.size);
|
||||
payload_bytes.size());
|
||||
ASSERT_EQ(payload_stream.gcount(), payload_bytes.size());
|
||||
payload_stream.close();
|
||||
EXPECT_EQ(payload_bytes, expected_payload);
|
||||
}
|
||||
|
||||
@@ -28,7 +28,6 @@
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/time/time.h"
|
||||
#include "internal/base/file_path.h"
|
||||
#include "internal/base/files.h"
|
||||
#include "internal/interop/authentication_status.h"
|
||||
|
||||
namespace nearby {
|
||||
@@ -369,7 +368,6 @@ struct FilePayload {
|
||||
// NearbyConnections library reads from this file. When receiving a file
|
||||
// payload it writes to this file.
|
||||
FilePath file_path;
|
||||
int64_t size;
|
||||
std::string parent_folder;
|
||||
};
|
||||
|
||||
@@ -416,11 +414,6 @@ struct Payload {
|
||||
absl::string_view parent_folder = absl::string_view())
|
||||
: id(id) {
|
||||
content.type = PayloadContent::Type::kFile;
|
||||
std::optional<uintmax_t> size = Files::GetFileSize(file_path);
|
||||
if (size.has_value()) {
|
||||
content.file_payload.size = *size;
|
||||
}
|
||||
|
||||
content.file_payload.file_path = file_path;
|
||||
content.file_payload.parent_folder = std::string(parent_folder);
|
||||
}
|
||||
|
||||
@@ -38,22 +38,6 @@ namespace {
|
||||
|
||||
using ::nearby::sharing::api::SharingPlatform;
|
||||
|
||||
// Called on the FileTaskRunner to actually open the files passed.
|
||||
std::vector<NearbyFileHandler::FileInfo> DoOpenFiles(
|
||||
absl::Span<const FilePath> file_paths) {
|
||||
std::vector<NearbyFileHandler::FileInfo> files;
|
||||
for (const auto& file_path : file_paths) {
|
||||
std::optional<uintmax_t> size = Files::GetFileSize(file_path);
|
||||
if (!size.has_value()) {
|
||||
LOG(ERROR) << __func__
|
||||
<< ": Failed to open file. File=" << file_path.ToString();
|
||||
return {};
|
||||
}
|
||||
files.push_back({*size, file_path});
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
NearbyFileHandler::NearbyFileHandler(SharingPlatform& platform,
|
||||
@@ -68,15 +52,6 @@ NearbyFileHandler::NearbyFileHandler(SharingPlatform& platform,
|
||||
|
||||
NearbyFileHandler::~NearbyFileHandler() = default;
|
||||
|
||||
void NearbyFileHandler::OpenFiles(std::vector<FilePath> file_paths,
|
||||
OpenFilesCallback callback) {
|
||||
sequenced_task_runner_->PostTask(
|
||||
[callback = std::move(callback), file_paths = std::move(file_paths)]() {
|
||||
auto opened_files = DoOpenFiles(file_paths);
|
||||
callback(opened_files);
|
||||
});
|
||||
}
|
||||
|
||||
void NearbyFileHandler::DeleteFilesFromDisk(
|
||||
std::vector<FilePath> file_paths, DeleteFilesFromDiskCallback callback) {
|
||||
sequenced_task_runner_->PostTask([callback = std::move(callback),
|
||||
|
||||
@@ -33,12 +33,6 @@ namespace sharing {
|
||||
// releasing files need to run on a MayBlock task runner.
|
||||
class NearbyFileHandler {
|
||||
public:
|
||||
struct FileInfo {
|
||||
uint64_t size;
|
||||
FilePath file_path;
|
||||
};
|
||||
|
||||
using OpenFilesCallback = std::function<void(std::vector<FileInfo>)>;
|
||||
using DeleteFilesFromDiskCallback = std::function<void()>;
|
||||
|
||||
// Pass in a TaskRunner to use for testing.
|
||||
@@ -46,10 +40,6 @@ class NearbyFileHandler {
|
||||
std::unique_ptr<TaskRunner> runner = nullptr);
|
||||
~NearbyFileHandler();
|
||||
|
||||
// Open the files given in |file_paths| and return the opened files sizes via
|
||||
// |callback|. If any file fails to open, return an empty list.
|
||||
void OpenFiles(std::vector<FilePath> file_paths, OpenFilesCallback callback);
|
||||
|
||||
void DeleteFilesFromDisk(std::vector<FilePath> file_paths,
|
||||
DeleteFilesFromDiskCallback callback);
|
||||
|
||||
|
||||
@@ -22,7 +22,6 @@
|
||||
#include <vector>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/synchronization/notification.h"
|
||||
#include "absl/time/time.h"
|
||||
#include "internal/base/file_path.h"
|
||||
#include "internal/base/files.h"
|
||||
@@ -46,27 +45,6 @@ bool CreateFile(FilePath& file_path) {
|
||||
return true;
|
||||
}
|
||||
|
||||
TEST(NearbyFileHandler, OpenFiles) {
|
||||
MockSharingPlatform mock_platform;
|
||||
NearbyFileHandler nearby_file_handler(mock_platform);
|
||||
absl::Notification notification;
|
||||
std::vector<NearbyFileHandler::FileInfo> result;
|
||||
FilePath test_file = Files::GetTemporaryDirectory().append(
|
||||
FilePath("nearby_nfh_test_abc.jpg"));
|
||||
|
||||
ASSERT_TRUE(CreateFile(test_file));
|
||||
nearby_file_handler.OpenFiles(
|
||||
{test_file}, [&result, ¬ification](
|
||||
std::vector<NearbyFileHandler::FileInfo> file_infos) {
|
||||
result = file_infos;
|
||||
notification.Notify();
|
||||
});
|
||||
|
||||
notification.WaitForNotificationWithTimeout(absl::Seconds(1));
|
||||
EXPECT_EQ(result.size(), 1);
|
||||
ASSERT_TRUE(Files::RemoveFile(test_file));
|
||||
}
|
||||
|
||||
TEST(NearbyFileHandler, DeleteAFileFromDisk) {
|
||||
MockSharingPlatform mock_platform;
|
||||
FakeClock clock;
|
||||
|
||||
@@ -129,10 +129,6 @@ constexpr absl::Duration kProcessShutdownPendingTimerDelay = // NOLINT
|
||||
absl::Seconds(15);
|
||||
constexpr absl::Duration kProcessNetworkChangeTimerDelay = absl::Seconds(1);
|
||||
|
||||
// Cooldown period after a successful incoming share before we allow the "Device
|
||||
// nearby is sharing" notification to appear again.
|
||||
constexpr absl::Duration kFastInitiationScannerCooldown = absl::Seconds(8);
|
||||
|
||||
// The maximum number of certificate downloads that can be performed during a
|
||||
// discovery session.
|
||||
// Assuming a 2min discovery session and 10s download interval.
|
||||
@@ -734,27 +730,40 @@ void NearbySharingServiceImpl::SendAttachments(
|
||||
|
||||
app_info_->SetActiveFlag();
|
||||
|
||||
OnTransferStarted(/*is_incoming=*/false);
|
||||
is_connecting_ = true;
|
||||
InvalidateSendSurfaceState();
|
||||
|
||||
// Send process initialized successfully, from now on status updated
|
||||
// will be sent out via OnOutgoingTransferUpdate().
|
||||
session->UpdateTransferMetadata(
|
||||
TransferMetadataBuilder()
|
||||
.set_status(TransferMetadata::Status::kConnecting)
|
||||
.build());
|
||||
|
||||
CreatePayloads(
|
||||
*session, [this, endpoint_info = std::move(*endpoint_info)](
|
||||
OutgoingShareSession& session, bool success) {
|
||||
OnCreatePayloads(std::move(endpoint_info), session, success);
|
||||
});
|
||||
|
||||
if (!CreatePayloads(*session)) {
|
||||
session->UpdateTransferMetadata(
|
||||
TransferMetadataBuilder()
|
||||
.set_status(TransferMetadata::Status::kMediaUnavailable)
|
||||
.build());
|
||||
} else {
|
||||
OutgoingSessionConnect(*session, std::move(*endpoint_info));
|
||||
}
|
||||
std::move(status_codes_callback)(StatusCodes::kOk);
|
||||
});
|
||||
}
|
||||
|
||||
void NearbySharingServiceImpl::OutgoingSessionConnect(
|
||||
OutgoingShareSession& session, std::vector<uint8_t> endpoint_info) {
|
||||
OnTransferStarted(/*is_incoming=*/false);
|
||||
is_connecting_ = true;
|
||||
InvalidateSendSurfaceState();
|
||||
// Send process initialized successfully, from now on status updated
|
||||
// will be sent out via OnOutgoingTransferUpdate().
|
||||
session.UpdateTransferMetadata(
|
||||
TransferMetadataBuilder()
|
||||
.set_status(TransferMetadata::Status::kConnecting)
|
||||
.build());
|
||||
|
||||
std::optional<std::vector<uint8_t>> bluetooth_mac_address =
|
||||
GetBluetoothMacAddressForShareTarget(session);
|
||||
int64_t share_target_id = session.share_target().id;
|
||||
session.Connect(
|
||||
std::move(endpoint_info), std::move(bluetooth_mac_address),
|
||||
settings_->GetDataUsage(), GetDisableWifiHotspotState(),
|
||||
absl::bind_front(&NearbySharingServiceImpl::OnOutgoingConnection, this,
|
||||
share_target_id));
|
||||
}
|
||||
|
||||
bool NearbySharingServiceImpl::OutgoingSessionAccept(
|
||||
OutgoingShareSession& session) {
|
||||
return session.AcceptTransfer(
|
||||
@@ -2176,68 +2185,38 @@ void NearbySharingServiceImpl::OnOutgoingConnection(
|
||||
}
|
||||
}
|
||||
|
||||
void NearbySharingServiceImpl::CreatePayloads(
|
||||
OutgoingShareSession& session,
|
||||
std::function<void(OutgoingShareSession&, bool)> callback) {
|
||||
int64_t share_target_id = session.share_target().id;
|
||||
if (!session.file_payloads().empty() || !session.text_payloads().empty() ||
|
||||
!session.wifi_credentials_payloads().empty()) {
|
||||
// We may have already created the payloads in the case of retry, so we can
|
||||
// skip this step.
|
||||
std::move(callback)(session, /*success=*/false);
|
||||
return;
|
||||
}
|
||||
session.CreateTextPayloads();
|
||||
session.CreateWifiCredentialsPayloads();
|
||||
file_handler_.OpenFiles(
|
||||
session.GetFilePaths(),
|
||||
[this, share_target_id, callback = std::move(callback)](
|
||||
std::vector<NearbyFileHandler::FileInfo> file_infos) {
|
||||
RunOnNearbySharingServiceThread(
|
||||
"open_files",
|
||||
[this, share_target_id, callback = std::move(callback),
|
||||
file_infos = std::move(file_infos)]() {
|
||||
OutgoingShareSession* session =
|
||||
outgoing_targets_manager_.GetOutgoingShareSession(
|
||||
share_target_id);
|
||||
if (session == nullptr) {
|
||||
return;
|
||||
}
|
||||
bool result = session->CreateFilePayloads(file_infos);
|
||||
std::move(callback)(*session, result);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void NearbySharingServiceImpl::OnCreatePayloads(
|
||||
std::vector<uint8_t> endpoint_info, OutgoingShareSession& session,
|
||||
bool success) {
|
||||
bool NearbySharingServiceImpl::CreatePayloads(OutgoingShareSession& session) {
|
||||
bool has_payloads = !session.text_payloads().empty() ||
|
||||
!session.file_payloads().empty() ||
|
||||
!session.wifi_credentials_payloads().empty();
|
||||
if (!success || !has_payloads) {
|
||||
if (has_payloads) {
|
||||
// We may have already created the payloads in the case of retry.
|
||||
// Retry is not implemented. So this is an error case.
|
||||
LOG(WARNING)
|
||||
<< __func__
|
||||
<< ": Failed to send attachments. Unexpected payloads already exist.";
|
||||
return false;
|
||||
}
|
||||
session.CreateTextPayloads();
|
||||
session.CreateWifiCredentialsPayloads();
|
||||
bool success = session.CreateFilePayloads();
|
||||
if (success) {
|
||||
has_payloads = !session.text_payloads().empty() ||
|
||||
!session.file_payloads().empty() ||
|
||||
!session.wifi_credentials_payloads().empty();
|
||||
if (!has_payloads) {
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
if (!success) {
|
||||
LOG(WARNING) << __func__
|
||||
<< ": Failed to send file to remote ShareTarget. Failed to "
|
||||
"create payloads.";
|
||||
session.UpdateTransferMetadata(
|
||||
TransferMetadataBuilder()
|
||||
.set_status(TransferMetadata::Status::kMediaUnavailable)
|
||||
.build());
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
// Log analytics event of describing attachments.
|
||||
analytics_recorder_.NewDescribeAttachments(session.attachment_container());
|
||||
|
||||
std::optional<std::vector<uint8_t>> bluetooth_mac_address =
|
||||
GetBluetoothMacAddressForShareTarget(session);
|
||||
|
||||
int64_t share_target_id = session.share_target().id;
|
||||
|
||||
session.Connect(
|
||||
std::move(endpoint_info), std::move(bluetooth_mac_address),
|
||||
settings_->GetDataUsage(), GetDisableWifiHotspotState(),
|
||||
absl::bind_front(&NearbySharingServiceImpl::OnOutgoingConnection, this,
|
||||
share_target_id));
|
||||
return true;
|
||||
}
|
||||
|
||||
void NearbySharingServiceImpl::Fail(IncomingShareSession& session,
|
||||
|
||||
@@ -287,11 +287,9 @@ class NearbySharingServiceImpl
|
||||
absl::string_view endpoint_id,
|
||||
NearbyConnection* connection, Status status);
|
||||
|
||||
void CreatePayloads(
|
||||
OutgoingShareSession& session,
|
||||
std::function<void(OutgoingShareSession&, bool)> callback);
|
||||
void OnCreatePayloads(std::vector<uint8_t> endpoint_info,
|
||||
OutgoingShareSession& session, bool success);
|
||||
bool CreatePayloads(OutgoingShareSession& session);
|
||||
void OutgoingSessionConnect(OutgoingShareSession& session,
|
||||
std::vector<uint8_t> endpoint_info);
|
||||
|
||||
void Fail(IncomingShareSession& session, TransferMetadata::Status status);
|
||||
void OnIncomingAdvertisementDecoded(
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
|
||||
#include "sharing/outgoing_share_session.h"
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
@@ -28,6 +27,7 @@
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/time/time.h"
|
||||
#include "internal/base/file_path.h"
|
||||
#include "internal/base/files.h"
|
||||
#include "internal/platform/clock.h"
|
||||
#include "internal/platform/task_runner.h"
|
||||
#include "sharing/analytics/analytics_recorder.h"
|
||||
@@ -39,7 +39,6 @@
|
||||
#include "sharing/nearby_connection.h"
|
||||
#include "sharing/nearby_connections_manager.h"
|
||||
#include "sharing/nearby_connections_types.h"
|
||||
#include "sharing/nearby_file_handler.h"
|
||||
#include "sharing/paired_key_verification_runner.h"
|
||||
#include "sharing/payload_tracker.h"
|
||||
#include "sharing/share_session.h"
|
||||
@@ -164,18 +163,6 @@ void OutgoingShareSession::OnConnectionDisconnected() {
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<FilePath> OutgoingShareSession::GetFilePaths() const {
|
||||
std::vector<FilePath> file_paths;
|
||||
file_paths.reserve(attachment_container().GetFileAttachments().size());
|
||||
for (const FileAttachment& file_attachment :
|
||||
attachment_container().GetFileAttachments()) {
|
||||
// All file attachments must have a file path.
|
||||
// That is verified in SendAttachments().
|
||||
file_paths.push_back(*file_attachment.file_path());
|
||||
}
|
||||
return file_paths;
|
||||
}
|
||||
|
||||
void OutgoingShareSession::CreateTextPayloads() {
|
||||
const std::vector<TextAttachment>& attachments =
|
||||
attachment_container().GetTextAttachments();
|
||||
@@ -214,24 +201,27 @@ void OutgoingShareSession::CreateWifiCredentialsPayloads() {
|
||||
}
|
||||
}
|
||||
|
||||
bool OutgoingShareSession::CreateFilePayloads(
|
||||
const std::vector<NearbyFileHandler::FileInfo>& files) {
|
||||
AttachmentContainer& container = mutable_attachment_container();
|
||||
if (files.size() != container.GetFileAttachments().size()) {
|
||||
return false;
|
||||
}
|
||||
if (files.empty()) {
|
||||
bool OutgoingShareSession::CreateFilePayloads() {
|
||||
if (attachment_container().GetFileAttachments().empty()) {
|
||||
return true;
|
||||
}
|
||||
AttachmentContainer& container = mutable_attachment_container();
|
||||
file_payloads_.clear();
|
||||
file_payloads_.reserve(files.size());
|
||||
file_payloads_.reserve(container.GetFileAttachments().size());
|
||||
|
||||
for (size_t i = 0; i < files.size(); ++i) {
|
||||
const NearbyFileHandler::FileInfo& file_info = files[i];
|
||||
for (int i = 0; i < container.GetFileAttachments().size(); ++i) {
|
||||
FileAttachment& attachment = container.GetMutableFileAttachment(i);
|
||||
attachment.set_size(file_info.size);
|
||||
Payload payload(file_info.file_path, attachment.parent_folder());
|
||||
payload.content.file_payload.size = file_info.size;
|
||||
// All file attachments must have a file path.
|
||||
// That is verified in SendAttachments().
|
||||
FilePath file_path = *attachment.file_path();
|
||||
std::optional<uintmax_t> file_size = Files::GetFileSize(file_path);
|
||||
if (!file_size.has_value()) {
|
||||
LOG(WARNING) << "Failed to get file size for file: "
|
||||
<< file_path.ToString();
|
||||
return false;
|
||||
}
|
||||
attachment.set_size(*file_size);
|
||||
Payload payload(file_path, attachment.parent_folder());
|
||||
file_payloads_.push_back(std::move(payload));
|
||||
SetAttachmentPayloadId(attachment.id(), file_payloads_.back().id);
|
||||
}
|
||||
|
||||
@@ -26,7 +26,6 @@
|
||||
#include "absl/functional/any_invocable.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/time/time.h"
|
||||
#include "internal/base/file_path.h"
|
||||
#include "internal/platform/clock.h"
|
||||
#include "internal/platform/task_runner.h"
|
||||
#include "sharing/analytics/analytics_recorder.h"
|
||||
@@ -35,7 +34,6 @@
|
||||
#include "sharing/nearby_connection.h"
|
||||
#include "sharing/nearby_connections_manager.h"
|
||||
#include "sharing/nearby_connections_types.h"
|
||||
#include "sharing/nearby_file_handler.h"
|
||||
#include "sharing/paired_key_verification_runner.h"
|
||||
#include "sharing/proto/enums.pb.h"
|
||||
#include "sharing/share_session.h"
|
||||
@@ -84,16 +82,11 @@ class OutgoingShareSession : public ShareSession {
|
||||
PairedKeyVerificationRunner::PairedKeyVerificationResult result,
|
||||
location::nearby::proto::sharing::OSType share_target_os_type);
|
||||
|
||||
std::vector<FilePath> GetFilePaths() const;
|
||||
|
||||
void CreateTextPayloads();
|
||||
void CreateWifiCredentialsPayloads();
|
||||
// Create file payloads and update the file size of all file attachments.
|
||||
// The list of file infos must be sorted in the same order as the file
|
||||
// attachments in the share target.
|
||||
// Returns true if all file payloads are created successfully.
|
||||
bool CreateFilePayloads(
|
||||
const std::vector<NearbyFileHandler::FileInfo>& files);
|
||||
bool CreateFilePayloads();
|
||||
|
||||
// Returns true if the introduction frame is written successfully.
|
||||
// `timeout_callback` is called if accept is not received from both sender and
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "sharing/outgoing_share_session.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
@@ -29,6 +30,7 @@
|
||||
#include "internal/analytics/mock_event_logger.h"
|
||||
#include "internal/analytics/sharing_log_matchers.h"
|
||||
#include "internal/base/file_path.h"
|
||||
#include "internal/base/files.h"
|
||||
#include "internal/network/url.h"
|
||||
#include "internal/test/fake_clock.h"
|
||||
#include "internal/test/fake_device_info.h"
|
||||
@@ -43,7 +45,6 @@
|
||||
#include "sharing/nearby_connection_impl.h"
|
||||
#include "sharing/nearby_connections_manager.h"
|
||||
#include "sharing/nearby_connections_types.h"
|
||||
#include "sharing/nearby_file_handler.h"
|
||||
#include "sharing/paired_key_verification_runner.h"
|
||||
#include "sharing/proto/analytics/nearby_sharing_log.pb.h"
|
||||
#include "sharing/proto/analytics/nearby_sharing_log.proto.static_reflection.h"
|
||||
@@ -83,6 +84,10 @@ using ::testing::StrictMock;
|
||||
using ::testing::proto::ProtoField;
|
||||
|
||||
constexpr absl::string_view kEndpointId = "ABCD";
|
||||
constexpr absl::string_view kFile1Name = "someFileName.jpg";
|
||||
constexpr absl::string_view kFile2Name = "someFileName2.jpg";
|
||||
constexpr int kFile1Size = 1234;
|
||||
constexpr int kFile2Size = 5678;
|
||||
|
||||
class OutgoingShareSessionTest : public ::testing::Test {
|
||||
public:
|
||||
@@ -94,14 +99,28 @@ class OutgoingShareSessionTest : public ::testing::Test {
|
||||
"A bit of text body", "Some text title", "text/html"),
|
||||
text2_(nearby::sharing::service::proto::TextMetadata::ADDRESS,
|
||||
"A bit of text body 2", "Some text title 2", "text/plain"),
|
||||
file1_(FilePath("/usr/local/tmp/someFileName.jpg"), /*mime_type=*/"",
|
||||
file1_(Files::GetTemporaryDirectory().append(FilePath(kFile1Name)),
|
||||
/*mime_type=*/"",
|
||||
/*parent_folder=*/"/usr/local/parent"),
|
||||
file2_(FilePath("/usr/local/tmp/someFileName2.jpg"), /*mime_type=*/"",
|
||||
file2_(Files::GetTemporaryDirectory().append(FilePath(kFile2Name)),
|
||||
/*mime_type=*/"",
|
||||
/*parent_folder=*/"/usr/local/parent2"),
|
||||
wifi1_(
|
||||
"GoogleGuest",
|
||||
nearby::sharing::service::proto::WifiCredentialsMetadata::WPA_PSK,
|
||||
"somepassword", /*is_hidden=*/true) {}
|
||||
"somepassword", /*is_hidden=*/true) {
|
||||
// Create temp file attachments
|
||||
FilePath file1_path =
|
||||
Files::GetTemporaryDirectory().append(FilePath(kFile1Name));
|
||||
FilePath file2_path =
|
||||
Files::GetTemporaryDirectory().append(FilePath(kFile2Name));
|
||||
std::ofstream file1_stream(file1_path.GetPath());
|
||||
file1_stream << std::string(kFile1Size, 'a');
|
||||
file1_stream.close();
|
||||
std::ofstream file2_stream(file2_path.GetPath());
|
||||
file2_stream << std::string(kFile2Size, 'b');
|
||||
file2_stream.close();
|
||||
}
|
||||
|
||||
std::unique_ptr<AttachmentContainer> CreateDefaultAttachmentContainer() {
|
||||
return AttachmentContainer::Builder(
|
||||
@@ -153,25 +172,6 @@ class OutgoingShareSessionTest : public ::testing::Test {
|
||||
WifiCredentialsAttachment wifi1_;
|
||||
};
|
||||
|
||||
TEST_F(OutgoingShareSessionTest, GetFilePaths) {
|
||||
OutgoingShareSession session(
|
||||
&fake_clock_, fake_task_runner_, &connections_manager_,
|
||||
analytics_recorder_, std::string(kEndpointId), share_target_,
|
||||
[](OutgoingShareSession&, const TransferMetadata&) {});
|
||||
auto container =
|
||||
AttachmentContainer::Builder(std::vector<TextAttachment>{},
|
||||
std::vector<FileAttachment>{file1_, file2_},
|
||||
std::vector<WifiCredentialsAttachment>{})
|
||||
.Build();
|
||||
session.InitiateSendAttachments(std::move(container));
|
||||
|
||||
auto file_paths = session.GetFilePaths();
|
||||
|
||||
ASSERT_THAT(file_paths, SizeIs(2));
|
||||
EXPECT_THAT(file_paths[0], Eq(file1_.file_path()));
|
||||
EXPECT_THAT(file_paths[1], Eq(file2_.file_path()));
|
||||
}
|
||||
|
||||
TEST_F(OutgoingShareSessionTest, CreateTextPayloadsWithNoTextAttachments) {
|
||||
OutgoingShareSession session(
|
||||
&fake_clock_, fake_task_runner_, &connections_manager_,
|
||||
@@ -212,19 +212,19 @@ TEST_F(OutgoingShareSessionTest, CreateFilePayloadsWithNoFileAttachments) {
|
||||
analytics_recorder_, std::string(kEndpointId), share_target_,
|
||||
[](OutgoingShareSession&, const TransferMetadata&) {});
|
||||
|
||||
EXPECT_THAT(
|
||||
session.CreateFilePayloads(std::vector<NearbyFileHandler::FileInfo>()),
|
||||
IsTrue());
|
||||
EXPECT_THAT(session.CreateFilePayloads(), IsTrue());
|
||||
const std::vector<Payload>& payloads = session.file_payloads();
|
||||
|
||||
EXPECT_THAT(payloads, IsEmpty());
|
||||
}
|
||||
|
||||
TEST_F(OutgoingShareSessionTest, CreateFilePayloadsWithWrongFileInfo) {
|
||||
TEST_F(OutgoingShareSessionTest,
|
||||
CreateFilePayloadsWithNonexistentFileAttachment) {
|
||||
InitSendAttachments(CreateDefaultAttachmentContainer());
|
||||
EXPECT_THAT(
|
||||
session_.CreateFilePayloads(std::vector<NearbyFileHandler::FileInfo>()),
|
||||
IsFalse());
|
||||
// Remove attachment file1.
|
||||
Files::RemoveFile(
|
||||
Files::GetTemporaryDirectory().append(FilePath(kFile1Name)));
|
||||
EXPECT_THAT(session_.CreateFilePayloads(), IsFalse());
|
||||
const std::vector<Payload>& payloads = session_.file_payloads();
|
||||
|
||||
EXPECT_THAT(payloads, IsEmpty());
|
||||
@@ -232,18 +232,12 @@ TEST_F(OutgoingShareSessionTest, CreateFilePayloadsWithWrongFileInfo) {
|
||||
|
||||
TEST_F(OutgoingShareSessionTest, CreateFilePayloads) {
|
||||
InitSendAttachments(CreateDefaultAttachmentContainer());
|
||||
std::vector<NearbyFileHandler::FileInfo> file_infos;
|
||||
file_infos.push_back({
|
||||
.size = 12355L,
|
||||
.file_path = file1_.file_path().value(),
|
||||
});
|
||||
session_.CreateFilePayloads(file_infos);
|
||||
session_.CreateFilePayloads();
|
||||
const std::vector<Payload>& payloads = session_.file_payloads();
|
||||
auto& attachment_payload_map = session_.attachment_payload_map();
|
||||
|
||||
ASSERT_THAT(payloads, SizeIs(1));
|
||||
EXPECT_THAT(payloads[0].content.type, Eq(PayloadContent::Type::kFile));
|
||||
EXPECT_THAT(payloads[0].content.file_payload.size, Eq(12355L));
|
||||
EXPECT_THAT(payloads[0].content.file_payload.parent_folder,
|
||||
Eq(file1_.parent_folder()));
|
||||
EXPECT_THAT(payloads[0].content.file_payload.file_path,
|
||||
@@ -254,7 +248,7 @@ TEST_F(OutgoingShareSessionTest, CreateFilePayloads) {
|
||||
EXPECT_THAT(attachment_payload_map.at(file1_.id()), Eq(payloads[0].id));
|
||||
|
||||
EXPECT_THAT(session_.attachment_container().GetFileAttachments()[0].size(),
|
||||
Eq(12355L));
|
||||
Eq(kFile1Size));
|
||||
}
|
||||
|
||||
TEST_F(OutgoingShareSessionTest, CreateWifiPayloadsWithNoWifiAttachments) {
|
||||
@@ -415,12 +409,7 @@ TEST_F(OutgoingShareSessionTest, SendIntroductionSuccess) {
|
||||
session_.set_session_id(1234);
|
||||
NearbyConnectionImpl connection(device_info_);
|
||||
ConnectionSuccess(&connection);
|
||||
std::vector<NearbyFileHandler::FileInfo> file_infos;
|
||||
file_infos.push_back({
|
||||
.size = 12355L,
|
||||
.file_path = file1_.file_path().value(),
|
||||
});
|
||||
session_.CreateFilePayloads(file_infos);
|
||||
session_.CreateFilePayloads();
|
||||
session_.CreateTextPayloads();
|
||||
session_.CreateWifiCredentialsPayloads();
|
||||
EXPECT_CALL(mock_event_logger_,
|
||||
@@ -467,7 +456,7 @@ TEST_F(OutgoingShareSessionTest, SendIntroductionSuccess) {
|
||||
ASSERT_THAT(intro_frame.file_metadata_size(), Eq(1));
|
||||
EXPECT_THAT(intro_frame.file_metadata(0).id(), Eq(file1_.id()));
|
||||
// File attachment size has been updated by CreateFilePayloads().
|
||||
EXPECT_THAT(intro_frame.file_metadata(0).size(), Eq(file_infos[0].size));
|
||||
EXPECT_THAT(intro_frame.file_metadata(0).size(), Eq(kFile1Size));
|
||||
EXPECT_THAT(intro_frame.file_metadata(0).name(), Eq(file1_.file_name()));
|
||||
EXPECT_THAT(intro_frame.file_metadata(0).payload_id(),
|
||||
Eq(file_payloads[0].id));
|
||||
@@ -679,12 +668,7 @@ TEST_F(OutgoingShareSessionTest, HandleConnectionResponseAcceptResponse) {
|
||||
TEST_F(OutgoingShareSessionTest, SendPayloads) {
|
||||
InitSendAttachments(CreateDefaultAttachmentContainer());
|
||||
session_.set_session_id(1234);
|
||||
std::vector<NearbyFileHandler::FileInfo> file_infos;
|
||||
file_infos.push_back({
|
||||
.size = 12355L,
|
||||
.file_path = file1_.file_path().value(),
|
||||
});
|
||||
session_.CreateFilePayloads(file_infos);
|
||||
session_.CreateFilePayloads();
|
||||
session_.CreateTextPayloads();
|
||||
session_.CreateWifiCredentialsPayloads();
|
||||
MockFunction<void()> payload_transder_update_callback;
|
||||
@@ -725,12 +709,7 @@ TEST_F(OutgoingShareSessionTest, SendPayloads) {
|
||||
TEST_F(OutgoingShareSessionTest, SendPayloadsSetsAdvancedProtectionFlags) {
|
||||
InitSendAttachments(CreateDefaultAttachmentContainer());
|
||||
session_.set_session_id(1234);
|
||||
std::vector<NearbyFileHandler::FileInfo> file_infos;
|
||||
file_infos.push_back({
|
||||
.size = 12355L,
|
||||
.file_path = file1_.file_path().value(),
|
||||
});
|
||||
session_.CreateFilePayloads(file_infos);
|
||||
session_.CreateFilePayloads();
|
||||
session_.CreateTextPayloads();
|
||||
session_.CreateWifiCredentialsPayloads();
|
||||
MockFunction<void()> payload_transder_update_callback;
|
||||
@@ -773,12 +752,7 @@ TEST_F(OutgoingShareSessionTest, SendPayloadsSetsAdvancedProtectionFlags) {
|
||||
TEST_F(OutgoingShareSessionTest, SendNextPayload) {
|
||||
InitSendAttachments(CreateDefaultAttachmentContainer());
|
||||
session_.set_session_id(1234);
|
||||
std::vector<NearbyFileHandler::FileInfo> file_infos;
|
||||
file_infos.push_back({
|
||||
.size = 12355L,
|
||||
.file_path = file1_.file_path().value(),
|
||||
});
|
||||
session_.CreateFilePayloads(file_infos);
|
||||
session_.CreateFilePayloads();
|
||||
session_.CreateTextPayloads();
|
||||
session_.CreateWifiCredentialsPayloads();
|
||||
MockFunction<void()> payload_transder_update_callback;
|
||||
|
||||
Reference in New Issue
Block a user