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/base/xmpp_connection.h" | 5 #include "jingle/notifier/base/xmpp_connection.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "base/message_loop/message_pump_default.h" | |
15 #include "jingle/glue/mock_task.h" | 14 #include "jingle/glue/mock_task.h" |
16 #include "jingle/glue/task_pump.h" | 15 #include "jingle/glue/task_pump.h" |
17 #include "jingle/notifier/base/weak_xmpp_client.h" | 16 #include "jingle/notifier/base/weak_xmpp_client.h" |
18 #include "net/cert/cert_verifier.h" | 17 #include "net/cert/cert_verifier.h" |
19 #include "net/url_request/url_request_context_getter.h" | 18 #include "net/url_request/url_request_context_getter.h" |
20 #include "net/url_request/url_request_test_util.h" | 19 #include "net/url_request/url_request_test_util.h" |
21 #include "talk/xmpp/prexmppauth.h" | 20 #include "talk/xmpp/prexmppauth.h" |
22 #include "talk/xmpp/xmppclientsettings.h" | 21 #include "talk/xmpp/xmppclientsettings.h" |
23 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 virtual ~MockXmppConnectionDelegate() {} | 67 virtual ~MockXmppConnectionDelegate() {} |
69 | 68 |
70 MOCK_METHOD1(OnConnect, void(base::WeakPtr<buzz::XmppTaskParentInterface>)); | 69 MOCK_METHOD1(OnConnect, void(base::WeakPtr<buzz::XmppTaskParentInterface>)); |
71 MOCK_METHOD3(OnError, | 70 MOCK_METHOD3(OnError, |
72 void(buzz::XmppEngine::Error, int, const buzz::XmlElement*)); | 71 void(buzz::XmppEngine::Error, int, const buzz::XmlElement*)); |
73 }; | 72 }; |
74 | 73 |
75 class XmppConnectionTest : public testing::Test { | 74 class XmppConnectionTest : public testing::Test { |
76 protected: | 75 protected: |
77 XmppConnectionTest() | 76 XmppConnectionTest() |
78 : mock_pre_xmpp_auth_(new MockPreXmppAuth()) { | 77 : mock_pre_xmpp_auth_(new MockPreXmppAuth()), |
79 scoped_ptr<base::MessagePump> pump(new base::MessagePumpDefault()); | 78 url_request_context_getter_(new net::TestURLRequestContextGetter( |
80 message_loop_.reset(new base::MessageLoop(pump.Pass())); | 79 message_loop_.message_loop_proxy())) {} |
81 | |
82 url_request_context_getter_ = new net::TestURLRequestContextGetter( | |
83 message_loop_->message_loop_proxy()); | |
84 } | |
85 | 80 |
86 virtual ~XmppConnectionTest() {} | 81 virtual ~XmppConnectionTest() {} |
87 | 82 |
88 virtual void TearDown() { | 83 virtual void TearDown() { |
89 // Clear out any messages posted by XmppConnection's destructor. | 84 // Clear out any messages posted by XmppConnection's destructor. |
90 message_loop_->RunUntilIdle(); | 85 message_loop_.RunUntilIdle(); |
91 } | 86 } |
92 | 87 |
93 // Needed by XmppConnection. | 88 // Needed by XmppConnection. |
94 scoped_ptr<base::MessageLoop> message_loop_; | 89 base::MessageLoop message_loop_; |
95 MockXmppConnectionDelegate mock_xmpp_connection_delegate_; | 90 MockXmppConnectionDelegate mock_xmpp_connection_delegate_; |
96 scoped_ptr<MockPreXmppAuth> mock_pre_xmpp_auth_; | 91 scoped_ptr<MockPreXmppAuth> mock_pre_xmpp_auth_; |
97 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; | 92 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; |
98 }; | 93 }; |
99 | 94 |
100 TEST_F(XmppConnectionTest, CreateDestroy) { | 95 TEST_F(XmppConnectionTest, CreateDestroy) { |
101 XmppConnection xmpp_connection(buzz::XmppClientSettings(), | 96 XmppConnection xmpp_connection(buzz::XmppClientSettings(), |
102 url_request_context_getter_, | 97 url_request_context_getter_, |
103 &mock_xmpp_connection_delegate_, NULL); | 98 &mock_xmpp_connection_delegate_, NULL); |
104 } | 99 } |
105 | 100 |
106 #if !defined(_MSC_VER) || _MSC_VER < 1700 // http://crbug.com/158570 | 101 #if !defined(_MSC_VER) || _MSC_VER < 1700 // http://crbug.com/158570 |
107 TEST_F(XmppConnectionTest, ImmediateFailure) { | 102 TEST_F(XmppConnectionTest, ImmediateFailure) { |
108 // ChromeAsyncSocket::Connect() will always return false since we're | 103 // ChromeAsyncSocket::Connect() will always return false since we're |
109 // not setting a valid host, but this gets bubbled up as ERROR_NONE | 104 // not setting a valid host, but this gets bubbled up as ERROR_NONE |
110 // due to XmppClient's inconsistent error-handling. | 105 // due to XmppClient's inconsistent error-handling. |
111 EXPECT_CALL(mock_xmpp_connection_delegate_, | 106 EXPECT_CALL(mock_xmpp_connection_delegate_, |
112 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); | 107 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); |
113 | 108 |
114 XmppConnection xmpp_connection(buzz::XmppClientSettings(), | 109 XmppConnection xmpp_connection(buzz::XmppClientSettings(), |
115 url_request_context_getter_, | 110 url_request_context_getter_, |
116 &mock_xmpp_connection_delegate_, NULL); | 111 &mock_xmpp_connection_delegate_, NULL); |
117 | 112 |
118 // We need to do this *before* |xmpp_connection| gets destroyed or | 113 // We need to do this *before* |xmpp_connection| gets destroyed or |
119 // our delegate won't be called. | 114 // our delegate won't be called. |
120 message_loop_->RunUntilIdle(); | 115 message_loop_.RunUntilIdle(); |
121 } | 116 } |
122 | 117 |
123 TEST_F(XmppConnectionTest, PreAuthFailure) { | 118 TEST_F(XmppConnectionTest, PreAuthFailure) { |
124 EXPECT_CALL(*mock_pre_xmpp_auth_, StartPreXmppAuth(_, _, _, _,_)); | 119 EXPECT_CALL(*mock_pre_xmpp_auth_, StartPreXmppAuth(_, _, _, _,_)); |
125 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthDone()).WillOnce(Return(true)); | 120 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthDone()).WillOnce(Return(true)); |
126 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthorized()).WillOnce(Return(false)); | 121 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthorized()).WillOnce(Return(false)); |
127 EXPECT_CALL(*mock_pre_xmpp_auth_, HadError()).WillOnce(Return(true)); | 122 EXPECT_CALL(*mock_pre_xmpp_auth_, HadError()).WillOnce(Return(true)); |
128 EXPECT_CALL(*mock_pre_xmpp_auth_, GetError()).WillOnce(Return(5)); | 123 EXPECT_CALL(*mock_pre_xmpp_auth_, GetError()).WillOnce(Return(5)); |
129 | 124 |
130 EXPECT_CALL(mock_xmpp_connection_delegate_, | 125 EXPECT_CALL(mock_xmpp_connection_delegate_, |
131 OnError(buzz::XmppEngine::ERROR_AUTH, 5, NULL)); | 126 OnError(buzz::XmppEngine::ERROR_AUTH, 5, NULL)); |
132 | 127 |
133 XmppConnection xmpp_connection( | 128 XmppConnection xmpp_connection( |
134 buzz::XmppClientSettings(), url_request_context_getter_, | 129 buzz::XmppClientSettings(), url_request_context_getter_, |
135 &mock_xmpp_connection_delegate_, mock_pre_xmpp_auth_.release()); | 130 &mock_xmpp_connection_delegate_, mock_pre_xmpp_auth_.release()); |
136 | 131 |
137 // We need to do this *before* |xmpp_connection| gets destroyed or | 132 // We need to do this *before* |xmpp_connection| gets destroyed or |
138 // our delegate won't be called. | 133 // our delegate won't be called. |
139 message_loop_->RunUntilIdle(); | 134 message_loop_.RunUntilIdle(); |
140 } | 135 } |
141 | 136 |
142 TEST_F(XmppConnectionTest, FailureAfterPreAuth) { | 137 TEST_F(XmppConnectionTest, FailureAfterPreAuth) { |
143 EXPECT_CALL(*mock_pre_xmpp_auth_, StartPreXmppAuth(_, _, _, _,_)); | 138 EXPECT_CALL(*mock_pre_xmpp_auth_, StartPreXmppAuth(_, _, _, _,_)); |
144 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthDone()).WillOnce(Return(true)); | 139 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthDone()).WillOnce(Return(true)); |
145 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthorized()).WillOnce(Return(true)); | 140 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthorized()).WillOnce(Return(true)); |
146 EXPECT_CALL(*mock_pre_xmpp_auth_, GetAuthMechanism()).WillOnce(Return("")); | 141 EXPECT_CALL(*mock_pre_xmpp_auth_, GetAuthMechanism()).WillOnce(Return("")); |
147 EXPECT_CALL(*mock_pre_xmpp_auth_, GetAuthToken()).WillOnce(Return("")); | 142 EXPECT_CALL(*mock_pre_xmpp_auth_, GetAuthToken()).WillOnce(Return("")); |
148 | 143 |
149 EXPECT_CALL(mock_xmpp_connection_delegate_, | 144 EXPECT_CALL(mock_xmpp_connection_delegate_, |
150 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); | 145 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); |
151 | 146 |
152 XmppConnection xmpp_connection( | 147 XmppConnection xmpp_connection( |
153 buzz::XmppClientSettings(), url_request_context_getter_, | 148 buzz::XmppClientSettings(), url_request_context_getter_, |
154 &mock_xmpp_connection_delegate_, mock_pre_xmpp_auth_.release()); | 149 &mock_xmpp_connection_delegate_, mock_pre_xmpp_auth_.release()); |
155 | 150 |
156 // We need to do this *before* |xmpp_connection| gets destroyed or | 151 // We need to do this *before* |xmpp_connection| gets destroyed or |
157 // our delegate won't be called. | 152 // our delegate won't be called. |
158 message_loop_->RunUntilIdle(); | 153 message_loop_.RunUntilIdle(); |
159 } | 154 } |
160 | 155 |
161 TEST_F(XmppConnectionTest, RaisedError) { | 156 TEST_F(XmppConnectionTest, RaisedError) { |
162 EXPECT_CALL(mock_xmpp_connection_delegate_, | 157 EXPECT_CALL(mock_xmpp_connection_delegate_, |
163 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); | 158 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); |
164 | 159 |
165 XmppConnection xmpp_connection(buzz::XmppClientSettings(), | 160 XmppConnection xmpp_connection(buzz::XmppClientSettings(), |
166 url_request_context_getter_, | 161 url_request_context_getter_, |
167 &mock_xmpp_connection_delegate_, NULL); | 162 &mock_xmpp_connection_delegate_, NULL); |
168 | 163 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 // We have to do this since the state enum is protected in | 240 // We have to do this since the state enum is protected in |
246 // talk_base::Task. | 241 // talk_base::Task. |
247 const int TASK_STATE_ERROR = 3; | 242 const int TASK_STATE_ERROR = 3; |
248 ON_CALL(*task, ProcessStart()) | 243 ON_CALL(*task, ProcessStart()) |
249 .WillByDefault(Return(TASK_STATE_ERROR)); | 244 .WillByDefault(Return(TASK_STATE_ERROR)); |
250 EXPECT_CALL(*task, ProcessStart()).Times(0); | 245 EXPECT_CALL(*task, ProcessStart()).Times(0); |
251 task->Start(); | 246 task->Start(); |
252 } | 247 } |
253 | 248 |
254 // This should destroy |task_pump|, but |task| still shouldn't run. | 249 // This should destroy |task_pump|, but |task| still shouldn't run. |
255 message_loop_->RunUntilIdle(); | 250 message_loop_.RunUntilIdle(); |
256 } | 251 } |
257 | 252 |
258 } // namespace notifier | 253 } // namespace notifier |
OLD | NEW |