blob: 3b4f3f9bd9c7fa49669a81aa57087428f97fa292 [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
Chris Watkins6fe52aa2017-11-28 03:24:0538ConnectionTimeObserverTest::ConnectionTimeObserverTest() = default;
tonychun684d14142015-07-31 00:05:4939
Chris Watkins6fe52aa2017-11-28 03:24:0540ConnectionTimeObserverTest::~ConnectionTimeObserverTest() = default;
tonychun684d14142015-07-31 00:05:4941
42void ConnectionTimeObserverTest::SetUp() {
43 connection_time_observer_.reset(new ConnectionTimeObserver());
44
45 base::TimeTicks now = base::TimeTicks::Now();
46 test_map_.insert(std::make_pair(
47 protocol::ConnectionToHost::State::INITIALIZING,
48 now + base::TimeDelta::FromMilliseconds(10)));
49 test_map_.insert(std::make_pair(
50 protocol::ConnectionToHost::State::CONNECTING,
51 now + base::TimeDelta::FromMilliseconds(20)));
52 test_map_.insert(std::make_pair(
53 protocol::ConnectionToHost::State::AUTHENTICATED,
54 now + base::TimeDelta::FromMilliseconds(30)));
55 test_map_.insert(std::make_pair(
56 protocol::ConnectionToHost::State::CONNECTED,
57 now + base::TimeDelta::FromMilliseconds(40)));
58 test_map_.insert(std::make_pair(
59 protocol::ConnectionToHost::State::CLOSED,
60 now + base::TimeDelta::FromMilliseconds(50)));
61
62 connection_time_observer_->SetTransitionTimesMapForTest(test_map_);
63}
64
65TEST_F(ConnectionTimeObserverTest, ChromotingConnectionSuccess) {
66 EXPECT_EQ(connection_time_observer_->GetStateTransitionTime(
67 protocol::ConnectionToHost::State::INITIALIZING,
68 protocol::ConnectionToHost::State::CONNECTING).InMilliseconds(), 10);
69 EXPECT_EQ(connection_time_observer_->GetStateTransitionTime(
70 protocol::ConnectionToHost::State::CONNECTING,
71 protocol::ConnectionToHost::State::AUTHENTICATED).InMilliseconds(), 10);
72 EXPECT_EQ(connection_time_observer_->GetStateTransitionTime(
73 protocol::ConnectionToHost::State::AUTHENTICATED,
74 protocol::ConnectionToHost::State::CONNECTED).InMilliseconds(), 10);
75 EXPECT_EQ(connection_time_observer_->GetStateTransitionTime(
76 protocol::ConnectionToHost::State::CONNECTED,
77 protocol::ConnectionToHost::State::CLOSED).InMilliseconds(), 10);
78}
79
80TEST_F(ConnectionTimeObserverTest, StartStateNotFound) {
81 EXPECT_TRUE(connection_time_observer_->GetStateTransitionTime(
82 protocol::ConnectionToHost::State::FAILED,
83 protocol::ConnectionToHost::State::CLOSED).is_max());
84}
85
86TEST_F(ConnectionTimeObserverTest, EndStateNotFound) {
87 EXPECT_TRUE(connection_time_observer_->GetStateTransitionTime(
88 protocol::ConnectionToHost::State::INITIALIZING,
89 protocol::ConnectionToHost::State::FAILED).is_max());
90}
91
92TEST_F(ConnectionTimeObserverTest, NegativeTransitionDelay) {
93 EXPECT_TRUE(connection_time_observer_->GetStateTransitionTime(
94 protocol::ConnectionToHost::State::CLOSED,
95 protocol::ConnectionToHost::State::INITIALIZING).is_max());
96}
97
98TEST_F(ConnectionTimeObserverTest, TestOnConnectionStateChangedWithTestMap) {
99 // Should fail to find FAILED.
100 EXPECT_TRUE(connection_time_observer_->GetStateTransitionTime(
101 protocol::ConnectionToHost::State::INITIALIZING,
102 protocol::ConnectionToHost::State::FAILED).is_max());
103
104 // Registers the time at which FAILED state occurred into the map.
105 connection_time_observer_->ConnectionStateChanged(
106 protocol::ConnectionToHost::State::FAILED,
107 protocol::ErrorCode::PEER_IS_OFFLINE);
108
109 EXPECT_FALSE(connection_time_observer_->GetStateTransitionTime(
110 protocol::ConnectionToHost::State::INITIALIZING,
111 protocol::ConnectionToHost::State::FAILED).is_zero());
112}
113
114TEST_F(ConnectionTimeObserverTest, TestOnConnectionStateChangedWithoutTestMap) {
115 connection_time_observer_->SetTransitionTimesMapForTest(
116 std::map<protocol::ConnectionToHost::State, base::TimeTicks>());
117
118 base::MessageLoopForIO message_loop;
119 base::RunLoop run_loop;
120
121 // Should fail to find INITIALIZING in an empty map.
122 EXPECT_TRUE(connection_time_observer_->GetStateTransitionTime(
123 protocol::ConnectionToHost::State::INITIALIZING,
124 protocol::ConnectionToHost::State::FAILED).is_max());
125
126 connection_time_observer_->ConnectionStateChanged(
127 protocol::ConnectionToHost::State::INITIALIZING,
128 protocol::ErrorCode::OK);
129 connection_time_observer_->ConnectionStateChanged(
130 protocol::ConnectionToHost::State::CONNECTING,
131 protocol::ErrorCode::OK);
132 connection_time_observer_->ConnectionStateChanged(
133 protocol::ConnectionToHost::State::AUTHENTICATED,
134 protocol::ErrorCode::OK);
135
136 // Wait for 10 milliseconds for CONNECTED state.
137 // Note: This test can only guarantee a positive TimeDelta between a previous
138 // state and the CONNECTED state. Prior states have non-deterministic times
139 // between each other.
140 base::Timer timer(true, false);
141 timer.Start(FROM_HERE,
142 base::TimeDelta::FromMilliseconds(10),
143 run_loop.QuitClosure());
144 run_loop.Run();
145
146 connection_time_observer_->ConnectionStateChanged(
147 protocol::ConnectionToHost::State::CONNECTED,
148 protocol::ErrorCode::OK);
149
150 // Verify that the time waited is positive and at least 10 milliseconds.
151 EXPECT_GE(connection_time_observer_->GetStateTransitionTime(
152 protocol::ConnectionToHost::State::AUTHENTICATED,
153 protocol::ConnectionToHost::State::CONNECTED).InMilliseconds(), 10);
154}
155
156TEST_F(ConnectionTimeObserverTest, TestOnConnectionStateDataDoesNotChange) {
157 base::TimeDelta first_time_delta =
158 connection_time_observer_->GetStateTransitionTime(
159 protocol::ConnectionToHost::State::INITIALIZING,
160 protocol::ConnectionToHost::State::CONNECTED);
161
162 // This check makes sure that the two states are actually found.
163 EXPECT_FALSE(first_time_delta.is_zero());
164
165 // The initial data should not change and should log an error.
166 connection_time_observer_->ConnectionStateChanged(
167 protocol::ConnectionToHost::State::CONNECTED, protocol::ErrorCode::OK);
168
169 EXPECT_EQ(connection_time_observer_->GetStateTransitionTime(
170 protocol::ConnectionToHost::State::INITIALIZING,
171 protocol::ConnectionToHost::State::CONNECTED), first_time_delta);
172}
173
174} // namespace test
175} // namespace remoting