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 23 matching lines...) Expand all Loading... |
34 | 34 |
35 case ArcBridgeService::State::STOPPED: | 35 case ArcBridgeService::State::STOPPED: |
36 message_loop_.PostTask(FROM_HERE, message_loop_.QuitWhenIdleClosure()); | 36 message_loop_.PostTask(FROM_HERE, message_loop_.QuitWhenIdleClosure()); |
37 break; | 37 break; |
38 | 38 |
39 default: | 39 default: |
40 break; | 40 break; |
41 } | 41 } |
42 } | 42 } |
43 | 43 |
| 44 void OnBridgeStopped(ArcBridgeService::StopReason stop_reason) override { |
| 45 stop_reason_ = stop_reason; |
| 46 } |
| 47 |
44 bool ready() const { return ready_; } | 48 bool ready() const { return ready_; } |
45 ArcBridgeService::State state() const { return state_; } | 49 ArcBridgeService::State state() const { return state_; } |
46 | 50 |
47 protected: | 51 protected: |
48 std::unique_ptr<ArcBridgeServiceImpl> service_; | 52 std::unique_ptr<ArcBridgeServiceImpl> service_; |
49 std::unique_ptr<FakeArcBridgeInstance> instance_; | 53 std::unique_ptr<FakeArcBridgeInstance> instance_; |
| 54 ArcBridgeService::StopReason stop_reason_; |
50 | 55 |
51 private: | 56 private: |
52 void SetUp() override { | 57 void SetUp() override { |
53 chromeos::DBusThreadManager::Initialize(); | 58 chromeos::DBusThreadManager::Initialize(); |
54 | 59 |
55 ready_ = false; | 60 ready_ = false; |
56 state_ = ArcBridgeService::State::STOPPED; | 61 state_ = ArcBridgeService::State::STOPPED; |
| 62 stop_reason_ = ArcBridgeService::StopReason::SHUTDOWN; |
57 | 63 |
58 instance_.reset(new FakeArcBridgeInstance()); | 64 instance_.reset(new FakeArcBridgeInstance()); |
59 service_.reset(new ArcBridgeServiceImpl( | 65 service_.reset(new ArcBridgeServiceImpl( |
60 base::MakeUnique<FakeArcBridgeBootstrap>(instance_.get()))); | 66 base::MakeUnique<FakeArcBridgeBootstrap>(instance_.get()))); |
61 | 67 |
62 service_->AddObserver(this); | 68 service_->AddObserver(this); |
63 } | 69 } |
64 | 70 |
65 void TearDown() override { | 71 void TearDown() override { |
66 service_->RemoveObserver(this); | 72 service_->RemoveObserver(this); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 | 134 |
129 service_->SetAvailable(true); | 135 service_->SetAvailable(true); |
130 service_->HandleStartup(); | 136 service_->HandleStartup(); |
131 instance_->WaitForInitCall(); | 137 instance_->WaitForInitCall(); |
132 ASSERT_EQ(ArcBridgeService::State::READY, state()); | 138 ASSERT_EQ(ArcBridgeService::State::READY, state()); |
133 ASSERT_EQ(1, instance_->init_calls()); | 139 ASSERT_EQ(1, instance_->init_calls()); |
134 | 140 |
135 // Simulate a connection loss. | 141 // Simulate a connection loss. |
136 service_->DisableReconnectDelayForTesting(); | 142 service_->DisableReconnectDelayForTesting(); |
137 service_->OnChannelClosed(); | 143 service_->OnChannelClosed(); |
138 instance_->SimulateCrash(); | 144 instance_->Stop(ArcBridgeService::StopReason::CRASH); |
139 instance_->WaitForInitCall(); | 145 instance_->WaitForInitCall(); |
140 ASSERT_EQ(ArcBridgeService::State::READY, state()); | 146 ASSERT_EQ(ArcBridgeService::State::READY, state()); |
141 ASSERT_EQ(2, instance_->init_calls()); | 147 ASSERT_EQ(2, instance_->init_calls()); |
142 | 148 |
143 service_->Shutdown(); | 149 service_->Shutdown(); |
144 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); | 150 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); |
145 } | 151 } |
146 | 152 |
| 153 // Makes sure OnBridgeStopped is called on stop. |
| 154 TEST_F(ArcBridgeTest, OnBridgeStopped) { |
| 155 ASSERT_FALSE(ready()); |
| 156 |
| 157 service_->DisableReconnectDelayForTesting(); |
| 158 service_->SetAvailable(true); |
| 159 service_->HandleStartup(); |
| 160 instance_->WaitForInitCall(); |
| 161 ASSERT_EQ(ArcBridgeService::State::READY, state()); |
| 162 |
| 163 // Simulate boot failure. |
| 164 service_->OnChannelClosed(); |
| 165 instance_->Stop(ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE); |
| 166 instance_->WaitForInitCall(); |
| 167 ASSERT_EQ(ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE, stop_reason_); |
| 168 ASSERT_EQ(ArcBridgeService::State::READY, state()); |
| 169 |
| 170 // Simulate crash. |
| 171 service_->OnChannelClosed(); |
| 172 instance_->Stop(ArcBridgeService::StopReason::CRASH); |
| 173 instance_->WaitForInitCall(); |
| 174 ASSERT_EQ(ArcBridgeService::StopReason::CRASH, stop_reason_); |
| 175 ASSERT_EQ(ArcBridgeService::State::READY, state()); |
| 176 |
| 177 // Graceful shutdown. |
| 178 service_->Shutdown(); |
| 179 ASSERT_EQ(ArcBridgeService::StopReason::SHUTDOWN, stop_reason_); |
| 180 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); |
| 181 } |
| 182 |
147 // Removing the same observer more than once should be okay. | 183 // Removing the same observer more than once should be okay. |
148 TEST_F(ArcBridgeTest, RemoveObserverTwice) { | 184 TEST_F(ArcBridgeTest, RemoveObserverTwice) { |
149 ASSERT_FALSE(ready()); | 185 ASSERT_FALSE(ready()); |
150 service_->RemoveObserver(this); | 186 service_->RemoveObserver(this); |
151 // The teardown method will also remove |this|. | 187 // The teardown method will also remove |this|. |
152 } | 188 } |
153 | 189 |
154 // Removing an unknown observer should be allowed. | 190 // Removing an unknown observer should be allowed. |
155 TEST_F(ArcBridgeTest, RemoveUnknownObserver) { | 191 TEST_F(ArcBridgeTest, RemoveUnknownObserver) { |
156 ASSERT_FALSE(ready()); | 192 ASSERT_FALSE(ready()); |
157 auto dummy_observer = base::MakeUnique<DummyObserver>(); | 193 auto dummy_observer = base::MakeUnique<DummyObserver>(); |
158 service_->RemoveObserver(dummy_observer.get()); | 194 service_->RemoveObserver(dummy_observer.get()); |
159 } | 195 } |
160 | 196 |
161 } // namespace arc | 197 } // namespace arc |
OLD | NEW |