blob: ef769bb2e4b689ca9b6b90b49abd9b23a9934245 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2019 The Chromium Authors
Alex Chau4fded64e2019-07-17 16:21:512// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Hira Mahmood0f3c35a82024-07-31 15:43:265#include "components/sharing_message/ack_message_handler.h"
Alex Chau4fded64e2019-07-17 16:21:516
Avi Drissman9269d4ed2023-01-07 01:38:067#include "base/functional/bind.h"
8#include "base/functional/callback_helpers.h"
Alex Chaue1bc15162019-10-24 10:55:409#include "base/test/mock_callback.h"
Hira Mahmood0f3c35a82024-07-31 15:43:2610#include "components/sharing_message/mock_sharing_message_sender.h"
Hira Mahmoode16b75f02024-07-18 17:46:4311#include "components/sharing_message/proto/sharing_message.pb.h"
Hira Mahmood0f3c35a82024-07-31 15:43:2612#include "components/sharing_message/sharing_fcm_sender.h"
Alex Chaue1bc15162019-10-24 10:55:4013#include "testing/gmock/include/gmock/gmock.h"
Alex Chau4fded64e2019-07-17 16:21:5114#include "testing/gtest/include/gtest/gtest.h"
15
Alex Chau4fded64e2019-07-17 16:21:5116namespace {
Himanshu Jaju63dbf1722019-10-30 09:55:5417constexpr char kTestMessageId[] = "test_message_id";
Alex Chau4fded64e2019-07-17 16:21:5118
Peter Kastingc11cb0a2019-10-07 11:15:2219class AckMessageHandlerTest : public testing::Test {
Alex Chau4fded64e2019-07-17 16:21:5120 protected:
Alex Chaue1bc15162019-10-24 10:55:4021 AckMessageHandlerTest()
Himanshu Jaju63dbf1722019-10-30 09:55:5422 : ack_message_handler_(&mock_response_callback_helper_) {}
Alex Chau4fded64e2019-07-17 16:21:5123
Himanshu Jajua69dbe52019-11-04 19:58:0524 testing::NiceMock<MockSharingMessageSender> mock_response_callback_helper_;
Alex Chaue1bc15162019-10-24 10:55:4025 AckMessageHandler ack_message_handler_;
Alex Chau4fded64e2019-07-17 16:21:5126};
27
Himanshu Jaju63dbf1722019-10-30 09:55:5428MATCHER_P(ProtoEquals, message, "") {
Hira Mahmood0f3c35a82024-07-31 15:43:2629 if (!arg) {
Himanshu Jaju63dbf1722019-10-30 09:55:5430 return false;
Hira Mahmood0f3c35a82024-07-31 15:43:2631 }
Himanshu Jaju63dbf1722019-10-30 09:55:5432
Alex Chaue1bc15162019-10-24 10:55:4033 std::string expected_serialized, actual_serialized;
Himanshu Jaju63dbf1722019-10-30 09:55:5434 message.SerializeToString(&expected_serialized);
35 arg->SerializeToString(&actual_serialized);
Alex Chaue1bc15162019-10-24 10:55:4036 return expected_serialized == actual_serialized;
37}
38
Alex Chau4fded64e2019-07-17 16:21:5139} // namespace
40
Alex Chaue1bc15162019-10-24 10:55:4041TEST_F(AckMessageHandlerTest, OnMessageNoResponse) {
Hira Mahmoode16b75f02024-07-18 17:46:4342 components_sharing_message::SharingMessage sharing_message;
Alex Chau4fded64e2019-07-17 16:21:5143 sharing_message.mutable_ack_message()->set_original_message_id(
44 kTestMessageId);
45
Alex Chaue1bc15162019-10-24 10:55:4046 base::MockCallback<SharingMessageHandler::DoneCallback> done_callback;
Himanshu Jaju63dbf1722019-10-30 09:55:5447 EXPECT_CALL(done_callback, Run(testing::Eq(nullptr)));
48
Richard Knoll5f8a4022020-01-15 17:08:1449 EXPECT_CALL(mock_response_callback_helper_,
50 OnAckReceived(testing::Eq(kTestMessageId), testing::Eq(nullptr)));
Alex Chaue1bc15162019-10-24 10:55:4051
52 ack_message_handler_.OnMessage(std::move(sharing_message),
53 done_callback.Get());
Alex Chaue1bc15162019-10-24 10:55:4054}
55
56TEST_F(AckMessageHandlerTest, OnMessageWithResponse) {
Hira Mahmoode16b75f02024-07-18 17:46:4357 components_sharing_message::SharingMessage sharing_message;
Alex Chaue1bc15162019-10-24 10:55:4058 sharing_message.mutable_ack_message()->set_original_message_id(
59 kTestMessageId);
Alex Chaue1bc15162019-10-24 10:55:4060 sharing_message.mutable_ack_message()->mutable_response_message();
61
Hira Mahmoode16b75f02024-07-18 17:46:4362 components_sharing_message::ResponseMessage response_message_copy =
Alex Chaue1bc15162019-10-24 10:55:4063 sharing_message.ack_message().response_message();
64
65 base::MockCallback<SharingMessageHandler::DoneCallback> done_callback;
Himanshu Jaju63dbf1722019-10-30 09:55:5466 EXPECT_CALL(done_callback, Run(testing::Eq(nullptr)));
67
Richard Knoll5f8a4022020-01-15 17:08:1468 EXPECT_CALL(mock_response_callback_helper_,
69 OnAckReceived(testing::Eq(kTestMessageId),
70 ProtoEquals(response_message_copy)));
Alex Chaue1bc15162019-10-24 10:55:4071
72 ack_message_handler_.OnMessage(std::move(sharing_message),
73 done_callback.Get());
Alex Chau4fded64e2019-07-17 16:21:5174}