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

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: Fixed chrome/browser/ui/BUILD.gn 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(
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_->SetEnabled(true);
93 service_->AddObserver(this);
94
95 IPC::ChannelHandle handle(IPC::Channel::GenerateUniqueRandomChannelID());
96 // Testing code does not do all the steps that are done by regular
97 // connection. In particular, it does not need to create a directory for
98 // the socket, so manually set the state to CONNECTING.
99 service_->SetState(ArcBridgeService::State::CONNECTING);
100 // Connect directly to the specified channel instead of going through
101 // D-Bus, since it is not available for tests.
102 EXPECT_TRUE(service_->Connect(handle, IPC::Channel::MODE_SERVER));
103 // Testing code does also not send D-Bus messages, so set the state to
104 // STARTING.
105 service_->SetState(ArcBridgeService::State::STARTING);
106 fake_sender_.reset(new IPCSenderFake(ipc_thread_->task_runner()));
107 EXPECT_TRUE(fake_sender_);
108 EXPECT_TRUE(fake_sender_->Connect(handle));
109 }
110
111 void TearDown() override {
112 fake_sender_.reset();
113 service_->RemoveObserver(this);
114 service_.reset();
115 ipc_thread_.reset();
116
117 chromeos::DBusThreadManager::Shutdown();
118 }
119
120 bool ready_;
121 ArcBridgeService::State state_;
122 base::MessageLoopForUI message_loop_;
123
124 // Thread in which IPC messaging is performed.
125 scoped_ptr<base::Thread> ipc_thread_;
126 };
127
128 // Shuts down the ArcBridgeService when it is ready.
129 class ScopedShutdownWhenReady : public ArcBridgeService::Observer {
130 public:
131 ScopedShutdownWhenReady(ArcBridgeService* service) : service_(service) {
132 service_->AddObserver(this);
133 }
134
135 ~ScopedShutdownWhenReady() override { service_->RemoveObserver(this); }
136
137 void OnStateChanged(ArcBridgeService::State state) override {
138 if (state == ArcBridgeService::State::READY) {
139 service_->Shutdown();
140 }
141 }
142
143 private:
144 ArcBridgeService* service_;
145 };
146
147 // Exercises the basic functionality of the ARC Bridge Service. A message from
148 // within the instance should cause the observer to be notified.
149 TEST_F(ArcBridgeTest, Basic) {
150 ASSERT_FALSE(ready());
151 ASSERT_EQ(ArcBridgeService::State::STARTING, state());
152
153 ScopedShutdownWhenReady shutdown(service_.get());
154
155 ASSERT_TRUE(fake_sender_->Send(new ArcInstanceHostMsg_InstanceReady()));
156
157 base::RunLoop run_loop;
158 run_loop.Run();
159
160 EXPECT_TRUE(ready());
161 ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
162 }
163
164 // If the ArcBridgeService is disabled, it should be stopped.
165 TEST_F(ArcBridgeTest, ShutdownWhenDisabled) {
166 ASSERT_FALSE(ready());
167
168 ASSERT_EQ(ArcBridgeService::State::STARTING, state());
169 service_->SetEnabled(false);
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