Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1368)

Side by Side Diff: components/arc/arc_bridge_service_unittest.cc

Issue 1412863004: arc-bridge: Add the ARC Bridge Service (Closed) Base URL: https://chromium.googlesource.com/a/chromium/src.git@master
Patch Set: Addressed feedback from Issue 1416313004 Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "chromeos/dbus/dbus_thread_manager.h"
11 #include "ipc/ipc_channel.h"
12 #include "ipc/ipc_channel_proxy.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace arc {
16
17 namespace {
18
19 // A fake sender that can connect to a specified IPC::ChannelHandle.
20 class IPCSenderFake : public IPC::Listener, public IPC::Sender {
21 public:
22 IPCSenderFake(
jochen (gone - plz use gerrit) 2015/11/12 23:40:59 explicit
Luis Héctor Chávez 2015/11/13 00:29:29 Done.
23 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner)
24 : ipc_task_runner_(ipc_task_runner) {}
25 ~IPCSenderFake() override {}
26
27 // Connects as a client to the specified |handle|.
28 bool Connect(const IPC::ChannelHandle& handle) {
29 ipc_channel_ = IPC::ChannelProxy::Create(handle, IPC::Channel::MODE_CLIENT,
30 this, ipc_task_runner_.get());
31 return ipc_channel_;
32 }
33
34 bool Send(IPC::Message* msg) override { return ipc_channel_->Send(msg); }
35
36 bool OnMessageReceived(const IPC::Message& message) override { return true; }
37
38 private:
39 // Task runner on which ipc operations are performed.
40 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_;
41
42 // The channel through which messages are sent.
43 scoped_ptr<IPC::ChannelProxy> ipc_channel_;
44
45 DISALLOW_COPY_AND_ASSIGN(IPCSenderFake);
46 };
47
48 } // namespace
49
50 class ArcBridgeTest : public testing::Test, public ArcBridgeService::Observer {
51 public:
52 ArcBridgeTest() : ready_(false) {}
53 ~ArcBridgeTest() override {}
54
55 void OnStateChanged(ArcBridgeService::State state) override {
56 state_ = state;
57 switch (state) {
58 case ArcBridgeService::State::READY:
59 ready_ = true;
60 break;
61
62 case ArcBridgeService::State::STOPPED:
63 message_loop_.PostTask(FROM_HERE, message_loop_.QuitWhenIdleClosure());
64 break;
65
66 default:
67 break;
68 }
69 }
70
71 bool ready() const { return ready_; }
72
73 ArcBridgeService::State state() const { return state_; }
74
75 protected:
76 scoped_ptr<IPCSenderFake> fake_sender_;
77
78 scoped_ptr<ArcBridgeService> service_;
79
80 private:
81 void SetUp() override {
82 chromeos::DBusThreadManager::Initialize();
83
84 ready_ = false;
85
86 ipc_thread_.reset(new base::Thread("IPC thread"));
87 ipc_thread_->StartWithOptions(
88 base::Thread::Options(base::MessageLoop::TYPE_IO, 0));
89 service_.reset(new ArcBridgeService(ipc_thread_->task_runner(),
90 message_loop_.task_runner()));
91
92 service_->AddObserver(this);
93
94 IPC::ChannelHandle handle(IPC::Channel::GenerateUniqueRandomChannelID());
95 // Testing code does not do all the steps that are done by regular
96 // connection. In particular, it does not need to create a directory for
97 // the socket, so manually set the state to CONNECTING.
98 service_->SetState(ArcBridgeService::State::CONNECTING);
99 // Connect directly to the specified channel instead of going through
100 // D-Bus, since it is not available for tests.
101 EXPECT_TRUE(service_->Connect(handle, IPC::Channel::MODE_SERVER));
102 // Testing code does also not send D-Bus messages, so set the state to
103 // STARTING.
104 service_->SetState(ArcBridgeService::State::STARTING);
105 fake_sender_.reset(new IPCSenderFake(ipc_thread_->task_runner()));
106 EXPECT_TRUE(fake_sender_);
107 EXPECT_TRUE(fake_sender_->Connect(handle));
108 }
109
110 void TearDown() override {
111 fake_sender_.reset();
112 service_->RemoveObserver(this);
113 service_.reset();
114 ipc_thread_.reset();
115
116 chromeos::DBusThreadManager::Shutdown();
117 }
118
119 bool ready_;
120 ArcBridgeService::State state_;
121 base::MessageLoopForUI message_loop_;
122
123 // Thread in which IPC messaging is performed.
124 scoped_ptr<base::Thread> ipc_thread_;
125 };
jochen (gone - plz use gerrit) 2015/11/12 23:40:59 disallow copy/assign
Luis Héctor Chávez 2015/11/13 00:29:29 Done.
126
127 // Shuts down the ArcBridgeService when it is ready.
128 class ScopedShutdownWhenReady : public ArcBridgeService::Observer {
129 public:
130 ScopedShutdownWhenReady(ArcBridgeService* service) : service_(service) {
131 service_->AddObserver(this);
132 }
133
134 ~ScopedShutdownWhenReady() override { service_->RemoveObserver(this); }
135
136 void OnStateChanged(ArcBridgeService::State state) override {
137 if (state == ArcBridgeService::State::READY) {
138 service_->Shutdown();
139 }
140 }
141
142 private:
143 ArcBridgeService* service_;
144 };
jochen (gone - plz use gerrit) 2015/11/12 23:40:59 disallow copy/assign
Luis Héctor Chávez 2015/11/13 00:29:29 Done.
145
146 // Exercises the basic functionality of the ARC Bridge Service. A message from
147 // within the instance should cause the observer to be notified.
148 TEST_F(ArcBridgeTest, Basic) {
149 ASSERT_FALSE(ready());
150 ASSERT_EQ(ArcBridgeService::State::STARTING, state());
151
152 ScopedShutdownWhenReady shutdown(service_.get());
153
154 ASSERT_TRUE(fake_sender_->Send(new ArcInstanceHostMsg_InstanceReady()));
155
156 base::RunLoop run_loop;
157 run_loop.Run();
158
159 EXPECT_TRUE(ready());
160 ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
161 }
162
163 // If the ArcBridgeService is shut down, it should be stopped, even
164 // mid-startup.
165 TEST_F(ArcBridgeTest, ShutdownMidStartup) {
166 ASSERT_FALSE(ready());
167
168 ASSERT_EQ(ArcBridgeService::State::STARTING, state());
169 service_->Shutdown();
170 // Some machines can reach the STOPPED state immediately.
171 ASSERT_TRUE(state() == ArcBridgeService::State::STOPPING ||
172 state() == ArcBridgeService::State::STOPPED);
173
174 base::RunLoop run_loop;
175 run_loop.Run();
176
177 ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
178 }
179
180 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698