| 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 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 13 #include "chromeos/dbus/dbus_thread_manager.h" | 13 #include "chromeos/dbus/dbus_thread_manager.h" |
| 14 #include "components/arc/arc_bridge_service_impl.h" | 14 #include "components/arc/arc_bridge_service_impl.h" |
| 15 #include "components/arc/test/fake_arc_session.h" | 15 #include "components/arc/test/fake_arc_session.h" |
| 16 #include "mojo/public/cpp/system/message_pipe.h" | 16 #include "mojo/public/cpp/system/message_pipe.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| 19 namespace arc { | 19 namespace arc { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 class DummyObserver : public ArcBridgeService::Observer {}; | 23 class DummyObserver : public ArcSessionObserver {}; |
| 24 | 24 |
| 25 } // namespace | 25 } // namespace |
| 26 | 26 |
| 27 // TODO(hidehiko): ArcBridgeTest gets complicated and has stale code. | 27 // TODO(hidehiko): ArcBridgeTest gets complicated and has stale code. |
| 28 // Simplify the code. | 28 // Simplify the code. |
| 29 class ArcBridgeTest : public testing::Test, | 29 class ArcBridgeTest : public testing::Test, public ArcSessionObserver { |
| 30 public ArcBridgeService::Observer { | |
| 31 public: | 30 public: |
| 32 ArcBridgeTest() = default; | 31 ArcBridgeTest() = default; |
| 33 | 32 |
| 34 void OnBridgeReady() override { | 33 void OnSessionReady() override { |
| 35 state_ = ArcBridgeService::State::READY; | 34 state_ = ArcBridgeService::State::READY; |
| 36 ready_ = true; | 35 ready_ = true; |
| 37 } | 36 } |
| 38 | 37 |
| 39 void OnBridgeStopped(ArcBridgeService::StopReason stop_reason) override { | 38 void OnSessionStopped(StopReason stop_reason) override { |
| 40 // The instance is already destructed in ArcBridgeServiceImpl::OnStopped(). | 39 // The instance is already destructed in ArcBridgeServiceImpl::OnStopped(). |
| 41 state_ = ArcBridgeService::State::STOPPED; | 40 state_ = ArcBridgeService::State::STOPPED; |
| 42 stop_reason_ = stop_reason; | 41 stop_reason_ = stop_reason; |
| 43 message_loop_.task_runner()->PostTask(FROM_HERE, | 42 message_loop_.task_runner()->PostTask(FROM_HERE, |
| 44 message_loop_.QuitWhenIdleClosure()); | 43 message_loop_.QuitWhenIdleClosure()); |
| 45 } | 44 } |
| 46 | 45 |
| 47 bool ready() const { return ready_; } | 46 bool ready() const { return ready_; } |
| 48 ArcBridgeService::State state() const { return state_; } | 47 ArcBridgeService::State state() const { return state_; } |
| 49 FakeArcSession* arc_session() const { | 48 FakeArcSession* arc_session() const { |
| 50 return static_cast<FakeArcSession*>(service_->GetArcSessionForTesting()); | 49 return static_cast<FakeArcSession*>(service_->GetArcSessionForTesting()); |
| 51 } | 50 } |
| 52 | 51 |
| 53 protected: | 52 protected: |
| 54 std::unique_ptr<ArcBridgeServiceImpl> service_; | 53 std::unique_ptr<ArcBridgeServiceImpl> service_; |
| 55 ArcBridgeService::StopReason stop_reason_; | 54 StopReason stop_reason_; |
| 56 | 55 |
| 57 static std::unique_ptr<ArcSession> CreateSuspendedArcSession() { | 56 static std::unique_ptr<ArcSession> CreateSuspendedArcSession() { |
| 58 auto arc_session = base::MakeUnique<FakeArcSession>(); | 57 auto arc_session = base::MakeUnique<FakeArcSession>(); |
| 59 arc_session->SuspendBoot(); | 58 arc_session->SuspendBoot(); |
| 60 return std::move(arc_session); | 59 return std::move(arc_session); |
| 61 } | 60 } |
| 62 | 61 |
| 63 static std::unique_ptr<ArcSession> CreateBootFailureArcSession( | 62 static std::unique_ptr<ArcSession> CreateBootFailureArcSession( |
| 64 ArcBridgeService::StopReason reason) { | 63 StopReason reason) { |
| 65 auto arc_session = base::MakeUnique<FakeArcSession>(); | 64 auto arc_session = base::MakeUnique<FakeArcSession>(); |
| 66 arc_session->EnableBootFailureEmulation(reason); | 65 arc_session->EnableBootFailureEmulation(reason); |
| 67 return std::move(arc_session); | 66 return std::move(arc_session); |
| 68 } | 67 } |
| 69 | 68 |
| 70 private: | 69 private: |
| 71 void SetUp() override { | 70 void SetUp() override { |
| 72 chromeos::DBusThreadManager::Initialize(); | 71 chromeos::DBusThreadManager::Initialize(); |
| 73 | 72 |
| 74 ready_ = false; | 73 ready_ = false; |
| 75 state_ = ArcBridgeService::State::STOPPED; | 74 state_ = ArcBridgeService::State::STOPPED; |
| 76 stop_reason_ = ArcBridgeService::StopReason::SHUTDOWN; | 75 stop_reason_ = StopReason::SHUTDOWN; |
| 77 | 76 |
| 78 // We inject FakeArcSession here so we do not need task_runner. | 77 // We inject FakeArcSession here so we do not need task_runner. |
| 79 service_.reset(new ArcBridgeServiceImpl(nullptr)); | 78 service_.reset(new ArcBridgeServiceImpl(nullptr)); |
| 80 service_->SetArcSessionFactoryForTesting( | 79 service_->SetArcSessionFactoryForTesting( |
| 81 base::Bind(FakeArcSession::Create)); | 80 base::Bind(FakeArcSession::Create)); |
| 82 service_->AddObserver(this); | 81 service_->AddObserver(this); |
| 83 } | 82 } |
| 84 | 83 |
| 85 void TearDown() override { | 84 void TearDown() override { |
| 86 service_->RemoveObserver(this); | 85 service_->RemoveObserver(this); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); | 123 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); |
| 125 } | 124 } |
| 126 | 125 |
| 127 // If the boot procedure is failed, then restarting mechanism should not | 126 // If the boot procedure is failed, then restarting mechanism should not |
| 128 // triggered. | 127 // triggered. |
| 129 TEST_F(ArcBridgeTest, BootFailure) { | 128 TEST_F(ArcBridgeTest, BootFailure) { |
| 130 ASSERT_TRUE(service_->stopped()); | 129 ASSERT_TRUE(service_->stopped()); |
| 131 | 130 |
| 132 service_->SetArcSessionFactoryForTesting( | 131 service_->SetArcSessionFactoryForTesting( |
| 133 base::Bind(ArcBridgeTest::CreateBootFailureArcSession, | 132 base::Bind(ArcBridgeTest::CreateBootFailureArcSession, |
| 134 ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE)); | 133 StopReason::GENERIC_BOOT_FAILURE)); |
| 135 service_->RequestStart(); | 134 service_->RequestStart(); |
| 136 EXPECT_EQ(ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE, stop_reason_); | 135 EXPECT_EQ(StopReason::GENERIC_BOOT_FAILURE, stop_reason_); |
| 137 ASSERT_TRUE(service_->stopped()); | 136 ASSERT_TRUE(service_->stopped()); |
| 138 } | 137 } |
| 139 | 138 |
| 140 // If the instance is stopped, it should be re-started. | 139 // If the instance is stopped, it should be re-started. |
| 141 TEST_F(ArcBridgeTest, Restart) { | 140 TEST_F(ArcBridgeTest, Restart) { |
| 142 ASSERT_FALSE(ready()); | 141 ASSERT_FALSE(ready()); |
| 143 | 142 |
| 144 service_->RequestStart(); | 143 service_->RequestStart(); |
| 145 ASSERT_EQ(ArcBridgeService::State::READY, state()); | 144 ASSERT_EQ(ArcBridgeService::State::READY, state()); |
| 146 | 145 |
| 147 // Simulate a connection loss. | 146 // Simulate a connection loss. |
| 148 service_->DisableReconnectDelayForTesting(); | 147 service_->DisableReconnectDelayForTesting(); |
| 149 ASSERT_TRUE(arc_session()); | 148 ASSERT_TRUE(arc_session()); |
| 150 arc_session()->StopWithReason(ArcBridgeService::StopReason::CRASH); | 149 arc_session()->StopWithReason(StopReason::CRASH); |
| 151 ASSERT_TRUE(service_->ready()); | 150 ASSERT_TRUE(service_->ready()); |
| 152 | 151 |
| 153 service_->RequestStop(); | 152 service_->RequestStop(); |
| 154 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); | 153 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); |
| 155 } | 154 } |
| 156 | 155 |
| 157 // Makes sure OnBridgeStopped is called on stop. | 156 // Makes sure OnBridgeStopped is called on stop. |
| 158 TEST_F(ArcBridgeTest, OnBridgeStopped) { | 157 TEST_F(ArcBridgeTest, OnBridgeStopped) { |
| 159 ASSERT_FALSE(ready()); | 158 ASSERT_FALSE(ready()); |
| 160 | 159 |
| 161 service_->DisableReconnectDelayForTesting(); | 160 service_->DisableReconnectDelayForTesting(); |
| 162 service_->RequestStart(); | 161 service_->RequestStart(); |
| 163 ASSERT_EQ(ArcBridgeService::State::READY, state()); | 162 ASSERT_EQ(ArcBridgeService::State::READY, state()); |
| 164 | 163 |
| 165 // Simulate boot failure. | 164 // Simulate boot failure. |
| 166 ASSERT_TRUE(arc_session()); | 165 ASSERT_TRUE(arc_session()); |
| 167 arc_session()->StopWithReason( | 166 arc_session()->StopWithReason(StopReason::GENERIC_BOOT_FAILURE); |
| 168 ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE); | 167 EXPECT_EQ(StopReason::GENERIC_BOOT_FAILURE, stop_reason_); |
| 169 EXPECT_EQ(ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE, stop_reason_); | |
| 170 ASSERT_TRUE(service_->ready()); | 168 ASSERT_TRUE(service_->ready()); |
| 171 | 169 |
| 172 // Simulate crash. | 170 // Simulate crash. |
| 173 ASSERT_TRUE(arc_session()); | 171 ASSERT_TRUE(arc_session()); |
| 174 arc_session()->StopWithReason(ArcBridgeService::StopReason::CRASH); | 172 arc_session()->StopWithReason(StopReason::CRASH); |
| 175 EXPECT_EQ(ArcBridgeService::StopReason::CRASH, stop_reason_); | 173 EXPECT_EQ(StopReason::CRASH, stop_reason_); |
| 176 ASSERT_TRUE(service_->ready()); | 174 ASSERT_TRUE(service_->ready()); |
| 177 | 175 |
| 178 // Graceful stop. | 176 // Graceful stop. |
| 179 service_->RequestStop(); | 177 service_->RequestStop(); |
| 180 ASSERT_EQ(ArcBridgeService::StopReason::SHUTDOWN, stop_reason_); | 178 ASSERT_EQ(StopReason::SHUTDOWN, stop_reason_); |
| 181 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); | 179 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); |
| 182 } | 180 } |
| 183 | 181 |
| 184 TEST_F(ArcBridgeTest, Shutdown) { | 182 TEST_F(ArcBridgeTest, Shutdown) { |
| 185 ASSERT_FALSE(ready()); | 183 ASSERT_FALSE(ready()); |
| 186 | 184 |
| 187 service_->DisableReconnectDelayForTesting(); | 185 service_->DisableReconnectDelayForTesting(); |
| 188 service_->RequestStart(); | 186 service_->RequestStart(); |
| 189 ASSERT_EQ(ArcBridgeService::State::READY, state()); | 187 ASSERT_EQ(ArcBridgeService::State::READY, state()); |
| 190 | 188 |
| 191 // Simulate shutdown. | 189 // Simulate shutdown. |
| 192 service_->OnShutdown(); | 190 service_->OnShutdown(); |
| 193 ASSERT_EQ(ArcBridgeService::StopReason::SHUTDOWN, stop_reason_); | 191 ASSERT_EQ(StopReason::SHUTDOWN, stop_reason_); |
| 194 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); | 192 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); |
| 195 } | 193 } |
| 196 | 194 |
| 197 // Removing the same observer more than once should be okay. | 195 // Removing the same observer more than once should be okay. |
| 198 TEST_F(ArcBridgeTest, RemoveObserverTwice) { | 196 TEST_F(ArcBridgeTest, RemoveObserverTwice) { |
| 199 ASSERT_FALSE(ready()); | 197 ASSERT_FALSE(ready()); |
| 200 auto dummy_observer = base::MakeUnique<DummyObserver>(); | 198 auto dummy_observer = base::MakeUnique<DummyObserver>(); |
| 201 service_->AddObserver(dummy_observer.get()); | 199 service_->AddObserver(dummy_observer.get()); |
| 202 // Call RemoveObserver() twice. | 200 // Call RemoveObserver() twice. |
| 203 service_->RemoveObserver(dummy_observer.get()); | 201 service_->RemoveObserver(dummy_observer.get()); |
| 204 service_->RemoveObserver(dummy_observer.get()); | 202 service_->RemoveObserver(dummy_observer.get()); |
| 205 } | 203 } |
| 206 | 204 |
| 207 // Removing an unknown observer should be allowed. | 205 // Removing an unknown observer should be allowed. |
| 208 TEST_F(ArcBridgeTest, RemoveUnknownObserver) { | 206 TEST_F(ArcBridgeTest, RemoveUnknownObserver) { |
| 209 ASSERT_FALSE(ready()); | 207 ASSERT_FALSE(ready()); |
| 210 auto dummy_observer = base::MakeUnique<DummyObserver>(); | 208 auto dummy_observer = base::MakeUnique<DummyObserver>(); |
| 211 service_->RemoveObserver(dummy_observer.get()); | 209 service_->RemoveObserver(dummy_observer.get()); |
| 212 } | 210 } |
| 213 | 211 |
| 214 } // namespace arc | 212 } // namespace arc |
| OLD | NEW |