OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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. |
| 4 |
| 5 #include "base/waitable_event.h" |
| 6 #include "remoting/jingle_glue/jingle_client.h" |
| 7 #include "remoting/jingle_glue/jingle_thread.h" |
| 8 #include "testing/gmock/include/gmock/gmock.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 using testing::_; |
| 12 |
| 13 namespace remoting { |
| 14 |
| 15 class MockJingleClientCallback : public JingleClient::Callback { |
| 16 public: |
| 17 ~MockJingleClientCallback() { } |
| 18 |
| 19 MOCK_METHOD2(OnStateChange, void(JingleClient*, JingleClient::State)); |
| 20 MOCK_METHOD3(OnAcceptConnection, bool( |
| 21 JingleClient*, const std::string&, |
| 22 JingleChannel::Callback**_callback)); |
| 23 MOCK_METHOD2(OnNewConnection, void( |
| 24 JingleClient*, scoped_refptr<JingleChannel>)); |
| 25 }; |
| 26 |
| 27 class JingleClientTest : public testing::Test { |
| 28 public: |
| 29 virtual ~JingleClientTest() { } |
| 30 |
| 31 static void OnClosed(bool* called) { |
| 32 *called = true; |
| 33 } |
| 34 |
| 35 // A helper that calls OnConnectionStateChanged(). Need this because we want |
| 36 // to call it on the jingle thread. |
| 37 static void ChangeState(JingleClient* client, buzz::XmppEngine::State state, |
| 38 base::WaitableEvent* done_event) { |
| 39 client->OnConnectionStateChanged(state); |
| 40 if (done_event) |
| 41 done_event->Signal(); |
| 42 } |
| 43 |
| 44 protected: |
| 45 virtual void SetUp() { |
| 46 client_ = new JingleClient(&thread_); |
| 47 // Fake initialization |
| 48 client_->initialized_ = true; |
| 49 client_->callback_ = &callback_; |
| 50 } |
| 51 |
| 52 JingleThread thread_; |
| 53 scoped_refptr<JingleClient> client_; |
| 54 MockJingleClientCallback callback_; |
| 55 }; |
| 56 |
| 57 TEST_F(JingleClientTest, OnStateChanged) { |
| 58 EXPECT_CALL(callback_, OnStateChange(_, JingleClient::CONNECTING)) |
| 59 .Times(1); |
| 60 |
| 61 thread_.Start(); |
| 62 |
| 63 base::WaitableEvent state_changed_event(true, false); |
| 64 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableFunction( |
| 65 &JingleClientTest::ChangeState, client_.get(), |
| 66 buzz::XmppEngine::STATE_OPENING, &state_changed_event)); |
| 67 state_changed_event.Wait(); |
| 68 |
| 69 client_->Close(); |
| 70 |
| 71 thread_.Stop(); |
| 72 } |
| 73 |
| 74 TEST_F(JingleClientTest, Close) { |
| 75 EXPECT_CALL(callback_, OnStateChange(_, _)) |
| 76 .Times(0); |
| 77 thread_.Start(); |
| 78 client_->Close(); |
| 79 // Verify that the channel doesn't call callback anymore. |
| 80 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableFunction( |
| 81 &JingleClientTest::ChangeState, client_.get(), |
| 82 buzz::XmppEngine::STATE_OPENING, |
| 83 static_cast<base::WaitableEvent*>(NULL))); |
| 84 thread_.Stop(); |
| 85 } |
| 86 |
| 87 TEST_F(JingleClientTest, ClosedTask) { |
| 88 thread_.Start(); |
| 89 bool closed = false; |
| 90 client_->Close(NewRunnableFunction(&JingleClientTest::OnClosed, |
| 91 &closed)); |
| 92 thread_.Stop(); |
| 93 EXPECT_TRUE(closed); |
| 94 } |
| 95 |
| 96 TEST_F(JingleClientTest, DoubleClose) { |
| 97 thread_.Start(); |
| 98 bool closed1 = false; |
| 99 client_->Close(NewRunnableFunction(&JingleClientTest::OnClosed, |
| 100 &closed1)); |
| 101 bool closed2 = false; |
| 102 client_->Close(NewRunnableFunction(&JingleClientTest::OnClosed, |
| 103 &closed2)); |
| 104 thread_.Stop(); |
| 105 EXPECT_TRUE(closed1 && closed2); |
| 106 } |
| 107 |
| 108 } // namespace remoting |
OLD | NEW |