1 // Copyright 2015 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 #include "remoting/test/app_remoting_connected_client_fixture.h"
7 #include "base/json/json_reader.h"
8 #include "base/logging.h"
9 #include "base/run_loop.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "base/timer/timer.h"
12 #include "base/values.h"
13 #include "remoting/protocol/host_stub.h"
14 #include "remoting/test/app_remoting_connection_helper.h"
15 #include "remoting/test/app_remoting_test_driver_environment.h"
16 #include "remoting/test/remote_application_details.h"
17 #include "remoting/test/test_chromoting_client.h"
21 void SimpleHostMessageHandler(
22 const std::string
& target_message_type
,
23 const std::string
& target_message_data
,
24 const base::Closure
& done_closure
,
25 bool* message_received
,
26 const remoting::protocol::ExtensionMessage
& message
) {
27 if (message
.type() == target_message_type
&&
28 message
.data() == target_message_data
) {
29 *message_received
= true;
38 AppRemotingConnectedClientFixture::AppRemotingConnectedClientFixture()
39 : application_details_(
40 AppRemotingSharedData
->GetDetailsFromAppName(GetParam())),
41 timer_(new base::Timer(true, false)) {
44 AppRemotingConnectedClientFixture::~AppRemotingConnectedClientFixture() {
47 void AppRemotingConnectedClientFixture::SetUp() {
48 connection_helper_
.reset(
49 new AppRemotingConnectionHelper(application_details_
));
50 connection_helper_
->Initialize(make_scoped_ptr(new TestChromotingClient()));
51 if (!connection_helper_
->StartConnection()) {
52 LOG(ERROR
) << "Remote host connection could not be established.";
57 void AppRemotingConnectedClientFixture::TearDown() {
58 connection_helper_
.reset();
61 bool AppRemotingConnectedClientFixture::VerifyResponseForSimpleHostMessage(
62 const std::string
& message_request_title
,
63 const std::string
& message_response_title
,
64 const std::string
& message_payload
,
65 const base::TimeDelta
& max_wait_time
) {
66 DCHECK(thread_checker_
.CalledOnValidThread());
68 bool message_received
= false;
70 DCHECK(!run_loop_
|| !run_loop_
->running());
71 run_loop_
.reset(new base::RunLoop());
73 HostMessageReceivedCallback host_message_received_callback_
=
74 base::Bind(&SimpleHostMessageHandler
, message_response_title
,
75 message_payload
, run_loop_
->QuitClosure(), &message_received
);
76 connection_helper_
->SetHostMessageReceivedCallback(
77 host_message_received_callback_
);
79 protocol::ExtensionMessage message
;
80 message
.set_type(message_request_title
);
81 message
.set_data(message_payload
);
82 connection_helper_
->host_stub()->DeliverClientMessage(message
);
84 DCHECK(!timer_
->IsRunning());
85 timer_
->Start(FROM_HERE
, max_wait_time
, run_loop_
->QuitClosure());
90 return message_received
;
94 } // namespace remoting