tonychun | 684d1414 | 2015-07-31 00:05:49 | [diff] [blame] | 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 | |
avi | 5a080f01 | 2015-12-22 23:15:43 | [diff] [blame] | 9 | #include "base/macros.h" |
tonychun | 684d1414 | 2015-07-31 00:05:49 | [diff] [blame] | 10 | #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 | |
| 16 | namespace remoting { |
| 17 | namespace test { |
| 18 | |
| 19 | class 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. |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 32 | std::unique_ptr<ConnectionTimeObserver> connection_time_observer_; |
tonychun | 684d1414 | 2015-07-31 00:05:49 | [diff] [blame] | 33 | |
| 34 | private: |
| 35 | DISALLOW_COPY_AND_ASSIGN(ConnectionTimeObserverTest); |
| 36 | }; |
| 37 | |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame^] | 38 | ConnectionTimeObserverTest::ConnectionTimeObserverTest() = default; |
tonychun | 684d1414 | 2015-07-31 00:05:49 | [diff] [blame] | 39 | |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame^] | 40 | ConnectionTimeObserverTest::~ConnectionTimeObserverTest() = default; |
tonychun | 684d1414 | 2015-07-31 00:05:49 | [diff] [blame] | 41 | |
| 42 | void 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 | |
| 65 | TEST_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 | |
| 80 | TEST_F(ConnectionTimeObserverTest, StartStateNotFound) { |
| 81 | EXPECT_TRUE(connection_time_observer_->GetStateTransitionTime( |
| 82 | protocol::ConnectionToHost::State::FAILED, |
| 83 | protocol::ConnectionToHost::State::CLOSED).is_max()); |
| 84 | } |
| 85 | |
| 86 | TEST_F(ConnectionTimeObserverTest, EndStateNotFound) { |
| 87 | EXPECT_TRUE(connection_time_observer_->GetStateTransitionTime( |
| 88 | protocol::ConnectionToHost::State::INITIALIZING, |
| 89 | protocol::ConnectionToHost::State::FAILED).is_max()); |
| 90 | } |
| 91 | |
| 92 | TEST_F(ConnectionTimeObserverTest, NegativeTransitionDelay) { |
| 93 | EXPECT_TRUE(connection_time_observer_->GetStateTransitionTime( |
| 94 | protocol::ConnectionToHost::State::CLOSED, |
| 95 | protocol::ConnectionToHost::State::INITIALIZING).is_max()); |
| 96 | } |
| 97 | |
| 98 | TEST_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 | |
| 114 | TEST_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 | |
| 156 | TEST_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 |