Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 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 | |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 7 | #include "base/bind_helpers.h" |
| 8 | #include "base/test/mock_callback.h" |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 9 | #include "chrome/browser/sharing/proto/sharing_message.pb.h" |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 10 | #include "testing/gmock/include/gmock/gmock.h" |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 13 | namespace { |
| 14 | |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 15 | class TestObserver { |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 16 | public: |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 17 | void OnAckReceived( |
| 18 | chrome_browser_sharing::MessageType message_type, |
| 19 | std::string message_id, |
| 20 | std::unique_ptr<chrome_browser_sharing::ResponseMessage> response) { |
Michael van Ouwerkerk | 9b86269 | 2019-10-17 08:55:25 | [diff] [blame] | 21 | received_message_type_ = message_type; |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 22 | received_message_id_ = std::move(message_id); |
| 23 | received_response_ = std::move(response); |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 24 | } |
| 25 | |
Michael van Ouwerkerk | 9b86269 | 2019-10-17 08:55:25 | [diff] [blame] | 26 | chrome_browser_sharing::MessageType received_message_type() const { |
| 27 | return received_message_type_; |
| 28 | } |
| 29 | |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 30 | const std::string& received_message_id() const { |
| 31 | return received_message_id_; |
| 32 | } |
| 33 | |
| 34 | const chrome_browser_sharing::ResponseMessage* received_response() const { |
| 35 | return received_response_.get(); |
| 36 | } |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 37 | |
| 38 | private: |
Michael van Ouwerkerk | 9b86269 | 2019-10-17 08:55:25 | [diff] [blame] | 39 | chrome_browser_sharing::MessageType received_message_type_; |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 40 | std::string received_message_id_; |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 41 | std::unique_ptr<chrome_browser_sharing::ResponseMessage> received_response_; |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 42 | }; |
| 43 | |
Peter Kasting | c11cb0a | 2019-10-07 11:15:22 | [diff] [blame] | 44 | class AckMessageHandlerTest : public testing::Test { |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 45 | protected: |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 46 | AckMessageHandlerTest() |
| 47 | : ack_message_handler_( |
| 48 | base::BindRepeating(&TestObserver::OnAckReceived, |
| 49 | base::Unretained(&test_observer_))) {} |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 50 | |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 51 | TestObserver test_observer_; |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 52 | AckMessageHandler ack_message_handler_; |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 53 | }; |
| 54 | |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 55 | bool ProtoEquals(const google::protobuf::MessageLite& expected, |
| 56 | const google::protobuf::MessageLite& actual) { |
| 57 | std::string expected_serialized, actual_serialized; |
| 58 | expected.SerializeToString(&expected_serialized); |
| 59 | actual.SerializeToString(&actual_serialized); |
| 60 | return expected_serialized == actual_serialized; |
| 61 | } |
| 62 | |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 63 | } // namespace |
| 64 | |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 65 | TEST_F(AckMessageHandlerTest, OnMessageNoResponse) { |
Peter Kasting | c11cb0a | 2019-10-07 11:15:22 | [diff] [blame] | 66 | constexpr char kTestMessageId[] = "test_message_id"; |
| 67 | |
| 68 | chrome_browser_sharing::SharingMessage sharing_message; |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 69 | sharing_message.mutable_ack_message()->set_original_message_id( |
| 70 | kTestMessageId); |
Michael van Ouwerkerk | 9b86269 | 2019-10-17 08:55:25 | [diff] [blame] | 71 | sharing_message.mutable_ack_message()->set_original_message_type( |
| 72 | chrome_browser_sharing::CLICK_TO_CALL_MESSAGE); |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 73 | |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 74 | base::MockCallback<SharingMessageHandler::DoneCallback> done_callback; |
| 75 | EXPECT_CALL(done_callback, Run(testing::Eq(nullptr))).Times(1); |
| 76 | |
| 77 | ack_message_handler_.OnMessage(std::move(sharing_message), |
| 78 | done_callback.Get()); |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 79 | |
| 80 | EXPECT_EQ(kTestMessageId, test_observer_.received_message_id()); |
Michael van Ouwerkerk | 9b86269 | 2019-10-17 08:55:25 | [diff] [blame] | 81 | EXPECT_EQ(chrome_browser_sharing::CLICK_TO_CALL_MESSAGE, |
| 82 | test_observer_.received_message_type()); |
Alex Chau | e1bc1516 | 2019-10-24 10:55:40 | [diff] [blame] | 83 | EXPECT_FALSE(test_observer_.received_response()); |
| 84 | } |
| 85 | |
| 86 | TEST_F(AckMessageHandlerTest, OnMessageWithResponse) { |
| 87 | constexpr char kTestMessageId[] = "test_message_id"; |
| 88 | |
| 89 | chrome_browser_sharing::SharingMessage sharing_message; |
| 90 | sharing_message.mutable_ack_message()->set_original_message_id( |
| 91 | kTestMessageId); |
| 92 | sharing_message.mutable_ack_message()->set_original_message_type( |
| 93 | chrome_browser_sharing::CLICK_TO_CALL_MESSAGE); |
| 94 | sharing_message.mutable_ack_message()->mutable_response_message(); |
| 95 | |
| 96 | chrome_browser_sharing::ResponseMessage response_message_copy = |
| 97 | sharing_message.ack_message().response_message(); |
| 98 | |
| 99 | base::MockCallback<SharingMessageHandler::DoneCallback> done_callback; |
| 100 | EXPECT_CALL(done_callback, Run(testing::Eq(nullptr))).Times(1); |
| 101 | |
| 102 | ack_message_handler_.OnMessage(std::move(sharing_message), |
| 103 | done_callback.Get()); |
| 104 | |
| 105 | EXPECT_EQ(kTestMessageId, test_observer_.received_message_id()); |
| 106 | EXPECT_EQ(chrome_browser_sharing::CLICK_TO_CALL_MESSAGE, |
| 107 | test_observer_.received_message_type()); |
| 108 | ASSERT_TRUE(test_observer_.received_response()); |
| 109 | EXPECT_TRUE( |
| 110 | ProtoEquals(response_message_copy, *test_observer_.received_response())); |
Alex Chau | 4fded64e | 2019-07-17 16:21:51 | [diff] [blame] | 111 | } |