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/callback_helpers.h" |
9 #include "base/macros.h" | 10 #include "base/macros.h" |
10 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
11 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
12 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
13 #include "chromeos/dbus/dbus_thread_manager.h" | 14 #include "chromeos/dbus/dbus_thread_manager.h" |
14 #include "components/arc/arc_bridge_service_impl.h" | 15 #include "components/arc/arc_bridge_service_impl.h" |
15 #include "components/arc/test/fake_arc_session.h" | 16 #include "components/arc/test/fake_arc_session.h" |
16 #include "mojo/public/cpp/system/message_pipe.h" | 17 #include "mojo/public/cpp/system/message_pipe.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 | 19 |
19 namespace arc { | 20 namespace arc { |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 class DummyObserver : public ArcSessionObserver {}; | 24 class DummyObserver : public ArcSessionObserver {}; |
24 | 25 |
25 } // namespace | 26 } // namespace |
26 | 27 |
27 // TODO(hidehiko): ArcBridgeTest gets complicated and has stale code. | |
28 // Simplify the code. | |
29 class ArcBridgeTest : public testing::Test, public ArcSessionObserver { | 28 class ArcBridgeTest : public testing::Test, public ArcSessionObserver { |
30 public: | 29 public: |
31 ArcBridgeTest() = default; | 30 ArcBridgeTest() = default; |
32 | 31 |
33 void OnSessionReady() override { | 32 void SetUp() override { |
34 state_ = ArcBridgeService::State::READY; | 33 chromeos::DBusThreadManager::Initialize(); |
35 ready_ = true; | 34 |
| 35 stop_reason_ = StopReason::SHUTDOWN; |
| 36 |
| 37 // We inject FakeArcSession here so we do not need task_runner. |
| 38 service_ = base::MakeUnique<ArcBridgeServiceImpl>(nullptr); |
| 39 service_->SetArcSessionFactoryForTesting( |
| 40 base::Bind(FakeArcSession::Create)); |
| 41 service_->AddObserver(this); |
36 } | 42 } |
37 | 43 |
38 void OnSessionStopped(StopReason stop_reason) override { | 44 void TearDown() override { |
39 // The instance is already destructed in ArcBridgeServiceImpl::OnStopped(). | 45 service_->RemoveObserver(this); |
40 state_ = ArcBridgeService::State::STOPPED; | 46 service_.reset(); |
41 stop_reason_ = stop_reason; | 47 |
42 message_loop_.task_runner()->PostTask(FROM_HERE, | 48 chromeos::DBusThreadManager::Shutdown(); |
43 message_loop_.QuitWhenIdleClosure()); | |
44 } | 49 } |
45 | 50 |
46 bool ready() const { return ready_; } | 51 ArcBridgeServiceImpl* arc_bridge_service() { return service_.get(); } |
47 ArcBridgeService::State state() const { return state_; } | 52 |
48 FakeArcSession* arc_session() const { | 53 FakeArcSession* arc_session() const { |
49 return static_cast<FakeArcSession*>(service_->GetArcSessionForTesting()); | 54 return static_cast<FakeArcSession*>(service_->GetArcSessionForTesting()); |
50 } | 55 } |
51 | 56 |
52 protected: | 57 StopReason stop_reason() { return stop_reason_; } |
53 std::unique_ptr<ArcBridgeServiceImpl> service_; | |
54 StopReason stop_reason_; | |
55 | 58 |
56 static std::unique_ptr<ArcSession> CreateSuspendedArcSession() { | 59 static std::unique_ptr<ArcSession> CreateSuspendedArcSession() { |
57 auto arc_session = base::MakeUnique<FakeArcSession>(); | 60 auto arc_session = base::MakeUnique<FakeArcSession>(); |
58 arc_session->SuspendBoot(); | 61 arc_session->SuspendBoot(); |
59 return std::move(arc_session); | 62 return std::move(arc_session); |
60 } | 63 } |
61 | 64 |
62 static std::unique_ptr<ArcSession> CreateBootFailureArcSession( | 65 static std::unique_ptr<ArcSession> CreateBootFailureArcSession( |
63 StopReason reason) { | 66 StopReason reason) { |
64 auto arc_session = base::MakeUnique<FakeArcSession>(); | 67 auto arc_session = base::MakeUnique<FakeArcSession>(); |
65 arc_session->EnableBootFailureEmulation(reason); | 68 arc_session->EnableBootFailureEmulation(reason); |
66 return std::move(arc_session); | 69 return std::move(arc_session); |
67 } | 70 } |
68 | 71 |
69 private: | 72 private: |
70 void SetUp() override { | 73 // ArcSessionObserver: |
71 chromeos::DBusThreadManager::Initialize(); | 74 void OnSessionStopped(StopReason stop_reason) override { |
72 | 75 // The instance is already destructed in ArcBridgeServiceImpl::OnStopped(). |
73 ready_ = false; | 76 stop_reason_ = stop_reason; |
74 state_ = ArcBridgeService::State::STOPPED; | |
75 stop_reason_ = StopReason::SHUTDOWN; | |
76 | |
77 // We inject FakeArcSession here so we do not need task_runner. | |
78 service_.reset(new ArcBridgeServiceImpl(nullptr)); | |
79 service_->SetArcSessionFactoryForTesting( | |
80 base::Bind(FakeArcSession::Create)); | |
81 service_->AddObserver(this); | |
82 } | 77 } |
83 | 78 |
84 void TearDown() override { | 79 StopReason stop_reason_; |
85 service_->RemoveObserver(this); | 80 std::unique_ptr<ArcBridgeServiceImpl> service_; |
86 service_.reset(); | |
87 | |
88 chromeos::DBusThreadManager::Shutdown(); | |
89 } | |
90 | |
91 bool ready_ = false; | |
92 ArcBridgeService::State state_; | |
93 base::MessageLoopForUI message_loop_; | 81 base::MessageLoopForUI message_loop_; |
94 | 82 |
95 DISALLOW_COPY_AND_ASSIGN(ArcBridgeTest); | 83 DISALLOW_COPY_AND_ASSIGN(ArcBridgeTest); |
96 }; | 84 }; |
97 | 85 |
98 // Exercises the basic functionality of the ARC Bridge Service. A message from | 86 // Exercises the basic functionality of the ARC Bridge Service. Observer should |
99 // within the instance should cause the observer to be notified. | 87 // be notified. |
100 TEST_F(ArcBridgeTest, Basic) { | 88 TEST_F(ArcBridgeTest, Basic) { |
101 ASSERT_FALSE(ready()); | 89 class Observer : public ArcSessionObserver { |
102 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); | 90 public: |
| 91 Observer() = default; |
103 | 92 |
104 service_->RequestStart(); | 93 bool IsReadyCalled() { return ready_called_; } |
105 ASSERT_EQ(ArcBridgeService::State::READY, state()); | 94 bool IsStoppedCalled() { return stopped_called_; } |
106 | 95 |
107 service_->RequestStop(); | 96 // ArcSessionObserver: |
108 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); | 97 void OnSessionReady() override { ready_called_ = true; } |
| 98 void OnSessionStopped(StopReason stop_reason) override { |
| 99 stopped_called_ = true; |
| 100 } |
| 101 |
| 102 private: |
| 103 bool ready_called_ = false; |
| 104 bool stopped_called_ = false; |
| 105 |
| 106 DISALLOW_COPY_AND_ASSIGN(Observer); |
| 107 }; |
| 108 |
| 109 Observer observer; |
| 110 arc_bridge_service()->AddObserver(&observer); |
| 111 base::ScopedClosureRunner teardown(base::Bind( |
| 112 [](ArcBridgeService* arc_bridge_service, Observer* observer) { |
| 113 arc_bridge_service->RemoveObserver(observer); |
| 114 }, |
| 115 arc_bridge_service(), &observer)); |
| 116 |
| 117 EXPECT_TRUE(arc_bridge_service()->stopped()); |
| 118 |
| 119 arc_bridge_service()->RequestStart(); |
| 120 EXPECT_TRUE(arc_bridge_service()->ready()); |
| 121 EXPECT_TRUE(observer.IsReadyCalled()); |
| 122 |
| 123 arc_bridge_service()->RequestStop(); |
| 124 EXPECT_TRUE(arc_bridge_service()->stopped()); |
| 125 EXPECT_TRUE(observer.IsStoppedCalled()); |
109 } | 126 } |
110 | 127 |
111 // If the ArcBridgeService is shut down, it should be stopped, even | 128 // If the ArcBridgeService accepts a request to stop ARC instance, it should |
112 // mid-startup. | 129 // stop it, even mid-startup. |
113 TEST_F(ArcBridgeTest, StopMidStartup) { | 130 TEST_F(ArcBridgeTest, StopMidStartup) { |
114 ASSERT_FALSE(ready()); | 131 arc_bridge_service()->SetArcSessionFactoryForTesting( |
| 132 base::Bind(ArcBridgeTest::CreateSuspendedArcSession)); |
| 133 EXPECT_TRUE(arc_bridge_service()->stopped()); |
115 | 134 |
116 service_->SetArcSessionFactoryForTesting( | 135 arc_bridge_service()->RequestStart(); |
117 base::Bind(ArcBridgeTest::CreateSuspendedArcSession)); | 136 EXPECT_FALSE(arc_bridge_service()->stopped()); |
118 service_->RequestStart(); | 137 EXPECT_FALSE(arc_bridge_service()->ready()); |
119 ASSERT_FALSE(service_->stopped()); | |
120 ASSERT_FALSE(service_->ready()); | |
121 | 138 |
122 service_->RequestStop(); | 139 arc_bridge_service()->RequestStop(); |
123 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); | 140 EXPECT_TRUE(arc_bridge_service()->stopped()); |
124 } | 141 } |
125 | 142 |
126 // If the boot procedure is failed, then restarting mechanism should not | 143 // If the boot procedure is failed, then restarting mechanism should not |
127 // triggered. | 144 // triggered. |
128 TEST_F(ArcBridgeTest, BootFailure) { | 145 TEST_F(ArcBridgeTest, BootFailure) { |
129 ASSERT_TRUE(service_->stopped()); | 146 arc_bridge_service()->SetArcSessionFactoryForTesting( |
130 | |
131 service_->SetArcSessionFactoryForTesting( | |
132 base::Bind(ArcBridgeTest::CreateBootFailureArcSession, | 147 base::Bind(ArcBridgeTest::CreateBootFailureArcSession, |
133 StopReason::GENERIC_BOOT_FAILURE)); | 148 StopReason::GENERIC_BOOT_FAILURE)); |
134 service_->RequestStart(); | 149 EXPECT_TRUE(arc_bridge_service()->stopped()); |
135 EXPECT_EQ(StopReason::GENERIC_BOOT_FAILURE, stop_reason_); | 150 |
136 ASSERT_TRUE(service_->stopped()); | 151 arc_bridge_service()->RequestStart(); |
| 152 EXPECT_EQ(StopReason::GENERIC_BOOT_FAILURE, stop_reason()); |
| 153 EXPECT_TRUE(arc_bridge_service()->stopped()); |
137 } | 154 } |
138 | 155 |
139 // If the instance is stopped, it should be re-started. | 156 // If the instance is stopped, it should be re-started. |
140 TEST_F(ArcBridgeTest, Restart) { | 157 TEST_F(ArcBridgeTest, Restart) { |
141 ASSERT_FALSE(ready()); | 158 arc_bridge_service()->DisableReconnectDelayForTesting(); |
| 159 EXPECT_TRUE(arc_bridge_service()->stopped()); |
142 | 160 |
143 service_->RequestStart(); | 161 arc_bridge_service()->RequestStart(); |
144 ASSERT_EQ(ArcBridgeService::State::READY, state()); | 162 EXPECT_TRUE(arc_bridge_service()->ready()); |
145 | 163 |
146 // Simulate a connection loss. | 164 // Simulate a connection loss. |
147 service_->DisableReconnectDelayForTesting(); | |
148 ASSERT_TRUE(arc_session()); | 165 ASSERT_TRUE(arc_session()); |
149 arc_session()->StopWithReason(StopReason::CRASH); | 166 arc_session()->StopWithReason(StopReason::CRASH); |
150 ASSERT_TRUE(service_->ready()); | 167 EXPECT_TRUE(arc_bridge_service()->ready()); |
151 | 168 |
152 service_->RequestStop(); | 169 arc_bridge_service()->RequestStop(); |
153 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); | 170 EXPECT_TRUE(arc_bridge_service()->stopped()); |
154 } | 171 } |
155 | 172 |
156 // Makes sure OnBridgeStopped is called on stop. | 173 // Makes sure OnSessionStopped is called on stop. |
157 TEST_F(ArcBridgeTest, OnBridgeStopped) { | 174 TEST_F(ArcBridgeTest, OnSessionStopped) { |
158 ASSERT_FALSE(ready()); | 175 arc_bridge_service()->DisableReconnectDelayForTesting(); |
| 176 EXPECT_TRUE(arc_bridge_service()->stopped()); |
159 | 177 |
160 service_->DisableReconnectDelayForTesting(); | 178 arc_bridge_service()->RequestStart(); |
161 service_->RequestStart(); | 179 EXPECT_TRUE(arc_bridge_service()->ready()); |
162 ASSERT_EQ(ArcBridgeService::State::READY, state()); | |
163 | 180 |
164 // Simulate boot failure. | 181 // Simulate boot failure. |
165 ASSERT_TRUE(arc_session()); | 182 ASSERT_TRUE(arc_session()); |
166 arc_session()->StopWithReason(StopReason::GENERIC_BOOT_FAILURE); | 183 arc_session()->StopWithReason(StopReason::GENERIC_BOOT_FAILURE); |
167 EXPECT_EQ(StopReason::GENERIC_BOOT_FAILURE, stop_reason_); | 184 EXPECT_EQ(StopReason::GENERIC_BOOT_FAILURE, stop_reason()); |
168 ASSERT_TRUE(service_->ready()); | 185 EXPECT_TRUE(arc_bridge_service()->ready()); |
169 | 186 |
170 // Simulate crash. | 187 // Simulate crash. |
171 ASSERT_TRUE(arc_session()); | 188 ASSERT_TRUE(arc_session()); |
172 arc_session()->StopWithReason(StopReason::CRASH); | 189 arc_session()->StopWithReason(StopReason::CRASH); |
173 EXPECT_EQ(StopReason::CRASH, stop_reason_); | 190 EXPECT_EQ(StopReason::CRASH, stop_reason()); |
174 ASSERT_TRUE(service_->ready()); | 191 EXPECT_TRUE(arc_bridge_service()->ready()); |
175 | 192 |
176 // Graceful stop. | 193 // Graceful stop. |
177 service_->RequestStop(); | 194 arc_bridge_service()->RequestStop(); |
178 ASSERT_EQ(StopReason::SHUTDOWN, stop_reason_); | 195 EXPECT_EQ(StopReason::SHUTDOWN, stop_reason()); |
179 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); | 196 EXPECT_TRUE(arc_bridge_service()->stopped()); |
180 } | 197 } |
181 | 198 |
182 TEST_F(ArcBridgeTest, Shutdown) { | 199 TEST_F(ArcBridgeTest, Shutdown) { |
183 ASSERT_FALSE(ready()); | 200 arc_bridge_service()->DisableReconnectDelayForTesting(); |
| 201 EXPECT_TRUE(arc_bridge_service()->stopped()); |
184 | 202 |
185 service_->DisableReconnectDelayForTesting(); | 203 arc_bridge_service()->RequestStart(); |
186 service_->RequestStart(); | 204 EXPECT_TRUE(arc_bridge_service()->ready()); |
187 ASSERT_EQ(ArcBridgeService::State::READY, state()); | |
188 | 205 |
189 // Simulate shutdown. | 206 // Simulate shutdown. |
190 service_->OnShutdown(); | 207 arc_bridge_service()->OnShutdown(); |
191 ASSERT_EQ(StopReason::SHUTDOWN, stop_reason_); | 208 EXPECT_EQ(StopReason::SHUTDOWN, stop_reason()); |
192 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); | 209 EXPECT_TRUE(arc_bridge_service()->stopped()); |
193 } | 210 } |
194 | 211 |
195 // Removing the same observer more than once should be okay. | 212 // Removing the same observer more than once should be okay. |
196 TEST_F(ArcBridgeTest, RemoveObserverTwice) { | 213 TEST_F(ArcBridgeTest, RemoveObserverTwice) { |
197 ASSERT_FALSE(ready()); | 214 EXPECT_TRUE(arc_bridge_service()->stopped()); |
198 auto dummy_observer = base::MakeUnique<DummyObserver>(); | 215 |
199 service_->AddObserver(dummy_observer.get()); | 216 DummyObserver dummy_observer; |
| 217 arc_bridge_service()->AddObserver(&dummy_observer); |
200 // Call RemoveObserver() twice. | 218 // Call RemoveObserver() twice. |
201 service_->RemoveObserver(dummy_observer.get()); | 219 arc_bridge_service()->RemoveObserver(&dummy_observer); |
202 service_->RemoveObserver(dummy_observer.get()); | 220 arc_bridge_service()->RemoveObserver(&dummy_observer); |
203 } | 221 } |
204 | 222 |
205 // Removing an unknown observer should be allowed. | 223 // Removing an unknown observer should be allowed. |
206 TEST_F(ArcBridgeTest, RemoveUnknownObserver) { | 224 TEST_F(ArcBridgeTest, RemoveUnknownObserver) { |
207 ASSERT_FALSE(ready()); | 225 EXPECT_TRUE(arc_bridge_service()->stopped()); |
208 auto dummy_observer = base::MakeUnique<DummyObserver>(); | 226 |
209 service_->RemoveObserver(dummy_observer.get()); | 227 DummyObserver dummy_observer; |
| 228 arc_bridge_service()->RemoveObserver(&dummy_observer); |
210 } | 229 } |
211 | 230 |
212 } // namespace arc | 231 } // namespace arc |
OLD | NEW |