| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
| 17 #include "base/memory/scoped_vector.h" |
| 17 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
| 18 #include "base/run_loop.h" | 19 #include "base/run_loop.h" |
| 19 #include "base/system_monitor/system_monitor.h" | 20 #include "base/system_monitor/system_monitor.h" |
| 20 #include "build/build_config.h" | 21 #include "build/build_config.h" |
| 21 #include "media/midi/midi_service.h" | 22 #include "media/midi/midi_service.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 24 |
| 24 namespace midi { | 25 namespace midi { |
| 25 | 26 |
| 26 namespace { | 27 namespace { |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 EndSession(client1.get(), 3U, 2U); | 215 EndSession(client1.get(), 3U, 2U); |
| 215 EndSession(client2.get(), 2U, 1U); | 216 EndSession(client2.get(), 2U, 1U); |
| 216 EndSession(client3.get(), 1U, 0U); | 217 EndSession(client3.get(), 1U, 0U); |
| 217 } | 218 } |
| 218 | 219 |
| 219 // TODO(toyoshim): Add a test for a MidiManagerClient that has multiple | 220 // TODO(toyoshim): Add a test for a MidiManagerClient that has multiple |
| 220 // sessions with multiple client_id. | 221 // sessions with multiple client_id. |
| 221 | 222 |
| 222 TEST_F(MidiManagerTest, TooManyPendingSessions) { | 223 TEST_F(MidiManagerTest, TooManyPendingSessions) { |
| 223 // Push as many client requests for starting session as possible. | 224 // Push as many client requests for starting session as possible. |
| 224 std::vector<std::unique_ptr<FakeMidiManagerClient>> many_existing_clients; | 225 ScopedVector<FakeMidiManagerClient> many_existing_clients; |
| 225 many_existing_clients.resize(MidiManager::kMaxPendingClientCount); | 226 many_existing_clients.resize(MidiManager::kMaxPendingClientCount); |
| 226 for (size_t i = 0; i < MidiManager::kMaxPendingClientCount; ++i) { | 227 for (size_t i = 0; i < MidiManager::kMaxPendingClientCount; ++i) { |
| 227 many_existing_clients[i] = base::MakeUnique<FakeMidiManagerClient>(); | 228 many_existing_clients[i] = new FakeMidiManagerClient; |
| 228 StartTheNthSession(many_existing_clients[i].get(), i + 1); | 229 StartTheNthSession(many_existing_clients[i], i + 1); |
| 229 } | 230 } |
| 230 EXPECT_TRUE(manager_->start_initialization_is_called_); | 231 EXPECT_TRUE(manager_->start_initialization_is_called_); |
| 231 | 232 |
| 232 // Push the last client that should be rejected for too many pending requests. | 233 // Push the last client that should be rejected for too many pending requests. |
| 233 std::unique_ptr<FakeMidiManagerClient> additional_client( | 234 std::unique_ptr<FakeMidiManagerClient> additional_client( |
| 234 new FakeMidiManagerClient); | 235 new FakeMidiManagerClient); |
| 235 manager_->start_initialization_is_called_ = false; | 236 manager_->start_initialization_is_called_ = false; |
| 236 manager_->StartSession(additional_client.get()); | 237 manager_->StartSession(additional_client.get()); |
| 237 EXPECT_FALSE(manager_->start_initialization_is_called_); | 238 EXPECT_FALSE(manager_->start_initialization_is_called_); |
| 238 manager_->start_initialization_is_called_ = true; | 239 manager_->start_initialization_is_called_ = true; |
| 239 EXPECT_EQ(Result::INITIALIZATION_ERROR, additional_client->result()); | 240 EXPECT_EQ(Result::INITIALIZATION_ERROR, additional_client->result()); |
| 240 | 241 |
| 241 // Other clients still should not receive a result. | 242 // Other clients still should not receive a result. |
| 242 RunLoopUntilIdle(); | 243 RunLoopUntilIdle(); |
| 243 for (size_t i = 0; i < many_existing_clients.size(); ++i) | 244 for (size_t i = 0; i < many_existing_clients.size(); ++i) |
| 244 EXPECT_EQ(Result::NOT_SUPPORTED, many_existing_clients[i]->result()); | 245 EXPECT_EQ(Result::NOT_SUPPORTED, many_existing_clients[i]->result()); |
| 245 | 246 |
| 246 // The Result::OK should be distributed to other clients. | 247 // The Result::OK should be distributed to other clients. |
| 247 CompleteInitialization(Result::OK); | 248 CompleteInitialization(Result::OK); |
| 248 for (size_t i = 0; i < many_existing_clients.size(); ++i) | 249 for (size_t i = 0; i < many_existing_clients.size(); ++i) |
| 249 EXPECT_EQ(Result::OK, many_existing_clients[i]->WaitForResult()); | 250 EXPECT_EQ(Result::OK, many_existing_clients[i]->WaitForResult()); |
| 250 | 251 |
| 251 // Close all successful sessions in FIFO order. | 252 // Close all successful sessions in FIFO order. |
| 252 size_t sessions = many_existing_clients.size(); | 253 size_t sessions = many_existing_clients.size(); |
| 253 for (size_t i = 0; i < many_existing_clients.size(); ++i, --sessions) | 254 for (size_t i = 0; i < many_existing_clients.size(); ++i, --sessions) |
| 254 EndSession(many_existing_clients[i].get(), sessions, sessions - 1); | 255 EndSession(many_existing_clients[i], sessions, sessions - 1); |
| 255 } | 256 } |
| 256 | 257 |
| 257 TEST_F(MidiManagerTest, AbortSession) { | 258 TEST_F(MidiManagerTest, AbortSession) { |
| 258 // A client starting a session can be destructed while an asynchronous | 259 // A client starting a session can be destructed while an asynchronous |
| 259 // initialization is performed. | 260 // initialization is performed. |
| 260 std::unique_ptr<FakeMidiManagerClient> client; | 261 std::unique_ptr<FakeMidiManagerClient> client; |
| 261 client.reset(new FakeMidiManagerClient); | 262 client.reset(new FakeMidiManagerClient); |
| 262 | 263 |
| 263 StartTheFirstSession(client.get()); | 264 StartTheFirstSession(client.get()); |
| 264 EndSession(client.get(), 0, 0); | 265 EndSession(client.get(), 0, 0); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 base::RunLoop run_loop; | 298 base::RunLoop run_loop; |
| 298 run_loop.RunUntilIdle(); | 299 run_loop.RunUntilIdle(); |
| 299 } | 300 } |
| 300 | 301 |
| 301 // TODO(toyoshim): Add multi-threaded unit tests to check races around | 302 // TODO(toyoshim): Add multi-threaded unit tests to check races around |
| 302 // StartInitialization(), CompleteInitialization(), and Finalize(). | 303 // StartInitialization(), CompleteInitialization(), and Finalize(). |
| 303 | 304 |
| 304 } // namespace | 305 } // namespace |
| 305 | 306 |
| 306 } // namespace midi | 307 } // namespace midi |
| OLD | NEW |