OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stddef.h> | 5 #include <stddef.h> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/test/test_mock_time_task_runner.h" | 9 #include "base/test/test_mock_time_task_runner.h" |
10 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 EngineAuthenticationHandlerTest() | 33 EngineAuthenticationHandlerTest() |
34 : runner_(new base::TestMockTimeTaskRunner), | 34 : runner_(new base::TestMockTimeTaskRunner), |
35 runner_handle_(runner_), | 35 runner_handle_(runner_), |
36 auth_handler_(new EngineAuthenticationHandler(&connection_handler_)), | 36 auth_handler_(new EngineAuthenticationHandler(&connection_handler_)), |
37 connection_(new testing::StrictMock<MockBlimpConnection>()) {} | 37 connection_(new testing::StrictMock<MockBlimpConnection>()) {} |
38 | 38 |
39 ~EngineAuthenticationHandlerTest() override {} | 39 ~EngineAuthenticationHandlerTest() override {} |
40 | 40 |
41 protected: | 41 protected: |
42 void ExpectOnConnection() { | 42 void ExpectOnConnection() { |
43 EXPECT_CALL(*connection_, SetConnectionErrorObserver(_)) | 43 EXPECT_CALL(*connection_, AddConnectionErrorObserver(_)) |
44 .Times(2) | 44 .WillOnce(SaveArg<0>(&error_observer_)); |
45 .WillRepeatedly(SaveArg<0>(&error_observer_)); | |
46 EXPECT_CALL(*connection_, SetIncomingMessageProcessor(_)) | 45 EXPECT_CALL(*connection_, SetIncomingMessageProcessor(_)) |
47 .Times(2) | 46 .WillOnce(SaveArg<0>(&incoming_message_processor_)); |
48 .WillRepeatedly(SaveArg<0>(&incoming_message_processor_)); | |
49 } | 47 } |
50 | 48 |
51 scoped_refptr<base::TestMockTimeTaskRunner> runner_; | 49 scoped_refptr<base::TestMockTimeTaskRunner> runner_; |
52 base::ThreadTaskRunnerHandle runner_handle_; | 50 base::ThreadTaskRunnerHandle runner_handle_; |
53 testing::StrictMock<MockConnectionHandler> connection_handler_; | 51 testing::StrictMock<MockConnectionHandler> connection_handler_; |
54 scoped_ptr<EngineAuthenticationHandler> auth_handler_; | 52 scoped_ptr<EngineAuthenticationHandler> auth_handler_; |
55 scoped_ptr<testing::StrictMock<MockBlimpConnection>> connection_; | 53 scoped_ptr<testing::StrictMock<MockBlimpConnection>> connection_; |
56 ConnectionErrorObserver* error_observer_ = nullptr; | 54 ConnectionErrorObserver* error_observer_ = nullptr; |
57 BlimpMessageProcessor* incoming_message_processor_ = nullptr; | 55 BlimpMessageProcessor* incoming_message_processor_ = nullptr; |
58 }; | 56 }; |
59 | 57 |
60 TEST_F(EngineAuthenticationHandlerTest, AuthenticationSucceeds) { | 58 TEST_F(EngineAuthenticationHandlerTest, AuthenticationSucceeds) { |
61 ExpectOnConnection(); | 59 ExpectOnConnection(); |
| 60 EXPECT_CALL(*connection_, RemoveConnectionErrorObserver(_)); |
62 EXPECT_CALL(connection_handler_, HandleConnectionPtr(Eq(connection_.get()))); | 61 EXPECT_CALL(connection_handler_, HandleConnectionPtr(Eq(connection_.get()))); |
63 auth_handler_->HandleConnection(std::move(connection_)); | 62 auth_handler_->HandleConnection(std::move(connection_)); |
64 EXPECT_NE(nullptr, error_observer_); | 63 EXPECT_NE(nullptr, error_observer_); |
65 EXPECT_NE(nullptr, incoming_message_processor_); | 64 EXPECT_NE(nullptr, incoming_message_processor_); |
66 | 65 |
67 scoped_ptr<BlimpMessage> blimp_message = CreateStartConnectionMessage("", 0); | 66 scoped_ptr<BlimpMessage> blimp_message = CreateStartConnectionMessage("", 0); |
68 net::TestCompletionCallback process_message_cb; | 67 net::TestCompletionCallback process_message_cb; |
69 incoming_message_processor_->ProcessMessage(std::move(blimp_message), | 68 incoming_message_processor_->ProcessMessage(std::move(blimp_message), |
70 process_message_cb.callback()); | 69 process_message_cb.callback()); |
71 EXPECT_EQ(net::OK, process_message_cb.WaitForResult()); | 70 EXPECT_EQ(net::OK, process_message_cb.WaitForResult()); |
72 EXPECT_EQ(nullptr, error_observer_); | |
73 EXPECT_EQ(nullptr, incoming_message_processor_); | |
74 } | 71 } |
75 | 72 |
76 TEST_F(EngineAuthenticationHandlerTest, WrongMessageReceived) { | 73 TEST_F(EngineAuthenticationHandlerTest, WrongMessageReceived) { |
77 ExpectOnConnection(); | 74 ExpectOnConnection(); |
78 auth_handler_->HandleConnection(std::move(connection_)); | 75 auth_handler_->HandleConnection(std::move(connection_)); |
79 | 76 |
80 InputMessage* input_message; | 77 InputMessage* input_message; |
81 scoped_ptr<BlimpMessage> blimp_message = CreateBlimpMessage(&input_message); | 78 scoped_ptr<BlimpMessage> blimp_message = CreateBlimpMessage(&input_message); |
82 net::TestCompletionCallback process_message_cb; | 79 net::TestCompletionCallback process_message_cb; |
83 incoming_message_processor_->ProcessMessage(std::move(blimp_message), | 80 incoming_message_processor_->ProcessMessage(std::move(blimp_message), |
84 process_message_cb.callback()); | 81 process_message_cb.callback()); |
85 EXPECT_EQ(net::OK, process_message_cb.WaitForResult()); | 82 EXPECT_EQ(net::OK, process_message_cb.WaitForResult()); |
86 EXPECT_EQ(nullptr, error_observer_); | |
87 EXPECT_EQ(nullptr, incoming_message_processor_); | |
88 } | 83 } |
89 | 84 |
90 TEST_F(EngineAuthenticationHandlerTest, ConnectionError) { | 85 TEST_F(EngineAuthenticationHandlerTest, ConnectionError) { |
91 ExpectOnConnection(); | 86 ExpectOnConnection(); |
92 auth_handler_->HandleConnection(std::move(connection_)); | 87 auth_handler_->HandleConnection(std::move(connection_)); |
93 EXPECT_NE(nullptr, error_observer_); | 88 EXPECT_NE(nullptr, error_observer_); |
94 EXPECT_NE(nullptr, incoming_message_processor_); | 89 EXPECT_NE(nullptr, incoming_message_processor_); |
95 error_observer_->OnConnectionError(net::ERR_FAILED); | 90 error_observer_->OnConnectionError(net::ERR_FAILED); |
96 EXPECT_EQ(nullptr, error_observer_); | |
97 EXPECT_EQ(nullptr, incoming_message_processor_); | |
98 } | 91 } |
99 | 92 |
100 TEST_F(EngineAuthenticationHandlerTest, Timeout) { | 93 TEST_F(EngineAuthenticationHandlerTest, Timeout) { |
101 ExpectOnConnection(); | 94 ExpectOnConnection(); |
102 auth_handler_->HandleConnection(std::move(connection_)); | 95 auth_handler_->HandleConnection(std::move(connection_)); |
103 EXPECT_NE(nullptr, error_observer_); | 96 EXPECT_NE(nullptr, error_observer_); |
104 EXPECT_NE(nullptr, incoming_message_processor_); | 97 EXPECT_NE(nullptr, incoming_message_processor_); |
105 | 98 |
106 runner_->FastForwardBy(base::TimeDelta::FromSeconds(11)); | 99 runner_->FastForwardBy(base::TimeDelta::FromSeconds(11)); |
107 EXPECT_EQ(nullptr, error_observer_); | |
108 EXPECT_EQ(nullptr, incoming_message_processor_); | |
109 } | 100 } |
110 | 101 |
111 TEST_F(EngineAuthenticationHandlerTest, AuthHandlerDeletedFirst) { | 102 TEST_F(EngineAuthenticationHandlerTest, AuthHandlerDeletedFirst) { |
112 ExpectOnConnection(); | 103 ExpectOnConnection(); |
113 auth_handler_->HandleConnection(std::move(connection_)); | 104 auth_handler_->HandleConnection(std::move(connection_)); |
114 auth_handler_.reset(); | 105 auth_handler_.reset(); |
115 | 106 |
116 scoped_ptr<BlimpMessage> blimp_message = CreateStartConnectionMessage("", 0); | 107 scoped_ptr<BlimpMessage> blimp_message = CreateStartConnectionMessage("", 0); |
117 net::TestCompletionCallback process_message_cb; | 108 net::TestCompletionCallback process_message_cb; |
118 incoming_message_processor_->ProcessMessage(std::move(blimp_message), | 109 incoming_message_processor_->ProcessMessage(std::move(blimp_message), |
119 process_message_cb.callback()); | 110 process_message_cb.callback()); |
120 EXPECT_EQ(net::OK, process_message_cb.WaitForResult()); | 111 EXPECT_EQ(net::OK, process_message_cb.WaitForResult()); |
121 } | 112 } |
122 | 113 |
123 } // namespace blimp | 114 } // namespace blimp |
OLD | NEW |