Roll src/third_party/WebKit a3b4a2e:7441784 (svn 202551:202552)
[chromium-blink-merge.git] / extensions / browser / api / cast_channel / cast_test_util.h
blob8d01087d8e92af9d812e70fed6541d6785fa14ae
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_
8 #include <string>
10 #include "base/message_loop/message_loop.h"
11 #include "extensions/browser/api/cast_channel/cast_socket.h"
12 #include "extensions/browser/api/cast_channel/cast_transport.h"
13 #include "extensions/common/api/cast_channel/cast_channel.pb.h"
14 #include "net/base/ip_endpoint.h"
15 #include "testing/gmock/include/gmock/gmock.h"
17 namespace extensions {
18 namespace api {
19 namespace cast_channel {
21 extern const char kTestExtensionId[];
23 class MockCastTransport : public extensions::api::cast_channel::CastTransport {
24 public:
25 MockCastTransport();
26 ~MockCastTransport() override;
28 void SetReadDelegate(scoped_ptr<CastTransport::Delegate> delegate) override;
30 MOCK_METHOD2(SendMessage,
31 void(const extensions::api::cast_channel::CastMessage& message,
32 const net::CompletionCallback& callback));
34 MOCK_METHOD0(Start, void(void));
36 // Gets the read delegate that is currently active for this transport.
37 CastTransport::Delegate* current_delegate() const;
39 private:
40 scoped_ptr<CastTransport::Delegate> delegate_;
42 DISALLOW_COPY_AND_ASSIGN(MockCastTransport);
45 class MockCastTransportDelegate : public CastTransport::Delegate {
46 public:
47 MockCastTransportDelegate();
48 ~MockCastTransportDelegate() override;
50 MOCK_METHOD1(OnError, void(ChannelError error));
51 MOCK_METHOD1(OnMessage, void(const CastMessage& message));
52 MOCK_METHOD0(Start, void(void));
54 private:
55 DISALLOW_COPY_AND_ASSIGN(MockCastTransportDelegate);
58 class MockCastSocket : public CastSocket {
59 public:
60 MockCastSocket();
61 ~MockCastSocket() override;
63 // Mockable version of Connect. Accepts a bare pointer to a mock object.
64 // (GMock won't compile with scoped_ptr method parameters.)
65 MOCK_METHOD2(ConnectRawPtr,
66 void(CastTransport::Delegate* delegate,
67 base::Callback<void(ChannelError)> callback));
69 // Proxy for ConnectRawPtr. Unpacks scoped_ptr into a GMock-friendly bare
70 // ptr.
71 void Connect(scoped_ptr<CastTransport::Delegate> delegate,
72 base::Callback<void(ChannelError)> callback) override {
73 delegate_ = delegate.Pass();
74 ConnectRawPtr(delegate_.get(), callback);
77 MOCK_METHOD1(Close, void(const net::CompletionCallback& callback));
78 MOCK_CONST_METHOD0(ip_endpoint, const net::IPEndPoint&());
79 MOCK_CONST_METHOD0(id, int());
80 MOCK_METHOD1(set_id, void(int id));
81 MOCK_CONST_METHOD0(channel_auth, ChannelAuthType());
82 MOCK_CONST_METHOD0(ready_state, ReadyState());
83 MOCK_CONST_METHOD0(error_state, ChannelError());
84 MOCK_CONST_METHOD0(keep_alive, bool(void));
85 MOCK_METHOD1(SetErrorState, void(ChannelError error_state));
87 CastTransport* transport() const override { return mock_transport_.get(); }
89 MockCastTransport* mock_transport() const { return mock_transport_.get(); }
91 private:
92 scoped_ptr<MockCastTransport> mock_transport_;
93 scoped_ptr<CastTransport::Delegate> delegate_;
95 DISALLOW_COPY_AND_ASSIGN(MockCastSocket);
98 // Creates the IPEndpoint 192.168.1.1.
99 net::IPEndPoint CreateIPEndPointForTest();
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;
112 ACTION_TEMPLATE(PostCompletionCallbackTask,
113 HAS_1_TEMPLATE_PARAMS(int, cb_idx),
114 AND_1_VALUE_PARAMS(rv)) {
115 base::MessageLoop::current()->PostTask(
116 FROM_HERE, base::Bind(testing::get<cb_idx>(args), rv));
119 } // namespace cast_channel
120 } // namespace api
121 } // namespace extensions
123 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TEST_UTIL_H_