| 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" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 } | 68 } |
| 69 | 69 |
| 70 private: | 70 private: |
| 71 void SetUp() override { | 71 void SetUp() override { |
| 72 chromeos::DBusThreadManager::Initialize(); | 72 chromeos::DBusThreadManager::Initialize(); |
| 73 | 73 |
| 74 ready_ = false; | 74 ready_ = false; |
| 75 state_ = ArcBridgeService::State::STOPPED; | 75 state_ = ArcBridgeService::State::STOPPED; |
| 76 stop_reason_ = ArcBridgeService::StopReason::SHUTDOWN; | 76 stop_reason_ = ArcBridgeService::StopReason::SHUTDOWN; |
| 77 | 77 |
| 78 service_.reset(new ArcBridgeServiceImpl()); | 78 // We inject FakeArcSession here so we do not need task_runner. |
| 79 service_.reset(new ArcBridgeServiceImpl(nullptr)); |
| 79 service_->SetArcSessionFactoryForTesting( | 80 service_->SetArcSessionFactoryForTesting( |
| 80 base::Bind(FakeArcSession::Create)); | 81 base::Bind(FakeArcSession::Create)); |
| 81 service_->AddObserver(this); | 82 service_->AddObserver(this); |
| 82 } | 83 } |
| 83 | 84 |
| 84 void TearDown() override { | 85 void TearDown() override { |
| 85 service_->RemoveObserver(this); | 86 service_->RemoveObserver(this); |
| 86 service_.reset(); | 87 service_.reset(); |
| 87 | 88 |
| 88 chromeos::DBusThreadManager::Shutdown(); | 89 chromeos::DBusThreadManager::Shutdown(); |
| 89 } | 90 } |
| 90 | 91 |
| 91 bool ready_ = false; | 92 bool ready_ = false; |
| 92 ArcBridgeService::State state_; | 93 ArcBridgeService::State state_; |
| 93 base::MessageLoopForUI message_loop_; | 94 base::MessageLoopForUI message_loop_; |
| 94 | 95 |
| 95 DISALLOW_COPY_AND_ASSIGN(ArcBridgeTest); | 96 DISALLOW_COPY_AND_ASSIGN(ArcBridgeTest); |
| 96 }; | 97 }; |
| 97 | 98 |
| 98 // Exercises the basic functionality of the ARC Bridge Service. A message from | 99 // Exercises the basic functionality of the ARC Bridge Service. A message from |
| 99 // within the instance should cause the observer to be notified. | 100 // within the instance should cause the observer to be notified. |
| 100 TEST_F(ArcBridgeTest, Basic) { | 101 TEST_F(ArcBridgeTest, Basic) { |
| 101 ASSERT_FALSE(ready()); | 102 ASSERT_FALSE(ready()); |
| 102 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); | 103 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); |
| 103 | 104 |
| 104 service_->HandleStartup(); | 105 service_->RequestStart(); |
| 105 ASSERT_EQ(ArcBridgeService::State::READY, state()); | 106 ASSERT_EQ(ArcBridgeService::State::READY, state()); |
| 106 | 107 |
| 107 service_->Shutdown(); | 108 service_->RequestStop(); |
| 108 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); | 109 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); |
| 109 } | 110 } |
| 110 | 111 |
| 111 // If the ArcBridgeService is shut down, it should be stopped, even | 112 // If the ArcBridgeService is shut down, it should be stopped, even |
| 112 // mid-startup. | 113 // mid-startup. |
| 113 TEST_F(ArcBridgeTest, ShutdownMidStartup) { | 114 TEST_F(ArcBridgeTest, StopMidStartup) { |
| 114 ASSERT_FALSE(ready()); | 115 ASSERT_FALSE(ready()); |
| 115 | 116 |
| 116 service_->SetArcSessionFactoryForTesting( | 117 service_->SetArcSessionFactoryForTesting( |
| 117 base::Bind(ArcBridgeTest::CreateSuspendedArcSession)); | 118 base::Bind(ArcBridgeTest::CreateSuspendedArcSession)); |
| 118 service_->HandleStartup(); | 119 service_->RequestStart(); |
| 119 ASSERT_FALSE(service_->stopped()); | 120 ASSERT_FALSE(service_->stopped()); |
| 120 ASSERT_FALSE(service_->ready()); | 121 ASSERT_FALSE(service_->ready()); |
| 121 | 122 |
| 122 service_->Shutdown(); | 123 service_->RequestStop(); |
| 123 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); | 124 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); |
| 124 } | 125 } |
| 125 | 126 |
| 126 // If the boot procedure is failed, then restarting mechanism should not | 127 // If the boot procedure is failed, then restarting mechanism should not |
| 127 // triggered. | 128 // triggered. |
| 128 TEST_F(ArcBridgeTest, BootFailure) { | 129 TEST_F(ArcBridgeTest, BootFailure) { |
| 129 ASSERT_TRUE(service_->stopped()); | 130 ASSERT_TRUE(service_->stopped()); |
| 130 | 131 |
| 131 service_->SetArcSessionFactoryForTesting( | 132 service_->SetArcSessionFactoryForTesting( |
| 132 base::Bind(ArcBridgeTest::CreateBootFailureArcSession, | 133 base::Bind(ArcBridgeTest::CreateBootFailureArcSession, |
| 133 ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE)); | 134 ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE)); |
| 134 service_->HandleStartup(); | 135 service_->RequestStart(); |
| 135 EXPECT_EQ(ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE, stop_reason_); | 136 EXPECT_EQ(ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE, stop_reason_); |
| 136 ASSERT_TRUE(service_->stopped()); | 137 ASSERT_TRUE(service_->stopped()); |
| 137 } | 138 } |
| 138 | 139 |
| 139 // If the instance is stopped, it should be re-started. | 140 // If the instance is stopped, it should be re-started. |
| 140 TEST_F(ArcBridgeTest, Restart) { | 141 TEST_F(ArcBridgeTest, Restart) { |
| 141 ASSERT_FALSE(ready()); | 142 ASSERT_FALSE(ready()); |
| 142 | 143 |
| 143 service_->HandleStartup(); | 144 service_->RequestStart(); |
| 144 ASSERT_EQ(ArcBridgeService::State::READY, state()); | 145 ASSERT_EQ(ArcBridgeService::State::READY, state()); |
| 145 | 146 |
| 146 // Simulate a connection loss. | 147 // Simulate a connection loss. |
| 147 service_->DisableReconnectDelayForTesting(); | 148 service_->DisableReconnectDelayForTesting(); |
| 148 ASSERT_TRUE(arc_session()); | 149 ASSERT_TRUE(arc_session()); |
| 149 arc_session()->StopWithReason(ArcBridgeService::StopReason::CRASH); | 150 arc_session()->StopWithReason(ArcBridgeService::StopReason::CRASH); |
| 150 ASSERT_TRUE(service_->ready()); | 151 ASSERT_TRUE(service_->ready()); |
| 151 | 152 |
| 152 service_->Shutdown(); | 153 service_->RequestStop(); |
| 153 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); | 154 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); |
| 154 } | 155 } |
| 155 | 156 |
| 156 // Makes sure OnBridgeStopped is called on stop. | 157 // Makes sure OnBridgeStopped is called on stop. |
| 157 TEST_F(ArcBridgeTest, OnBridgeStopped) { | 158 TEST_F(ArcBridgeTest, OnBridgeStopped) { |
| 158 ASSERT_FALSE(ready()); | 159 ASSERT_FALSE(ready()); |
| 159 | 160 |
| 160 service_->DisableReconnectDelayForTesting(); | 161 service_->DisableReconnectDelayForTesting(); |
| 161 service_->HandleStartup(); | 162 service_->RequestStart(); |
| 162 ASSERT_EQ(ArcBridgeService::State::READY, state()); | 163 ASSERT_EQ(ArcBridgeService::State::READY, state()); |
| 163 | 164 |
| 164 // Simulate boot failure. | 165 // Simulate boot failure. |
| 165 ASSERT_TRUE(arc_session()); | 166 ASSERT_TRUE(arc_session()); |
| 166 arc_session()->StopWithReason( | 167 arc_session()->StopWithReason( |
| 167 ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE); | 168 ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE); |
| 168 EXPECT_EQ(ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE, stop_reason_); | 169 EXPECT_EQ(ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE, stop_reason_); |
| 169 ASSERT_TRUE(service_->ready()); | 170 ASSERT_TRUE(service_->ready()); |
| 170 | 171 |
| 171 // Simulate crash. | 172 // Simulate crash. |
| 172 ASSERT_TRUE(arc_session()); | 173 ASSERT_TRUE(arc_session()); |
| 173 arc_session()->StopWithReason(ArcBridgeService::StopReason::CRASH); | 174 arc_session()->StopWithReason(ArcBridgeService::StopReason::CRASH); |
| 174 EXPECT_EQ(ArcBridgeService::StopReason::CRASH, stop_reason_); | 175 EXPECT_EQ(ArcBridgeService::StopReason::CRASH, stop_reason_); |
| 175 ASSERT_TRUE(service_->ready()); | 176 ASSERT_TRUE(service_->ready()); |
| 176 | 177 |
| 177 // Graceful shutdown. | 178 // Graceful stop. |
| 178 service_->Shutdown(); | 179 service_->RequestStop(); |
| 179 ASSERT_EQ(ArcBridgeService::StopReason::SHUTDOWN, stop_reason_); | 180 ASSERT_EQ(ArcBridgeService::StopReason::SHUTDOWN, stop_reason_); |
| 180 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); | 181 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); |
| 181 } | 182 } |
| 183 |
| 184 TEST_F(ArcBridgeTest, Shutdown) { |
| 185 ASSERT_FALSE(ready()); |
| 186 |
| 187 service_->DisableReconnectDelayForTesting(); |
| 188 service_->RequestStart(); |
| 189 ASSERT_EQ(ArcBridgeService::State::READY, state()); |
| 190 |
| 191 // Simulate shutdown. |
| 192 service_->OnShutdown(); |
| 193 ASSERT_EQ(ArcBridgeService::StopReason::SHUTDOWN, stop_reason_); |
| 194 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); |
| 195 } |
| 182 | 196 |
| 183 // Removing the same observer more than once should be okay. | 197 // Removing the same observer more than once should be okay. |
| 184 TEST_F(ArcBridgeTest, RemoveObserverTwice) { | 198 TEST_F(ArcBridgeTest, RemoveObserverTwice) { |
| 185 ASSERT_FALSE(ready()); | 199 ASSERT_FALSE(ready()); |
| 186 auto dummy_observer = base::MakeUnique<DummyObserver>(); | 200 auto dummy_observer = base::MakeUnique<DummyObserver>(); |
| 187 service_->AddObserver(dummy_observer.get()); | 201 service_->AddObserver(dummy_observer.get()); |
| 188 // Call RemoveObserver() twice. | 202 // Call RemoveObserver() twice. |
| 189 service_->RemoveObserver(dummy_observer.get()); | 203 service_->RemoveObserver(dummy_observer.get()); |
| 190 service_->RemoveObserver(dummy_observer.get()); | 204 service_->RemoveObserver(dummy_observer.get()); |
| 191 } | 205 } |
| 192 | 206 |
| 193 // Removing an unknown observer should be allowed. | 207 // Removing an unknown observer should be allowed. |
| 194 TEST_F(ArcBridgeTest, RemoveUnknownObserver) { | 208 TEST_F(ArcBridgeTest, RemoveUnknownObserver) { |
| 195 ASSERT_FALSE(ready()); | 209 ASSERT_FALSE(ready()); |
| 196 auto dummy_observer = base::MakeUnique<DummyObserver>(); | 210 auto dummy_observer = base::MakeUnique<DummyObserver>(); |
| 197 service_->RemoveObserver(dummy_observer.get()); | 211 service_->RemoveObserver(dummy_observer.get()); |
| 198 } | 212 } |
| 199 | 213 |
| 200 } // namespace arc | 214 } // namespace arc |
| OLD | NEW |