Chromium Code Reviews| Index: remoting/test/connection_time_observer_unittest.cc |
| diff --git a/remoting/test/connection_time_observer_unittest.cc b/remoting/test/connection_time_observer_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..af007d8d3903704d7399dc9368e6185bf199b1fa |
| --- /dev/null |
| +++ b/remoting/test/connection_time_observer_unittest.cc |
| @@ -0,0 +1,197 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "remoting/test/connection_time_observer.h" |
| + |
| +#include <utility> |
| + |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/run_loop.h" |
| +#include "base/time/time.h" |
| +#include "base/timer/timer.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace remoting { |
| +namespace test { |
| + |
| +class ConnectionTimeObserverTest : public ::testing::Test { |
| + public: |
| + ConnectionTimeObserverTest(); |
| + ~ConnectionTimeObserverTest() override; |
| + |
| + std::map<protocol::ConnectionToHost::State, base::TimeTicks> test_map_; |
| + |
|
joedow
2015/07/27 16:25:39
These members should have comments and live in the
tonychun
2015/07/27 21:20:42
Done.
|
| + scoped_ptr<base::Timer> timer_; |
| + |
| + protected: |
| + // Test interface. |
| + void SetUp() override; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(ConnectionTimeObserverTest); |
| +}; |
| + |
| +ConnectionTimeObserverTest::ConnectionTimeObserverTest() { |
| +} |
| + |
| +ConnectionTimeObserverTest::~ConnectionTimeObserverTest() { |
| +} |
| + |
| +void ConnectionTimeObserverTest::SetUp() { |
| + timer_.reset(new base::Timer(true, false)); |
| + |
| + base::TimeTicks now = base::TimeTicks::Now(); |
| + test_map_.insert(std::make_pair( |
| + protocol::ConnectionToHost::State::INITIALIZING, |
| + now + base::TimeDelta::FromMilliseconds(10))); |
| + test_map_.insert(std::make_pair( |
| + protocol::ConnectionToHost::State::CONNECTING, |
| + now + base::TimeDelta::FromMilliseconds(20))); |
| + test_map_.insert(std::make_pair( |
| + protocol::ConnectionToHost::State::AUTHENTICATED, |
| + now + base::TimeDelta::FromMilliseconds(30))); |
| + test_map_.insert(std::make_pair( |
| + protocol::ConnectionToHost::State::CONNECTED, |
| + now + base::TimeDelta::FromMilliseconds(40))); |
| + test_map_.insert(std::make_pair( |
| + protocol::ConnectionToHost::State::CLOSED, |
| + now + base::TimeDelta::FromMilliseconds(50))); |
| +} |
| + |
| +TEST_F(ConnectionTimeObserverTest, ChromotingConnectionSuccess) { |
| + ConnectionTimeObserver connection_time_observer(timer_.get()); |
| + |
| + connection_time_observer.SetTransitionTimesMapForTest(test_map_); |
| + |
| + EXPECT_EQ(connection_time_observer.GetStateTransitionDelayTimeDelta( |
| + protocol::ConnectionToHost::State::INITIALIZING, |
| + protocol::ConnectionToHost::State::CONNECTING).InMilliseconds(), 10); |
| + EXPECT_EQ(connection_time_observer.GetStateTransitionDelayTimeDelta( |
| + protocol::ConnectionToHost::State::CONNECTING, |
| + protocol::ConnectionToHost::State::AUTHENTICATED).InMilliseconds(), 10); |
| + EXPECT_EQ(connection_time_observer.GetStateTransitionDelayTimeDelta( |
| + protocol::ConnectionToHost::State::AUTHENTICATED, |
| + protocol::ConnectionToHost::State::CONNECTED).InMilliseconds(), 10); |
| + EXPECT_EQ(connection_time_observer.GetStateTransitionDelayTimeDelta( |
| + protocol::ConnectionToHost::State::CONNECTED, |
| + protocol::ConnectionToHost::State::CLOSED).InMilliseconds(), 10); |
| +} |
| + |
| +TEST_F(ConnectionTimeObserverTest, StartStateNotFound) { |
| + ConnectionTimeObserver connection_time_observer(timer_.get()); |
| + |
| + connection_time_observer.SetTransitionTimesMapForTest(test_map_); |
| + |
| + EXPECT_EQ(connection_time_observer.GetStateTransitionDelayTimeDelta( |
| + protocol::ConnectionToHost::State::FAILED, |
| + protocol::ConnectionToHost::State::CLOSED), |
| + base::TimeDelta::Max()); |
| +} |
| + |
| +TEST_F(ConnectionTimeObserverTest, EndStateNotFound) { |
| + ConnectionTimeObserver connection_time_observer(timer_.get()); |
| + |
| + connection_time_observer.SetTransitionTimesMapForTest(test_map_); |
| + |
| + EXPECT_EQ(connection_time_observer.GetStateTransitionDelayTimeDelta( |
| + protocol::ConnectionToHost::State::INITIALIZING, |
| + protocol::ConnectionToHost::State::FAILED), |
| + base::TimeDelta::Max()); |
| +} |
| + |
| +TEST_F(ConnectionTimeObserverTest, NegativeTransitionDelay) { |
| + ConnectionTimeObserver connection_time_observer(timer_.get()); |
| + |
| + connection_time_observer.SetTransitionTimesMapForTest(test_map_); |
| + |
| + EXPECT_EQ(connection_time_observer.GetStateTransitionDelayTimeDelta( |
| + protocol::ConnectionToHost::State::CLOSED, |
| + protocol::ConnectionToHost::State::INITIALIZING), |
| + base::TimeDelta::Max()); |
| +} |
| + |
| +TEST_F(ConnectionTimeObserverTest, TestOnConnectionStateChangedWithTestMap) { |
| + ConnectionTimeObserver connection_time_observer(timer_.get()); |
| + |
| + connection_time_observer.SetTransitionTimesMapForTest(test_map_); |
| + |
| + // Should fail to find FAILED. |
| + EXPECT_EQ(connection_time_observer.GetStateTransitionDelayTimeDelta( |
| + protocol::ConnectionToHost::State::INITIALIZING, |
| + protocol::ConnectionToHost::State::FAILED), |
| + base::TimeDelta::Max()); |
| + |
| + // Registers the time at which FAILED state occurred into the map. |
| + connection_time_observer.ConnectionStateChanged( |
| + protocol::ConnectionToHost::State::FAILED, |
| + protocol::ErrorCode::PEER_IS_OFFLINE); |
| + |
| + EXPECT_FALSE(connection_time_observer.GetStateTransitionDelayTimeDelta( |
| + protocol::ConnectionToHost::State::INITIALIZING, |
| + protocol::ConnectionToHost::State::FAILED).is_zero()); |
| +} |
| + |
| +TEST_F(ConnectionTimeObserverTest, TestOnConnectionStateChangedWithoutTestMap) { |
| + ConnectionTimeObserver connection_time_observer(timer_.get()); |
| + |
| + base::MessageLoopForIO message_loop; |
| + base::RunLoop run_loop; |
| + |
| + // Should fail to find INITIALIZING in an empty map. |
| + EXPECT_EQ(connection_time_observer.GetStateTransitionDelayTimeDelta( |
| + protocol::ConnectionToHost::State::INITIALIZING, |
| + protocol::ConnectionToHost::State::FAILED), |
| + base::TimeDelta::Max()); |
| + |
| + connection_time_observer.SetInitializingState(); |
| + connection_time_observer.ConnectionStateChanged( |
| + protocol::ConnectionToHost::State::CONNECTING, |
| + protocol::ErrorCode::OK); |
| + connection_time_observer.ConnectionStateChanged( |
| + protocol::ConnectionToHost::State::AUTHENTICATED, |
| + protocol::ErrorCode::OK); |
| + |
| + // Wait for 10 milliseconds for CONNECTED state. |
| + // Note: This test can only guarantee a positive TimeDelta between a previous |
| + // state and the CONNECTED state. Prior states have non-deterministic times |
| + // between each other. |
| + base::Timer timer(true, false); |
| + timer.Start(FROM_HERE, |
| + base::TimeDelta::FromMilliseconds(10), |
| + run_loop.QuitClosure()); |
| + run_loop.Run(); |
| + |
| + connection_time_observer.ConnectionStateChanged( |
| + protocol::ConnectionToHost::State::CONNECTED, |
| + protocol::ErrorCode::OK); |
| + |
| + EXPECT_FALSE(connection_time_observer.GetStateTransitionDelayTimeDelta( |
| + protocol::ConnectionToHost::State::INITIALIZING, |
| + 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
|
| +} |
| + |
| +TEST_F(ConnectionTimeObserverTest, TestOnConnectionStateDataDoesNotChange) { |
| + ConnectionTimeObserver connection_time_observer(timer_.get()); |
| + |
| + connection_time_observer.SetTransitionTimesMapForTest(test_map_); |
| + |
| + base::TimeDelta first_time_delta = |
| + connection_time_observer.GetStateTransitionDelayTimeDelta( |
| + protocol::ConnectionToHost::State::INITIALIZING, |
| + protocol::ConnectionToHost::State::CONNECTED); |
| + |
| + // This check makes sure that the two states are actually found. |
| + EXPECT_FALSE(first_time_delta.is_zero()); |
| + |
| + // The initial data should not change and should log an error. |
| + connection_time_observer.ConnectionStateChanged( |
| + protocol::ConnectionToHost::State::CONNECTED, protocol::ErrorCode::OK); |
| + |
| + EXPECT_EQ(connection_time_observer.GetStateTransitionDelayTimeDelta( |
| + protocol::ConnectionToHost::State::INITIALIZING, |
| + protocol::ConnectionToHost::State::CONNECTED), first_time_delta); |
| +} |
| + |
| +} // namespace test |
| +} // namespace remoting |