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