OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "components/arc/arc_bridge_service.h" |
| 6 |
| 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "chromeos/dbus/dbus_thread_manager.h" |
| 10 #include "components/arc/common/arc_host_messages.h" |
| 11 #include "ipc/ipc_channel.h" |
| 12 #include "ipc/ipc_channel_proxy.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace arc { |
| 16 |
| 17 namespace { |
| 18 |
| 19 // A fake sender that can connect to a specified IPC::ChannelHandle. |
| 20 class IPCSenderFake : public IPC::Listener, public IPC::Sender { |
| 21 public: |
| 22 explicit IPCSenderFake( |
| 23 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) |
| 24 : ipc_task_runner_(ipc_task_runner) {} |
| 25 ~IPCSenderFake() override {} |
| 26 |
| 27 // Connects as a client to the specified |handle|. |
| 28 bool Connect(const IPC::ChannelHandle& handle) { |
| 29 ipc_channel_ = IPC::ChannelProxy::Create(handle, IPC::Channel::MODE_CLIENT, |
| 30 this, ipc_task_runner_.get()); |
| 31 return ipc_channel_; |
| 32 } |
| 33 |
| 34 bool Send(IPC::Message* msg) override { return ipc_channel_->Send(msg); } |
| 35 |
| 36 bool OnMessageReceived(const IPC::Message& message) override { return true; } |
| 37 |
| 38 private: |
| 39 // Task runner on which ipc operations are performed. |
| 40 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_; |
| 41 |
| 42 // The channel through which messages are sent. |
| 43 scoped_ptr<IPC::ChannelProxy> ipc_channel_; |
| 44 |
| 45 DISALLOW_COPY_AND_ASSIGN(IPCSenderFake); |
| 46 }; |
| 47 |
| 48 } // namespace |
| 49 |
| 50 class ArcBridgeTest : public testing::Test, public ArcBridgeService::Observer { |
| 51 public: |
| 52 ArcBridgeTest() : ready_(false) {} |
| 53 ~ArcBridgeTest() override {} |
| 54 |
| 55 void OnStateChanged(ArcBridgeService::State state) override { |
| 56 state_ = state; |
| 57 switch (state) { |
| 58 case ArcBridgeService::State::READY: |
| 59 ready_ = true; |
| 60 break; |
| 61 |
| 62 case ArcBridgeService::State::STOPPED: |
| 63 message_loop_.PostTask(FROM_HERE, message_loop_.QuitWhenIdleClosure()); |
| 64 break; |
| 65 |
| 66 default: |
| 67 break; |
| 68 } |
| 69 } |
| 70 |
| 71 bool ready() const { return ready_; } |
| 72 |
| 73 ArcBridgeService::State state() const { return state_; } |
| 74 |
| 75 protected: |
| 76 scoped_ptr<IPCSenderFake> fake_sender_; |
| 77 |
| 78 scoped_ptr<ArcBridgeService> service_; |
| 79 |
| 80 private: |
| 81 void SetUp() override { |
| 82 chromeos::DBusThreadManager::Initialize(); |
| 83 |
| 84 ready_ = false; |
| 85 |
| 86 ipc_thread_.reset(new base::Thread("IPC thread")); |
| 87 ipc_thread_->StartWithOptions( |
| 88 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
| 89 service_.reset(new ArcBridgeService(ipc_thread_->task_runner(), |
| 90 message_loop_.task_runner())); |
| 91 |
| 92 service_->AddObserver(this); |
| 93 |
| 94 IPC::ChannelHandle handle(IPC::Channel::GenerateUniqueRandomChannelID()); |
| 95 // Testing code does not do all the steps that are done by regular |
| 96 // connection. In particular, it does not need to create a directory for |
| 97 // the socket, so manually set the state to CONNECTING. |
| 98 service_->SetState(ArcBridgeService::State::CONNECTING); |
| 99 // Connect directly to the specified channel instead of going through |
| 100 // D-Bus, since it is not available for tests. |
| 101 EXPECT_TRUE(service_->Connect(handle, IPC::Channel::MODE_SERVER)); |
| 102 // Testing code does also not send D-Bus messages, so set the state to |
| 103 // STARTING. |
| 104 service_->SetState(ArcBridgeService::State::STARTING); |
| 105 fake_sender_.reset(new IPCSenderFake(ipc_thread_->task_runner())); |
| 106 EXPECT_TRUE(fake_sender_); |
| 107 EXPECT_TRUE(fake_sender_->Connect(handle)); |
| 108 } |
| 109 |
| 110 void TearDown() override { |
| 111 fake_sender_.reset(); |
| 112 service_->RemoveObserver(this); |
| 113 service_.reset(); |
| 114 ipc_thread_.reset(); |
| 115 |
| 116 chromeos::DBusThreadManager::Shutdown(); |
| 117 } |
| 118 |
| 119 bool ready_; |
| 120 ArcBridgeService::State state_; |
| 121 base::MessageLoopForUI message_loop_; |
| 122 |
| 123 // Thread in which IPC messaging is performed. |
| 124 scoped_ptr<base::Thread> ipc_thread_; |
| 125 |
| 126 DISALLOW_COPY_AND_ASSIGN(ArcBridgeTest); |
| 127 }; |
| 128 |
| 129 // Shuts down the ArcBridgeService when it is ready. |
| 130 class ScopedShutdownWhenReady : public ArcBridgeService::Observer { |
| 131 public: |
| 132 ScopedShutdownWhenReady(ArcBridgeService* service) : service_(service) { |
| 133 service_->AddObserver(this); |
| 134 } |
| 135 |
| 136 ~ScopedShutdownWhenReady() override { service_->RemoveObserver(this); } |
| 137 |
| 138 void OnStateChanged(ArcBridgeService::State state) override { |
| 139 if (state == ArcBridgeService::State::READY) { |
| 140 service_->Shutdown(); |
| 141 } |
| 142 } |
| 143 |
| 144 private: |
| 145 ArcBridgeService* service_; |
| 146 |
| 147 DISALLOW_COPY_AND_ASSIGN(ScopedShutdownWhenReady); |
| 148 }; |
| 149 |
| 150 // Exercises the basic functionality of the ARC Bridge Service. A message from |
| 151 // within the instance should cause the observer to be notified. |
| 152 TEST_F(ArcBridgeTest, Basic) { |
| 153 ASSERT_FALSE(ready()); |
| 154 ASSERT_EQ(ArcBridgeService::State::STARTING, state()); |
| 155 |
| 156 ScopedShutdownWhenReady shutdown(service_.get()); |
| 157 |
| 158 ASSERT_TRUE(fake_sender_->Send(new ArcInstanceHostMsg_InstanceReady())); |
| 159 |
| 160 base::RunLoop run_loop; |
| 161 run_loop.Run(); |
| 162 |
| 163 EXPECT_TRUE(ready()); |
| 164 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); |
| 165 } |
| 166 |
| 167 // If the ArcBridgeService is shut down, it should be stopped, even |
| 168 // mid-startup. |
| 169 TEST_F(ArcBridgeTest, ShutdownMidStartup) { |
| 170 ASSERT_FALSE(ready()); |
| 171 |
| 172 ASSERT_EQ(ArcBridgeService::State::STARTING, state()); |
| 173 service_->Shutdown(); |
| 174 // Some machines can reach the STOPPED state immediately. |
| 175 ASSERT_TRUE(state() == ArcBridgeService::State::STOPPING || |
| 176 state() == ArcBridgeService::State::STOPPED); |
| 177 |
| 178 base::RunLoop run_loop; |
| 179 run_loop.Run(); |
| 180 |
| 181 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); |
| 182 } |
| 183 |
| 184 } // namespace arc |
OLD | NEW |