summaryrefslogtreecommitdiffstats
path: root/remoting/test/chromoting_test_fixture.cc
blob: 705995f0d5532dfac598c284964ca61dcab12a67 (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
// 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/chromoting_test_fixture.h"

#include "base/logging.h"
#include "base/run_loop.h"
#include "base/timer/timer.h"
#include "remoting/test/chromoting_test_driver_environment.h"
#include "remoting/test/connection_setup_info.h"
#include "remoting/test/connection_time_observer.h"
#include "remoting/test/host_info.h"
#include "remoting/test/test_chromoting_client.h"

namespace remoting {
namespace test {

ChromotingTestFixture::ChromotingTestFixture()
    : test_chromoting_client_(new TestChromotingClient()) {
}

ChromotingTestFixture::~ChromotingTestFixture() {
}

void ChromotingTestFixture::Disconnect() {
  test_chromoting_client_->EndConnection();
}

void ChromotingTestFixture::CreateObserver() {
  if (connection_time_observer_) {
    DestroyObserver();
  }

  connection_time_observer_.reset(new ConnectionTimeObserver());
  test_chromoting_client_->AddRemoteConnectionObserver(
      connection_time_observer_.get());
}

void ChromotingTestFixture::DestroyObserver() {
  if (!connection_time_observer_) {
    return;
  }

  test_chromoting_client_->RemoveRemoteConnectionObserver(
      connection_time_observer_.get());
  connection_time_observer_.reset();
}

bool ChromotingTestFixture::ConnectToHost(
    const base::TimeDelta& max_time_to_connect) {
  DCHECK(thread_checker_.CalledOnValidThread());

  if (!g_chromoting_shared_data->host_info().IsReadyForConnection()) {
    LOG(ERROR) << "Can't connect to " << g_chromoting_shared_data->host_name();
    return false;
  }

  // Host is online and ready, initiate a remote session.
  base::RunLoop run_loop;

  CreateObserver();

  base::Timer timer(true, false);
  timer.Start(FROM_HERE, max_time_to_connect, run_loop.QuitClosure());

  connection_time_observer_->ConnectionStateChanged(
      protocol::ConnectionToHost::State::INITIALIZING,
      protocol::ErrorCode::OK);
  test_chromoting_client_->StartConnection(
      g_chromoting_shared_data->host_info().GenerateConnectionSetupInfo(
          g_chromoting_shared_data->access_token(),
          g_chromoting_shared_data->user_name(),
          g_chromoting_shared_data->pin()));
  run_loop.Run();

  // Note: Under different network conditions, connection time will have
  // greater variance. |max_time_to_connect| may need to be larger in high
  // latency network environments.
  if (connection_time_observer_->GetStateTransitionTime(
      protocol::ConnectionToHost::State::INITIALIZING,
      protocol::ConnectionToHost::State::CONNECTED).is_max()) {
    LOG(ERROR) << "Chromoting client did not connect to requested host";
    return false;
  }

  return true;
}

void ChromotingTestFixture::TearDown() {
  // If a chromoting connection is still connected, we want to end the
  // connection before starting the next test.
  Disconnect();
  DestroyObserver();
}

}  // namespace test
}  // namespace remoting