| 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" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 } // namespace | 27 } // namespace |
| 28 | 28 |
| 29 class ArcSessionRunnerTest : public testing::Test, public ArcSessionObserver { | 29 class ArcSessionRunnerTest : public testing::Test, public ArcSessionObserver { |
| 30 public: | 30 public: |
| 31 ArcSessionRunnerTest() = default; | 31 ArcSessionRunnerTest() = default; |
| 32 | 32 |
| 33 void SetUp() override { | 33 void SetUp() override { |
| 34 chromeos::DBusThreadManager::Initialize(); | 34 chromeos::DBusThreadManager::Initialize(); |
| 35 | 35 |
| 36 stop_reason_ = StopReason::SHUTDOWN; | 36 stop_reason_ = StopReason::SHUTDOWN; |
| 37 restarting_ = false; |
| 37 | 38 |
| 38 // We inject FakeArcSession here so we do not need task_runner. | 39 // We inject FakeArcSession here so we do not need task_runner. |
| 39 arc_session_runner_ = | 40 arc_session_runner_ = |
| 40 base::MakeUnique<ArcSessionRunner>(base::Bind(FakeArcSession::Create)); | 41 base::MakeUnique<ArcSessionRunner>(base::Bind(FakeArcSession::Create)); |
| 41 arc_session_runner_->AddObserver(this); | 42 arc_session_runner_->AddObserver(this); |
| 42 } | 43 } |
| 43 | 44 |
| 44 void TearDown() override { | 45 void TearDown() override { |
| 45 arc_session_runner_->RemoveObserver(this); | 46 arc_session_runner_->RemoveObserver(this); |
| 46 arc_session_runner_.reset(); | 47 arc_session_runner_.reset(); |
| 47 | 48 |
| 48 chromeos::DBusThreadManager::Shutdown(); | 49 chromeos::DBusThreadManager::Shutdown(); |
| 49 } | 50 } |
| 50 | 51 |
| 51 ArcSessionRunner* arc_session_runner() { return arc_session_runner_.get(); } | 52 ArcSessionRunner* arc_session_runner() { return arc_session_runner_.get(); } |
| 52 | 53 |
| 53 FakeArcSession* arc_session() { | 54 FakeArcSession* arc_session() { |
| 54 return static_cast<FakeArcSession*>( | 55 return static_cast<FakeArcSession*>( |
| 55 arc_session_runner_->GetArcSessionForTesting()); | 56 arc_session_runner_->GetArcSessionForTesting()); |
| 56 } | 57 } |
| 57 | 58 |
| 58 StopReason stop_reason() { return stop_reason_; } | 59 StopReason stop_reason() { return stop_reason_; } |
| 60 bool restarting() { return restarting_; } |
| 59 | 61 |
| 60 void ResetArcSessionFactory( | 62 void ResetArcSessionFactory( |
| 61 const ArcSessionRunner::ArcSessionFactory& factory) { | 63 const ArcSessionRunner::ArcSessionFactory& factory) { |
| 62 arc_session_runner_->RemoveObserver(this); | 64 arc_session_runner_->RemoveObserver(this); |
| 63 arc_session_runner_ = base::MakeUnique<ArcSessionRunner>(factory); | 65 arc_session_runner_ = base::MakeUnique<ArcSessionRunner>(factory); |
| 64 arc_session_runner_->AddObserver(this); | 66 arc_session_runner_->AddObserver(this); |
| 65 } | 67 } |
| 66 | 68 |
| 67 static std::unique_ptr<ArcSession> CreateSuspendedArcSession() { | 69 static std::unique_ptr<ArcSession> CreateSuspendedArcSession() { |
| 68 auto arc_session = base::MakeUnique<FakeArcSession>(); | 70 auto arc_session = base::MakeUnique<FakeArcSession>(); |
| 69 arc_session->SuspendBoot(); | 71 arc_session->SuspendBoot(); |
| 70 return std::move(arc_session); | 72 return std::move(arc_session); |
| 71 } | 73 } |
| 72 | 74 |
| 73 static std::unique_ptr<ArcSession> CreateBootFailureArcSession( | 75 static std::unique_ptr<ArcSession> CreateBootFailureArcSession( |
| 74 StopReason reason) { | 76 StopReason reason) { |
| 75 auto arc_session = base::MakeUnique<FakeArcSession>(); | 77 auto arc_session = base::MakeUnique<FakeArcSession>(); |
| 76 arc_session->EnableBootFailureEmulation(reason); | 78 arc_session->EnableBootFailureEmulation(reason); |
| 77 return std::move(arc_session); | 79 return std::move(arc_session); |
| 78 } | 80 } |
| 79 | 81 |
| 80 private: | 82 private: |
| 81 // ArcSessionObserver: | 83 // ArcSessionObserver: |
| 82 void OnSessionStopped(StopReason stop_reason) override { | 84 void OnSessionStopped(StopReason stop_reason, bool restarting) override { |
| 83 // The instance is already destructed in | 85 // The instance is already destructed in |
| 84 // ArcSessionRunner::OnSessionStopped(). | 86 // ArcSessionRunner::OnSessionStopped(). |
| 85 stop_reason_ = stop_reason; | 87 stop_reason_ = stop_reason; |
| 88 restarting_ = restarting; |
| 86 } | 89 } |
| 87 | 90 |
| 88 StopReason stop_reason_; | 91 StopReason stop_reason_; |
| 92 bool restarting_; |
| 89 std::unique_ptr<ArcSessionRunner> arc_session_runner_; | 93 std::unique_ptr<ArcSessionRunner> arc_session_runner_; |
| 90 base::MessageLoopForUI message_loop_; | 94 base::MessageLoopForUI message_loop_; |
| 91 | 95 |
| 92 DISALLOW_COPY_AND_ASSIGN(ArcSessionRunnerTest); | 96 DISALLOW_COPY_AND_ASSIGN(ArcSessionRunnerTest); |
| 93 }; | 97 }; |
| 94 | 98 |
| 95 // Exercises the basic functionality of the ArcSessionRunner. Observer should | 99 // Exercises the basic functionality of the ArcSessionRunner. Observer should |
| 96 // be notified. | 100 // be notified. |
| 97 TEST_F(ArcSessionRunnerTest, Basic) { | 101 TEST_F(ArcSessionRunnerTest, Basic) { |
| 98 class Observer : public ArcSessionObserver { | 102 class Observer : public ArcSessionObserver { |
| 99 public: | 103 public: |
| 100 Observer() = default; | 104 Observer() = default; |
| 101 | 105 |
| 102 bool ready_called() const { return ready_called_; } | 106 bool ready_called() const { return ready_called_; } |
| 103 bool stopped_called() const { return stopped_called_; } | 107 bool stopped_called() const { return stopped_called_; } |
| 104 | 108 |
| 105 // ArcSessionObserver: | 109 // ArcSessionObserver: |
| 106 void OnSessionReady() override { ready_called_ = true; } | 110 void OnSessionReady() override { ready_called_ = true; } |
| 107 void OnSessionStopped(StopReason reason) override { | 111 void OnSessionStopped(StopReason reason, bool restarting) override { |
| 108 stopped_called_ = true; | 112 stopped_called_ = true; |
| 109 } | 113 } |
| 110 | 114 |
| 111 private: | 115 private: |
| 112 bool ready_called_ = false; | 116 bool ready_called_ = false; |
| 113 bool stopped_called_ = false; | 117 bool stopped_called_ = false; |
| 114 | 118 |
| 115 DISALLOW_COPY_AND_ASSIGN(Observer); | 119 DISALLOW_COPY_AND_ASSIGN(Observer); |
| 116 }; | 120 }; |
| 117 | 121 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 arc_session_runner()->SetRestartDelayForTesting(base::TimeDelta()); | 190 arc_session_runner()->SetRestartDelayForTesting(base::TimeDelta()); |
| 187 EXPECT_TRUE(arc_session_runner()->IsStopped()); | 191 EXPECT_TRUE(arc_session_runner()->IsStopped()); |
| 188 | 192 |
| 189 arc_session_runner()->RequestStart(); | 193 arc_session_runner()->RequestStart(); |
| 190 EXPECT_TRUE(arc_session_runner()->IsRunning()); | 194 EXPECT_TRUE(arc_session_runner()->IsRunning()); |
| 191 | 195 |
| 192 // Simulate boot failure. | 196 // Simulate boot failure. |
| 193 ASSERT_TRUE(arc_session()); | 197 ASSERT_TRUE(arc_session()); |
| 194 arc_session()->StopWithReason(StopReason::GENERIC_BOOT_FAILURE); | 198 arc_session()->StopWithReason(StopReason::GENERIC_BOOT_FAILURE); |
| 195 EXPECT_EQ(StopReason::GENERIC_BOOT_FAILURE, stop_reason()); | 199 EXPECT_EQ(StopReason::GENERIC_BOOT_FAILURE, stop_reason()); |
| 200 EXPECT_TRUE(restarting()); |
| 196 EXPECT_TRUE(arc_session_runner()->IsStopped()); | 201 EXPECT_TRUE(arc_session_runner()->IsStopped()); |
| 197 base::RunLoop().RunUntilIdle(); | 202 base::RunLoop().RunUntilIdle(); |
| 198 EXPECT_TRUE(arc_session_runner()->IsRunning()); | 203 EXPECT_TRUE(arc_session_runner()->IsRunning()); |
| 199 | 204 |
| 200 // Simulate crash. | 205 // Simulate crash. |
| 201 ASSERT_TRUE(arc_session()); | 206 ASSERT_TRUE(arc_session()); |
| 202 arc_session()->StopWithReason(StopReason::CRASH); | 207 arc_session()->StopWithReason(StopReason::CRASH); |
| 203 EXPECT_EQ(StopReason::CRASH, stop_reason()); | 208 EXPECT_EQ(StopReason::CRASH, stop_reason()); |
| 209 EXPECT_TRUE(restarting()); |
| 204 EXPECT_TRUE(arc_session_runner()->IsStopped()); | 210 EXPECT_TRUE(arc_session_runner()->IsStopped()); |
| 205 base::RunLoop().RunUntilIdle(); | 211 base::RunLoop().RunUntilIdle(); |
| 206 EXPECT_TRUE(arc_session_runner()->IsRunning()); | 212 EXPECT_TRUE(arc_session_runner()->IsRunning()); |
| 207 | 213 |
| 208 // Graceful stop. | 214 // Graceful stop. |
| 209 arc_session_runner()->RequestStop(); | 215 arc_session_runner()->RequestStop(); |
| 210 EXPECT_EQ(StopReason::SHUTDOWN, stop_reason()); | 216 EXPECT_EQ(StopReason::SHUTDOWN, stop_reason()); |
| 217 EXPECT_FALSE(restarting()); |
| 211 EXPECT_TRUE(arc_session_runner()->IsStopped()); | 218 EXPECT_TRUE(arc_session_runner()->IsStopped()); |
| 212 } | 219 } |
| 213 | 220 |
| 214 TEST_F(ArcSessionRunnerTest, Shutdown) { | 221 TEST_F(ArcSessionRunnerTest, Shutdown) { |
| 215 arc_session_runner()->SetRestartDelayForTesting(base::TimeDelta()); | 222 arc_session_runner()->SetRestartDelayForTesting(base::TimeDelta()); |
| 216 EXPECT_TRUE(arc_session_runner()->IsStopped()); | 223 EXPECT_TRUE(arc_session_runner()->IsStopped()); |
| 217 | 224 |
| 218 arc_session_runner()->RequestStart(); | 225 arc_session_runner()->RequestStart(); |
| 219 EXPECT_TRUE(arc_session_runner()->IsRunning()); | 226 EXPECT_TRUE(arc_session_runner()->IsRunning()); |
| 220 | 227 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 237 | 244 |
| 238 // Removing an unknown observer should be allowed. | 245 // Removing an unknown observer should be allowed. |
| 239 TEST_F(ArcSessionRunnerTest, RemoveUnknownObserver) { | 246 TEST_F(ArcSessionRunnerTest, RemoveUnknownObserver) { |
| 240 EXPECT_TRUE(arc_session_runner()->IsStopped()); | 247 EXPECT_TRUE(arc_session_runner()->IsStopped()); |
| 241 | 248 |
| 242 DummyObserver dummy_observer; | 249 DummyObserver dummy_observer; |
| 243 arc_session_runner()->RemoveObserver(&dummy_observer); | 250 arc_session_runner()->RemoveObserver(&dummy_observer); |
| 244 } | 251 } |
| 245 | 252 |
| 246 } // namespace arc | 253 } // namespace arc |
| OLD | NEW |