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..f664cdaedbaf4e9fbd4b00ae1d80bec4130ebef9 |
--- /dev/null |
+++ b/components/arc/arc_bridge_service_unittest.cc |
@@ -0,0 +1,115 @@ |
+// 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; |
+ } |
+ |
+ 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() {} |
satorux1
2015/10/28 05:45:23
Initialize ready_ here with ready_(false)? unitial
Luis Héctor Chávez
2015/10/28 17:19:14
Done.
|
+ ~ArcBridgeTest() override {} |
+ |
+ void OnStateChanged(ArcBridgeService::State state) override { |
+ if (state == ArcBridgeService::READY) { |
+ ready_ = true; |
+ message_loop_.Quit(); |
+ } |
+ } |
+ |
+ bool IsReady() { return ready_; } |
+ |
+ 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()); |
+ EXPECT_TRUE(service_->Connect(handle, IPC::Channel::MODE_SERVER)); |
hidehiko
2015/10/28 17:48:45
It'd be good to note that, for testing, we do not
Luis Héctor Chávez
2015/10/28 21:06:32
Done.
|
+ 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(); |
satorux1
2015/10/28 05:45:23
do you need to reset fake_sender_ and service_ her
Luis Héctor Chávez
2015/10/28 17:19:14
It's not necessary, but I'd like to stop the threa
|
+ } |
+ |
+ 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) { |
+ EXPECT_FALSE(IsReady()); |
hidehiko
2015/10/28 17:48:45
nit: maybe ASSERT_FALSE, as it does not make sense
Luis Héctor Chávez
2015/10/28 21:06:32
Done.
|
+ |
+ EXPECT_TRUE(fake_sender_->Send(new ArcInstanceHostMsg_InstanceReady())); |
hidehiko
2015/10/28 17:48:45
ditto.
Luis Héctor Chávez
2015/10/28 21:06:32
Done.
|
+ |
+ base::RunLoop run_loop; |
+ run_loop.Run(); |
+ |
+ EXPECT_TRUE(IsReady()); |
+} |
+ |
+} // namespace arc |