| 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/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 uint32 port_index, | 30 uint32 port_index, |
| 31 const std::vector<uint8>& data, | 31 const std::vector<uint8>& data, |
| 32 double timestamp) OVERRIDE {} | 32 double timestamp) OVERRIDE {} |
| 33 | 33 |
| 34 // Utility functions for testing. | 34 // Utility functions for testing. |
| 35 void CallCompleteInitialization(MidiResult result) { | 35 void CallCompleteInitialization(MidiResult result) { |
| 36 CompleteInitialization(result); | 36 CompleteInitialization(result); |
| 37 } | 37 } |
| 38 | 38 |
| 39 size_t GetClientCount() const { | 39 size_t GetClientCount() const { |
| 40 return get_clients_size_for_testing(); | 40 return clients_size_for_testing(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 size_t GetPendingClientCount() const { | 43 size_t GetPendingClientCount() const { |
| 44 return get_pending_clients_size_for_testing(); | 44 return pending_clients_size_for_testing(); |
| 45 } | 45 } |
| 46 | 46 |
| 47 bool start_initialization_is_called_; | 47 bool start_initialization_is_called_; |
| 48 | 48 |
| 49 private: | 49 private: |
| 50 DISALLOW_COPY_AND_ASSIGN(FakeMidiManager); | 50 DISALLOW_COPY_AND_ASSIGN(FakeMidiManager); |
| 51 }; | 51 }; |
| 52 | 52 |
| 53 class FakeMidiManagerClient : public MidiManagerClient { | 53 class FakeMidiManagerClient : public MidiManagerClient { |
| 54 public: | 54 public: |
| 55 explicit FakeMidiManagerClient(int client_id) | 55 explicit FakeMidiManagerClient(int client_id) |
| 56 : client_id_(client_id), | 56 : client_id_(client_id), |
| 57 result_(MIDI_NOT_SUPPORTED), | 57 result_(MIDI_NOT_SUPPORTED), |
| 58 wait_for_result_(true) {} | 58 wait_for_result_(true) {} |
| 59 virtual ~FakeMidiManagerClient() {} | 59 virtual ~FakeMidiManagerClient() {} |
| 60 | 60 |
| 61 // MidiManagerClient implementation. | 61 // MidiManagerClient implementation. |
| 62 virtual void CompleteStartSession(int client_id, MidiResult result) OVERRIDE { | 62 virtual void CompleteStartSession(int client_id, MidiResult result) OVERRIDE { |
| 63 EXPECT_TRUE(wait_for_result_); | 63 EXPECT_TRUE(wait_for_result_); |
| 64 CHECK_EQ(client_id_, client_id); | 64 CHECK_EQ(client_id_, client_id); |
| 65 result_ = result; | 65 result_ = result; |
| 66 wait_for_result_ = false; | 66 wait_for_result_ = false; |
| 67 } | 67 } |
| 68 | 68 |
| 69 virtual void ReceiveMidiData(uint32 port_index, const uint8* data, | 69 virtual void ReceiveMidiData(uint32 port_index, const uint8* data, |
| 70 size_t size, double timestamp) OVERRIDE {} | 70 size_t size, double timestamp) OVERRIDE {} |
| 71 virtual void AccumulateMidiBytesSent(size_t size) OVERRIDE {} | 71 virtual void AccumulateMidiBytesSent(size_t size) OVERRIDE {} |
| 72 | 72 |
| 73 int get_client_id() const { return client_id_; } | 73 int client_id() const { return client_id_; } |
| 74 MidiResult get_result() const { return result_; } | 74 MidiResult result() const { return result_; } |
| 75 | 75 |
| 76 MidiResult WaitForResult() { | 76 MidiResult WaitForResult() { |
| 77 base::RunLoop run_loop; | 77 base::RunLoop run_loop; |
| 78 // CompleteStartSession() is called inside the message loop on the same | 78 // CompleteStartSession() is called inside the message loop on the same |
| 79 // thread. Protection for |wait_for_result_| is not needed. | 79 // thread. Protection for |wait_for_result_| is not needed. |
| 80 while (wait_for_result_) | 80 while (wait_for_result_) |
| 81 run_loop.RunUntilIdle(); | 81 run_loop.RunUntilIdle(); |
| 82 return get_result(); | 82 return result(); |
| 83 } | 83 } |
| 84 | 84 |
| 85 private: | 85 private: |
| 86 int client_id_; | 86 int client_id_; |
| 87 MidiResult result_; | 87 MidiResult result_; |
| 88 bool wait_for_result_; | 88 bool wait_for_result_; |
| 89 | 89 |
| 90 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient); | 90 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient); |
| 91 }; | 91 }; |
| 92 | 92 |
| 93 class MidiManagerTest : public ::testing::Test { | 93 class MidiManagerTest : public ::testing::Test { |
| 94 public: | 94 public: |
| 95 MidiManagerTest() | 95 MidiManagerTest() |
| 96 : manager_(new FakeMidiManager), | 96 : manager_(new FakeMidiManager), |
| 97 message_loop_(new base::MessageLoop) {} | 97 message_loop_(new base::MessageLoop) {} |
| 98 virtual ~MidiManagerTest() {} | 98 virtual ~MidiManagerTest() {} |
| 99 | 99 |
| 100 protected: | 100 protected: |
| 101 void StartTheFirstSession(FakeMidiManagerClient* client) { | 101 void StartTheFirstSession(FakeMidiManagerClient* client) { |
| 102 EXPECT_FALSE(manager_->start_initialization_is_called_); | 102 EXPECT_FALSE(manager_->start_initialization_is_called_); |
| 103 EXPECT_EQ(0U, manager_->GetClientCount()); | 103 EXPECT_EQ(0U, manager_->GetClientCount()); |
| 104 EXPECT_EQ(0U, manager_->GetPendingClientCount()); | 104 EXPECT_EQ(0U, manager_->GetPendingClientCount()); |
| 105 manager_->StartSession(client, client->get_client_id()); | 105 manager_->StartSession(client, client->client_id()); |
| 106 EXPECT_EQ(0U, manager_->GetClientCount()); | 106 EXPECT_EQ(0U, manager_->GetClientCount()); |
| 107 EXPECT_EQ(1U, manager_->GetPendingClientCount()); | 107 EXPECT_EQ(1U, manager_->GetPendingClientCount()); |
| 108 EXPECT_TRUE(manager_->start_initialization_is_called_); | 108 EXPECT_TRUE(manager_->start_initialization_is_called_); |
| 109 EXPECT_EQ(0U, manager_->GetClientCount()); | 109 EXPECT_EQ(0U, manager_->GetClientCount()); |
| 110 EXPECT_EQ(1U, manager_->GetPendingClientCount()); | 110 EXPECT_EQ(1U, manager_->GetPendingClientCount()); |
| 111 EXPECT_TRUE(manager_->start_initialization_is_called_); | 111 EXPECT_TRUE(manager_->start_initialization_is_called_); |
| 112 } | 112 } |
| 113 | 113 |
| 114 void StartTheNthSession(FakeMidiManagerClient* client, size_t nth) { | 114 void StartTheNthSession(FakeMidiManagerClient* client, size_t nth) { |
| 115 EXPECT_EQ(nth != 1, manager_->start_initialization_is_called_); | 115 EXPECT_EQ(nth != 1, manager_->start_initialization_is_called_); |
| 116 EXPECT_EQ(0U, manager_->GetClientCount()); | 116 EXPECT_EQ(0U, manager_->GetClientCount()); |
| 117 EXPECT_EQ(nth - 1, manager_->GetPendingClientCount()); | 117 EXPECT_EQ(nth - 1, manager_->GetPendingClientCount()); |
| 118 | 118 |
| 119 // StartInitialization() should not be called for the second and later | 119 // StartInitialization() should not be called for the second and later |
| 120 // sessions. | 120 // sessions. |
| 121 manager_->start_initialization_is_called_ = false; | 121 manager_->start_initialization_is_called_ = false; |
| 122 manager_->StartSession(client, client->get_client_id()); | 122 manager_->StartSession(client, client->client_id()); |
| 123 EXPECT_EQ(nth == 1, manager_->start_initialization_is_called_); | 123 EXPECT_EQ(nth == 1, manager_->start_initialization_is_called_); |
| 124 manager_->start_initialization_is_called_ = true; | 124 manager_->start_initialization_is_called_ = true; |
| 125 } | 125 } |
| 126 | 126 |
| 127 void EndSession(FakeMidiManagerClient* client, size_t before, size_t after) { | 127 void EndSession(FakeMidiManagerClient* client, size_t before, size_t after) { |
| 128 EXPECT_EQ(before, manager_->GetClientCount()); | 128 EXPECT_EQ(before, manager_->GetClientCount()); |
| 129 manager_->EndSession(client); | 129 manager_->EndSession(client); |
| 130 EXPECT_EQ(after, manager_->GetClientCount()); | 130 EXPECT_EQ(after, manager_->GetClientCount()); |
| 131 } | 131 } |
| 132 | 132 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 for (size_t i = 0; i < MidiManager::kMaxPendingClientCount; ++i) { | 190 for (size_t i = 0; i < MidiManager::kMaxPendingClientCount; ++i) { |
| 191 many_existing_clients[i] = new FakeMidiManagerClient(i); | 191 many_existing_clients[i] = new FakeMidiManagerClient(i); |
| 192 StartTheNthSession(many_existing_clients[i], i + 1); | 192 StartTheNthSession(many_existing_clients[i], i + 1); |
| 193 } | 193 } |
| 194 | 194 |
| 195 // Push the last client that should be rejected for too many pending requests. | 195 // Push the last client that should be rejected for too many pending requests. |
| 196 scoped_ptr<FakeMidiManagerClient> additional_client( | 196 scoped_ptr<FakeMidiManagerClient> additional_client( |
| 197 new FakeMidiManagerClient(MidiManager::kMaxPendingClientCount)); | 197 new FakeMidiManagerClient(MidiManager::kMaxPendingClientCount)); |
| 198 manager_->start_initialization_is_called_ = false; | 198 manager_->start_initialization_is_called_ = false; |
| 199 manager_->StartSession(additional_client.get(), | 199 manager_->StartSession(additional_client.get(), |
| 200 additional_client->get_client_id()); | 200 additional_client->client_id()); |
| 201 EXPECT_FALSE(manager_->start_initialization_is_called_); | 201 EXPECT_FALSE(manager_->start_initialization_is_called_); |
| 202 EXPECT_EQ(MIDI_INITIALIZATION_ERROR, additional_client->get_result()); | 202 EXPECT_EQ(MIDI_INITIALIZATION_ERROR, additional_client->result()); |
| 203 | 203 |
| 204 // Other clients still should not receive a result. | 204 // Other clients still should not receive a result. |
| 205 RunLoopUntilIdle(); | 205 RunLoopUntilIdle(); |
| 206 for (size_t i = 0; i < many_existing_clients.size(); ++i) | 206 for (size_t i = 0; i < many_existing_clients.size(); ++i) |
| 207 EXPECT_EQ(MIDI_NOT_SUPPORTED, many_existing_clients[i]->get_result()); | 207 EXPECT_EQ(MIDI_NOT_SUPPORTED, many_existing_clients[i]->result()); |
| 208 | 208 |
| 209 // The result MIDI_OK should be distributed to other clients. | 209 // The result MIDI_OK should be distributed to other clients. |
| 210 CompleteInitialization(MIDI_OK); | 210 CompleteInitialization(MIDI_OK); |
| 211 for (size_t i = 0; i < many_existing_clients.size(); ++i) | 211 for (size_t i = 0; i < many_existing_clients.size(); ++i) |
| 212 EXPECT_EQ(MIDI_OK, many_existing_clients[i]->WaitForResult()); | 212 EXPECT_EQ(MIDI_OK, many_existing_clients[i]->WaitForResult()); |
| 213 | 213 |
| 214 // Close all successful sessions in FIFO order. | 214 // Close all successful sessions in FIFO order. |
| 215 size_t sessions = many_existing_clients.size(); | 215 size_t sessions = many_existing_clients.size(); |
| 216 for (size_t i = 0; i < many_existing_clients.size(); ++i, --sessions) | 216 for (size_t i = 0; i < many_existing_clients.size(); ++i, --sessions) |
| 217 EndSession(many_existing_clients[i], sessions, sessions - 1); | 217 EndSession(many_existing_clients[i], sessions, sessions - 1); |
| 218 } | 218 } |
| 219 | 219 |
| 220 TEST_F(MidiManagerTest, CreateMidiManager) { | 220 TEST_F(MidiManagerTest, CreateMidiManager) { |
| 221 scoped_ptr<FakeMidiManagerClient> client; | 221 scoped_ptr<FakeMidiManagerClient> client; |
| 222 client.reset(new FakeMidiManagerClient(0)); | 222 client.reset(new FakeMidiManagerClient(0)); |
| 223 | 223 |
| 224 scoped_ptr<MidiManager> manager(MidiManager::Create()); | 224 scoped_ptr<MidiManager> manager(MidiManager::Create()); |
| 225 manager->StartSession(client.get(), client->get_client_id()); | 225 manager->StartSession(client.get(), client->client_id()); |
| 226 // This #ifdef needs to be identical to the one in media/midi/midi_manager.cc | 226 |
| 227 // This #ifdef needs to be identical to the one in media/midi/midi_manager.cc. |
| 228 // Do not change the condition for disabling this test. |
| 227 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(USE_ALSA) && \ | 229 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(USE_ALSA) && \ |
| 228 !defined(OS_ANDROID) && !defined(OS_CHROMEOS) | 230 !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
| 229 EXPECT_EQ(MIDI_NOT_SUPPORTED, client->WaitForResult()); | 231 EXPECT_EQ(MIDI_NOT_SUPPORTED, client->WaitForResult()); |
| 230 #else | 232 #else |
| 231 EXPECT_EQ(MIDI_OK, client->WaitForResult()); | 233 EXPECT_EQ(MIDI_OK, client->WaitForResult()); |
| 232 #endif | 234 #endif |
| 233 } | 235 } |
| 234 | 236 |
| 235 } // namespace | 237 } // namespace |
| 236 | 238 |
| 237 } // namespace media | 239 } // namespace media |
| OLD | NEW |