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