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

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

Issue 2574013003: Refactor ArcSessionRunner part 1. (Closed)
Patch Set: rebase to the split CL Created 4 years 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 <memory>
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback_helpers.h"
10 #include "base/macros.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/run_loop.h"
13 #include "base/single_thread_task_runner.h"
14 #include "chromeos/dbus/dbus_thread_manager.h"
15 #include "components/arc/arc_bridge_service_impl.h"
16 #include "components/arc/test/fake_arc_session.h"
17 #include "mojo/public/cpp/system/message_pipe.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace arc {
21
22 namespace {
23
24 class DummyObserver : public ArcSessionObserver {};
25
26 } // namespace
27
28 class ArcBridgeTest : public testing::Test, public ArcSessionObserver {
29 public:
30 ArcBridgeTest() = default;
31
32 void SetUp() override {
33 chromeos::DBusThreadManager::Initialize();
34
35 stop_reason_ = StopReason::SHUTDOWN;
36
37 // We inject FakeArcSession here so we do not need task_runner.
38 service_.reset(new ArcBridgeServiceImpl(nullptr));
39 service_->SetArcSessionFactoryForTesting(
40 base::Bind(FakeArcSession::Create));
41 service_->AddObserver(this);
42 }
43
44 void TearDown() override {
45 service_->RemoveObserver(this);
46 service_.reset();
47
48 chromeos::DBusThreadManager::Shutdown();
49 }
50
51 ArcBridgeServiceImpl* arc_bridge_service() { return service_.get(); }
52
53 FakeArcSession* arc_session() const {
54 return static_cast<FakeArcSession*>(service_->GetArcSessionForTesting());
55 }
56
57 StopReason stop_reason() { return stop_reason_; }
58
59 static std::unique_ptr<ArcSession> CreateSuspendedArcSession() {
60 auto arc_session = base::MakeUnique<FakeArcSession>();
61 arc_session->SuspendBoot();
62 return std::move(arc_session);
63 }
64
65 static std::unique_ptr<ArcSession> CreateBootFailureArcSession(
66 StopReason reason) {
67 auto arc_session = base::MakeUnique<FakeArcSession>();
68 arc_session->EnableBootFailureEmulation(reason);
69 return std::move(arc_session);
70 }
71
72 private:
73 // ArcSessionObserver:
74 void OnSessionStopped(StopReason stop_reason) override {
75 // The instance is already destructed in ArcBridgeServiceImpl::OnStopped().
76 stop_reason_ = stop_reason;
77 }
78
79 StopReason stop_reason_;
80 std::unique_ptr<ArcBridgeServiceImpl> service_;
81 base::MessageLoopForUI message_loop_;
82
83 DISALLOW_COPY_AND_ASSIGN(ArcBridgeTest);
84 };
85
86 // Exercises the basic functionality of the ARC Bridge Service. Observer should
87 // be notified.
88 TEST_F(ArcBridgeTest, Basic) {
89 class Observer : public ArcSessionObserver {
90 public:
91 Observer() = default;
92
93 bool IsReadyCalled() { return ready_called_; }
94 bool IsStoppedCalled() { return stopped_called_; }
95
96 // ArcSessionObserver:
97 void OnSessionReady() override { ready_called_ = true; }
98 void OnSessionStopped(StopReason stop_reason) override {
99 stopped_called_ = true;
100 }
101
102 private:
103 bool ready_called_ = false;
104 bool stopped_called_ = false;
105
106 DISALLOW_COPY_AND_ASSIGN(Observer);
107 };
108
109 Observer observer;
110 arc_bridge_service()->AddObserver(&observer);
111 base::ScopedClosureRunner teardown(base::Bind(
112 [](ArcBridgeService* arc_bridge_service, Observer* observer) {
113 arc_bridge_service->RemoveObserver(observer);
114 },
115 arc_bridge_service(), &observer));
116
117 EXPECT_TRUE(arc_bridge_service()->stopped());
118
119 arc_bridge_service()->RequestStart();
120 EXPECT_TRUE(arc_bridge_service()->ready());
121 EXPECT_TRUE(observer.IsReadyCalled());
122
123 arc_bridge_service()->RequestStop();
124 EXPECT_TRUE(arc_bridge_service()->stopped());
125 EXPECT_TRUE(observer.IsStoppedCalled());
126 }
127
128 // If the ArcBridgeService accepts a request to stop ARC instance, it should
129 // stop it, even mid-startup.
130 TEST_F(ArcBridgeTest, StopMidStartup) {
131 arc_bridge_service()->SetArcSessionFactoryForTesting(
132 base::Bind(ArcBridgeTest::CreateSuspendedArcSession));
133 EXPECT_TRUE(arc_bridge_service()->stopped());
134
135 arc_bridge_service()->RequestStart();
136 EXPECT_FALSE(arc_bridge_service()->stopped());
137 EXPECT_FALSE(arc_bridge_service()->ready());
138
139 arc_bridge_service()->RequestStop();
140 EXPECT_TRUE(arc_bridge_service()->stopped());
141 }
142
143 // If the boot procedure is failed, then restarting mechanism should not
144 // triggered.
145 TEST_F(ArcBridgeTest, BootFailure) {
146 arc_bridge_service()->SetArcSessionFactoryForTesting(
147 base::Bind(ArcBridgeTest::CreateBootFailureArcSession,
148 StopReason::GENERIC_BOOT_FAILURE));
149 EXPECT_TRUE(arc_bridge_service()->stopped());
150
151 arc_bridge_service()->RequestStart();
152 EXPECT_EQ(StopReason::GENERIC_BOOT_FAILURE, stop_reason());
153 EXPECT_TRUE(arc_bridge_service()->stopped());
154 }
155
156 // If the instance is stopped, it should be re-started.
157 TEST_F(ArcBridgeTest, Restart) {
158 arc_bridge_service()->SetRestartDelayForTesting(base::TimeDelta());
159 EXPECT_TRUE(arc_bridge_service()->stopped());
160
161 arc_bridge_service()->RequestStart();
162 EXPECT_TRUE(arc_bridge_service()->ready());
163
164 // Simulate a connection loss.
165 ASSERT_TRUE(arc_session());
166 arc_session()->StopWithReason(StopReason::CRASH);
167 EXPECT_TRUE(arc_bridge_service()->stopped());
168 base::RunLoop().RunUntilIdle();
169 EXPECT_TRUE(arc_bridge_service()->ready());
170
171 arc_bridge_service()->RequestStop();
172 EXPECT_TRUE(arc_bridge_service()->stopped());
173 }
174
175 // Makes sure OnSessionStopped is called on stop.
176 TEST_F(ArcBridgeTest, OnSessionStopped) {
177 arc_bridge_service()->SetRestartDelayForTesting(base::TimeDelta());
178 EXPECT_TRUE(arc_bridge_service()->stopped());
179
180 arc_bridge_service()->RequestStart();
181 EXPECT_TRUE(arc_bridge_service()->ready());
182
183 // Simulate boot failure.
184 ASSERT_TRUE(arc_session());
185 arc_session()->StopWithReason(StopReason::GENERIC_BOOT_FAILURE);
186 EXPECT_EQ(StopReason::GENERIC_BOOT_FAILURE, stop_reason());
187 EXPECT_TRUE(arc_bridge_service()->stopped());
188 base::RunLoop().RunUntilIdle();
189 EXPECT_TRUE(arc_bridge_service()->ready());
190
191 // Simulate crash.
192 ASSERT_TRUE(arc_session());
193 arc_session()->StopWithReason(StopReason::CRASH);
194 EXPECT_EQ(StopReason::CRASH, stop_reason());
195 EXPECT_TRUE(arc_bridge_service()->stopped());
196 base::RunLoop().RunUntilIdle();
197 EXPECT_TRUE(arc_bridge_service()->ready());
198
199 // Graceful stop.
200 arc_bridge_service()->RequestStop();
201 EXPECT_EQ(StopReason::SHUTDOWN, stop_reason());
202 EXPECT_TRUE(arc_bridge_service()->stopped());
203 }
204
205 TEST_F(ArcBridgeTest, Shutdown) {
206 arc_bridge_service()->SetRestartDelayForTesting(base::TimeDelta());
207 EXPECT_TRUE(arc_bridge_service()->stopped());
208
209 arc_bridge_service()->RequestStart();
210 EXPECT_TRUE(arc_bridge_service()->ready());
211
212 // Simulate shutdown.
213 arc_bridge_service()->OnShutdown();
214 EXPECT_EQ(StopReason::SHUTDOWN, stop_reason());
215 EXPECT_TRUE(arc_bridge_service()->stopped());
216 }
217
218 // Removing the same observer more than once should be okay.
219 TEST_F(ArcBridgeTest, RemoveObserverTwice) {
220 EXPECT_TRUE(arc_bridge_service()->stopped());
221
222 DummyObserver dummy_observer;
223 arc_bridge_service()->AddObserver(&dummy_observer);
224 // Call RemoveObserver() twice.
225 arc_bridge_service()->RemoveObserver(&dummy_observer);
226 arc_bridge_service()->RemoveObserver(&dummy_observer);
227 }
228
229 // Removing an unknown observer should be allowed.
230 TEST_F(ArcBridgeTest, RemoveUnknownObserver) {
231 EXPECT_TRUE(arc_bridge_service()->stopped());
232
233 DummyObserver dummy_observer;
234 arc_bridge_service()->RemoveObserver(&dummy_observer);
235 }
236
237 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698