Chromium Code Reviews| 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; | |
|
oshima
2015/11/02 19:13:49
return ipc_channel_;
Luis Héctor Chávez
2015/11/04 17:49:19
Done.
| |
| 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_; } | |
|
oshima
2015/11/02 19:13:49
bool ready() const {
or
rename variable to is_re
Luis Héctor Chávez
2015/11/04 17:49:19
Done.
| |
| 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 // Connect directly to the specified channel instead of going through | |
| 86 // D-Bus, since it is not available for tests. | |
| 87 EXPECT_TRUE(service_->Connect(handle, IPC::Channel::MODE_SERVER)); | |
| 88 fake_sender_.reset(new IPCSenderFake()); | |
| 89 EXPECT_TRUE(fake_sender_); | |
| 90 EXPECT_TRUE(fake_sender_->Connect(handle)); | |
| 91 } | |
| 92 | |
| 93 void TearDown() override { | |
| 94 fake_sender_.reset(); | |
| 95 service_->RemoveObserver(this); | |
| 96 service_.reset(); | |
| 97 } | |
| 98 | |
| 99 bool ready_; | |
| 100 base::MessageLoopForUI message_loop_; | |
| 101 scoped_ptr<ArcBridgeService> service_; | |
| 102 }; | |
| 103 | |
| 104 // Exercises the basic functionality of the ARC Bridge Service. A message from | |
| 105 // within the instance should cause the observer to be notified. | |
| 106 TEST_F(ArcBridgeTest, Basic) { | |
| 107 ASSERT_FALSE(IsReady()); | |
| 108 | |
| 109 ASSERT_TRUE(fake_sender_->Send(new ArcInstanceHostMsg_InstanceReady())); | |
| 110 | |
| 111 base::RunLoop run_loop; | |
| 112 run_loop.Run(); | |
| 113 | |
| 114 EXPECT_TRUE(IsReady()); | |
| 115 } | |
| 116 | |
| 117 } // namespace arc | |
| OLD | NEW |