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.
5 #ifndef EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TEST_UTIL_H_
6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TEST_UTIL_H_
10 #include "extensions/browser/api/cast_channel/cast_socket.h"
11 #include "extensions/browser/api/cast_channel/cast_transport.h"
12 #include "extensions/common/api/cast_channel/cast_channel.pb.h"
13 #include "net/base/ip_endpoint.h"
14 #include "testing/gmock/include/gmock/gmock.h"
16 namespace extensions
{
18 namespace cast_channel
{
20 extern const char kTestExtensionId
[];
22 class MockCastTransport
23 : public extensions::core_api::cast_channel::CastTransport
{
26 ~MockCastTransport() override
;
28 void SetReadDelegate(scoped_ptr
<CastTransport::Delegate
> delegate
) override
;
32 void(const extensions::core_api::cast_channel::CastMessage
& message
,
33 const net::CompletionCallback
& callback
));
35 MOCK_METHOD0(Start
, void(void));
37 // Gets the read delegate that is currently active for this transport.
38 CastTransport::Delegate
* current_delegate() const;
41 scoped_ptr
<CastTransport::Delegate
> delegate_
;
43 DISALLOW_COPY_AND_ASSIGN(MockCastTransport
);
46 class MockCastTransportDelegate
: public CastTransport::Delegate
{
48 MockCastTransportDelegate();
49 ~MockCastTransportDelegate() override
;
51 MOCK_METHOD1(OnError
, void(ChannelError error
));
52 MOCK_METHOD1(OnMessage
, void(const CastMessage
& message
));
53 MOCK_METHOD0(Start
, void(void));
56 DISALLOW_COPY_AND_ASSIGN(MockCastTransportDelegate
);
59 class MockCastSocket
: public CastSocket
{
62 ~MockCastSocket() override
;
64 // Mockable version of Connect. Accepts a bare pointer to a mock object.
65 // (GMock won't compile with scoped_ptr method parameters.)
66 MOCK_METHOD2(ConnectRawPtr
,
67 void(CastTransport::Delegate
* delegate
,
68 base::Callback
<void(ChannelError
)> callback
));
70 // Proxy for ConnectRawPtr. Unpacks scoped_ptr into a GMock-friendly bare
72 void Connect(scoped_ptr
<CastTransport::Delegate
> delegate
,
73 base::Callback
<void(ChannelError
)> callback
) override
{
74 delegate_
= delegate
.Pass();
75 ConnectRawPtr(delegate_
.get(), callback
);
78 MOCK_METHOD1(Close
, void(const net::CompletionCallback
& callback
));
79 MOCK_CONST_METHOD0(ip_endpoint
, const net::IPEndPoint
&());
80 MOCK_CONST_METHOD0(id
, int());
81 MOCK_METHOD1(set_id
, void(int id
));
82 MOCK_CONST_METHOD0(channel_auth
, ChannelAuthType());
83 MOCK_CONST_METHOD0(ready_state
, ReadyState());
84 MOCK_CONST_METHOD0(error_state
, ChannelError());
85 MOCK_CONST_METHOD0(keep_alive
, bool(void));
86 MOCK_METHOD1(SetErrorState
, void(ChannelError error_state
));
88 CastTransport
* transport() const override
{ return mock_transport_
.get(); }
90 MockCastTransport
* mock_transport() const { return mock_transport_
.get(); }
93 scoped_ptr
<MockCastTransport
> mock_transport_
;
94 scoped_ptr
<CastTransport::Delegate
> delegate_
;
96 DISALLOW_COPY_AND_ASSIGN(MockCastSocket
);
99 // Creates the IPEndpoint 192.168.1.1.
100 net::IPEndPoint
CreateIPEndPointForTest();
102 // Checks if two proto messages are the same.
104 // third_party/cacheinvalidation/overrides/google/cacheinvalidation/deps/gmock.h
105 // TODO(kmarshall): promote to a shared testing library.
106 MATCHER_P(EqualsProto
, message
, "") {
107 std::string expected_serialized
, actual_serialized
;
108 message
.SerializeToString(&expected_serialized
);
109 arg
.SerializeToString(&actual_serialized
);
110 return expected_serialized
== actual_serialized
;
113 ACTION_TEMPLATE(RunCompletionCallback
,
114 HAS_1_TEMPLATE_PARAMS(int, cb_idx
),
115 AND_1_VALUE_PARAMS(rv
)) {
116 testing::get
<cb_idx
>(args
).Run(rv
);
119 } // namespace cast_channel
120 } // namespace core_api
121 } // namespace extensions
123 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TEST_UTIL_H_