| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "jingle/notifier/communicator/single_login_attempt.h" | 5 #include "jingle/notifier/communicator/single_login_attempt.h" |
| 6 | 6 |
| 7 #include <cstddef> | 7 #include <cstddef> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "jingle/notifier/base/const_communicator.h" | 12 #include "jingle/notifier/base/const_communicator.h" |
| 13 #include "jingle/notifier/base/fake_base_task.h" | 13 #include "jingle/notifier/base/fake_base_task.h" |
| 14 #include "jingle/notifier/communicator/login_settings.h" | 14 #include "jingle/notifier/communicator/login_settings.h" |
| 15 #include "net/base/mock_host_resolver.h" | 15 #include "net/base/mock_host_resolver.h" |
| 16 #include "net/url_request/url_request_test_util.h" | 16 #include "net/url_request/url_request_test_util.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 #include "talk/xmllite/xmlelement.h" | 17 #include "talk/xmllite/xmlelement.h" |
| 19 #include "talk/xmpp/constants.h" | 18 #include "talk/xmpp/constants.h" |
| 20 #include "talk/xmpp/xmppengine.h" | 19 #include "talk/xmpp/xmppengine.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 21 |
| 22 namespace buzz { | 22 namespace buzz { |
| 23 class XmppTaskParentInterface; | 23 class XmppTaskParentInterface; |
| 24 } // namespace buzz | 24 } // namespace buzz |
| 25 | 25 |
| 26 namespace notifier { | 26 namespace notifier { |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 enum DelegateState { IDLE, CONNECTED, NEED_RECONNECT, REDIRECTED }; | 30 enum DelegateState { |
| 31 IDLE, CONNECTED, REDIRECTED, CREDENTIALS_REJECTED, SETTINGS_EXHAUSTED |
| 32 }; |
| 31 | 33 |
| 32 class FakeDelegate : public SingleLoginAttempt::Delegate { | 34 class FakeDelegate : public SingleLoginAttempt::Delegate { |
| 33 public: | 35 public: |
| 34 FakeDelegate() : state_(IDLE) {} | 36 FakeDelegate() : state_(IDLE) {} |
| 35 | 37 |
| 36 void OnConnect(base::WeakPtr<buzz::XmppTaskParentInterface> base_task) { | 38 void OnConnect( |
| 39 base::WeakPtr<buzz::XmppTaskParentInterface> base_task) OVERRIDE { |
| 37 state_ = CONNECTED; | 40 state_ = CONNECTED; |
| 38 base_task_ = base_task; | 41 base_task_ = base_task; |
| 39 } | 42 } |
| 40 | 43 |
| 41 virtual void OnNeedReconnect() { | |
| 42 state_ = NEED_RECONNECT; | |
| 43 } | |
| 44 | |
| 45 virtual void OnRedirect(const ServerInformation& redirect_server) OVERRIDE { | 44 virtual void OnRedirect(const ServerInformation& redirect_server) OVERRIDE { |
| 46 state_ = REDIRECTED; | 45 state_ = REDIRECTED; |
| 47 redirect_server_ = redirect_server; | 46 redirect_server_ = redirect_server; |
| 48 } | 47 } |
| 49 | 48 |
| 49 virtual void OnCredentialsRejected() OVERRIDE { |
| 50 state_ = CREDENTIALS_REJECTED; |
| 51 } |
| 52 |
| 53 virtual void OnSettingsExhausted() OVERRIDE { |
| 54 state_ = SETTINGS_EXHAUSTED; |
| 55 } |
| 56 |
| 50 DelegateState state() const { return state_; } | 57 DelegateState state() const { return state_; } |
| 51 | 58 |
| 52 base::WeakPtr<buzz::XmppTaskParentInterface> base_task() const { | 59 base::WeakPtr<buzz::XmppTaskParentInterface> base_task() const { |
| 53 return base_task_; | 60 return base_task_; |
| 54 } | 61 } |
| 55 | 62 |
| 56 const ServerInformation& redirect_server() const { | 63 const ServerInformation& redirect_server() const { |
| 57 return redirect_server_; | 64 return redirect_server_; |
| 58 } | 65 } |
| 59 | 66 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 | 115 |
| 109 // Fire OnConnect and make sure the base task gets passed to the | 116 // Fire OnConnect and make sure the base task gets passed to the |
| 110 // delegate properly. | 117 // delegate properly. |
| 111 TEST_F(SingleLoginAttemptTest, Basic) { | 118 TEST_F(SingleLoginAttemptTest, Basic) { |
| 112 attempt_.OnConnect(fake_base_task_.AsWeakPtr()); | 119 attempt_.OnConnect(fake_base_task_.AsWeakPtr()); |
| 113 EXPECT_EQ(CONNECTED, fake_delegate_.state()); | 120 EXPECT_EQ(CONNECTED, fake_delegate_.state()); |
| 114 EXPECT_EQ(fake_base_task_.AsWeakPtr().get(), | 121 EXPECT_EQ(fake_base_task_.AsWeakPtr().get(), |
| 115 fake_delegate_.base_task().get()); | 122 fake_delegate_.base_task().get()); |
| 116 } | 123 } |
| 117 | 124 |
| 118 // Fire OnErrors and make sure the delegate gets the OnNeedReconnect() | 125 // Fire OnErrors and make sure the delegate gets the |
| 119 // event. | 126 // OnSettingsExhausted() event. |
| 120 TEST_F(SingleLoginAttemptTest, Error) { | 127 TEST_F(SingleLoginAttemptTest, Error) { |
| 121 for (int i = 0; i < 2; ++i) { | 128 for (int i = 0; i < 2; ++i) { |
| 122 EXPECT_EQ(IDLE, fake_delegate_.state()); | 129 EXPECT_EQ(IDLE, fake_delegate_.state()); |
| 123 attempt_.OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL); | 130 attempt_.OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL); |
| 124 } | 131 } |
| 125 EXPECT_EQ(NEED_RECONNECT, fake_delegate_.state()); | 132 EXPECT_EQ(SETTINGS_EXHAUSTED, fake_delegate_.state()); |
| 126 } | 133 } |
| 127 | 134 |
| 128 // Fire OnErrors but replace the last one with OnConnect, and make | 135 // Fire OnErrors but replace the last one with OnConnect, and make |
| 129 // sure the delegate still gets the OnConnect message. | 136 // sure the delegate still gets the OnConnect message. |
| 130 TEST_F(SingleLoginAttemptTest, ErrorThenSuccess) { | 137 TEST_F(SingleLoginAttemptTest, ErrorThenSuccess) { |
| 131 attempt_.OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL); | 138 attempt_.OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL); |
| 132 attempt_.OnConnect(fake_base_task_.AsWeakPtr()); | 139 attempt_.OnConnect(fake_base_task_.AsWeakPtr()); |
| 133 EXPECT_EQ(CONNECTED, fake_delegate_.state()); | 140 EXPECT_EQ(CONNECTED, fake_delegate_.state()); |
| 134 EXPECT_EQ(fake_base_task_.AsWeakPtr().get(), | 141 EXPECT_EQ(fake_base_task_.AsWeakPtr().get(), |
| 135 fake_delegate_.base_task().get()); | 142 fake_delegate_.base_task().get()); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 | 233 |
| 227 // Fire a redirect with a missing see-other-host element and make sure | 234 // Fire a redirect with a missing see-other-host element and make sure |
| 228 // the delegate does not get a redirect. | 235 // the delegate does not get a redirect. |
| 229 TEST_F(SingleLoginAttemptTest, RedirectMissingSeeOtherHost) { | 236 TEST_F(SingleLoginAttemptTest, RedirectMissingSeeOtherHost) { |
| 230 scoped_ptr<buzz::XmlElement> redirect_error(MakeRedirectError("")); | 237 scoped_ptr<buzz::XmlElement> redirect_error(MakeRedirectError("")); |
| 231 redirect_error->RemoveChildAfter(NULL); | 238 redirect_error->RemoveChildAfter(NULL); |
| 232 FireRedirect(redirect_error.get()); | 239 FireRedirect(redirect_error.get()); |
| 233 EXPECT_EQ(IDLE, fake_delegate_.state()); | 240 EXPECT_EQ(IDLE, fake_delegate_.state()); |
| 234 } | 241 } |
| 235 | 242 |
| 243 // Fire 'Unauthorized' errors and make sure the delegate gets the |
| 244 // OnCredentialsRejected() event. |
| 245 TEST_F(SingleLoginAttemptTest, CredentialsRejected) { |
| 246 attempt_.OnError(buzz::XmppEngine::ERROR_UNAUTHORIZED, 0, NULL); |
| 247 EXPECT_EQ(CREDENTIALS_REJECTED, fake_delegate_.state()); |
| 248 } |
| 249 |
| 236 } // namespace | 250 } // namespace |
| 237 | 251 |
| 238 } // namespace notifier | 252 } // namespace notifier |
| OLD | NEW |