Chromium Code Reviews| Index: media/midi/midi_manager_unittest.cc |
| diff --git a/media/midi/midi_manager_unittest.cc b/media/midi/midi_manager_unittest.cc |
| index 456c2b0988ebcadf5931dcd11cc9b23231eb887e..5811665fed1ee21344a6dfb72768c03c266cdf49 100644 |
| --- a/media/midi/midi_manager_unittest.cc |
| +++ b/media/midi/midi_manager_unittest.cc |
| @@ -4,8 +4,11 @@ |
| #include "media/midi/midi_manager.h" |
| +#include <vector> |
| + |
| #include "base/logging.h" |
| #include "base/memory/scoped_ptr.h" |
| +#include "base/memory/scoped_vector.h" |
| #include "base/run_loop.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| @@ -57,6 +60,7 @@ class FakeMidiManagerClient : public MidiManagerClient { |
| // MidiManagerClient implementation. |
| virtual void CompleteStartSession(int client_id, MidiResult result) OVERRIDE { |
| + EXPECT_TRUE(wait_for_result_); |
| CHECK_EQ(client_id_, client_id); |
| result_ = result; |
| wait_for_result_ = false; |
| @@ -89,7 +93,8 @@ class FakeMidiManagerClient : public MidiManagerClient { |
| class MidiManagerTest : public ::testing::Test { |
| public: |
| MidiManagerTest() |
| - : message_loop_(new base::MessageLoop), manager_(new FakeMidiManager) {} |
| + : manager_(new FakeMidiManager), |
| + message_loop_(new base::MessageLoop) {} |
| virtual ~MidiManagerTest() {} |
| protected: |
| @@ -106,15 +111,17 @@ class MidiManagerTest : public ::testing::Test { |
| EXPECT_TRUE(manager_->start_initialization_is_called_); |
| } |
| - void StartTheSecondSession(FakeMidiManagerClient* client) { |
| - EXPECT_TRUE(manager_->start_initialization_is_called_); |
| + void StartTheNthSession(FakeMidiManagerClient* client, size_t nth) { |
| + EXPECT_EQ(nth != 1, manager_->start_initialization_is_called_); |
| EXPECT_EQ(0U, manager_->GetClientCount()); |
| - EXPECT_EQ(1U, manager_->GetPendingClientCount()); |
| + EXPECT_EQ(nth - 1, manager_->GetPendingClientCount()); |
| - // StartInitialization() should not be called for the second session. |
| + // StartInitialization() should not be called for the second and later |
| + // sessions. |
| manager_->start_initialization_is_called_ = false; |
| manager_->StartSession(client, client->get_client_id()); |
| - EXPECT_FALSE(manager_->start_initialization_is_called_); |
| + EXPECT_EQ(nth == 1, manager_->start_initialization_is_called_); |
| + manager_->start_initialization_is_called_ = true; |
| } |
| void EndSession(FakeMidiManagerClient* client, size_t before, size_t after) { |
| @@ -127,9 +134,16 @@ class MidiManagerTest : public ::testing::Test { |
| manager_->CallCompleteInitialization(result); |
| } |
| + void RunLoopUntilIdle() { |
| + base::RunLoop run_loop; |
| + run_loop.RunUntilIdle(); |
| + } |
| + |
| + protected: |
| + scoped_ptr<FakeMidiManager> manager_; |
| + |
| private: |
| scoped_ptr<base::MessageLoop> message_loop_; |
| - scoped_ptr<FakeMidiManager> manager_; |
| DISALLOW_COPY_AND_ASSIGN(MidiManagerTest); |
| }; |
| @@ -161,7 +175,7 @@ TEST_F(MidiManagerTest, StartMultipleSessions) { |
| client2.reset(new FakeMidiManagerClient(1)); |
| StartTheFirstSession(client1.get()); |
| - StartTheSecondSession(client2.get()); |
| + StartTheNthSession(client2.get(), 2); |
| CompleteInitialization(MIDI_OK); |
| EXPECT_EQ(MIDI_OK, client1->WaitForResult()); |
| EXPECT_EQ(MIDI_OK, client2->WaitForResult()); |
| @@ -169,6 +183,39 @@ TEST_F(MidiManagerTest, StartMultipleSessions) { |
| EndSession(client2.get(), 1U, 0U); |
| } |
| +TEST_F(MidiManagerTest, TooManyPendingSessions) { |
| + // Push as many client requests for starting session as possible. |
| + ScopedVector<FakeMidiManagerClient> clients; |
|
yukawa
2014/05/07 00:07:13
I feel |clients| and |client| are typographically
Takashi Toyoshima
2014/05/07 02:09:11
Sound reasonable.
Done.
|
| + clients.resize(MidiManager::kMaxPendingClientCount); |
| + for (size_t i = 0; i < MidiManager::kMaxPendingClientCount; ++i) { |
| + clients[i] = new FakeMidiManagerClient(i); |
| + StartTheNthSession(clients[i], i + 1); |
| + } |
| + |
| + // Push the last client that should be rejected for too many pending requests. |
| + scoped_ptr<FakeMidiManagerClient> client( |
| + new FakeMidiManagerClient(MidiManager::kMaxPendingClientCount)); |
| + manager_->start_initialization_is_called_ = false; |
| + manager_->StartSession(client.get(), client->get_client_id()); |
| + EXPECT_FALSE(manager_->start_initialization_is_called_); |
| + EXPECT_EQ(MIDI_INITIALIZATION_ERROR, client->get_result()); |
| + |
| + // Other clients still should not receive a result. |
| + RunLoopUntilIdle(); |
| + for (size_t i = 0; i < clients.size(); ++i) |
| + EXPECT_EQ(MIDI_NOT_SUPPORTED, clients[i]->get_result()); |
| + |
| + // The result MIDI_OK should be distributed to other clients. |
| + CompleteInitialization(MIDI_OK); |
| + for (size_t i = 0; i < clients.size(); ++i) |
| + EXPECT_EQ(MIDI_OK, clients[i]->WaitForResult()); |
| + |
| + // Close all successful sessions in FIFO order. |
| + size_t sessions = clients.size(); |
| + for (size_t i = 0; i < clients.size(); ++i, --sessions) |
| + EndSession(clients[i], sessions, sessions - 1); |
| +} |
| + |
| TEST_F(MidiManagerTest, CreateMidiManager) { |
| scoped_ptr<FakeMidiManagerClient> client; |
| client.reset(new FakeMidiManagerClient(0)); |