Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / remoting / test / connection_time_observer_unittest.cc
blob3a4929a77b556b8d3be86bb49147dabdd28233ab
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.
5 #include "remoting/test/connection_time_observer.h"
7 #include <utility>
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"
15 namespace remoting {
16 namespace test {
18 class ConnectionTimeObserverTest : public ::testing::Test {
19 public:
20 ConnectionTimeObserverTest();
21 ~ConnectionTimeObserverTest() override;
23 protected:
24 // Test interface.
25 void SetUp() override;
27 // A map of fake times to calculate TimeDelta between two states.
28 std::map<protocol::ConnectionToHost::State, base::TimeTicks> test_map_;
30 // Observes and saves the times when the chromoting connection state changes.
31 scoped_ptr<ConnectionTimeObserver> connection_time_observer_;
33 private:
34 DISALLOW_COPY_AND_ASSIGN(ConnectionTimeObserverTest);
37 ConnectionTimeObserverTest::ConnectionTimeObserverTest() {
40 ConnectionTimeObserverTest::~ConnectionTimeObserverTest() {
43 void ConnectionTimeObserverTest::SetUp() {
44 connection_time_observer_.reset(new ConnectionTimeObserver());
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)));
63 connection_time_observer_->SetTransitionTimesMapForTest(test_map_);
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);
81 TEST_F(ConnectionTimeObserverTest, StartStateNotFound) {
82 EXPECT_TRUE(connection_time_observer_->GetStateTransitionTime(
83 protocol::ConnectionToHost::State::FAILED,
84 protocol::ConnectionToHost::State::CLOSED).is_max());
87 TEST_F(ConnectionTimeObserverTest, EndStateNotFound) {
88 EXPECT_TRUE(connection_time_observer_->GetStateTransitionTime(
89 protocol::ConnectionToHost::State::INITIALIZING,
90 protocol::ConnectionToHost::State::FAILED).is_max());
93 TEST_F(ConnectionTimeObserverTest, NegativeTransitionDelay) {
94 EXPECT_TRUE(connection_time_observer_->GetStateTransitionTime(
95 protocol::ConnectionToHost::State::CLOSED,
96 protocol::ConnectionToHost::State::INITIALIZING).is_max());
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());
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);
110 EXPECT_FALSE(connection_time_observer_->GetStateTransitionTime(
111 protocol::ConnectionToHost::State::INITIALIZING,
112 protocol::ConnectionToHost::State::FAILED).is_zero());
115 TEST_F(ConnectionTimeObserverTest, TestOnConnectionStateChangedWithoutTestMap) {
116 connection_time_observer_->SetTransitionTimesMapForTest(
117 std::map<protocol::ConnectionToHost::State, base::TimeTicks>());
119 base::MessageLoopForIO message_loop;
120 base::RunLoop run_loop;
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());
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);
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();
147 connection_time_observer_->ConnectionStateChanged(
148 protocol::ConnectionToHost::State::CONNECTED,
149 protocol::ErrorCode::OK);
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);
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);
163 // This check makes sure that the two states are actually found.
164 EXPECT_FALSE(first_time_delta.is_zero());
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);
170 EXPECT_EQ(connection_time_observer_->GetStateTransitionTime(
171 protocol::ConnectionToHost::State::INITIALIZING,
172 protocol::ConnectionToHost::State::CONNECTED), first_time_delta);
175 } // namespace test
176 } // namespace remoting