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 if (!ipc_channel_) |
| 36 return false; |
| 37 return true; |
| 38 } |
| 39 |
| 40 bool Send(IPC::Message* msg) override { |
| 41 return ipc_channel_->Send(msg); |
| 42 } |
| 43 |
| 44 bool OnMessageReceived(const IPC::Message& message) override { |
| 45 return true; |
| 46 } |
| 47 |
| 48 private: |
| 49 // Thread in which IPC messaging is performed. |
| 50 base::Thread ipc_thread_; |
| 51 |
| 52 // The channel through which messages are sent. |
| 53 scoped_ptr<IPC::ChannelProxy> ipc_channel_; |
| 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 IsReady() { 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 EXPECT_TRUE(service_->Connect(handle, IPC::Channel::MODE_SERVER)); |
| 86 fake_sender_.reset(new IPCSenderFake()); |
| 87 EXPECT_TRUE(fake_sender_); |
| 88 EXPECT_TRUE(fake_sender_->Connect(handle)); |
| 89 } |
| 90 |
| 91 void TearDown() override { |
| 92 fake_sender_.reset(); |
| 93 service_->RemoveObserver(this); |
| 94 service_.reset(); |
| 95 } |
| 96 |
| 97 bool ready_; |
| 98 base::MessageLoopForUI message_loop_; |
| 99 scoped_ptr<ArcBridgeService> service_; |
| 100 }; |
| 101 |
| 102 // Exercises the basic functionality of the ARC Bridge Service. A message from |
| 103 // within the instance should cause the observer to be notified. |
| 104 TEST_F(ArcBridgeTest, Basic) { |
| 105 EXPECT_FALSE(IsReady()); |
| 106 |
| 107 EXPECT_TRUE(fake_sender_->Send(new ArcInstanceHostMsg_InstanceReady())); |
| 108 |
| 109 base::RunLoop run_loop; |
| 110 run_loop.Run(); |
| 111 |
| 112 EXPECT_TRUE(IsReady()); |
| 113 } |
| 114 |
| 115 } // namespace arc |
OLD | NEW |