| 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 {} |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 bool wait_for_result_; | 97 bool wait_for_result_; |
| 94 | 98 |
| 95 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient); | 99 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient); |
| 96 }; | 100 }; |
| 97 | 101 |
| 98 class MidiManagerTest : public ::testing::Test { | 102 class MidiManagerTest : public ::testing::Test { |
| 99 public: | 103 public: |
| 100 MidiManagerTest() | 104 MidiManagerTest() |
| 101 : manager_(new FakeMidiManager), | 105 : manager_(new FakeMidiManager), |
| 102 message_loop_(new base::MessageLoop) {} | 106 message_loop_(new base::MessageLoop) {} |
| 103 ~MidiManagerTest() override {} | 107 ~MidiManagerTest() override { |
| 108 manager_->Shutdown(); |
| 109 base::RunLoop run_loop; |
| 110 run_loop.RunUntilIdle(); |
| 111 EXPECT_EQ(manager_->start_initialization_is_called_, |
| 112 manager_->finalize_is_called_); |
| 113 } |
| 104 | 114 |
| 105 protected: | 115 protected: |
| 106 void StartTheFirstSession(FakeMidiManagerClient* client) { | 116 void StartTheFirstSession(FakeMidiManagerClient* client) { |
| 107 EXPECT_FALSE(manager_->start_initialization_is_called_); | 117 EXPECT_FALSE(manager_->start_initialization_is_called_); |
| 108 EXPECT_EQ(0U, manager_->GetClientCount()); | 118 EXPECT_EQ(0U, manager_->GetClientCount()); |
| 109 EXPECT_EQ(0U, manager_->GetPendingClientCount()); | 119 EXPECT_EQ(0U, manager_->GetPendingClientCount()); |
| 110 manager_->StartSession(client); | 120 manager_->StartSession(client); |
| 111 EXPECT_EQ(0U, manager_->GetClientCount()); | 121 EXPECT_EQ(0U, manager_->GetClientCount()); |
| 112 EXPECT_EQ(1U, manager_->GetPendingClientCount()); | 122 EXPECT_EQ(1U, manager_->GetPendingClientCount()); |
| 113 EXPECT_TRUE(manager_->start_initialization_is_called_); | 123 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. | 207 // sessions with multiple client_id. |
| 198 | 208 |
| 199 TEST_F(MidiManagerTest, TooManyPendingSessions) { | 209 TEST_F(MidiManagerTest, TooManyPendingSessions) { |
| 200 // Push as many client requests for starting session as possible. | 210 // Push as many client requests for starting session as possible. |
| 201 ScopedVector<FakeMidiManagerClient> many_existing_clients; | 211 ScopedVector<FakeMidiManagerClient> many_existing_clients; |
| 202 many_existing_clients.resize(MidiManager::kMaxPendingClientCount); | 212 many_existing_clients.resize(MidiManager::kMaxPendingClientCount); |
| 203 for (size_t i = 0; i < MidiManager::kMaxPendingClientCount; ++i) { | 213 for (size_t i = 0; i < MidiManager::kMaxPendingClientCount; ++i) { |
| 204 many_existing_clients[i] = new FakeMidiManagerClient; | 214 many_existing_clients[i] = new FakeMidiManagerClient; |
| 205 StartTheNthSession(many_existing_clients[i], i + 1); | 215 StartTheNthSession(many_existing_clients[i], i + 1); |
| 206 } | 216 } |
| 217 EXPECT_TRUE(manager_->start_initialization_is_called_); |
| 207 | 218 |
| 208 // Push the last client that should be rejected for too many pending requests. | 219 // Push the last client that should be rejected for too many pending requests. |
| 209 scoped_ptr<FakeMidiManagerClient> additional_client( | 220 scoped_ptr<FakeMidiManagerClient> additional_client( |
| 210 new FakeMidiManagerClient); | 221 new FakeMidiManagerClient); |
| 211 manager_->start_initialization_is_called_ = false; | 222 manager_->start_initialization_is_called_ = false; |
| 212 manager_->StartSession(additional_client.get()); | 223 manager_->StartSession(additional_client.get()); |
| 213 EXPECT_FALSE(manager_->start_initialization_is_called_); | 224 EXPECT_FALSE(manager_->start_initialization_is_called_); |
| 225 manager_->start_initialization_is_called_ = true; |
| 214 EXPECT_EQ(Result::INITIALIZATION_ERROR, additional_client->result()); | 226 EXPECT_EQ(Result::INITIALIZATION_ERROR, additional_client->result()); |
| 215 | 227 |
| 216 // Other clients still should not receive a result. | 228 // Other clients still should not receive a result. |
| 217 RunLoopUntilIdle(); | 229 RunLoopUntilIdle(); |
| 218 for (size_t i = 0; i < many_existing_clients.size(); ++i) | 230 for (size_t i = 0; i < many_existing_clients.size(); ++i) |
| 219 EXPECT_EQ(Result::NOT_SUPPORTED, many_existing_clients[i]->result()); | 231 EXPECT_EQ(Result::NOT_SUPPORTED, many_existing_clients[i]->result()); |
| 220 | 232 |
| 221 // The Result::OK should be distributed to other clients. | 233 // The Result::OK should be distributed to other clients. |
| 222 CompleteInitialization(Result::OK); | 234 CompleteInitialization(Result::OK); |
| 223 for (size_t i = 0; i < many_existing_clients.size(); ++i) | 235 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. | 272 // Do not change the condition for disabling this test. |
| 261 #if !defined(OS_MACOSX) && !defined(OS_WIN) && \ | 273 #if !defined(OS_MACOSX) && !defined(OS_WIN) && \ |
| 262 !(defined(USE_ALSA) && defined(USE_UDEV)) && !defined(OS_ANDROID) | 274 !(defined(USE_ALSA) && defined(USE_UDEV)) && !defined(OS_ANDROID) |
| 263 EXPECT_EQ(Result::NOT_SUPPORTED, result); | 275 EXPECT_EQ(Result::NOT_SUPPORTED, result); |
| 264 #elif defined(USE_ALSA) | 276 #elif defined(USE_ALSA) |
| 265 // Temporary until http://crbug.com/371230 is resolved. | 277 // Temporary until http://crbug.com/371230 is resolved. |
| 266 EXPECT_TRUE(result == Result::OK || result == Result::INITIALIZATION_ERROR); | 278 EXPECT_TRUE(result == Result::OK || result == Result::INITIALIZATION_ERROR); |
| 267 #else | 279 #else |
| 268 EXPECT_EQ(Result::OK, result); | 280 EXPECT_EQ(Result::OK, result); |
| 269 #endif | 281 #endif |
| 282 |
| 283 manager->Shutdown(); |
| 284 base::RunLoop run_loop; |
| 285 run_loop.RunUntilIdle(); |
| 270 } | 286 } |
| 271 | 287 |
| 288 // TODO(toyoshim): Add multi-threaded unit tests to check races around |
| 289 // StartInitialization(), CompleteInitialization(), and Finalize(). |
| 290 |
| 272 } // namespace | 291 } // namespace |
| 273 | 292 |
| 274 } // namespace midi | 293 } // namespace midi |
| 275 } // namespace media | 294 } // namespace media |
| OLD | NEW |