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 <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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 | 61 |
62 case ArcBridgeService::State::STOPPED: | 62 case ArcBridgeService::State::STOPPED: |
63 message_loop_.PostTask(FROM_HERE, message_loop_.QuitWhenIdleClosure()); | 63 message_loop_.PostTask(FROM_HERE, message_loop_.QuitWhenIdleClosure()); |
64 break; | 64 break; |
65 | 65 |
66 default: | 66 default: |
67 break; | 67 break; |
68 } | 68 } |
69 } | 69 } |
70 | 70 |
| 71 void OnInstanceBootPhase(InstanceBootPhase boot_phase) override { |
| 72 boot_phase_ = boot_phase; |
| 73 } |
| 74 |
71 bool ready() const { return ready_; } | 75 bool ready() const { return ready_; } |
| 76 InstanceBootPhase boot_phase() const { return boot_phase_; } |
72 ArcBridgeService::State state() const { return state_; } | 77 ArcBridgeService::State state() const { return state_; } |
73 | 78 |
74 protected: | 79 protected: |
75 scoped_ptr<ArcBridgeService> service_; | 80 scoped_ptr<ArcBridgeService> service_; |
76 scoped_ptr<FakeArcBridgeInstance> instance_; | 81 scoped_ptr<FakeArcBridgeInstance> instance_; |
77 | 82 |
78 private: | 83 private: |
79 void SetUp() override { | 84 void SetUp() override { |
80 chromeos::DBusThreadManager::Initialize(); | 85 chromeos::DBusThreadManager::Initialize(); |
81 | 86 |
82 ready_ = false; | 87 ready_ = false; |
83 state_ = ArcBridgeService::State::STOPPED; | 88 state_ = ArcBridgeService::State::STOPPED; |
| 89 boot_phase_ = INSTANCE_BOOT_PHASE_NOT_RUNNING; |
84 | 90 |
85 ipc_support_.reset(new IPC::ScopedIPCSupport(message_loop_.task_runner())); | 91 ipc_support_.reset(new IPC::ScopedIPCSupport(message_loop_.task_runner())); |
86 instance_.reset(new FakeArcBridgeInstance()); | 92 instance_.reset(new FakeArcBridgeInstance()); |
87 service_.reset(new ArcBridgeServiceImpl( | 93 service_.reset(new ArcBridgeServiceImpl( |
88 make_scoped_ptr(new FakeArcBridgeBootstrap(instance_.get())))); | 94 make_scoped_ptr(new FakeArcBridgeBootstrap(instance_.get())))); |
89 | 95 |
90 service_->AddObserver(this); | 96 service_->AddObserver(this); |
91 } | 97 } |
92 | 98 |
93 void TearDown() override { | 99 void TearDown() override { |
94 service_->RemoveObserver(this); | 100 service_->RemoveObserver(this); |
95 instance_.reset(); | 101 instance_.reset(); |
96 service_.reset(); | 102 service_.reset(); |
97 ipc_support_.reset(); | 103 ipc_support_.reset(); |
98 | 104 |
99 chromeos::DBusThreadManager::Shutdown(); | 105 chromeos::DBusThreadManager::Shutdown(); |
100 } | 106 } |
101 | 107 |
102 bool ready_; | 108 bool ready_; |
| 109 InstanceBootPhase boot_phase_; |
103 ArcBridgeService::State state_; | 110 ArcBridgeService::State state_; |
104 scoped_ptr<IPC::ScopedIPCSupport> ipc_support_; | 111 scoped_ptr<IPC::ScopedIPCSupport> ipc_support_; |
105 base::MessageLoopForUI message_loop_; | 112 base::MessageLoopForUI message_loop_; |
106 | 113 |
107 DISALLOW_COPY_AND_ASSIGN(ArcBridgeTest); | 114 DISALLOW_COPY_AND_ASSIGN(ArcBridgeTest); |
108 }; | 115 }; |
109 | 116 |
110 // Shuts down the instance reports booted. | 117 // Shuts down the instance reports booted. |
111 class ScopedShutdownWhenReady : public ArcBridgeService::Observer { | 118 class ScopedShutdownWhenBooted : public ArcBridgeService::Observer { |
112 public: | 119 public: |
113 explicit ScopedShutdownWhenReady(ArcBridgeService* service) | 120 explicit ScopedShutdownWhenBooted(ArcBridgeService* service) |
114 : service_(service) { | 121 : service_(service) { |
115 service_->AddObserver(this); | 122 service_->AddObserver(this); |
116 } | 123 } |
117 | 124 |
118 ~ScopedShutdownWhenReady() override { service_->RemoveObserver(this); } | 125 ~ScopedShutdownWhenBooted() override { service_->RemoveObserver(this); } |
119 | 126 |
120 void OnStateChanged(ArcBridgeService::State state) override { | 127 void OnInstanceBootPhase(InstanceBootPhase boot_phase) override { |
121 if (state == ArcBridgeService::State::READY) { | 128 if (boot_phase == INSTANCE_BOOT_PHASE_BOOT_COMPLETED) { |
122 service_->Shutdown(); | 129 service_->Shutdown(); |
123 } | 130 } |
124 } | 131 } |
125 | 132 |
126 private: | 133 private: |
127 ArcBridgeService* service_; | 134 ArcBridgeService* service_; |
128 | 135 |
129 DISALLOW_COPY_AND_ASSIGN(ScopedShutdownWhenReady); | 136 DISALLOW_COPY_AND_ASSIGN(ScopedShutdownWhenBooted); |
130 }; | 137 }; |
131 | 138 |
132 // Exercises the basic functionality of the ARC Bridge Service. A message from | 139 // Exercises the basic functionality of the ARC Bridge Service. A message from |
133 // within the instance should cause the observer to be notified. | 140 // within the instance should cause the observer to be notified. |
134 TEST_F(ArcBridgeTest, Basic) { | 141 TEST_F(ArcBridgeTest, Basic) { |
135 ASSERT_FALSE(ready()); | 142 ASSERT_FALSE(ready()); |
136 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); | 143 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); |
137 | 144 |
138 ScopedShutdownWhenReady shutdown(service_.get()); | 145 ScopedShutdownWhenBooted shutdown(service_.get()); |
139 | 146 |
140 service_->SetAvailable(true); | 147 service_->SetAvailable(true); |
141 service_->HandleStartup(); | 148 service_->HandleStartup(); |
142 | 149 |
143 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); | 150 ASSERT_EQ(ArcBridgeService::State::CONNECTED, state()); |
144 | 151 |
145 base::RunLoop run_loop; | 152 base::RunLoop run_loop; |
146 run_loop.Run(); | 153 run_loop.Run(); |
147 | 154 |
148 EXPECT_TRUE(ready()); | 155 EXPECT_TRUE(ready()); |
| 156 ASSERT_EQ(INSTANCE_BOOT_PHASE_BOOT_COMPLETED, boot_phase()); |
149 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); | 157 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); |
150 } | 158 } |
151 | 159 |
152 // If not all pre-requisites are met, the instance is not started. | 160 // If not all pre-requisites are met, the instance is not started. |
153 TEST_F(ArcBridgeTest, Prerequisites) { | 161 TEST_F(ArcBridgeTest, Prerequisites) { |
154 ASSERT_FALSE(ready()); | 162 ASSERT_FALSE(ready()); |
155 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); | 163 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); |
156 service_->SetAvailable(true); | 164 service_->SetAvailable(true); |
157 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); | 165 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); |
158 service_->SetAvailable(false); | 166 service_->SetAvailable(false); |
159 service_->HandleStartup(); | 167 service_->HandleStartup(); |
160 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); | 168 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); |
161 } | 169 } |
162 | 170 |
163 // If the ArcBridgeService is shut down, it should be stopped, even | 171 // If the ArcBridgeService is shut down, it should be stopped, even |
164 // mid-startup. | 172 // mid-startup. |
165 TEST_F(ArcBridgeTest, ShutdownMidStartup) { | 173 TEST_F(ArcBridgeTest, ShutdownMidStartup) { |
166 ASSERT_FALSE(ready()); | 174 ASSERT_FALSE(ready()); |
167 | 175 |
168 service_->SetAvailable(true); | 176 service_->SetAvailable(true); |
169 service_->HandleStartup(); | 177 service_->HandleStartup(); |
170 | 178 |
171 ASSERT_EQ(ArcBridgeService::State::READY, state()); | 179 ASSERT_EQ(ArcBridgeService::State::CONNECTED, state()); |
172 service_->Shutdown(); | 180 service_->Shutdown(); |
173 // Some machines can reach the STOPPED state immediately. | 181 // Some machines can reach the STOPPED state immediately. |
174 ASSERT_TRUE(state() == ArcBridgeService::State::STOPPING || | 182 ASSERT_TRUE(state() == ArcBridgeService::State::STOPPING || |
175 state() == ArcBridgeService::State::STOPPED); | 183 state() == ArcBridgeService::State::STOPPED); |
176 | 184 |
177 base::RunLoop run_loop; | 185 base::RunLoop run_loop; |
178 run_loop.Run(); | 186 run_loop.Run(); |
179 | 187 |
180 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); | 188 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); |
181 } | 189 } |
182 | 190 |
183 } // namespace arc | 191 } // namespace arc |
OLD | NEW |