Index: components/arc/arc_bridge_service_unittest.cc |
diff --git a/components/arc/arc_bridge_service_unittest.cc b/components/arc/arc_bridge_service_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0fbe67b1ae23fda37070473acdf17d335a2ac4c5 |
--- /dev/null |
+++ b/components/arc/arc_bridge_service_unittest.cc |
@@ -0,0 +1,117 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/arc/arc_bridge_service.h" |
+ |
+#include "base/memory/ref_counted.h" |
+#include "base/run_loop.h" |
+#include "chromeos/arc/bridge/common/arc_host_messages.h" |
+#include "ipc/ipc_channel.h" |
+#include "ipc/ipc_channel_proxy.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace arc { |
+ |
+namespace { |
+ |
+// A fake sender that can connect to a specified IPC::ChannelHandle. |
+class IPCSenderFake : public IPC::Listener, |
+ public IPC::Sender { |
+ public: |
+ IPCSenderFake() |
+ : ipc_thread_("IPCSenderFake") { |
+ ipc_thread_.StartWithOptions( |
+ base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
+ } |
+ ~IPCSenderFake() override {} |
+ |
+ // Connects as a client to the specified |handle|. |
+ bool Connect(const IPC::ChannelHandle& handle) { |
+ ipc_channel_ = IPC::ChannelProxy::Create(handle, |
+ IPC::Channel::MODE_CLIENT, |
+ this, |
+ ipc_thread_.task_runner().get()); |
+ if (!ipc_channel_) |
+ return false; |
+ return true; |
oshima
2015/11/02 19:13:49
return ipc_channel_;
Luis Héctor Chávez
2015/11/04 17:49:19
Done.
|
+ } |
+ |
+ bool Send(IPC::Message* msg) override { |
+ return ipc_channel_->Send(msg); |
+ } |
+ |
+ bool OnMessageReceived(const IPC::Message& message) override { |
+ return true; |
+ } |
+ |
+ private: |
+ // Thread in which IPC messaging is performed. |
+ base::Thread ipc_thread_; |
+ |
+ // The channel through which messages are sent. |
+ scoped_ptr<IPC::ChannelProxy> ipc_channel_; |
+}; |
+ |
+} // namespace |
+ |
+class ArcBridgeTest : public testing::Test, |
+ public ArcBridgeService::Observer { |
+ public: |
+ ArcBridgeTest() : ready_(false) {} |
+ ~ArcBridgeTest() override {} |
+ |
+ void OnStateChanged(ArcBridgeService::State state) override { |
+ if (state == ArcBridgeService::READY) { |
+ ready_ = true; |
+ message_loop_.Quit(); |
+ } |
+ } |
+ |
+ 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.
|
+ |
+ protected: |
+ scoped_ptr<IPCSenderFake> fake_sender_; |
+ |
+ private: |
+ void SetUp() override { |
+ ready_ = false; |
+ service_.reset(new ArcBridgeService(message_loop_.task_runner())); |
+ |
+ service_->AddObserver(this); |
+ service_->SetEnabled(true); |
+ |
+ IPC::ChannelHandle handle(IPC::Channel::GenerateUniqueRandomChannelID()); |
+ // Connect directly to the specified channel instead of going through |
+ // D-Bus, since it is not available for tests. |
+ EXPECT_TRUE(service_->Connect(handle, IPC::Channel::MODE_SERVER)); |
+ fake_sender_.reset(new IPCSenderFake()); |
+ EXPECT_TRUE(fake_sender_); |
+ EXPECT_TRUE(fake_sender_->Connect(handle)); |
+ } |
+ |
+ void TearDown() override { |
+ fake_sender_.reset(); |
+ service_->RemoveObserver(this); |
+ service_.reset(); |
+ } |
+ |
+ bool ready_; |
+ base::MessageLoopForUI message_loop_; |
+ scoped_ptr<ArcBridgeService> service_; |
+}; |
+ |
+// Exercises the basic functionality of the ARC Bridge Service. A message from |
+// within the instance should cause the observer to be notified. |
+TEST_F(ArcBridgeTest, Basic) { |
+ ASSERT_FALSE(IsReady()); |
+ |
+ ASSERT_TRUE(fake_sender_->Send(new ArcInstanceHostMsg_InstanceReady())); |
+ |
+ base::RunLoop run_loop; |
+ run_loop.Run(); |
+ |
+ EXPECT_TRUE(IsReady()); |
+} |
+ |
+} // namespace arc |