Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / remoting / test / chromoting_test_fixture.cc
blob705995f0d5532dfac598c284964ca61dcab12a67
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/chromoting_test_fixture.h"
7 #include "base/logging.h"
8 #include "base/run_loop.h"
9 #include "base/timer/timer.h"
10 #include "remoting/test/chromoting_test_driver_environment.h"
11 #include "remoting/test/connection_setup_info.h"
12 #include "remoting/test/connection_time_observer.h"
13 #include "remoting/test/host_info.h"
14 #include "remoting/test/test_chromoting_client.h"
16 namespace remoting {
17 namespace test {
19 ChromotingTestFixture::ChromotingTestFixture()
20 : test_chromoting_client_(new TestChromotingClient()) {
23 ChromotingTestFixture::~ChromotingTestFixture() {
26 void ChromotingTestFixture::Disconnect() {
27 test_chromoting_client_->EndConnection();
30 void ChromotingTestFixture::CreateObserver() {
31 if (connection_time_observer_) {
32 DestroyObserver();
35 connection_time_observer_.reset(new ConnectionTimeObserver());
36 test_chromoting_client_->AddRemoteConnectionObserver(
37 connection_time_observer_.get());
40 void ChromotingTestFixture::DestroyObserver() {
41 if (!connection_time_observer_) {
42 return;
45 test_chromoting_client_->RemoveRemoteConnectionObserver(
46 connection_time_observer_.get());
47 connection_time_observer_.reset();
50 bool ChromotingTestFixture::ConnectToHost(
51 const base::TimeDelta& max_time_to_connect) {
52 DCHECK(thread_checker_.CalledOnValidThread());
54 if (!g_chromoting_shared_data->host_info().IsReadyForConnection()) {
55 LOG(ERROR) << "Can't connect to " << g_chromoting_shared_data->host_name();
56 return false;
59 // Host is online and ready, initiate a remote session.
60 base::RunLoop run_loop;
62 CreateObserver();
64 base::Timer timer(true, false);
65 timer.Start(FROM_HERE, max_time_to_connect, run_loop.QuitClosure());
67 connection_time_observer_->ConnectionStateChanged(
68 protocol::ConnectionToHost::State::INITIALIZING,
69 protocol::ErrorCode::OK);
70 test_chromoting_client_->StartConnection(
71 g_chromoting_shared_data->host_info().GenerateConnectionSetupInfo(
72 g_chromoting_shared_data->access_token(),
73 g_chromoting_shared_data->user_name(),
74 g_chromoting_shared_data->pin()));
75 run_loop.Run();
77 // Note: Under different network conditions, connection time will have
78 // greater variance. |max_time_to_connect| may need to be larger in high
79 // latency network environments.
80 if (connection_time_observer_->GetStateTransitionTime(
81 protocol::ConnectionToHost::State::INITIALIZING,
82 protocol::ConnectionToHost::State::CONNECTED).is_max()) {
83 LOG(ERROR) << "Chromoting client did not connect to requested host";
84 return false;
87 return true;
90 void ChromotingTestFixture::TearDown() {
91 // If a chromoting connection is still connected, we want to end the
92 // connection before starting the next test.
93 Disconnect();
94 DestroyObserver();
97 } // namespace test
98 } // namespace remoting