| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "media/midi/midi_manager.h" | 5 #include "media/midi/midi_manager.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 void StartInitialization() override { | 29 void StartInitialization() override { |
| 30 start_initialization_is_called_ = true; | 30 start_initialization_is_called_ = true; |
| 31 } | 31 } |
| 32 | 32 |
| 33 void DispatchSendMidiData(MidiManagerClient* client, | 33 void DispatchSendMidiData(MidiManagerClient* client, |
| 34 uint32 port_index, | 34 uint32 port_index, |
| 35 const std::vector<uint8>& data, | 35 const std::vector<uint8>& data, |
| 36 double timestamp) override {} | 36 double timestamp) override {} |
| 37 | 37 |
| 38 // Utility functions for testing. | 38 // Utility functions for testing. |
| 39 void CallCompleteInitialization(MidiResult result) { | 39 void CallCompleteInitialization(Result result) { |
| 40 CompleteInitialization(result); | 40 CompleteInitialization(result); |
| 41 } | 41 } |
| 42 | 42 |
| 43 size_t GetClientCount() const { | 43 size_t GetClientCount() const { |
| 44 return clients_size_for_testing(); | 44 return clients_size_for_testing(); |
| 45 } | 45 } |
| 46 | 46 |
| 47 size_t GetPendingClientCount() const { | 47 size_t GetPendingClientCount() const { |
| 48 return pending_clients_size_for_testing(); | 48 return pending_clients_size_for_testing(); |
| 49 } | 49 } |
| 50 | 50 |
| 51 bool start_initialization_is_called_; | 51 bool start_initialization_is_called_; |
| 52 | 52 |
| 53 private: | 53 private: |
| 54 DISALLOW_COPY_AND_ASSIGN(FakeMidiManager); | 54 DISALLOW_COPY_AND_ASSIGN(FakeMidiManager); |
| 55 }; | 55 }; |
| 56 | 56 |
| 57 class FakeMidiManagerClient : public MidiManagerClient { | 57 class FakeMidiManagerClient : public MidiManagerClient { |
| 58 public: | 58 public: |
| 59 FakeMidiManagerClient() | 59 FakeMidiManagerClient() |
| 60 : result_(MIDI_NOT_SUPPORTED), | 60 : result_(Result::NOT_SUPPORTED), wait_for_result_(true) {} |
| 61 wait_for_result_(true) {} | |
| 62 ~FakeMidiManagerClient() override {} | 61 ~FakeMidiManagerClient() override {} |
| 63 | 62 |
| 64 // MidiManagerClient implementation. | 63 // MidiManagerClient implementation. |
| 65 void AddInputPort(const MidiPortInfo& info) override {} | 64 void AddInputPort(const MidiPortInfo& info) override {} |
| 66 void AddOutputPort(const MidiPortInfo& info) override {} | 65 void AddOutputPort(const MidiPortInfo& info) override {} |
| 67 void SetInputPortState(uint32 port_index, MidiPortState state) override {} | 66 void SetInputPortState(uint32 port_index, MidiPortState state) override {} |
| 68 void SetOutputPortState(uint32 port_index, MidiPortState state) override {} | 67 void SetOutputPortState(uint32 port_index, MidiPortState state) override {} |
| 69 | 68 |
| 70 void CompleteStartSession(MidiResult result) override { | 69 void CompleteStartSession(Result result) override { |
| 71 EXPECT_TRUE(wait_for_result_); | 70 EXPECT_TRUE(wait_for_result_); |
| 72 result_ = result; | 71 result_ = result; |
| 73 wait_for_result_ = false; | 72 wait_for_result_ = false; |
| 74 } | 73 } |
| 75 | 74 |
| 76 void ReceiveMidiData(uint32 port_index, | 75 void ReceiveMidiData(uint32 port_index, |
| 77 const uint8* data, | 76 const uint8* data, |
| 78 size_t size, | 77 size_t size, |
| 79 double timestamp) override {} | 78 double timestamp) override {} |
| 80 void AccumulateMidiBytesSent(size_t size) override {} | 79 void AccumulateMidiBytesSent(size_t size) override {} |
| 81 | 80 |
| 82 MidiResult result() const { return result_; } | 81 Result result() const { return result_; } |
| 83 | 82 |
| 84 MidiResult WaitForResult() { | 83 Result WaitForResult() { |
| 85 while (wait_for_result_) { | 84 while (wait_for_result_) { |
| 86 base::RunLoop run_loop; | 85 base::RunLoop run_loop; |
| 87 run_loop.RunUntilIdle(); | 86 run_loop.RunUntilIdle(); |
| 88 } | 87 } |
| 89 return result(); | 88 return result(); |
| 90 } | 89 } |
| 91 | 90 |
| 92 private: | 91 private: |
| 93 MidiResult result_; | 92 Result result_; |
| 94 bool wait_for_result_; | 93 bool wait_for_result_; |
| 95 | 94 |
| 96 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient); | 95 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient); |
| 97 }; | 96 }; |
| 98 | 97 |
| 99 class MidiManagerTest : public ::testing::Test { | 98 class MidiManagerTest : public ::testing::Test { |
| 100 public: | 99 public: |
| 101 MidiManagerTest() | 100 MidiManagerTest() |
| 102 : manager_(new FakeMidiManager), | 101 : manager_(new FakeMidiManager), |
| 103 message_loop_(new base::MessageLoop) {} | 102 message_loop_(new base::MessageLoop) {} |
| (...skipping 25 matching lines...) Expand all Loading... |
| 129 EXPECT_EQ(nth == 1, manager_->start_initialization_is_called_); | 128 EXPECT_EQ(nth == 1, manager_->start_initialization_is_called_); |
| 130 manager_->start_initialization_is_called_ = true; | 129 manager_->start_initialization_is_called_ = true; |
| 131 } | 130 } |
| 132 | 131 |
| 133 void EndSession(FakeMidiManagerClient* client, size_t before, size_t after) { | 132 void EndSession(FakeMidiManagerClient* client, size_t before, size_t after) { |
| 134 EXPECT_EQ(before, manager_->GetClientCount()); | 133 EXPECT_EQ(before, manager_->GetClientCount()); |
| 135 manager_->EndSession(client); | 134 manager_->EndSession(client); |
| 136 EXPECT_EQ(after, manager_->GetClientCount()); | 135 EXPECT_EQ(after, manager_->GetClientCount()); |
| 137 } | 136 } |
| 138 | 137 |
| 139 void CompleteInitialization(MidiResult result) { | 138 void CompleteInitialization(Result result) { |
| 140 manager_->CallCompleteInitialization(result); | 139 manager_->CallCompleteInitialization(result); |
| 141 } | 140 } |
| 142 | 141 |
| 143 void RunLoopUntilIdle() { | 142 void RunLoopUntilIdle() { |
| 144 base::RunLoop run_loop; | 143 base::RunLoop run_loop; |
| 145 run_loop.RunUntilIdle(); | 144 run_loop.RunUntilIdle(); |
| 146 } | 145 } |
| 147 | 146 |
| 148 protected: | 147 protected: |
| 149 scoped_ptr<FakeMidiManager> manager_; | 148 scoped_ptr<FakeMidiManager> manager_; |
| 150 | 149 |
| 151 private: | 150 private: |
| 152 scoped_ptr<base::MessageLoop> message_loop_; | 151 scoped_ptr<base::MessageLoop> message_loop_; |
| 153 | 152 |
| 154 DISALLOW_COPY_AND_ASSIGN(MidiManagerTest); | 153 DISALLOW_COPY_AND_ASSIGN(MidiManagerTest); |
| 155 }; | 154 }; |
| 156 | 155 |
| 157 TEST_F(MidiManagerTest, StartAndEndSession) { | 156 TEST_F(MidiManagerTest, StartAndEndSession) { |
| 158 scoped_ptr<FakeMidiManagerClient> client; | 157 scoped_ptr<FakeMidiManagerClient> client; |
| 159 client.reset(new FakeMidiManagerClient); | 158 client.reset(new FakeMidiManagerClient); |
| 160 | 159 |
| 161 StartTheFirstSession(client.get()); | 160 StartTheFirstSession(client.get()); |
| 162 CompleteInitialization(MIDI_OK); | 161 CompleteInitialization(Result::OK); |
| 163 EXPECT_EQ(MIDI_OK, client->WaitForResult()); | 162 EXPECT_EQ(Result::OK, client->WaitForResult()); |
| 164 EndSession(client.get(), 1U, 0U); | 163 EndSession(client.get(), 1U, 0U); |
| 165 } | 164 } |
| 166 | 165 |
| 167 TEST_F(MidiManagerTest, StartAndEndSessionWithError) { | 166 TEST_F(MidiManagerTest, StartAndEndSessionWithError) { |
| 168 scoped_ptr<FakeMidiManagerClient> client; | 167 scoped_ptr<FakeMidiManagerClient> client; |
| 169 client.reset(new FakeMidiManagerClient); | 168 client.reset(new FakeMidiManagerClient); |
| 170 | 169 |
| 171 StartTheFirstSession(client.get()); | 170 StartTheFirstSession(client.get()); |
| 172 CompleteInitialization(MIDI_INITIALIZATION_ERROR); | 171 CompleteInitialization(Result::INITIALIZATION_ERROR); |
| 173 EXPECT_EQ(MIDI_INITIALIZATION_ERROR, client->WaitForResult()); | 172 EXPECT_EQ(Result::INITIALIZATION_ERROR, client->WaitForResult()); |
| 174 EndSession(client.get(), 0U, 0U); | 173 EndSession(client.get(), 0U, 0U); |
| 175 } | 174 } |
| 176 | 175 |
| 177 TEST_F(MidiManagerTest, StartMultipleSessions) { | 176 TEST_F(MidiManagerTest, StartMultipleSessions) { |
| 178 scoped_ptr<FakeMidiManagerClient> client1; | 177 scoped_ptr<FakeMidiManagerClient> client1; |
| 179 scoped_ptr<FakeMidiManagerClient> client2; | 178 scoped_ptr<FakeMidiManagerClient> client2; |
| 180 scoped_ptr<FakeMidiManagerClient> client3; | 179 scoped_ptr<FakeMidiManagerClient> client3; |
| 181 client1.reset(new FakeMidiManagerClient); | 180 client1.reset(new FakeMidiManagerClient); |
| 182 client2.reset(new FakeMidiManagerClient); | 181 client2.reset(new FakeMidiManagerClient); |
| 183 client3.reset(new FakeMidiManagerClient); | 182 client3.reset(new FakeMidiManagerClient); |
| 184 | 183 |
| 185 StartTheFirstSession(client1.get()); | 184 StartTheFirstSession(client1.get()); |
| 186 StartTheNthSession(client2.get(), 2); | 185 StartTheNthSession(client2.get(), 2); |
| 187 StartTheNthSession(client3.get(), 3); | 186 StartTheNthSession(client3.get(), 3); |
| 188 CompleteInitialization(MIDI_OK); | 187 CompleteInitialization(Result::OK); |
| 189 EXPECT_EQ(MIDI_OK, client1->WaitForResult()); | 188 EXPECT_EQ(Result::OK, client1->WaitForResult()); |
| 190 EXPECT_EQ(MIDI_OK, client2->WaitForResult()); | 189 EXPECT_EQ(Result::OK, client2->WaitForResult()); |
| 191 EXPECT_EQ(MIDI_OK, client3->WaitForResult()); | 190 EXPECT_EQ(Result::OK, client3->WaitForResult()); |
| 192 EndSession(client1.get(), 3U, 2U); | 191 EndSession(client1.get(), 3U, 2U); |
| 193 EndSession(client2.get(), 2U, 1U); | 192 EndSession(client2.get(), 2U, 1U); |
| 194 EndSession(client3.get(), 1U, 0U); | 193 EndSession(client3.get(), 1U, 0U); |
| 195 } | 194 } |
| 196 | 195 |
| 197 // TODO(toyoshim): Add a test for a MidiManagerClient that has multiple | 196 // TODO(toyoshim): Add a test for a MidiManagerClient that has multiple |
| 198 // sessions with multiple client_id. | 197 // sessions with multiple client_id. |
| 199 | 198 |
| 200 TEST_F(MidiManagerTest, TooManyPendingSessions) { | 199 TEST_F(MidiManagerTest, TooManyPendingSessions) { |
| 201 // Push as many client requests for starting session as possible. | 200 // Push as many client requests for starting session as possible. |
| 202 ScopedVector<FakeMidiManagerClient> many_existing_clients; | 201 ScopedVector<FakeMidiManagerClient> many_existing_clients; |
| 203 many_existing_clients.resize(MidiManager::kMaxPendingClientCount); | 202 many_existing_clients.resize(MidiManager::kMaxPendingClientCount); |
| 204 for (size_t i = 0; i < MidiManager::kMaxPendingClientCount; ++i) { | 203 for (size_t i = 0; i < MidiManager::kMaxPendingClientCount; ++i) { |
| 205 many_existing_clients[i] = new FakeMidiManagerClient; | 204 many_existing_clients[i] = new FakeMidiManagerClient; |
| 206 StartTheNthSession(many_existing_clients[i], i + 1); | 205 StartTheNthSession(many_existing_clients[i], i + 1); |
| 207 } | 206 } |
| 208 | 207 |
| 209 // Push the last client that should be rejected for too many pending requests. | 208 // Push the last client that should be rejected for too many pending requests. |
| 210 scoped_ptr<FakeMidiManagerClient> additional_client( | 209 scoped_ptr<FakeMidiManagerClient> additional_client( |
| 211 new FakeMidiManagerClient); | 210 new FakeMidiManagerClient); |
| 212 manager_->start_initialization_is_called_ = false; | 211 manager_->start_initialization_is_called_ = false; |
| 213 manager_->StartSession(additional_client.get()); | 212 manager_->StartSession(additional_client.get()); |
| 214 EXPECT_FALSE(manager_->start_initialization_is_called_); | 213 EXPECT_FALSE(manager_->start_initialization_is_called_); |
| 215 EXPECT_EQ(MIDI_INITIALIZATION_ERROR, additional_client->result()); | 214 EXPECT_EQ(Result::INITIALIZATION_ERROR, additional_client->result()); |
| 216 | 215 |
| 217 // Other clients still should not receive a result. | 216 // Other clients still should not receive a result. |
| 218 RunLoopUntilIdle(); | 217 RunLoopUntilIdle(); |
| 219 for (size_t i = 0; i < many_existing_clients.size(); ++i) | 218 for (size_t i = 0; i < many_existing_clients.size(); ++i) |
| 220 EXPECT_EQ(MIDI_NOT_SUPPORTED, many_existing_clients[i]->result()); | 219 EXPECT_EQ(Result::NOT_SUPPORTED, many_existing_clients[i]->result()); |
| 221 | 220 |
| 222 // The result MIDI_OK should be distributed to other clients. | 221 // The Result::OK should be distributed to other clients. |
| 223 CompleteInitialization(MIDI_OK); | 222 CompleteInitialization(Result::OK); |
| 224 for (size_t i = 0; i < many_existing_clients.size(); ++i) | 223 for (size_t i = 0; i < many_existing_clients.size(); ++i) |
| 225 EXPECT_EQ(MIDI_OK, many_existing_clients[i]->WaitForResult()); | 224 EXPECT_EQ(Result::OK, many_existing_clients[i]->WaitForResult()); |
| 226 | 225 |
| 227 // Close all successful sessions in FIFO order. | 226 // Close all successful sessions in FIFO order. |
| 228 size_t sessions = many_existing_clients.size(); | 227 size_t sessions = many_existing_clients.size(); |
| 229 for (size_t i = 0; i < many_existing_clients.size(); ++i, --sessions) | 228 for (size_t i = 0; i < many_existing_clients.size(); ++i, --sessions) |
| 230 EndSession(many_existing_clients[i], sessions, sessions - 1); | 229 EndSession(many_existing_clients[i], sessions, sessions - 1); |
| 231 } | 230 } |
| 232 | 231 |
| 233 TEST_F(MidiManagerTest, AbortSession) { | 232 TEST_F(MidiManagerTest, AbortSession) { |
| 234 // A client starting a session can be destructed while an asynchronous | 233 // A client starting a session can be destructed while an asynchronous |
| 235 // initialization is performed. | 234 // initialization is performed. |
| 236 scoped_ptr<FakeMidiManagerClient> client; | 235 scoped_ptr<FakeMidiManagerClient> client; |
| 237 client.reset(new FakeMidiManagerClient); | 236 client.reset(new FakeMidiManagerClient); |
| 238 | 237 |
| 239 StartTheFirstSession(client.get()); | 238 StartTheFirstSession(client.get()); |
| 240 EndSession(client.get(), 0, 0); | 239 EndSession(client.get(), 0, 0); |
| 241 client.reset(); | 240 client.reset(); |
| 242 | 241 |
| 243 // Following function should not call the destructed |client| function. | 242 // Following function should not call the destructed |client| function. |
| 244 CompleteInitialization(MIDI_OK); | 243 CompleteInitialization(Result::OK); |
| 245 base::RunLoop run_loop; | 244 base::RunLoop run_loop; |
| 246 run_loop.RunUntilIdle(); | 245 run_loop.RunUntilIdle(); |
| 247 } | 246 } |
| 248 | 247 |
| 249 TEST_F(MidiManagerTest, CreateMidiManager) { | 248 TEST_F(MidiManagerTest, CreateMidiManager) { |
| 250 // SystemMonitor is needed on Windows. | 249 // SystemMonitor is needed on Windows. |
| 251 base::SystemMonitor system_monitor; | 250 base::SystemMonitor system_monitor; |
| 252 | 251 |
| 253 scoped_ptr<FakeMidiManagerClient> client; | 252 scoped_ptr<FakeMidiManagerClient> client; |
| 254 client.reset(new FakeMidiManagerClient); | 253 client.reset(new FakeMidiManagerClient); |
| 255 | 254 |
| 256 scoped_ptr<MidiManager> manager(MidiManager::Create()); | 255 scoped_ptr<MidiManager> manager(MidiManager::Create()); |
| 257 manager->StartSession(client.get()); | 256 manager->StartSession(client.get()); |
| 258 | 257 |
| 259 MidiResult result = client->WaitForResult(); | 258 Result result = client->WaitForResult(); |
| 260 // This #ifdef needs to be identical to the one in media/midi/midi_manager.cc. | 259 // This #ifdef needs to be identical to the one in media/midi/midi_manager.cc. |
| 261 // Do not change the condition for disabling this test. | 260 // Do not change the condition for disabling this test. |
| 262 #if !defined(OS_MACOSX) && !defined(OS_WIN) && \ | 261 #if !defined(OS_MACOSX) && !defined(OS_WIN) && \ |
| 263 !(defined(USE_ALSA) && defined(USE_UDEV)) && !defined(OS_ANDROID) | 262 !(defined(USE_ALSA) && defined(USE_UDEV)) && !defined(OS_ANDROID) |
| 264 EXPECT_EQ(MIDI_NOT_SUPPORTED, result); | 263 EXPECT_EQ(Result::NOT_SUPPORTED, result); |
| 265 #elif defined(USE_ALSA) | 264 #elif defined(USE_ALSA) |
| 266 // Temporary until http://crbug.com/371230 is resolved. | 265 // Temporary until http://crbug.com/371230 is resolved. |
| 267 EXPECT_TRUE((result == MIDI_OK) || (result == MIDI_INITIALIZATION_ERROR)); | 266 EXPECT_TRUE(result == Result::OK || result == Result::INITIALIZATION_ERROR); |
| 268 #else | 267 #else |
| 269 EXPECT_EQ(MIDI_OK, result); | 268 EXPECT_EQ(Result::OK, result); |
| 270 #endif | 269 #endif |
| 271 } | 270 } |
| 272 | 271 |
| 273 } // namespace | 272 } // namespace |
| 274 | 273 |
| 275 } // namespace midi | 274 } // namespace midi |
| 276 } // namespace media | 275 } // namespace media |
| OLD | NEW |