Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/sharing/ack_message_handler.h" |
| 6 | |
Avi Drissman | 9269d4ed | 2023-01-07 01:38:06 | [diff] [blame] | 7 | #include "base/functional/bind.h" |
| 8 | #include "base/functional/callback_helpers.h" |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 9 | #include "base/test/mock_callback.h" |
Richard Knoll | a5e43b8 | 2020-01-28 19:49:03 | [diff] [blame] | 10 | #include "chrome/browser/sharing/mock_sharing_message_sender.h" |
Michael van Ouwerkerk | 741c6652 | 2019-12-17 13:51:13 | [diff] [blame] | 11 | #include "chrome/browser/sharing/proto/sharing_message.pb.h" |
Richard Knoll | 367f223 | 2019-11-14 18:11:34 | [diff] [blame] | 12 | #include "chrome/browser/sharing/sharing_fcm_sender.h" |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 13 | #include "testing/gmock/include/gmock/gmock.h" |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 16 | namespace { |
Himanshu Jaju | 63dbf172 | 2019-10-30 09:55:54 | [diff] [blame] | 17 | constexpr char kTestMessageId[] = "test_message_id"; |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 18 | |
Peter Kasting | c11cb0a | 2019-10-07 11:15:22 | [diff] [blame] | 19 | class AckMessageHandlerTest : public testing::Test { |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 20 | protected: |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 21 | AckMessageHandlerTest() |
Himanshu Jaju | 63dbf172 | 2019-10-30 09:55:54 | [diff] [blame] | 22 | : ack_message_handler_(&mock_response_callback_helper_) {} |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 23 | |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 24 | testing::NiceMock<MockSharingMessageSender> mock_response_callback_helper_; |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 25 | AckMessageHandler ack_message_handler_; |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 26 | }; |
| 27 | |
Himanshu Jaju | 63dbf172 | 2019-10-30 09:55:54 | [diff] [blame] | 28 | MATCHER_P(ProtoEquals, message, "") { |
| 29 | if (!arg) |
| 30 | return false; |
| 31 | |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 32 | std::string expected_serialized, actual_serialized; |
Himanshu Jaju | 63dbf172 | 2019-10-30 09:55:54 | [diff] [blame] | 33 | message.SerializeToString(&expected_serialized); |
| 34 | arg->SerializeToString(&actual_serialized); |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 35 | return expected_serialized == actual_serialized; |
| 36 | } |
| 37 | |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 38 | } // namespace |
| 39 | |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 40 | TEST_F(AckMessageHandlerTest, OnMessageNoResponse) { |
Peter Kasting | c11cb0a | 2019-10-07 11:15:22 | [diff] [blame] | 41 | chrome_browser_sharing::SharingMessage sharing_message; |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 42 | sharing_message.mutable_ack_message()->set_original_message_id( |
| 43 | kTestMessageId); |
| 44 | |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 45 | base::MockCallback<SharingMessageHandler::DoneCallback> done_callback; |
Himanshu Jaju | 63dbf172 | 2019-10-30 09:55:54 | [diff] [blame] | 46 | EXPECT_CALL(done_callback, Run(testing::Eq(nullptr))); |
| 47 | |
Richard Knoll | 5f8a402 | 2020-01-15 17:08:14 | [diff] [blame] | 48 | EXPECT_CALL(mock_response_callback_helper_, |
| 49 | OnAckReceived(testing::Eq(kTestMessageId), testing::Eq(nullptr))); |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 50 | |
| 51 | ack_message_handler_.OnMessage(std::move(sharing_message), |
| 52 | done_callback.Get()); |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | TEST_F(AckMessageHandlerTest, OnMessageWithResponse) { |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 56 | chrome_browser_sharing::SharingMessage sharing_message; |
| 57 | sharing_message.mutable_ack_message()->set_original_message_id( |
| 58 | kTestMessageId); |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 59 | sharing_message.mutable_ack_message()->mutable_response_message(); |
| 60 | |
| 61 | chrome_browser_sharing::ResponseMessage response_message_copy = |
| 62 | sharing_message.ack_message().response_message(); |
| 63 | |
| 64 | base::MockCallback<SharingMessageHandler::DoneCallback> done_callback; |
Himanshu Jaju | 63dbf172 | 2019-10-30 09:55:54 | [diff] [blame] | 65 | EXPECT_CALL(done_callback, Run(testing::Eq(nullptr))); |
| 66 | |
Richard Knoll | 5f8a402 | 2020-01-15 17:08:14 | [diff] [blame] | 67 | EXPECT_CALL(mock_response_callback_helper_, |
| 68 | OnAckReceived(testing::Eq(kTestMessageId), |
| 69 | ProtoEquals(response_message_copy))); |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 70 | |
| 71 | ack_message_handler_.OnMessage(std::move(sharing_message), |
| 72 | done_callback.Get()); |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 73 | } |