| OLD | NEW |
| 1 // Copyright (c) 2011 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/listener/mediator_thread_impl.h" | 5 #include "jingle/notifier/listener/mediator_thread_impl.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "jingle/notifier/base/fake_base_task.h" | 10 #include "jingle/notifier/base/fake_base_task.h" |
| 11 #include "jingle/notifier/base/notifier_options.h" | 11 #include "jingle/notifier/base/notifier_options.h" |
| 12 #include "jingle/notifier/listener/notification_defines.h" | 12 #include "jingle/notifier/listener/notification_defines.h" |
| 13 #include "net/base/capturing_net_log.h" | 13 #include "net/base/capturing_net_log.h" |
| 14 #include "net/url_request/url_request_context_getter.h" | 14 #include "net/url_request/url_request_context_getter.h" |
| 15 #include "net/url_request/url_request_test_util.h" | 15 #include "net/url_request/url_request_test_util.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| 19 namespace notifier { | 19 namespace notifier { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 using ::testing::StrictMock; | 23 using ::testing::StrictMock; |
| 24 | 24 |
| 25 // TODO(sanjeevr): Move this to net_test_support. | |
| 26 // Used to return a dummy context. | |
| 27 class TestURLRequestContextGetter : public net::URLRequestContextGetter { | |
| 28 public: | |
| 29 TestURLRequestContextGetter() | |
| 30 : message_loop_proxy_(base::MessageLoopProxy::current()) { | |
| 31 } | |
| 32 virtual ~TestURLRequestContextGetter() { } | |
| 33 | |
| 34 // net::URLRequestContextGetter: | |
| 35 virtual net::URLRequestContext* GetURLRequestContext() { | |
| 36 if (!context_) | |
| 37 context_ = new TestURLRequestContext(); | |
| 38 return context_.get(); | |
| 39 } | |
| 40 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const { | |
| 41 return message_loop_proxy_; | |
| 42 } | |
| 43 | |
| 44 private: | |
| 45 scoped_refptr<net::URLRequestContext> context_; | |
| 46 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | |
| 47 }; | |
| 48 | |
| 49 class MockObserver : public MediatorThread::Observer { | 25 class MockObserver : public MediatorThread::Observer { |
| 50 public: | 26 public: |
| 51 MOCK_METHOD1(OnConnectionStateChange, void(bool)); | 27 MOCK_METHOD1(OnConnectionStateChange, void(bool)); |
| 52 MOCK_METHOD1(OnSubscriptionStateChange, void(bool)); | 28 MOCK_METHOD1(OnSubscriptionStateChange, void(bool)); |
| 53 MOCK_METHOD1(OnIncomingNotification, void(const Notification&)); | 29 MOCK_METHOD1(OnIncomingNotification, void(const Notification&)); |
| 54 MOCK_METHOD0(OnOutgoingNotification, void()); | 30 MOCK_METHOD0(OnOutgoingNotification, void()); |
| 55 }; | 31 }; |
| 56 | 32 |
| 57 } // namespace | 33 } // namespace |
| 58 | 34 |
| 59 class MediatorThreadTest : public testing::Test { | 35 class MediatorThreadTest : public testing::Test { |
| 60 protected: | 36 protected: |
| 61 MediatorThreadTest() { | 37 MediatorThreadTest() { |
| 62 notifier_options_.request_context_getter = | 38 notifier_options_.request_context_getter = |
| 63 new TestURLRequestContextGetter(); | 39 new TestURLRequestContextGetter(message_loop_.message_loop_proxy()); |
| 64 } | 40 } |
| 65 | 41 |
| 66 virtual ~MediatorThreadTest() {} | 42 virtual ~MediatorThreadTest() {} |
| 67 | 43 |
| 68 virtual void SetUp() { | 44 virtual void SetUp() { |
| 69 mediator_thread_.reset(new MediatorThreadImpl(notifier_options_)); | 45 mediator_thread_.reset(new MediatorThreadImpl(notifier_options_)); |
| 70 mediator_thread_->AddObserver(&mock_observer_); | 46 mediator_thread_->AddObserver(&mock_observer_); |
| 71 } | 47 } |
| 72 | 48 |
| 73 virtual void TearDown() { | 49 virtual void TearDown() { |
| 74 // Clear out any messages posted by MediatorThread's | 50 // Clear out any messages posted by MediatorThread's |
| 75 // destructor. | 51 // destructor. |
| 76 message_loop_.RunAllPending(); | 52 message_loop_.RunAllPending(); |
| 77 mediator_thread_->RemoveObserver(&mock_observer_); | 53 mediator_thread_->RemoveObserver(&mock_observer_); |
| 78 mediator_thread_.reset(); | 54 mediator_thread_.reset(); |
| 79 } | 55 } |
| 80 | 56 |
| 81 // Needed by TestURLRequestContextGetter. | 57 MessageLoop message_loop_; |
| 82 MessageLoopForIO message_loop_; | |
| 83 NotifierOptions notifier_options_; | 58 NotifierOptions notifier_options_; |
| 84 StrictMock<MockObserver> mock_observer_; | 59 StrictMock<MockObserver> mock_observer_; |
| 85 scoped_ptr<MediatorThreadImpl> mediator_thread_; | 60 scoped_ptr<MediatorThreadImpl> mediator_thread_; |
| 86 FakeBaseTask fake_base_task_; | 61 FakeBaseTask fake_base_task_; |
| 87 }; | 62 }; |
| 88 | 63 |
| 89 TEST_F(MediatorThreadTest, SendNotificationBasic) { | 64 TEST_F(MediatorThreadTest, SendNotificationBasic) { |
| 90 EXPECT_CALL(mock_observer_, OnConnectionStateChange(true)); | 65 EXPECT_CALL(mock_observer_, OnConnectionStateChange(true)); |
| 91 EXPECT_CALL(mock_observer_, OnOutgoingNotification()); | 66 EXPECT_CALL(mock_observer_, OnOutgoingNotification()); |
| 92 | 67 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 114 | 89 |
| 115 for (int i = 0; i < 5; ++i) { | 90 for (int i = 0; i < 5; ++i) { |
| 116 mediator_thread_->SendNotification(Notification()); | 91 mediator_thread_->SendNotification(Notification()); |
| 117 } | 92 } |
| 118 mediator_thread_->TriggerOnConnectForTest( | 93 mediator_thread_->TriggerOnConnectForTest( |
| 119 base::WeakPtr<buzz::XmppTaskParentInterface>()); | 94 base::WeakPtr<buzz::XmppTaskParentInterface>()); |
| 120 mediator_thread_->TriggerOnConnectForTest(fake_base_task_.AsWeakPtr()); | 95 mediator_thread_->TriggerOnConnectForTest(fake_base_task_.AsWeakPtr()); |
| 121 } | 96 } |
| 122 | 97 |
| 123 } // namespace notifier | 98 } // namespace notifier |
| OLD | NEW |