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