blob: d77fc1a7187af780c836470f495f4655fd3cc96d [file] [log] [blame]
tonychun684d14142015-07-31 00:05:491// 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.
4
5#include "remoting/test/connection_time_observer.h"
6
7#include <utility>
8
avi5a080f012015-12-22 23:15:439#include "base/macros.h"
tonychun684d14142015-07-31 00:05:4910#include "base/message_loop/message_loop.h"
11#include "base/run_loop.h"
12#include "base/time/time.h"
13#include "base/timer/timer.h"
14#include "testing/gtest/include/gtest/gtest.h"
15
16namespace remoting {
17namespace test {
18
19class ConnectionTimeObserverTest : public ::testing::Test {
20 public:
21 ConnectionTimeObserverTest();
22 ~ConnectionTimeObserverTest() override;
23
24 protected:
25 // Test interface.
26 void SetUp() override;
27
28 // A map of fake times to calculate TimeDelta between two states.
29 std::map<protocol::ConnectionToHost::State, base::TimeTicks> test_map_;
30
31 // Observes and saves the times when the chromoting connection state changes.
dcheng0765c492016-04-06 22:41:5332 std::unique_ptr<ConnectionTimeObserver> connection_time_observer_;
tonychun684d14142015-07-31 00:05:4933
34 private:
35 DISALLOW_COPY_AND_ASSIGN(ConnectionTimeObserverTest);
36};
37
38ConnectionTimeObserverTest::ConnectionTimeObserverTest() {
39}
40
41ConnectionTimeObserverTest::~ConnectionTimeObserverTest() {
42}
43
44void ConnectionTimeObserverTest::SetUp() {
45 connection_time_observer_.reset(new ConnectionTimeObserver());
46
47 base::TimeTicks now = base::TimeTicks::Now();
48 test_map_.insert(std::make_pair(
49 protocol::ConnectionToHost::State::INITIALIZING,
50 now + base::TimeDelta::FromMilliseconds(10)));
51 test_map_.insert(std::make_pair(
52 protocol::ConnectionToHost::State::CONNECTING,
53 now + base::TimeDelta::FromMilliseconds(20)));
54 test_map_.insert(std::make_pair(
55 protocol::ConnectionToHost::State::AUTHENTICATED,
56 now + base::TimeDelta::FromMilliseconds(30)));
57 test_map_.insert(std::make_pair(
58 protocol::ConnectionToHost::State::CONNECTED,
59 now + base::TimeDelta::FromMilliseconds(40)));
60 test_map_.insert(std::make_pair(
61 protocol::ConnectionToHost::State::CLOSED,
62 now + base::TimeDelta::FromMilliseconds(50)));
63
64 connection_time_observer_->SetTransitionTimesMapForTest(test_map_);
65}
66
67TEST_F(ConnectionTimeObserverTest, ChromotingConnectionSuccess) {
68 EXPECT_EQ(connection_time_observer_->GetStateTransitionTime(
69 protocol::ConnectionToHost::State::INITIALIZING,
70 protocol::ConnectionToHost::State::CONNECTING).InMilliseconds(), 10);
71 EXPECT_EQ(connection_time_observer_->GetStateTransitionTime(
72 protocol::ConnectionToHost::State::CONNECTING,
73 protocol::ConnectionToHost::State::AUTHENTICATED).InMilliseconds(), 10);
74 EXPECT_EQ(connection_time_observer_->GetStateTransitionTime(
75 protocol::ConnectionToHost::State::AUTHENTICATED,
76 protocol::ConnectionToHost::State::CONNECTED).InMilliseconds(), 10);
77 EXPECT_EQ(connection_time_observer_->GetStateTransitionTime(
78 protocol::ConnectionToHost::State::CONNECTED,
79 protocol::ConnectionToHost::State::CLOSED).InMilliseconds(), 10);
80}
81
82TEST_F(ConnectionTimeObserverTest, StartStateNotFound) {
83 EXPECT_TRUE(connection_time_observer_->GetStateTransitionTime(
84 protocol::ConnectionToHost::State::FAILED,
85 protocol::ConnectionToHost::State::CLOSED).is_max());
86}
87
88TEST_F(ConnectionTimeObserverTest, EndStateNotFound) {
89 EXPECT_TRUE(connection_time_observer_->GetStateTransitionTime(
90 protocol::ConnectionToHost::State::INITIALIZING,
91 protocol::ConnectionToHost::State::FAILED).is_max());
92}
93
94TEST_F(ConnectionTimeObserverTest, NegativeTransitionDelay) {
95 EXPECT_TRUE(connection_time_observer_->GetStateTransitionTime(
96 protocol::ConnectionToHost::State::CLOSED,
97 protocol::ConnectionToHost::State::INITIALIZING).is_max());
98}
99
100TEST_F(ConnectionTimeObserverTest, TestOnConnectionStateChangedWithTestMap) {
101 // Should fail to find FAILED.
102 EXPECT_TRUE(connection_time_observer_->GetStateTransitionTime(
103 protocol::ConnectionToHost::State::INITIALIZING,
104 protocol::ConnectionToHost::State::FAILED).is_max());
105
106 // Registers the time at which FAILED state occurred into the map.
107 connection_time_observer_->ConnectionStateChanged(
108 protocol::ConnectionToHost::State::FAILED,
109 protocol::ErrorCode::PEER_IS_OFFLINE);
110
111 EXPECT_FALSE(connection_time_observer_->GetStateTransitionTime(
112 protocol::ConnectionToHost::State::INITIALIZING,
113 protocol::ConnectionToHost::State::FAILED).is_zero());
114}
115
116TEST_F(ConnectionTimeObserverTest, TestOnConnectionStateChangedWithoutTestMap) {
117 connection_time_observer_->SetTransitionTimesMapForTest(
118 std::map<protocol::ConnectionToHost::State, base::TimeTicks>());
119
120 base::MessageLoopForIO message_loop;
121 base::RunLoop run_loop;
122
123 // Should fail to find INITIALIZING in an empty map.
124 EXPECT_TRUE(connection_time_observer_->GetStateTransitionTime(
125 protocol::ConnectionToHost::State::INITIALIZING,
126 protocol::ConnectionToHost::State::FAILED).is_max());
127
128 connection_time_observer_->ConnectionStateChanged(
129 protocol::ConnectionToHost::State::INITIALIZING,
130 protocol::ErrorCode::OK);
131 connection_time_observer_->ConnectionStateChanged(
132 protocol::ConnectionToHost::State::CONNECTING,
133 protocol::ErrorCode::OK);
134 connection_time_observer_->ConnectionStateChanged(
135 protocol::ConnectionToHost::State::AUTHENTICATED,
136 protocol::ErrorCode::OK);
137
138 // Wait for 10 milliseconds for CONNECTED state.
139 // Note: This test can only guarantee a positive TimeDelta between a previous
140 // state and the CONNECTED state. Prior states have non-deterministic times
141 // between each other.
142 base::Timer timer(true, false);
143 timer.Start(FROM_HERE,
144 base::TimeDelta::FromMilliseconds(10),
145 run_loop.QuitClosure());
146 run_loop.Run();
147
148 connection_time_observer_->ConnectionStateChanged(
149 protocol::ConnectionToHost::State::CONNECTED,
150 protocol::ErrorCode::OK);
151
152 // Verify that the time waited is positive and at least 10 milliseconds.
153 EXPECT_GE(connection_time_observer_->GetStateTransitionTime(
154 protocol::ConnectionToHost::State::AUTHENTICATED,
155 protocol::ConnectionToHost::State::CONNECTED).InMilliseconds(), 10);
156}
157
158TEST_F(ConnectionTimeObserverTest, TestOnConnectionStateDataDoesNotChange) {
159 base::TimeDelta first_time_delta =
160 connection_time_observer_->GetStateTransitionTime(
161 protocol::ConnectionToHost::State::INITIALIZING,
162 protocol::ConnectionToHost::State::CONNECTED);
163
164 // This check makes sure that the two states are actually found.
165 EXPECT_FALSE(first_time_delta.is_zero());
166
167 // The initial data should not change and should log an error.
168 connection_time_observer_->ConnectionStateChanged(
169 protocol::ConnectionToHost::State::CONNECTED, protocol::ErrorCode::OK);
170
171 EXPECT_EQ(connection_time_observer_->GetStateTransitionTime(
172 protocol::ConnectionToHost::State::INITIALIZING,
173 protocol::ConnectionToHost::State::CONNECTED), first_time_delta);
174}
175
176} // namespace test
177} // namespace remoting