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 <string> |
| 6 |
| 7 #include "base/compiler_specific.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "base/scoped_ptr.h" |
| 10 #include "base/task.h" |
| 11 #include "chrome/browser/sync/notifier/server_notifier_thread.h" |
| 12 #include "chrome/browser/sync/notifier/state_writer.h" |
| 13 #include "jingle/notifier/base/fake_base_task.h" |
| 14 #include "jingle/notifier/base/notifier_options.h" |
| 15 #include "talk/xmpp/xmppclientsettings.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace sync_notifier { |
| 20 |
| 21 class FakeServerNotifierThread : public ServerNotifierThread { |
| 22 public: |
| 23 FakeServerNotifierThread() |
| 24 : ServerNotifierThread(notifier::NotifierOptions(), "fake state", |
| 25 ALLOW_THIS_IN_INITIALIZER_LIST(this)) {} |
| 26 |
| 27 virtual ~FakeServerNotifierThread() {} |
| 28 |
| 29 virtual void Start() { |
| 30 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); |
| 31 ServerNotifierThread::Start(); |
| 32 worker_message_loop()->PostTask( |
| 33 FROM_HERE, |
| 34 NewRunnableMethod(this, |
| 35 &FakeServerNotifierThread::InitFakes)); |
| 36 } |
| 37 |
| 38 virtual void Logout() { |
| 39 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); |
| 40 worker_message_loop()->PostTask( |
| 41 FROM_HERE, |
| 42 NewRunnableMethod(this, |
| 43 &FakeServerNotifierThread::DestroyFakes)); |
| 44 ServerNotifierThread::Logout(); |
| 45 } |
| 46 |
| 47 // We prevent the real connection attempt from happening and use |
| 48 // SimulateConnection()/SimulateDisconnection() instead. |
| 49 virtual void Login(const buzz::XmppClientSettings& settings) { |
| 50 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); |
| 51 } |
| 52 |
| 53 // We pass ourselves as the StateWriter in the constructor, so shim |
| 54 // out WriteState() to prevent an infinite loop. |
| 55 virtual void WriteState(const std::string& state) { |
| 56 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); |
| 57 } |
| 58 |
| 59 void SimulateConnect() { |
| 60 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); |
| 61 worker_message_loop()->PostTask( |
| 62 FROM_HERE, |
| 63 NewRunnableMethod(this, |
| 64 &FakeServerNotifierThread::DoSimulateConnect)); |
| 65 } |
| 66 |
| 67 void SimulateDisconnect() { |
| 68 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); |
| 69 worker_message_loop()->PostTask( |
| 70 FROM_HERE, |
| 71 NewRunnableMethod(this, |
| 72 &FakeServerNotifierThread::DoSimulateDisconnect)); |
| 73 } |
| 74 |
| 75 private: |
| 76 void InitFakes() { |
| 77 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); |
| 78 fake_base_task_.reset(new notifier::FakeBaseTask()); |
| 79 } |
| 80 |
| 81 void DestroyFakes() { |
| 82 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); |
| 83 fake_base_task_.reset(); |
| 84 } |
| 85 |
| 86 void DoSimulateConnect() { |
| 87 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); |
| 88 OnConnect(fake_base_task_->AsWeakPtr()); |
| 89 } |
| 90 |
| 91 void DoSimulateDisconnect() { |
| 92 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); |
| 93 OnDisconnect(); |
| 94 } |
| 95 |
| 96 // Used only on the worker thread. |
| 97 scoped_ptr<notifier::FakeBaseTask> fake_base_task_; |
| 98 }; |
| 99 |
| 100 } // namespace sync_notifier |
| 101 |
| 102 DISABLE_RUNNABLE_METHOD_REFCOUNT(sync_notifier::FakeServerNotifierThread); |
| 103 |
| 104 namespace sync_notifier { |
| 105 |
| 106 namespace { |
| 107 |
| 108 class ServerNotifierThreadTest : public testing::Test { |
| 109 protected: |
| 110 MessageLoop message_loop_; |
| 111 }; |
| 112 |
| 113 TEST_F(ServerNotifierThreadTest, Basic) { |
| 114 FakeServerNotifierThread server_notifier_thread; |
| 115 |
| 116 server_notifier_thread.Start(); |
| 117 server_notifier_thread.Login(buzz::XmppClientSettings()); |
| 118 server_notifier_thread.SimulateConnect(); |
| 119 server_notifier_thread.ListenForUpdates(); |
| 120 server_notifier_thread.SubscribeForUpdates(std::vector<std::string>()); |
| 121 server_notifier_thread.Logout(); |
| 122 } |
| 123 |
| 124 TEST_F(ServerNotifierThreadTest, DisconnectBeforeListen) { |
| 125 FakeServerNotifierThread server_notifier_thread; |
| 126 |
| 127 server_notifier_thread.Start(); |
| 128 server_notifier_thread.Login(buzz::XmppClientSettings()); |
| 129 server_notifier_thread.ListenForUpdates(); |
| 130 server_notifier_thread.SubscribeForUpdates(std::vector<std::string>()); |
| 131 server_notifier_thread.Logout(); |
| 132 } |
| 133 |
| 134 TEST_F(ServerNotifierThreadTest, Disconnected) { |
| 135 FakeServerNotifierThread server_notifier_thread; |
| 136 |
| 137 server_notifier_thread.Start(); |
| 138 server_notifier_thread.Login(buzz::XmppClientSettings()); |
| 139 server_notifier_thread.SimulateConnect(); |
| 140 server_notifier_thread.SimulateDisconnect(); |
| 141 server_notifier_thread.ListenForUpdates(); |
| 142 server_notifier_thread.SubscribeForUpdates(std::vector<std::string>()); |
| 143 server_notifier_thread.Logout(); |
| 144 } |
| 145 |
| 146 } // namespace |
| 147 |
| 148 } // namespace sync_notifier |
OLD | NEW |