Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(434)

Side by Side Diff: components/arc/arc_bridge_service_unittest.cc

Issue 1548833002: arc-bridge: Restart ARC instance on crash (Closed) Base URL: https://chromium.googlesource.com/a/chromium/src.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <utility> 5 #include <utility>
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/run_loop.h" 9 #include "base/run_loop.h"
10 #include "chromeos/dbus/dbus_thread_manager.h" 10 #include "chromeos/dbus/dbus_thread_manager.h"
(...skipping 17 matching lines...) Expand all
28 28
29 void Start() override { 29 void Start() override {
30 DCHECK(delegate_); 30 DCHECK(delegate_);
31 ArcBridgeInstancePtr instance; 31 ArcBridgeInstancePtr instance;
32 instance_->Bind(mojo::GetProxy(&instance)); 32 instance_->Bind(mojo::GetProxy(&instance));
33 delegate_->OnConnectionEstablished(std::move(instance)); 33 delegate_->OnConnectionEstablished(std::move(instance));
34 } 34 }
35 35
36 void Stop() override { 36 void Stop() override {
37 DCHECK(delegate_); 37 DCHECK(delegate_);
38 instance_->Unbind();
38 delegate_->OnStopped(); 39 delegate_->OnStopped();
39 } 40 }
40 41
41 private: 42 private:
42 // Owned by the caller. 43 // Owned by the caller.
43 FakeArcBridgeInstance* instance_; 44 FakeArcBridgeInstance* instance_;
44 45
45 DISALLOW_COPY_AND_ASSIGN(FakeArcBridgeBootstrap); 46 DISALLOW_COPY_AND_ASSIGN(FakeArcBridgeBootstrap);
46 }; 47 };
47 48
(...skipping 17 matching lines...) Expand all
65 66
66 default: 67 default:
67 break; 68 break;
68 } 69 }
69 } 70 }
70 71
71 bool ready() const { return ready_; } 72 bool ready() const { return ready_; }
72 ArcBridgeService::State state() const { return state_; } 73 ArcBridgeService::State state() const { return state_; }
73 74
74 protected: 75 protected:
75 scoped_ptr<ArcBridgeService> service_; 76 scoped_ptr<ArcBridgeServiceImpl> service_;
76 scoped_ptr<FakeArcBridgeInstance> instance_; 77 scoped_ptr<FakeArcBridgeInstance> instance_;
77 78
78 private: 79 private:
79 void SetUp() override { 80 void SetUp() override {
80 chromeos::DBusThreadManager::Initialize(); 81 chromeos::DBusThreadManager::Initialize();
81 82
82 ready_ = false; 83 ready_ = false;
83 state_ = ArcBridgeService::State::STOPPED; 84 state_ = ArcBridgeService::State::STOPPED;
84 85
85 ipc_support_.reset(new IPC::ScopedIPCSupport(message_loop_.task_runner())); 86 ipc_support_.reset(new IPC::ScopedIPCSupport(message_loop_.task_runner()));
(...skipping 14 matching lines...) Expand all
100 } 101 }
101 102
102 bool ready_; 103 bool ready_;
103 ArcBridgeService::State state_; 104 ArcBridgeService::State state_;
104 scoped_ptr<IPC::ScopedIPCSupport> ipc_support_; 105 scoped_ptr<IPC::ScopedIPCSupport> ipc_support_;
105 base::MessageLoopForUI message_loop_; 106 base::MessageLoopForUI message_loop_;
106 107
107 DISALLOW_COPY_AND_ASSIGN(ArcBridgeTest); 108 DISALLOW_COPY_AND_ASSIGN(ArcBridgeTest);
108 }; 109 };
109 110
110 // Shuts down the instance reports booted.
111 class ScopedShutdownWhenReady : public ArcBridgeService::Observer {
112 public:
113 explicit ScopedShutdownWhenReady(ArcBridgeService* service)
114 : service_(service) {
115 service_->AddObserver(this);
116 }
117
118 ~ScopedShutdownWhenReady() override { service_->RemoveObserver(this); }
119
120 void OnStateChanged(ArcBridgeService::State state) override {
121 if (state == ArcBridgeService::State::READY) {
122 service_->Shutdown();
123 }
124 }
125
126 private:
127 ArcBridgeService* service_;
128
129 DISALLOW_COPY_AND_ASSIGN(ScopedShutdownWhenReady);
130 };
131
132 // Exercises the basic functionality of the ARC Bridge Service. A message from 111 // Exercises the basic functionality of the ARC Bridge Service. A message from
133 // within the instance should cause the observer to be notified. 112 // within the instance should cause the observer to be notified.
134 TEST_F(ArcBridgeTest, Basic) { 113 TEST_F(ArcBridgeTest, Basic) {
135 ASSERT_FALSE(ready()); 114 ASSERT_FALSE(ready());
136 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); 115 ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
137 116
138 ScopedShutdownWhenReady shutdown(service_.get());
139
140 service_->SetAvailable(true); 117 service_->SetAvailable(true);
141 service_->HandleStartup(); 118 service_->HandleStartup();
119 instance_->WaitForInitCall();
120 ASSERT_EQ(ArcBridgeService::State::READY, state());
142 121
143 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); 122 service_->Shutdown();
144
145 base::RunLoop run_loop;
146 run_loop.Run();
147
148 EXPECT_TRUE(ready());
149 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); 123 ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
150 } 124 }
151 125
152 // If not all pre-requisites are met, the instance is not started. 126 // If not all pre-requisites are met, the instance is not started.
153 TEST_F(ArcBridgeTest, Prerequisites) { 127 TEST_F(ArcBridgeTest, Prerequisites) {
154 ASSERT_FALSE(ready()); 128 ASSERT_FALSE(ready());
155 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); 129 ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
156 service_->SetAvailable(true); 130 service_->SetAvailable(true);
157 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); 131 ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
158 service_->SetAvailable(false); 132 service_->SetAvailable(false);
159 service_->HandleStartup(); 133 service_->HandleStartup();
160 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); 134 ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
161 } 135 }
162 136
163 // If the ArcBridgeService is shut down, it should be stopped, even 137 // If the ArcBridgeService is shut down, it should be stopped, even
164 // mid-startup. 138 // mid-startup.
165 TEST_F(ArcBridgeTest, ShutdownMidStartup) { 139 TEST_F(ArcBridgeTest, ShutdownMidStartup) {
166 ASSERT_FALSE(ready()); 140 ASSERT_FALSE(ready());
167 141
168 service_->SetAvailable(true); 142 service_->SetAvailable(true);
169 service_->HandleStartup(); 143 service_->HandleStartup();
144 // WaitForInitCall() omitted.
145 ASSERT_EQ(ArcBridgeService::State::READY, state());
170 146
171 ASSERT_EQ(ArcBridgeService::State::READY, state());
172 service_->Shutdown(); 147 service_->Shutdown();
173 // Some machines can reach the STOPPED state immediately.
174 ASSERT_TRUE(state() == ArcBridgeService::State::STOPPING ||
175 state() == ArcBridgeService::State::STOPPED);
176
177 base::RunLoop run_loop;
178 run_loop.Run();
179
180 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); 148 ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
181 } 149 }
182 150
151 // If the channel is disconnected, it should be re-established.
152 TEST_F(ArcBridgeTest, Restart) {
153 ASSERT_FALSE(ready());
154 ASSERT_EQ(0, instance_->init_calls());
155
156 service_->SetAvailable(true);
157 service_->HandleStartup();
158 instance_->WaitForInitCall();
159 ASSERT_EQ(ArcBridgeService::State::READY, state());
160 ASSERT_EQ(1, instance_->init_calls());
161
162 // Simulate a connection loss.
163 service_->OnChannelClosed();
164 instance_->WaitForInitCall();
165 ASSERT_EQ(ArcBridgeService::State::READY, state());
166 ASSERT_EQ(2, instance_->init_calls());
167
168 service_->Shutdown();
169 ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
170 }
171
183 } // namespace arc 172 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698