summaryrefslogtreecommitdiffstats
path: root/remoting/test/app_remoting_connected_client_fixture.cc
blob: 71ceae6d79944c1179bc79397483fb3aab2c9d69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/test/app_remoting_connected_client_fixture.h"

#include <utility>

#include "base/callback_helpers.h"
#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/run_loop.h"
#include "base/thread_task_runner_handle.h"
#include "base/timer/timer.h"
#include "base/values.h"
#include "remoting/protocol/host_stub.h"
#include "remoting/test/app_remoting_connection_helper.h"
#include "remoting/test/app_remoting_test_driver_environment.h"
#include "remoting/test/remote_application_details.h"
#include "remoting/test/test_chromoting_client.h"

namespace {

void SimpleHostMessageHandler(
    const std::string& target_message_type,
    const std::string& target_message_data,
    const base::Closure& done_closure,
    bool* message_received,
    const remoting::protocol::ExtensionMessage& message) {
  if (message.type() == target_message_type &&
      message.data() == target_message_data) {
    *message_received = true;
    done_closure.Run();
  }
}
}  // namespace

namespace remoting {
namespace test {

AppRemotingConnectedClientFixture::AppRemotingConnectedClientFixture()
    : application_details_(
          AppRemotingSharedData->GetDetailsFromAppName(GetParam())),
      timer_(new base::Timer(true, false)) {}

AppRemotingConnectedClientFixture::~AppRemotingConnectedClientFixture() {}

void AppRemotingConnectedClientFixture::SetUp() {
  connection_helper_.reset(
      new AppRemotingConnectionHelper(application_details_));
  scoped_ptr<TestChromotingClient> test_chromoting_client(
      new TestChromotingClient());

  test_chromoting_client->AddRemoteConnectionObserver(this);

  connection_helper_->Initialize(std::move(test_chromoting_client));
  if (!connection_helper_->StartConnection()) {
    LOG(ERROR) << "Remote host connection could not be established.";
    FAIL();
  }
}

void AppRemotingConnectedClientFixture::TearDown() {
  connection_helper_->test_chromoting_client()->RemoveRemoteConnectionObserver(
      this);
  connection_helper_.reset();
}

void AppRemotingConnectedClientFixture::HostMessageReceived(
    const protocol::ExtensionMessage& message) {
  DCHECK(thread_checker_.CalledOnValidThread());

  if (!host_message_received_callback_.is_null()) {
    host_message_received_callback_.Run(message);
  }
}

bool AppRemotingConnectedClientFixture::VerifyResponseForSimpleHostMessage(
    const std::string& message_request_title,
    const std::string& message_response_title,
    const std::string& message_payload,
    const base::TimeDelta& max_wait_time) {
  DCHECK(thread_checker_.CalledOnValidThread());

  bool message_received = false;

  DCHECK(!run_loop_ || !run_loop_->running());
  run_loop_.reset(new base::RunLoop());

  host_message_received_callback_ =
      base::Bind(&SimpleHostMessageHandler, message_response_title,
                 message_payload, run_loop_->QuitClosure(), &message_received);

  protocol::ExtensionMessage message;
  message.set_type(message_request_title);
  message.set_data(message_payload);
  connection_helper_->host_stub()->DeliverClientMessage(message);

  DCHECK(!timer_->IsRunning());
  timer_->Start(FROM_HERE, max_wait_time, run_loop_->QuitClosure());

  run_loop_->Run();
  timer_->Stop();
  host_message_received_callback_.Reset();

  return message_received;
}

}  // namespace test
}  // namespace remoting