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

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

Powered by Google App Engine
This is Rietveld 408576698