OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "remoting/test/connection_time_observer.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "base/run_loop.h" | |
11 #include "base/time/time.h" | |
12 #include "base/timer/timer.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace remoting { | |
16 namespace test { | |
17 | |
18 class ConnectionTimeObserverTest : public ::testing::Test { | |
19 public: | |
20 ConnectionTimeObserverTest(); | |
21 ~ConnectionTimeObserverTest() override; | |
22 | |
23 std::map<protocol::ConnectionToHost::State, base::TimeTicks> test_map_; | |
24 | |
joedow
2015/07/27 16:25:39
These members should have comments and live in the
tonychun
2015/07/27 21:20:42
Done.
| |
25 scoped_ptr<base::Timer> timer_; | |
26 | |
27 protected: | |
28 // Test interface. | |
29 void SetUp() override; | |
30 | |
31 private: | |
32 DISALLOW_COPY_AND_ASSIGN(ConnectionTimeObserverTest); | |
33 }; | |
34 | |
35 ConnectionTimeObserverTest::ConnectionTimeObserverTest() { | |
36 } | |
37 | |
38 ConnectionTimeObserverTest::~ConnectionTimeObserverTest() { | |
39 } | |
40 | |
41 void ConnectionTimeObserverTest::SetUp() { | |
42 timer_.reset(new base::Timer(true, false)); | |
43 | |
44 base::TimeTicks now = base::TimeTicks::Now(); | |
45 test_map_.insert(std::make_pair( | |
46 protocol::ConnectionToHost::State::INITIALIZING, | |
47 now + base::TimeDelta::FromMilliseconds(10))); | |
48 test_map_.insert(std::make_pair( | |
49 protocol::ConnectionToHost::State::CONNECTING, | |
50 now + base::TimeDelta::FromMilliseconds(20))); | |
51 test_map_.insert(std::make_pair( | |
52 protocol::ConnectionToHost::State::AUTHENTICATED, | |
53 now + base::TimeDelta::FromMilliseconds(30))); | |
54 test_map_.insert(std::make_pair( | |
55 protocol::ConnectionToHost::State::CONNECTED, | |
56 now + base::TimeDelta::FromMilliseconds(40))); | |
57 test_map_.insert(std::make_pair( | |
58 protocol::ConnectionToHost::State::CLOSED, | |
59 now + base::TimeDelta::FromMilliseconds(50))); | |
60 } | |
61 | |
62 TEST_F(ConnectionTimeObserverTest, ChromotingConnectionSuccess) { | |
63 ConnectionTimeObserver connection_time_observer(timer_.get()); | |
64 | |
65 connection_time_observer.SetTransitionTimesMapForTest(test_map_); | |
66 | |
67 EXPECT_EQ(connection_time_observer.GetStateTransitionDelayTimeDelta( | |
68 protocol::ConnectionToHost::State::INITIALIZING, | |
69 protocol::ConnectionToHost::State::CONNECTING).InMilliseconds(), 10); | |
70 EXPECT_EQ(connection_time_observer.GetStateTransitionDelayTimeDelta( | |
71 protocol::ConnectionToHost::State::CONNECTING, | |
72 protocol::ConnectionToHost::State::AUTHENTICATED).InMilliseconds(), 10); | |
73 EXPECT_EQ(connection_time_observer.GetStateTransitionDelayTimeDelta( | |
74 protocol::ConnectionToHost::State::AUTHENTICATED, | |
75 protocol::ConnectionToHost::State::CONNECTED).InMilliseconds(), 10); | |
76 EXPECT_EQ(connection_time_observer.GetStateTransitionDelayTimeDelta( | |
77 protocol::ConnectionToHost::State::CONNECTED, | |
78 protocol::ConnectionToHost::State::CLOSED).InMilliseconds(), 10); | |
79 } | |
80 | |
81 TEST_F(ConnectionTimeObserverTest, StartStateNotFound) { | |
82 ConnectionTimeObserver connection_time_observer(timer_.get()); | |
83 | |
84 connection_time_observer.SetTransitionTimesMapForTest(test_map_); | |
85 | |
86 EXPECT_EQ(connection_time_observer.GetStateTransitionDelayTimeDelta( | |
87 protocol::ConnectionToHost::State::FAILED, | |
88 protocol::ConnectionToHost::State::CLOSED), | |
89 base::TimeDelta::Max()); | |
90 } | |
91 | |
92 TEST_F(ConnectionTimeObserverTest, EndStateNotFound) { | |
93 ConnectionTimeObserver connection_time_observer(timer_.get()); | |
94 | |
95 connection_time_observer.SetTransitionTimesMapForTest(test_map_); | |
96 | |
97 EXPECT_EQ(connection_time_observer.GetStateTransitionDelayTimeDelta( | |
98 protocol::ConnectionToHost::State::INITIALIZING, | |
99 protocol::ConnectionToHost::State::FAILED), | |
100 base::TimeDelta::Max()); | |
101 } | |
102 | |
103 TEST_F(ConnectionTimeObserverTest, NegativeTransitionDelay) { | |
104 ConnectionTimeObserver connection_time_observer(timer_.get()); | |
105 | |
106 connection_time_observer.SetTransitionTimesMapForTest(test_map_); | |
107 | |
108 EXPECT_EQ(connection_time_observer.GetStateTransitionDelayTimeDelta( | |
109 protocol::ConnectionToHost::State::CLOSED, | |
110 protocol::ConnectionToHost::State::INITIALIZING), | |
111 base::TimeDelta::Max()); | |
112 } | |
113 | |
114 TEST_F(ConnectionTimeObserverTest, TestOnConnectionStateChangedWithTestMap) { | |
115 ConnectionTimeObserver connection_time_observer(timer_.get()); | |
116 | |
117 connection_time_observer.SetTransitionTimesMapForTest(test_map_); | |
118 | |
119 // Should fail to find FAILED. | |
120 EXPECT_EQ(connection_time_observer.GetStateTransitionDelayTimeDelta( | |
121 protocol::ConnectionToHost::State::INITIALIZING, | |
122 protocol::ConnectionToHost::State::FAILED), | |
123 base::TimeDelta::Max()); | |
124 | |
125 // Registers the time at which FAILED state occurred into the map. | |
126 connection_time_observer.ConnectionStateChanged( | |
127 protocol::ConnectionToHost::State::FAILED, | |
128 protocol::ErrorCode::PEER_IS_OFFLINE); | |
129 | |
130 EXPECT_FALSE(connection_time_observer.GetStateTransitionDelayTimeDelta( | |
131 protocol::ConnectionToHost::State::INITIALIZING, | |
132 protocol::ConnectionToHost::State::FAILED).is_zero()); | |
133 } | |
134 | |
135 TEST_F(ConnectionTimeObserverTest, TestOnConnectionStateChangedWithoutTestMap) { | |
136 ConnectionTimeObserver connection_time_observer(timer_.get()); | |
137 | |
138 base::MessageLoopForIO message_loop; | |
139 base::RunLoop run_loop; | |
140 | |
141 // Should fail to find INITIALIZING in an empty map. | |
142 EXPECT_EQ(connection_time_observer.GetStateTransitionDelayTimeDelta( | |
143 protocol::ConnectionToHost::State::INITIALIZING, | |
144 protocol::ConnectionToHost::State::FAILED), | |
145 base::TimeDelta::Max()); | |
146 | |
147 connection_time_observer.SetInitializingState(); | |
148 connection_time_observer.ConnectionStateChanged( | |
149 protocol::ConnectionToHost::State::CONNECTING, | |
150 protocol::ErrorCode::OK); | |
151 connection_time_observer.ConnectionStateChanged( | |
152 protocol::ConnectionToHost::State::AUTHENTICATED, | |
153 protocol::ErrorCode::OK); | |
154 | |
155 // Wait for 10 milliseconds for CONNECTED state. | |
156 // Note: This test can only guarantee a positive TimeDelta between a previous | |
157 // state and the CONNECTED state. Prior states have non-deterministic times | |
158 // between each other. | |
159 base::Timer timer(true, false); | |
160 timer.Start(FROM_HERE, | |
161 base::TimeDelta::FromMilliseconds(10), | |
162 run_loop.QuitClosure()); | |
163 run_loop.Run(); | |
164 | |
165 connection_time_observer.ConnectionStateChanged( | |
166 protocol::ConnectionToHost::State::CONNECTED, | |
167 protocol::ErrorCode::OK); | |
168 | |
169 EXPECT_FALSE(connection_time_observer.GetStateTransitionDelayTimeDelta( | |
170 protocol::ConnectionToHost::State::INITIALIZING, | |
171 protocol::ConnectionToHost::State::CONNECTED).is_zero()); | |
anandc
2015/07/27 18:22:29
Strictly speaking, this should expect_false is_les
tonychun
2015/07/27 21:20:42
I've made it check specifically for the 10 millise
| |
172 } | |
173 | |
174 TEST_F(ConnectionTimeObserverTest, TestOnConnectionStateDataDoesNotChange) { | |
175 ConnectionTimeObserver connection_time_observer(timer_.get()); | |
176 | |
177 connection_time_observer.SetTransitionTimesMapForTest(test_map_); | |
178 | |
179 base::TimeDelta first_time_delta = | |
180 connection_time_observer.GetStateTransitionDelayTimeDelta( | |
181 protocol::ConnectionToHost::State::INITIALIZING, | |
182 protocol::ConnectionToHost::State::CONNECTED); | |
183 | |
184 // This check makes sure that the two states are actually found. | |
185 EXPECT_FALSE(first_time_delta.is_zero()); | |
186 | |
187 // The initial data should not change and should log an error. | |
188 connection_time_observer.ConnectionStateChanged( | |
189 protocol::ConnectionToHost::State::CONNECTED, protocol::ErrorCode::OK); | |
190 | |
191 EXPECT_EQ(connection_time_observer.GetStateTransitionDelayTimeDelta( | |
192 protocol::ConnectionToHost::State::INITIALIZING, | |
193 protocol::ConnectionToHost::State::CONNECTED), first_time_delta); | |
194 } | |
195 | |
196 } // namespace test | |
197 } // namespace remoting | |
OLD | NEW |