Chromium Code Reviews| 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> | |
| 8 | |
| 7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 12 |
| 11 namespace media { | 13 namespace media { |
| 12 | 14 |
| 13 namespace { | 15 namespace { |
| 14 | 16 |
| 15 class FakeMidiManager : public MidiManager { | 17 class FakeMidiManager : public MidiManager { |
| 16 public: | 18 public: |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 46 | 48 |
| 47 bool start_initialization_is_called_; | 49 bool start_initialization_is_called_; |
| 48 bool complete_initialization_synchronously_; | 50 bool complete_initialization_synchronously_; |
| 49 | 51 |
| 50 private: | 52 private: |
| 51 DISALLOW_COPY_AND_ASSIGN(FakeMidiManager); | 53 DISALLOW_COPY_AND_ASSIGN(FakeMidiManager); |
| 52 }; | 54 }; |
| 53 | 55 |
| 54 class FakeMidiManagerClient : public MidiManagerClient { | 56 class FakeMidiManagerClient : public MidiManagerClient { |
| 55 public: | 57 public: |
| 56 FakeMidiManagerClient(int client_id) : client_id_(client_id) {} | 58 FakeMidiManagerClient(int client_id) |
| 59 : client_id_(client_id), | |
| 60 result_(MIDI_NOT_SUPPORTED), | |
| 61 start_session_is_completed_(false) {} | |
| 57 virtual ~FakeMidiManagerClient() {} | 62 virtual ~FakeMidiManagerClient() {} |
| 58 | 63 |
| 59 // MidiManagerClient implementation. | 64 // MidiManagerClient implementation. |
| 60 virtual void CompleteStartSession(int client_id, MidiResult result) OVERRIDE { | 65 virtual void CompleteStartSession(int client_id, MidiResult result) OVERRIDE { |
| 61 DCHECK_EQ(client_id_, client_id); | 66 EXPECT_FALSE(start_session_is_completed_); |
|
Takashi Toyoshima
2014/05/04 09:13:36
do not allow multiple completion callbacks
| |
| 67 EXPECT_EQ(client_id_, client_id); | |
| 62 result_ = result; | 68 result_ = result; |
| 69 start_session_is_completed_ = true; | |
| 63 } | 70 } |
| 64 | 71 |
| 65 virtual void ReceiveMidiData(uint32 port_index, const uint8* data, | 72 virtual void ReceiveMidiData(uint32 port_index, const uint8* data, |
| 66 size_t size, double timestamp) OVERRIDE {} | 73 size_t size, double timestamp) OVERRIDE {} |
| 67 virtual void AccumulateMidiBytesSent(size_t size) OVERRIDE {} | 74 virtual void AccumulateMidiBytesSent(size_t size) OVERRIDE {} |
| 68 | 75 |
| 69 int GetClientId() { | 76 int GetClientId() { |
| 70 return client_id_; | 77 return client_id_; |
| 71 } | 78 } |
| 72 | 79 |
| 73 MidiResult GetResult() { | 80 MidiResult GetResult() { |
| 74 return result_; | 81 return result_; |
| 75 } | 82 } |
| 76 | 83 |
| 77 private: | 84 private: |
| 78 int client_id_; | 85 int client_id_; |
| 79 MidiResult result_; | 86 MidiResult result_; |
| 87 bool start_session_is_completed_; | |
| 80 | 88 |
| 81 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient); | 89 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient); |
| 82 }; | 90 }; |
| 83 | 91 |
| 84 class MidiManagerTest : public ::testing::Test { | 92 class MidiManagerTest : public ::testing::Test { |
| 85 public: | 93 public: |
| 86 MidiManagerTest() : manager_(new FakeMidiManager) {} | 94 MidiManagerTest() : manager_(new FakeMidiManager) {} |
| 87 virtual ~MidiManagerTest() {} | 95 virtual ~MidiManagerTest() {} |
| 88 | 96 |
| 89 protected: | 97 protected: |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 103 } else { | 111 } else { |
| 104 EXPECT_EQ(0U, manager_->GetClientCount()); | 112 EXPECT_EQ(0U, manager_->GetClientCount()); |
| 105 EXPECT_EQ(1U, manager_->GetPendingClientCount()); | 113 EXPECT_EQ(1U, manager_->GetPendingClientCount()); |
| 106 EXPECT_TRUE(manager_->start_initialization_is_called_); | 114 EXPECT_TRUE(manager_->start_initialization_is_called_); |
| 107 EXPECT_EQ(0U, manager_->GetClientCount()); | 115 EXPECT_EQ(0U, manager_->GetClientCount()); |
| 108 EXPECT_EQ(1U, manager_->GetPendingClientCount()); | 116 EXPECT_EQ(1U, manager_->GetPendingClientCount()); |
| 109 EXPECT_TRUE(manager_->start_initialization_is_called_); | 117 EXPECT_TRUE(manager_->start_initialization_is_called_); |
| 110 } | 118 } |
| 111 } | 119 } |
| 112 | 120 |
| 113 void StartTheSecondSession(FakeMidiManagerClient* client) { | 121 void StartTheNthSession(FakeMidiManagerClient* client, size_t nth) { |
| 114 EXPECT_TRUE(manager_->start_initialization_is_called_); | 122 EXPECT_EQ(nth != 1, manager_->start_initialization_is_called_); |
| 115 EXPECT_EQ(0U, manager_->GetClientCount()); | 123 EXPECT_EQ(0U, manager_->GetClientCount()); |
| 116 EXPECT_EQ(1U, manager_->GetPendingClientCount()); | 124 EXPECT_EQ(nth - 1, manager_->GetPendingClientCount()); |
| 117 | 125 |
| 118 // StartInitialization() should not be called for the second session. | 126 // StartInitialization() should not be called for the second and later |
| 127 // sessions. | |
| 119 manager_->start_initialization_is_called_ = false; | 128 manager_->start_initialization_is_called_ = false; |
| 120 manager_->StartSession(client, client->GetClientId()); | 129 manager_->StartSession(client, client->GetClientId()); |
| 121 EXPECT_FALSE(manager_->start_initialization_is_called_); | 130 EXPECT_EQ(nth == 1, manager_->start_initialization_is_called_); |
| 131 manager_->start_initialization_is_called_ = true; | |
| 122 } | 132 } |
| 123 | 133 |
| 124 void EndSession(FakeMidiManagerClient* client, size_t before, size_t after) { | 134 void EndSession(FakeMidiManagerClient* client, size_t before, size_t after) { |
| 125 EXPECT_EQ(before, manager_->GetClientCount()); | 135 EXPECT_EQ(before, manager_->GetClientCount()); |
| 126 manager_->EndSession(client); | 136 manager_->EndSession(client); |
| 127 EXPECT_EQ(after, manager_->GetClientCount()); | 137 EXPECT_EQ(after, manager_->GetClientCount()); |
| 128 } | 138 } |
| 129 | 139 |
| 130 void CompleteInitialization(MidiResult result) { | 140 void CompleteInitialization(MidiResult result) { |
| 131 manager_->CallCompleteInitialization(result); | 141 manager_->CallCompleteInitialization(result); |
| 132 } | 142 } |
| 133 | 143 |
| 134 private: | 144 protected: |
| 135 scoped_ptr<FakeMidiManager> manager_; | 145 scoped_ptr<FakeMidiManager> manager_; |
| 136 | 146 |
| 147 private: | |
| 137 DISALLOW_COPY_AND_ASSIGN(MidiManagerTest); | 148 DISALLOW_COPY_AND_ASSIGN(MidiManagerTest); |
| 138 }; | 149 }; |
| 139 | 150 |
| 140 // Check if calling CompleteInitialization() does not acquire the same lock | 151 // Check if calling CompleteInitialization() does not acquire the same lock |
| 141 // on the same thread. | 152 // on the same thread. |
| 142 TEST_F(MidiManagerTest, StartAndEndSessionSynchronously) { | 153 TEST_F(MidiManagerTest, StartAndEndSessionSynchronously) { |
| 143 scoped_ptr<FakeMidiManagerClient> client; | 154 scoped_ptr<FakeMidiManagerClient> client; |
| 144 client.reset(new FakeMidiManagerClient(0)); | 155 client.reset(new FakeMidiManagerClient(0)); |
| 145 | 156 |
| 146 StartTheFirstSession(client.get(), true); | 157 StartTheFirstSession(client.get(), true); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 167 EndSession(client.get(), 0U, 0U); | 178 EndSession(client.get(), 0U, 0U); |
| 168 } | 179 } |
| 169 | 180 |
| 170 TEST_F(MidiManagerTest, StartMultipleSessions) { | 181 TEST_F(MidiManagerTest, StartMultipleSessions) { |
| 171 scoped_ptr<FakeMidiManagerClient> client1; | 182 scoped_ptr<FakeMidiManagerClient> client1; |
| 172 scoped_ptr<FakeMidiManagerClient> client2; | 183 scoped_ptr<FakeMidiManagerClient> client2; |
| 173 client1.reset(new FakeMidiManagerClient(0)); | 184 client1.reset(new FakeMidiManagerClient(0)); |
| 174 client2.reset(new FakeMidiManagerClient(1)); | 185 client2.reset(new FakeMidiManagerClient(1)); |
| 175 | 186 |
| 176 StartTheFirstSession(client1.get(), false); | 187 StartTheFirstSession(client1.get(), false); |
| 177 StartTheSecondSession(client2.get()); | 188 StartTheNthSession(client2.get(), 2); |
| 178 CompleteInitialization(MIDI_OK); | 189 CompleteInitialization(MIDI_OK); |
| 179 EXPECT_EQ(MIDI_OK, client1->GetResult()); | 190 EXPECT_EQ(MIDI_OK, client1->GetResult()); |
| 180 EXPECT_EQ(MIDI_OK, client2->GetResult()); | 191 EXPECT_EQ(MIDI_OK, client2->GetResult()); |
| 181 EndSession(client1.get(), 2U, 1U); | 192 EndSession(client1.get(), 2U, 1U); |
| 182 EndSession(client2.get(), 1U, 0U); | 193 EndSession(client2.get(), 1U, 0U); |
| 183 } | 194 } |
| 184 | 195 |
| 196 TEST_F(MidiManagerTest, TooManyPendingSessions) { | |
| 197 // Push as many client requests for starting session as possible. | |
| 198 std::vector<FakeMidiManagerClient*> clients; | |
| 199 clients.resize(MidiManager::kMaxPendingClientCount); | |
| 200 for (size_t i = 0; i < MidiManager::kMaxPendingClientCount; ++i) { | |
| 201 scoped_ptr<FakeMidiManagerClient> client(new FakeMidiManagerClient(i)); | |
| 202 StartTheNthSession(client.get(), i + 1); | |
| 203 clients[i] = client.release(); | |
| 204 } | |
| 205 | |
| 206 // Push the last client that should be rejected for too many pending requests. | |
| 207 scoped_ptr<FakeMidiManagerClient> client( | |
| 208 new FakeMidiManagerClient(MidiManager::kMaxPendingClientCount)); | |
| 209 manager_->start_initialization_is_called_ = false; | |
| 210 manager_->StartSession(client.get(), client->GetClientId()); | |
| 211 EXPECT_FALSE(manager_->start_initialization_is_called_); | |
| 212 EXPECT_EQ(MIDI_INITIALIZATION_ERROR, client->GetResult()); | |
| 213 | |
| 214 // Other clients still should not receive a result. | |
| 215 for (size_t i = 0; i < clients.size(); ++i) | |
| 216 EXPECT_EQ(MIDI_NOT_SUPPORTED, clients[i]->GetResult()); | |
| 217 | |
| 218 // The result MIDI_OK should be distributed to other clients. | |
| 219 CompleteInitialization(MIDI_OK); | |
| 220 for (size_t i = 0; i < clients.size(); ++i) | |
| 221 EXPECT_EQ(MIDI_OK, clients[i]->GetResult()); | |
| 222 | |
| 223 // Close all successful sessions in FIFO order. | |
| 224 size_t sessions = clients.size(); | |
| 225 for (size_t i = 0; i < clients.size(); ++i, --sessions) { | |
| 226 EndSession(clients[i], sessions, sessions - 1); | |
| 227 } | |
| 228 } | |
| 229 | |
| 185 } // namespace | 230 } // namespace |
| 186 | 231 |
| 187 } // namespace media | 232 } // namespace media |
| OLD | NEW |