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/arc/bridge/common/arc_host_messages.h" |
| 10 #include "ipc/ipc_channel.h" |
| 11 #include "ipc/ipc_channel_proxy.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace arc { |
| 15 |
| 16 namespace { |
| 17 |
| 18 // A fake sender that can connect to a specified IPC::ChannelHandle. |
| 19 class IPCSenderFake : public IPC::Listener, |
| 20 public IPC::Sender { |
| 21 public: |
| 22 IPCSenderFake() |
| 23 : ipc_thread_("IPCSenderFake") { |
| 24 ipc_thread_.StartWithOptions( |
| 25 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
| 26 } |
| 27 ~IPCSenderFake() override {} |
| 28 |
| 29 // Connects as a client to the specified |handle|. |
| 30 bool Connect(const IPC::ChannelHandle& handle) { |
| 31 ipc_channel_ = IPC::ChannelProxy::Create(handle, |
| 32 IPC::Channel::MODE_CLIENT, |
| 33 this, |
| 34 ipc_thread_.task_runner().get()); |
| 35 return ipc_channel_; |
| 36 } |
| 37 |
| 38 bool Send(IPC::Message* msg) override { |
| 39 return ipc_channel_->Send(msg); |
| 40 } |
| 41 |
| 42 bool OnMessageReceived(const IPC::Message& message) override { |
| 43 return true; |
| 44 } |
| 45 |
| 46 private: |
| 47 // Thread in which IPC messaging is performed. |
| 48 base::Thread ipc_thread_; |
| 49 |
| 50 // The channel through which messages are sent. |
| 51 scoped_ptr<IPC::ChannelProxy> ipc_channel_; |
| 52 |
| 53 DISALLOW_COPY_AND_ASSIGN(IPCSenderFake); |
| 54 }; |
| 55 |
| 56 } // namespace |
| 57 |
| 58 class ArcBridgeTest : public testing::Test, |
| 59 public ArcBridgeService::Observer { |
| 60 public: |
| 61 ArcBridgeTest() : ready_(false) {} |
| 62 ~ArcBridgeTest() override {} |
| 63 |
| 64 void OnStateChanged(ArcBridgeService::State state) override { |
| 65 if (state == ArcBridgeService::READY) { |
| 66 ready_ = true; |
| 67 message_loop_.Quit(); |
| 68 } |
| 69 } |
| 70 |
| 71 bool ready() const { return ready_; } |
| 72 |
| 73 protected: |
| 74 scoped_ptr<IPCSenderFake> fake_sender_; |
| 75 |
| 76 private: |
| 77 void SetUp() override { |
| 78 ready_ = false; |
| 79 service_.reset(new ArcBridgeService(message_loop_.task_runner())); |
| 80 |
| 81 service_->AddObserver(this); |
| 82 service_->SetEnabled(true); |
| 83 |
| 84 IPC::ChannelHandle handle(IPC::Channel::GenerateUniqueRandomChannelID()); |
| 85 // Testing code does not do all the steps that are done by regular |
| 86 // connection. In particular, it does not need to create a directory for |
| 87 // the socket, so manually set the state to CONNECTING. |
| 88 service_->SetState(ArcBridgeService::CONNECTING); |
| 89 // Connect directly to the specified channel instead of going through |
| 90 // D-Bus, since it is not available for tests. |
| 91 EXPECT_TRUE(service_->Connect(handle, IPC::Channel::MODE_SERVER)); |
| 92 // Testing code does also not send D-Bus messages, so set the state to |
| 93 // STARTING. |
| 94 service_->SetState(ArcBridgeService::STARTING); |
| 95 fake_sender_.reset(new IPCSenderFake()); |
| 96 EXPECT_TRUE(fake_sender_); |
| 97 EXPECT_TRUE(fake_sender_->Connect(handle)); |
| 98 } |
| 99 |
| 100 void TearDown() override { |
| 101 fake_sender_.reset(); |
| 102 service_->RemoveObserver(this); |
| 103 service_.reset(); |
| 104 } |
| 105 |
| 106 bool ready_; |
| 107 base::MessageLoopForUI message_loop_; |
| 108 scoped_ptr<ArcBridgeService> service_; |
| 109 }; |
| 110 |
| 111 // Exercises the basic functionality of the ARC Bridge Service. A message from |
| 112 // within the instance should cause the observer to be notified. |
| 113 TEST_F(ArcBridgeTest, Basic) { |
| 114 ASSERT_FALSE(ready()); |
| 115 |
| 116 ASSERT_TRUE(fake_sender_->Send(new ArcInstanceHostMsg_InstanceReady())); |
| 117 |
| 118 base::RunLoop run_loop; |
| 119 run_loop.Run(); |
| 120 |
| 121 EXPECT_TRUE(ready()); |
| 122 } |
| 123 |
| 124 } // namespace arc |
OLD | NEW |