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