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 #ifndef CONTENT_BROWSER_PRESENTATION_PRESENTATION_TEST_UTILS_H_
6 #define CONTENT_BROWSER_PRESENTATION_PRESENTATION_TEST_UTILS_H_
7 
8 #include "content/browser/presentation/presentation_service_impl.h"
9 #include "content/public/browser/presentation_request.h"
10 #include "content/public/browser/presentation_service_delegate.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 
13 using blink::mojom::PresentationConnection;
14 using blink::mojom::PresentationConnectionCloseReason;
15 using blink::mojom::PresentationConnectionMessagePtr;
16 using blink::mojom::PresentationConnectionResult;
17 using blink::mojom::PresentationConnectionResultPtr;
18 using blink::mojom::PresentationConnectionState;
19 using blink::mojom::PresentationController;
20 using blink::mojom::PresentationError;
21 using blink::mojom::PresentationErrorPtr;
22 using blink::mojom::PresentationErrorType;
23 using blink::mojom::PresentationInfo;
24 using blink::mojom::PresentationInfoPtr;
25 using blink::mojom::ScreenAvailability;
26 using ::testing::_;
27 using ::testing::Eq;
28 using ::testing::Mock;
29 using ::testing::Return;
30 using ::testing::SaveArg;
31 using NewPresentationCallback =
32     content::PresentationServiceImpl::NewPresentationCallback;
33 
34 namespace content {
35 
36 namespace {
37 
38 MATCHER_P(PresentationUrlsAre, expected_urls, "") {
39   return arg.presentation_urls == expected_urls;
40 }
41 
42 // Matches blink::mojom::PresentationInfo.
43 MATCHER_P(InfoEquals, expected, "") {
44   return expected.url == arg.url && expected.id == arg.id;
45 }
46 
47 // Matches blink::mojom::PresentationInfoPtr.
48 MATCHER_P(InfoPtrEquals, expected, "") {
49   return expected.url == arg->url && expected.id == arg->id;
50 }
51 
ACTION_TEMPLATE(SaveArgByMove,HAS_1_TEMPLATE_PARAMS (int,k),AND_1_VALUE_PARAMS (pointer))52 ACTION_TEMPLATE(SaveArgByMove,
53                 HAS_1_TEMPLATE_PARAMS(int, k),
54                 AND_1_VALUE_PARAMS(pointer)) {
55   *pointer = std::move(::testing::get<k>(args));
56 }
57 
58 }  // namespace
59 
60 class MockPresentationServiceDelegate
61     : public ControllerPresentationServiceDelegate {
62  public:
63   MockPresentationServiceDelegate();
64   ~MockPresentationServiceDelegate() override;
65 
66   MOCK_METHOD3(AddObserver,
67                void(int render_process_id,
68                     int render_frame_id,
69                     PresentationServiceDelegate::Observer* observer));
70   MOCK_METHOD2(RemoveObserver,
71                void(int render_process_id, int render_frame_id));
72 
73   bool AddScreenAvailabilityListener(
74       int render_process_id,
75       int routing_id,
76       PresentationScreenAvailabilityListener* listener) override;
77 
78   MOCK_METHOD0(AddScreenAvailabilityListener, bool());
79 
80   MOCK_METHOD3(RemoveScreenAvailabilityListener,
81                void(int render_process_id,
82                     int routing_id,
83                     PresentationScreenAvailabilityListener* listener));
84   MOCK_METHOD2(Reset, void(int render_process_id, int routing_id));
85   MOCK_METHOD2(SetDefaultPresentationUrls,
86                void(const PresentationRequest& request,
87                     DefaultPresentationConnectionCallback callback));
88   MOCK_METHOD3(StartPresentation,
89                void(const PresentationRequest& request,
90                     PresentationConnectionCallback success_cb,
91                     PresentationConnectionErrorCallback error_cb));
92   MOCK_METHOD4(ReconnectPresentation,
93                void(const PresentationRequest& request,
94                     const std::string& presentation_id,
95                     PresentationConnectionCallback success_cb,
96                     PresentationConnectionErrorCallback error_cb));
97   MOCK_METHOD3(CloseConnection,
98                void(int render_process_id,
99                     int render_frame_id,
100                     const std::string& presentation_id));
101   MOCK_METHOD3(Terminate,
102                void(int render_process_id,
103                     int render_frame_id,
104                     const std::string& presentation_id));
105   MOCK_METHOD3(GetFlingingController,
106                std::unique_ptr<media::FlingingController>(
107                    int render_process_id,
108                    int render_frame_id,
109                    const std::string& presentation_id));
110   MOCK_METHOD5(SendMessage,
111                void(int render_process_id,
112                     int render_frame_id,
113                     const PresentationInfo& presentation_info,
114                     PresentationConnectionMessagePtr message,
115                     const SendMessageCallback& send_message_cb));
116   MOCK_METHOD4(
117       ListenForConnectionStateChange,
118       void(int render_process_id,
119            int render_frame_id,
120            const PresentationInfo& connection,
121            const PresentationConnectionStateChangedCallback& state_changed_cb));
122 
set_screen_availability_listening_supported(bool value)123   void set_screen_availability_listening_supported(bool value) {
124     screen_availability_listening_supported_ = value;
125   }
126 
127  private:
128   bool screen_availability_listening_supported_ = true;
129 };
130 
131 class MockPresentationReceiver : public blink::mojom::PresentationReceiver {
132  public:
133   MockPresentationReceiver();
134   ~MockPresentationReceiver() override;
135 
136   MOCK_METHOD3(
137       OnReceiverConnectionAvailable,
138       void(PresentationInfoPtr info,
139            mojo::PendingRemote<PresentationConnection> controller_connection,
140            mojo::PendingReceiver<PresentationConnection>
141                presentation_receiver_receiver));
142 };
143 
144 class MockReceiverPresentationServiceDelegate
145     : public ReceiverPresentationServiceDelegate {
146  public:
147   MockReceiverPresentationServiceDelegate();
148   ~MockReceiverPresentationServiceDelegate() override;
149   MOCK_METHOD3(AddObserver,
150                void(int render_process_id,
151                     int render_frame_id,
152                     PresentationServiceDelegate::Observer* observer));
153   MOCK_METHOD2(RemoveObserver,
154                void(int render_process_id, int render_frame_id));
155   MOCK_METHOD2(Reset, void(int render_process_id, int routing_id));
156   MOCK_METHOD1(RegisterReceiverConnectionAvailableCallback,
157                void(const ReceiverConnectionAvailableCallback&));
158 };
159 
160 class MockPresentationConnection : public PresentationConnection {
161  public:
162   MockPresentationConnection();
163   ~MockPresentationConnection() override;
164 
165   MOCK_METHOD1(OnMessage, void(PresentationConnectionMessagePtr message));
166   MOCK_METHOD1(DidChangeState, void(PresentationConnectionState state));
167   MOCK_METHOD1(DidClose, void(blink::mojom::PresentationConnectionCloseReason));
168 };
169 
170 class MockPresentationController : public blink::mojom::PresentationController {
171  public:
172   MockPresentationController();
173   ~MockPresentationController() override;
174   MOCK_METHOD2(OnScreenAvailabilityUpdated,
175                void(const GURL& url, ScreenAvailability availability));
176   MOCK_METHOD2(OnConnectionStateChanged,
177                void(PresentationInfoPtr connection,
178                     PresentationConnectionState new_state));
179   MOCK_METHOD3(OnConnectionClosed,
180                void(PresentationInfoPtr connection,
181                     PresentationConnectionCloseReason reason,
182                     const std::string& message));
183   MOCK_METHOD2(
184       OnConnectionMessagesReceived,
185       void(const PresentationInfo& presentation_info,
186            const std::vector<PresentationConnectionMessagePtr>& messages));
187   MOCK_METHOD1(OnDefaultPresentationStarted,
188                void(PresentationConnectionResultPtr result));
189 };
190 
191 }  // namespace content
192 
193 #endif  // CONTENT_BROWSER_PRESENTATION_PRESENTATION_TEST_UTILS_H_
194