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