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