Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / remoting / test / app_remoting_connected_client_fixture.cc
blob3660fb4b2c6a36d76b42f72b412b5ab3ef3148b0
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/callback_helpers.h"
8 #include "base/json/json_reader.h"
9 #include "base/logging.h"
10 #include "base/run_loop.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/timer/timer.h"
13 #include "base/values.h"
14 #include "remoting/protocol/host_stub.h"
15 #include "remoting/test/app_remoting_connection_helper.h"
16 #include "remoting/test/app_remoting_test_driver_environment.h"
17 #include "remoting/test/remote_application_details.h"
18 #include "remoting/test/test_chromoting_client.h"
20 namespace {
22 void SimpleHostMessageHandler(
23 const std::string& target_message_type,
24 const std::string& target_message_data,
25 const base::Closure& done_closure,
26 bool* message_received,
27 const remoting::protocol::ExtensionMessage& message) {
28 if (message.type() == target_message_type &&
29 message.data() == target_message_data) {
30 *message_received = true;
31 done_closure.Run();
34 } // namespace
36 namespace remoting {
37 namespace test {
39 AppRemotingConnectedClientFixture::AppRemotingConnectedClientFixture()
40 : application_details_(
41 AppRemotingSharedData->GetDetailsFromAppName(GetParam())),
42 timer_(new base::Timer(true, false)) {
45 AppRemotingConnectedClientFixture::~AppRemotingConnectedClientFixture() {
48 void AppRemotingConnectedClientFixture::SetUp() {
49 connection_helper_.reset(
50 new AppRemotingConnectionHelper(application_details_));
51 scoped_ptr<TestChromotingClient> test_chromoting_client(
52 new TestChromotingClient());
54 test_chromoting_client->AddRemoteConnectionObserver(this);
56 connection_helper_->Initialize(test_chromoting_client.Pass());
57 if (!connection_helper_->StartConnection()) {
58 LOG(ERROR) << "Remote host connection could not be established.";
59 FAIL();
63 void AppRemotingConnectedClientFixture::TearDown() {
64 connection_helper_->test_chromoting_client()->RemoveRemoteConnectionObserver(
65 this);
66 connection_helper_.reset();
69 void AppRemotingConnectedClientFixture::HostMessageReceived(
70 const protocol::ExtensionMessage& message) {
71 DCHECK(thread_checker_.CalledOnValidThread());
73 if (!host_message_received_callback_.is_null()) {
74 host_message_received_callback_.Run(message);
78 bool AppRemotingConnectedClientFixture::VerifyResponseForSimpleHostMessage(
79 const std::string& message_request_title,
80 const std::string& message_response_title,
81 const std::string& message_payload,
82 const base::TimeDelta& max_wait_time) {
83 DCHECK(thread_checker_.CalledOnValidThread());
85 bool message_received = false;
87 DCHECK(!run_loop_ || !run_loop_->running());
88 run_loop_.reset(new base::RunLoop());
90 host_message_received_callback_ =
91 base::Bind(&SimpleHostMessageHandler, message_response_title,
92 message_payload, run_loop_->QuitClosure(), &message_received);
94 protocol::ExtensionMessage message;
95 message.set_type(message_request_title);
96 message.set_data(message_payload);
97 connection_helper_->host_stub()->DeliverClientMessage(message);
99 DCHECK(!timer_->IsRunning());
100 timer_->Start(FROM_HERE, max_wait_time, run_loop_->QuitClosure());
102 run_loop_->Run();
103 timer_->Stop();
104 host_message_received_callback_.Reset();
106 return message_received;
109 } // namespace test
110 } // namespace remoting