| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 #ifndef COMPONENTS_CAST_CHANNEL_CAST_TEST_UTIL_H_ | |
| 6 #define COMPONENTS_CAST_CHANNEL_CAST_TEST_UTIL_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/macros.h" | |
| 12 #include "base/threading/thread_task_runner_handle.h" | |
| 13 #include "components/cast_channel/cast_socket.h" | |
| 14 #include "components/cast_channel/cast_transport.h" | |
| 15 #include "components/cast_channel/proto/cast_channel.pb.h" | |
| 16 #include "net/base/ip_endpoint.h" | |
| 17 #include "testing/gmock/include/gmock/gmock.h" | |
| 18 | |
| 19 namespace cast_channel { | |
| 20 | |
| 21 class MockCastTransport : public CastTransport { | |
| 22 public: | |
| 23 MockCastTransport(); | |
| 24 ~MockCastTransport() override; | |
| 25 | |
| 26 void SetReadDelegate( | |
| 27 std::unique_ptr<CastTransport::Delegate> delegate) override; | |
| 28 | |
| 29 MOCK_METHOD2(SendMessage, | |
| 30 void(const CastMessage& message, | |
| 31 const net::CompletionCallback& callback)); | |
| 32 | |
| 33 MOCK_METHOD0(Start, void(void)); | |
| 34 | |
| 35 // Gets the read delegate that is currently active for this transport. | |
| 36 CastTransport::Delegate* current_delegate() const; | |
| 37 | |
| 38 private: | |
| 39 std::unique_ptr<CastTransport::Delegate> delegate_; | |
| 40 | |
| 41 DISALLOW_COPY_AND_ASSIGN(MockCastTransport); | |
| 42 }; | |
| 43 | |
| 44 class MockCastTransportDelegate : public CastTransport::Delegate { | |
| 45 public: | |
| 46 MockCastTransportDelegate(); | |
| 47 ~MockCastTransportDelegate() override; | |
| 48 | |
| 49 MOCK_METHOD1(OnError, void(ChannelError error)); | |
| 50 MOCK_METHOD1(OnMessage, void(const CastMessage& message)); | |
| 51 MOCK_METHOD0(Start, void(void)); | |
| 52 | |
| 53 private: | |
| 54 DISALLOW_COPY_AND_ASSIGN(MockCastTransportDelegate); | |
| 55 }; | |
| 56 | |
| 57 class MockCastSocket : public CastSocket { | |
| 58 public: | |
| 59 MockCastSocket(); | |
| 60 ~MockCastSocket() override; | |
| 61 | |
| 62 // Mockable version of Connect. Accepts a bare pointer to a mock object. | |
| 63 // (GMock won't compile with scoped_ptr method parameters.) | |
| 64 MOCK_METHOD2(ConnectRawPtr, | |
| 65 void(CastTransport::Delegate* delegate, | |
| 66 base::Callback<void(ChannelError)> callback)); | |
| 67 | |
| 68 // Proxy for ConnectRawPtr. Unpacks scoped_ptr into a GMock-friendly bare | |
| 69 // ptr. | |
| 70 void Connect(std::unique_ptr<CastTransport::Delegate> delegate, | |
| 71 base::Callback<void(ChannelError)> callback) override { | |
| 72 delegate_ = std::move(delegate); | |
| 73 ConnectRawPtr(delegate_.get(), callback); | |
| 74 } | |
| 75 | |
| 76 MOCK_METHOD1(Close, void(const net::CompletionCallback& callback)); | |
| 77 MOCK_CONST_METHOD0(ip_endpoint, const net::IPEndPoint&()); | |
| 78 MOCK_CONST_METHOD0(id, int()); | |
| 79 MOCK_METHOD1(set_id, void(int id)); | |
| 80 MOCK_CONST_METHOD0(channel_auth, ChannelAuthType()); | |
| 81 MOCK_CONST_METHOD0(ready_state, ReadyState()); | |
| 82 MOCK_CONST_METHOD0(error_state, ChannelError()); | |
| 83 MOCK_CONST_METHOD0(keep_alive, bool(void)); | |
| 84 MOCK_CONST_METHOD0(audio_only, bool(void)); | |
| 85 MOCK_METHOD1(SetErrorState, void(ChannelError error_state)); | |
| 86 | |
| 87 CastTransport* transport() const override { return mock_transport_.get(); } | |
| 88 | |
| 89 MockCastTransport* mock_transport() const { return mock_transport_.get(); } | |
| 90 | |
| 91 private: | |
| 92 std::unique_ptr<MockCastTransport> mock_transport_; | |
| 93 std::unique_ptr<CastTransport::Delegate> delegate_; | |
| 94 | |
| 95 DISALLOW_COPY_AND_ASSIGN(MockCastSocket); | |
| 96 }; | |
| 97 | |
| 98 // Creates the IPEndpoint 192.168.1.1. | |
| 99 net::IPEndPoint CreateIPEndPointForTest(); | |
| 100 | |
| 101 // Checks if two proto messages are the same. | |
| 102 // From | |
| 103 // third_party/cacheinvalidation/overrides/google/cacheinvalidation/deps/gmock.h | |
| 104 // TODO(kmarshall): promote to a shared testing library. | |
| 105 MATCHER_P(EqualsProto, message, "") { | |
| 106 std::string expected_serialized, actual_serialized; | |
| 107 message.SerializeToString(&expected_serialized); | |
| 108 arg.SerializeToString(&actual_serialized); | |
| 109 return expected_serialized == actual_serialized; | |
| 110 } | |
| 111 | |
| 112 ACTION_TEMPLATE(PostCompletionCallbackTask, | |
| 113 HAS_1_TEMPLATE_PARAMS(int, cb_idx), | |
| 114 AND_1_VALUE_PARAMS(rv)) { | |
| 115 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 116 FROM_HERE, base::Bind(testing::get<cb_idx>(args), rv)); | |
| 117 } | |
| 118 | |
| 119 } // namespace cast_channel | |
| 120 | |
| 121 #endif // COMPONENTS_CAST_CHANNEL_CAST_TEST_UTIL_H_ | |
| OLD | NEW |