From c9c05b73fc17152b659f204f69e97a70286ad78b Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Fri, 9 Aug 2024 13:32:05 -0700 Subject: [PATCH] cleanup tests PiperOrigin-RevId: 661384334 --- sharing/BUILD | 22 +++++++++-- sharing/incoming_frames_reader_test.cc | 53 +++++++++++++++----------- 2 files changed, 50 insertions(+), 25 deletions(-) diff --git a/sharing/BUILD b/sharing/BUILD index 433f6a4d..d31ae85e 100644 --- a/sharing/BUILD +++ b/sharing/BUILD @@ -222,10 +222,24 @@ cc_library( ], ) +cc_library( + name = "nearby_connection_impl", + srcs = ["nearby_connection_impl.cc"], + hdrs = ["nearby_connection_impl.h"], + deps = [ + ":connection_types", + ":types", + "//internal/platform:types", + "//sharing/internal/public:logging", + "@com_google_absl//absl/base:core_headers", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/synchronization", + ], +) + cc_library( name = "nearby_sharing_service", srcs = [ - "nearby_connection_impl.cc", "nearby_connections_manager.cc", "nearby_connections_manager_factory.cc", "nearby_connections_manager_impl.cc", @@ -245,7 +259,6 @@ cc_library( hdrs = [ "connection_lifecycle_listener.h", "endpoint_discovery_listener.h", - "nearby_connection_impl.h", "nearby_connections_manager_factory.h", "nearby_connections_manager_impl.h", "nearby_connections_service.h", @@ -277,6 +290,7 @@ cc_library( ":attachments", ":connection_types", ":incoming_frame_reader", + ":nearby_connection_impl", ":nearby_sharing_decoder_impl", ":paired_key_verification_runner", ":share_session", @@ -464,10 +478,12 @@ cc_test( srcs = ["incoming_frames_reader_test.cc"], deps = [ ":incoming_frame_reader", + ":nearby_connection_impl", ":nearby_sharing_decoder_impl", ":test_support", "//internal/platform/implementation/g3", # fixdeps: keep "//internal/test", + "//sharing/internal/public:logging", "//sharing/proto:wire_format_cc_proto", "@com_github_protobuf_matchers//protobuf-matchers", "@com_google_absl//absl/synchronization", @@ -481,8 +497,8 @@ cc_test( srcs = ["nearby_connection_impl_test.cc"], deps = [ ":incoming_frame_reader", + ":nearby_connection_impl", ":nearby_sharing_decoder_impl", - ":nearby_sharing_service", ":test_support", "//internal/platform/implementation/g3", # fixdeps: keep "//internal/test", diff --git a/sharing/incoming_frames_reader_test.cc b/sharing/incoming_frames_reader_test.cc index 361e64b8..b789d842 100644 --- a/sharing/incoming_frames_reader_test.cc +++ b/sharing/incoming_frames_reader_test.cc @@ -24,8 +24,11 @@ #include "absl/synchronization/notification.h" #include "absl/time/time.h" #include "internal/test/fake_clock.h" +#include "internal/test/fake_device_info.h" #include "internal/test/fake_task_runner.h" -#include "sharing/fake_nearby_connection.h" +#include "sharing/fake_nearby_connections_manager.h" +#include "sharing/internal/public/logging.h" +#include "sharing/nearby_connection_impl.h" #include "sharing/nearby_sharing_decoder_impl.h" #include "sharing/proto/wire_format.pb.h" @@ -80,16 +83,22 @@ std::optional> GetInvalidFrame() { class IncomingFramesReaderTest : public testing::Test { public: - IncomingFramesReaderTest() = default; + IncomingFramesReaderTest() { + nearby_connection_ = std::make_unique( + fake_device_info_, &fake_nearby_connections_manager_, "endpoint_id"); + } ~IncomingFramesReaderTest() override = default; void SetUp() override { FakeTaskRunner::ResetPendingTasksCount(); frames_reader_ = std::make_shared( - fake_task_runner_, nearby_sharing_decoder_, &fake_nearby_connection_); + fake_task_runner_, nearby_sharing_decoder_, nearby_connection_.get()); } - FakeNearbyConnection& connection() { return fake_nearby_connection_; } + NearbyConnectionImpl& connection() { + NL_CHECK(nearby_connection_); + return *nearby_connection_; + } IncomingFramesReader* frames_reader() { return frames_reader_.get(); } @@ -102,11 +111,16 @@ class IncomingFramesReaderTest : public testing::Test { } void ReleaseFrameReader() { frames_reader_.reset(); } + void CloseConnection() { + nearby_connection_ = nullptr; + } private: FakeClock fake_clock_; FakeTaskRunner fake_task_runner_ {&fake_clock_, 1}; - FakeNearbyConnection fake_nearby_connection_; + FakeDeviceInfo fake_device_info_; + FakeNearbyConnectionsManager fake_nearby_connections_manager_; + std::unique_ptr nearby_connection_; NearbySharingDecoderImpl nearby_sharing_decoder_; std::shared_ptr frames_reader_ = nullptr; }; @@ -123,18 +137,13 @@ TEST_F(IncomingFramesReaderTest, ReadTimedOut) { Sync(); FastForward(kTimeout); EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kTimeout)); - // Ensure that the OnDataReadFromConnection callback is not run since the - // read timed out. - EXPECT_FALSE(connection().has_read_callback_been_run()); - // Ensure that the IncomingFramesReader does not close the connection. - EXPECT_FALSE(connection().IsClosed()); } TEST_F(IncomingFramesReaderTest, ReadAnyFrameSuccessful) { std::optional> introduction_frame = GetIntroductionFrame(); ASSERT_TRUE(introduction_frame.has_value()); - connection().AppendReadableData(*introduction_frame); + connection().WriteMessage(*introduction_frame); absl::Notification notification; frames_reader()->ReadFrame([&](std::optional frame) { @@ -148,7 +157,7 @@ TEST_F(IncomingFramesReaderTest, ReadSuccessful) { std::optional> introduction_frame = GetIntroductionFrame(); ASSERT_TRUE(introduction_frame.has_value()); - connection().AppendReadableData(*introduction_frame); + connection().WriteMessage(*introduction_frame); absl::Notification notification; frames_reader()->ReadFrame( @@ -164,12 +173,12 @@ TEST_F(IncomingFramesReaderTest, ReadSuccessful) { TEST_F(IncomingFramesReaderTest, ReadSuccessful_JumbledFramesOrdering) { std::optional> cancel_frame = GetCancelFrame(); ASSERT_TRUE(cancel_frame.has_value()); - connection().AppendReadableData(*cancel_frame); + connection().WriteMessage(*cancel_frame); std::optional> introduction_frame = GetIntroductionFrame(); ASSERT_TRUE(introduction_frame.has_value()); - connection().AppendReadableData(*introduction_frame); + connection().WriteMessage(*introduction_frame); absl::Notification notification; frames_reader()->ReadFrame( @@ -185,12 +194,12 @@ TEST_F(IncomingFramesReaderTest, ReadSuccessful_JumbledFramesOrdering) { TEST_F(IncomingFramesReaderTest, JumbledFramesOrdering_ReadFromCache) { std::optional> cancel_frame = GetCancelFrame(); ASSERT_TRUE(cancel_frame.has_value()); - connection().AppendReadableData(*cancel_frame); + connection().WriteMessage(*cancel_frame); std::optional> introduction_frame = GetIntroductionFrame(); ASSERT_TRUE(introduction_frame.has_value()); - connection().AppendReadableData(*introduction_frame); + connection().WriteMessage(*introduction_frame); absl::Notification notification; frames_reader()->ReadFrame( @@ -222,7 +231,7 @@ TEST_F(IncomingFramesReaderTest, ReadAfterConnectionClosed) { }, kTimeout); Sync(); - connection().Close(); + CloseConnection(); EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kTimeout)); } @@ -244,12 +253,12 @@ TEST_F(IncomingFramesReaderTest, ReadTwoFramesWithTimeoutSuccessfully) { std::optional> cancel_frame = GetCancelFrame(); ASSERT_TRUE(cancel_frame.has_value()); - connection().AppendReadableData(*cancel_frame); + connection().WriteMessage(*cancel_frame); std::optional> introduction_frame = GetIntroductionFrame(); ASSERT_TRUE(introduction_frame.has_value()); - connection().AppendReadableData(*introduction_frame); + connection().WriteMessage(*introduction_frame); Sync(); EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kTimeout)); @@ -268,11 +277,11 @@ TEST_F(IncomingFramesReaderTest, ReadTwoFramesWithoutTimeoutSuccessfully) { std::optional> introduction_frame = GetIntroductionFrame(); ASSERT_TRUE(introduction_frame.has_value()); - connection().AppendReadableData(*introduction_frame); + connection().WriteMessage(*introduction_frame); std::optional> cancel_frame = GetCancelFrame(); ASSERT_TRUE(cancel_frame.has_value()); - connection().AppendReadableData(*cancel_frame); + connection().WriteMessage(*cancel_frame); Sync(); EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kTimeout)); @@ -300,7 +309,7 @@ TEST_F(IncomingFramesReaderTest, ReadInvalidFrame) { std::optional> invalid_frame = GetInvalidFrame(); ASSERT_TRUE(invalid_frame.has_value()); - connection().AppendReadableData(*invalid_frame); + connection().WriteMessage(*invalid_frame); Sync(); EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kTimeout));