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