| 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" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/system_monitor/system_monitor.h" | 15 #include "base/system_monitor/system_monitor.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 namespace media { | 18 namespace media { |
| 19 namespace midi { | 19 namespace midi { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 class FakeMidiManager : public MidiManager { | 23 class FakeMidiManager : public MidiManager { |
| 24 public: | 24 public: |
| 25 FakeMidiManager() : start_initialization_is_called_(false) {} | 25 FakeMidiManager() |
| 26 : start_initialization_is_called_(false), finalize_is_called_(false) {} |
| 26 ~FakeMidiManager() override {} | 27 ~FakeMidiManager() override {} |
| 27 | 28 |
| 28 // MidiManager implementation. | 29 // MidiManager implementation. |
| 29 void StartInitialization() override { | 30 void StartInitialization() override { |
| 30 start_initialization_is_called_ = true; | 31 start_initialization_is_called_ = true; |
| 31 } | 32 } |
| 32 | 33 |
| 34 void Finalize() override { finalize_is_called_ = true; } |
| 35 |
| 33 void DispatchSendMidiData(MidiManagerClient* client, | 36 void DispatchSendMidiData(MidiManagerClient* client, |
| 34 uint32 port_index, | 37 uint32 port_index, |
| 35 const std::vector<uint8>& data, | 38 const std::vector<uint8>& data, |
| 36 double timestamp) override {} | 39 double timestamp) override {} |
| 37 | 40 |
| 38 // Utility functions for testing. | 41 // Utility functions for testing. |
| 39 void CallCompleteInitialization(Result result) { | 42 void CallCompleteInitialization(Result result) { |
| 40 CompleteInitialization(result); | 43 CompleteInitialization(result); |
| 41 } | 44 } |
| 42 | 45 |
| 43 size_t GetClientCount() const { | 46 size_t GetClientCount() const { |
| 44 return clients_size_for_testing(); | 47 return clients_size_for_testing(); |
| 45 } | 48 } |
| 46 | 49 |
| 47 size_t GetPendingClientCount() const { | 50 size_t GetPendingClientCount() const { |
| 48 return pending_clients_size_for_testing(); | 51 return pending_clients_size_for_testing(); |
| 49 } | 52 } |
| 50 | 53 |
| 51 bool start_initialization_is_called_; | 54 bool start_initialization_is_called_; |
| 55 bool finalize_is_called_; |
| 52 | 56 |
| 53 private: | 57 private: |
| 54 DISALLOW_COPY_AND_ASSIGN(FakeMidiManager); | 58 DISALLOW_COPY_AND_ASSIGN(FakeMidiManager); |
| 55 }; | 59 }; |
| 56 | 60 |
| 57 class FakeMidiManagerClient : public MidiManagerClient { | 61 class FakeMidiManagerClient : public MidiManagerClient { |
| 58 public: | 62 public: |
| 59 FakeMidiManagerClient() | 63 FakeMidiManagerClient() |
| 60 : result_(Result::NOT_SUPPORTED), wait_for_result_(true) {} | 64 : result_(Result::NOT_SUPPORTED), wait_for_result_(true) {} |
| 61 ~FakeMidiManagerClient() override {} | 65 ~FakeMidiManagerClient() override {} |
| 62 | 66 |
| 63 // MidiManagerClient implementation. | 67 // MidiManagerClient implementation. |
| 64 void AddInputPort(const MidiPortInfo& info) override {} | 68 void AddInputPort(const MidiPortInfo& info) override {} |
| 65 void AddOutputPort(const MidiPortInfo& info) override {} | 69 void AddOutputPort(const MidiPortInfo& info) override {} |
| 66 void SetInputPortState(uint32 port_index, MidiPortState state) override {} | 70 void SetInputPortState(uint32 port_index, MidiPortState state) override {} |
| 67 void SetOutputPortState(uint32 port_index, MidiPortState state) override {} | 71 void SetOutputPortState(uint32 port_index, MidiPortState state) override {} |
| 68 | 72 |
| 69 void CompleteStartSession(Result result) override { | 73 void CompleteStartSession(Result result) override { |
| 70 EXPECT_TRUE(wait_for_result_); | 74 EXPECT_TRUE(wait_for_result_); |
| 71 result_ = result; | 75 result_ = result; |
| 72 wait_for_result_ = false; | 76 wait_for_result_ = false; |
| 73 } | 77 } |
| 74 | 78 |
| 75 void ReceiveMidiData(uint32 port_index, | 79 void ReceiveMidiData(uint32 port_index, |
| 76 const uint8* data, | 80 const uint8* data, |
| 77 size_t size, | 81 size_t size, |
| 78 double timestamp) override {} | 82 double timestamp) override {} |
| 79 void AccumulateMidiBytesSent(size_t size) override {} | 83 void AccumulateMidiBytesSent(size_t size) override {} |
| 84 void Detached() override {} |
| 80 | 85 |
| 81 Result result() const { return result_; } | 86 Result result() const { return result_; } |
| 82 | 87 |
| 83 Result WaitForResult() { | 88 Result WaitForResult() { |
| 84 while (wait_for_result_) { | 89 while (wait_for_result_) { |
| 85 base::RunLoop run_loop; | 90 base::RunLoop run_loop; |
| 86 run_loop.RunUntilIdle(); | 91 run_loop.RunUntilIdle(); |
| 87 } | 92 } |
| 88 return result(); | 93 return result(); |
| 89 } | 94 } |
| 90 | 95 |
| 91 private: | 96 private: |
| 92 Result result_; | 97 Result result_; |
| 93 bool wait_for_result_; | 98 bool wait_for_result_; |
| 94 | 99 |
| 95 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient); | 100 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient); |
| 96 }; | 101 }; |
| 97 | 102 |
| 98 class MidiManagerTest : public ::testing::Test { | 103 class MidiManagerTest : public ::testing::Test { |
| 99 public: | 104 public: |
| 100 MidiManagerTest() | 105 MidiManagerTest() |
| 101 : manager_(new FakeMidiManager), | 106 : manager_(new FakeMidiManager), |
| 102 message_loop_(new base::MessageLoop) {} | 107 message_loop_(new base::MessageLoop) {} |
| 103 ~MidiManagerTest() override {} | 108 ~MidiManagerTest() override { |
| 109 manager_->Shutdown(); |
| 110 base::RunLoop run_loop; |
| 111 run_loop.RunUntilIdle(); |
| 112 EXPECT_EQ(manager_->start_initialization_is_called_, |
| 113 manager_->finalize_is_called_); |
| 114 } |
| 104 | 115 |
| 105 protected: | 116 protected: |
| 106 void StartTheFirstSession(FakeMidiManagerClient* client) { | 117 void StartTheFirstSession(FakeMidiManagerClient* client) { |
| 107 EXPECT_FALSE(manager_->start_initialization_is_called_); | 118 EXPECT_FALSE(manager_->start_initialization_is_called_); |
| 108 EXPECT_EQ(0U, manager_->GetClientCount()); | 119 EXPECT_EQ(0U, manager_->GetClientCount()); |
| 109 EXPECT_EQ(0U, manager_->GetPendingClientCount()); | 120 EXPECT_EQ(0U, manager_->GetPendingClientCount()); |
| 110 manager_->StartSession(client); | 121 manager_->StartSession(client); |
| 111 EXPECT_EQ(0U, manager_->GetClientCount()); | 122 EXPECT_EQ(0U, manager_->GetClientCount()); |
| 112 EXPECT_EQ(1U, manager_->GetPendingClientCount()); | 123 EXPECT_EQ(1U, manager_->GetPendingClientCount()); |
| 113 EXPECT_TRUE(manager_->start_initialization_is_called_); | 124 EXPECT_TRUE(manager_->start_initialization_is_called_); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 // sessions with multiple client_id. | 208 // sessions with multiple client_id. |
| 198 | 209 |
| 199 TEST_F(MidiManagerTest, TooManyPendingSessions) { | 210 TEST_F(MidiManagerTest, TooManyPendingSessions) { |
| 200 // Push as many client requests for starting session as possible. | 211 // Push as many client requests for starting session as possible. |
| 201 ScopedVector<FakeMidiManagerClient> many_existing_clients; | 212 ScopedVector<FakeMidiManagerClient> many_existing_clients; |
| 202 many_existing_clients.resize(MidiManager::kMaxPendingClientCount); | 213 many_existing_clients.resize(MidiManager::kMaxPendingClientCount); |
| 203 for (size_t i = 0; i < MidiManager::kMaxPendingClientCount; ++i) { | 214 for (size_t i = 0; i < MidiManager::kMaxPendingClientCount; ++i) { |
| 204 many_existing_clients[i] = new FakeMidiManagerClient; | 215 many_existing_clients[i] = new FakeMidiManagerClient; |
| 205 StartTheNthSession(many_existing_clients[i], i + 1); | 216 StartTheNthSession(many_existing_clients[i], i + 1); |
| 206 } | 217 } |
| 218 EXPECT_TRUE(manager_->start_initialization_is_called_); |
| 207 | 219 |
| 208 // Push the last client that should be rejected for too many pending requests. | 220 // Push the last client that should be rejected for too many pending requests. |
| 209 scoped_ptr<FakeMidiManagerClient> additional_client( | 221 scoped_ptr<FakeMidiManagerClient> additional_client( |
| 210 new FakeMidiManagerClient); | 222 new FakeMidiManagerClient); |
| 211 manager_->start_initialization_is_called_ = false; | 223 manager_->start_initialization_is_called_ = false; |
| 212 manager_->StartSession(additional_client.get()); | 224 manager_->StartSession(additional_client.get()); |
| 213 EXPECT_FALSE(manager_->start_initialization_is_called_); | 225 EXPECT_FALSE(manager_->start_initialization_is_called_); |
| 226 manager_->start_initialization_is_called_ = true; |
| 214 EXPECT_EQ(Result::INITIALIZATION_ERROR, additional_client->result()); | 227 EXPECT_EQ(Result::INITIALIZATION_ERROR, additional_client->result()); |
| 215 | 228 |
| 216 // Other clients still should not receive a result. | 229 // Other clients still should not receive a result. |
| 217 RunLoopUntilIdle(); | 230 RunLoopUntilIdle(); |
| 218 for (size_t i = 0; i < many_existing_clients.size(); ++i) | 231 for (size_t i = 0; i < many_existing_clients.size(); ++i) |
| 219 EXPECT_EQ(Result::NOT_SUPPORTED, many_existing_clients[i]->result()); | 232 EXPECT_EQ(Result::NOT_SUPPORTED, many_existing_clients[i]->result()); |
| 220 | 233 |
| 221 // The Result::OK should be distributed to other clients. | 234 // The Result::OK should be distributed to other clients. |
| 222 CompleteInitialization(Result::OK); | 235 CompleteInitialization(Result::OK); |
| 223 for (size_t i = 0; i < many_existing_clients.size(); ++i) | 236 for (size_t i = 0; i < many_existing_clients.size(); ++i) |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 // Do not change the condition for disabling this test. | 273 // Do not change the condition for disabling this test. |
| 261 #if !defined(OS_MACOSX) && !defined(OS_WIN) && \ | 274 #if !defined(OS_MACOSX) && !defined(OS_WIN) && \ |
| 262 !(defined(USE_ALSA) && defined(USE_UDEV)) && !defined(OS_ANDROID) | 275 !(defined(USE_ALSA) && defined(USE_UDEV)) && !defined(OS_ANDROID) |
| 263 EXPECT_EQ(Result::NOT_SUPPORTED, result); | 276 EXPECT_EQ(Result::NOT_SUPPORTED, result); |
| 264 #elif defined(USE_ALSA) | 277 #elif defined(USE_ALSA) |
| 265 // Temporary until http://crbug.com/371230 is resolved. | 278 // Temporary until http://crbug.com/371230 is resolved. |
| 266 EXPECT_TRUE(result == Result::OK || result == Result::INITIALIZATION_ERROR); | 279 EXPECT_TRUE(result == Result::OK || result == Result::INITIALIZATION_ERROR); |
| 267 #else | 280 #else |
| 268 EXPECT_EQ(Result::OK, result); | 281 EXPECT_EQ(Result::OK, result); |
| 269 #endif | 282 #endif |
| 283 |
| 284 manager->Shutdown(); |
| 285 base::RunLoop run_loop; |
| 286 run_loop.RunUntilIdle(); |
| 270 } | 287 } |
| 271 | 288 |
| 289 // TODO(toyoshim): Add multi-threaded unit tests to check races around |
| 290 // StartInitialization(), CompleteInitialization(), and Finalize(). |
| 291 |
| 272 } // namespace | 292 } // namespace |
| 273 | 293 |
| 274 } // namespace midi | 294 } // namespace midi |
| 275 } // namespace media | 295 } // namespace media |
| OLD | NEW |