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" |
| 11 #include "base/memory/scoped_vector.h" |
9 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
11 | 14 |
12 namespace media { | 15 namespace media { |
13 | 16 |
14 namespace { | 17 namespace { |
15 | 18 |
16 class FakeMidiManager : public MidiManager { | 19 class FakeMidiManager : public MidiManager { |
17 public: | 20 public: |
18 FakeMidiManager() : start_initialization_is_called_(false) {} | 21 FakeMidiManager() : start_initialization_is_called_(false) {} |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 class FakeMidiManagerClient : public MidiManagerClient { | 53 class FakeMidiManagerClient : public MidiManagerClient { |
51 public: | 54 public: |
52 explicit FakeMidiManagerClient(int client_id) | 55 explicit FakeMidiManagerClient(int client_id) |
53 : client_id_(client_id), | 56 : client_id_(client_id), |
54 result_(MIDI_NOT_SUPPORTED), | 57 result_(MIDI_NOT_SUPPORTED), |
55 wait_for_result_(true) {} | 58 wait_for_result_(true) {} |
56 virtual ~FakeMidiManagerClient() {} | 59 virtual ~FakeMidiManagerClient() {} |
57 | 60 |
58 // MidiManagerClient implementation. | 61 // MidiManagerClient implementation. |
59 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_); |
60 CHECK_EQ(client_id_, client_id); | 64 CHECK_EQ(client_id_, client_id); |
61 result_ = result; | 65 result_ = result; |
62 wait_for_result_ = false; | 66 wait_for_result_ = false; |
63 } | 67 } |
64 | 68 |
65 virtual void ReceiveMidiData(uint32 port_index, const uint8* data, | 69 virtual void ReceiveMidiData(uint32 port_index, const uint8* data, |
66 size_t size, double timestamp) OVERRIDE {} | 70 size_t size, double timestamp) OVERRIDE {} |
67 virtual void AccumulateMidiBytesSent(size_t size) OVERRIDE {} | 71 virtual void AccumulateMidiBytesSent(size_t size) OVERRIDE {} |
68 | 72 |
69 int get_client_id() const { return client_id_; } | 73 int get_client_id() const { return client_id_; } |
(...skipping 12 matching lines...) Expand all Loading... |
82 int client_id_; | 86 int client_id_; |
83 MidiResult result_; | 87 MidiResult result_; |
84 bool wait_for_result_; | 88 bool wait_for_result_; |
85 | 89 |
86 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient); | 90 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient); |
87 }; | 91 }; |
88 | 92 |
89 class MidiManagerTest : public ::testing::Test { | 93 class MidiManagerTest : public ::testing::Test { |
90 public: | 94 public: |
91 MidiManagerTest() | 95 MidiManagerTest() |
92 : message_loop_(new base::MessageLoop), manager_(new FakeMidiManager) {} | 96 : manager_(new FakeMidiManager), |
| 97 message_loop_(new base::MessageLoop) {} |
93 virtual ~MidiManagerTest() {} | 98 virtual ~MidiManagerTest() {} |
94 | 99 |
95 protected: | 100 protected: |
96 void StartTheFirstSession(FakeMidiManagerClient* client) { | 101 void StartTheFirstSession(FakeMidiManagerClient* client) { |
97 EXPECT_FALSE(manager_->start_initialization_is_called_); | 102 EXPECT_FALSE(manager_->start_initialization_is_called_); |
98 EXPECT_EQ(0U, manager_->GetClientCount()); | 103 EXPECT_EQ(0U, manager_->GetClientCount()); |
99 EXPECT_EQ(0U, manager_->GetPendingClientCount()); | 104 EXPECT_EQ(0U, manager_->GetPendingClientCount()); |
100 manager_->StartSession(client, client->get_client_id()); | 105 manager_->StartSession(client, client->get_client_id()); |
101 EXPECT_EQ(0U, manager_->GetClientCount()); | 106 EXPECT_EQ(0U, manager_->GetClientCount()); |
102 EXPECT_EQ(1U, manager_->GetPendingClientCount()); | 107 EXPECT_EQ(1U, manager_->GetPendingClientCount()); |
103 EXPECT_TRUE(manager_->start_initialization_is_called_); | 108 EXPECT_TRUE(manager_->start_initialization_is_called_); |
104 EXPECT_EQ(0U, manager_->GetClientCount()); | 109 EXPECT_EQ(0U, manager_->GetClientCount()); |
105 EXPECT_EQ(1U, manager_->GetPendingClientCount()); | 110 EXPECT_EQ(1U, manager_->GetPendingClientCount()); |
106 EXPECT_TRUE(manager_->start_initialization_is_called_); | 111 EXPECT_TRUE(manager_->start_initialization_is_called_); |
107 } | 112 } |
108 | 113 |
109 void StartTheSecondSession(FakeMidiManagerClient* client) { | 114 void StartTheNthSession(FakeMidiManagerClient* client, size_t nth) { |
110 EXPECT_TRUE(manager_->start_initialization_is_called_); | 115 EXPECT_EQ(nth != 1, manager_->start_initialization_is_called_); |
111 EXPECT_EQ(0U, manager_->GetClientCount()); | 116 EXPECT_EQ(0U, manager_->GetClientCount()); |
112 EXPECT_EQ(1U, manager_->GetPendingClientCount()); | 117 EXPECT_EQ(nth - 1, manager_->GetPendingClientCount()); |
113 | 118 |
114 // StartInitialization() should not be called for the second session. | 119 // StartInitialization() should not be called for the second and later |
| 120 // sessions. |
115 manager_->start_initialization_is_called_ = false; | 121 manager_->start_initialization_is_called_ = false; |
116 manager_->StartSession(client, client->get_client_id()); | 122 manager_->StartSession(client, client->get_client_id()); |
117 EXPECT_FALSE(manager_->start_initialization_is_called_); | 123 EXPECT_EQ(nth == 1, manager_->start_initialization_is_called_); |
| 124 manager_->start_initialization_is_called_ = true; |
118 } | 125 } |
119 | 126 |
120 void EndSession(FakeMidiManagerClient* client, size_t before, size_t after) { | 127 void EndSession(FakeMidiManagerClient* client, size_t before, size_t after) { |
121 EXPECT_EQ(before, manager_->GetClientCount()); | 128 EXPECT_EQ(before, manager_->GetClientCount()); |
122 manager_->EndSession(client); | 129 manager_->EndSession(client); |
123 EXPECT_EQ(after, manager_->GetClientCount()); | 130 EXPECT_EQ(after, manager_->GetClientCount()); |
124 } | 131 } |
125 | 132 |
126 void CompleteInitialization(MidiResult result) { | 133 void CompleteInitialization(MidiResult result) { |
127 manager_->CallCompleteInitialization(result); | 134 manager_->CallCompleteInitialization(result); |
128 } | 135 } |
129 | 136 |
| 137 void RunLoopUntilIdle() { |
| 138 base::RunLoop run_loop; |
| 139 run_loop.RunUntilIdle(); |
| 140 } |
| 141 |
| 142 protected: |
| 143 scoped_ptr<FakeMidiManager> manager_; |
| 144 |
130 private: | 145 private: |
131 scoped_ptr<base::MessageLoop> message_loop_; | 146 scoped_ptr<base::MessageLoop> message_loop_; |
132 scoped_ptr<FakeMidiManager> manager_; | |
133 | 147 |
134 DISALLOW_COPY_AND_ASSIGN(MidiManagerTest); | 148 DISALLOW_COPY_AND_ASSIGN(MidiManagerTest); |
135 }; | 149 }; |
136 | 150 |
137 TEST_F(MidiManagerTest, StartAndEndSession) { | 151 TEST_F(MidiManagerTest, StartAndEndSession) { |
138 scoped_ptr<FakeMidiManagerClient> client; | 152 scoped_ptr<FakeMidiManagerClient> client; |
139 client.reset(new FakeMidiManagerClient(0)); | 153 client.reset(new FakeMidiManagerClient(0)); |
140 | 154 |
141 StartTheFirstSession(client.get()); | 155 StartTheFirstSession(client.get()); |
142 CompleteInitialization(MIDI_OK); | 156 CompleteInitialization(MIDI_OK); |
(...skipping 11 matching lines...) Expand all Loading... |
154 EndSession(client.get(), 0U, 0U); | 168 EndSession(client.get(), 0U, 0U); |
155 } | 169 } |
156 | 170 |
157 TEST_F(MidiManagerTest, StartMultipleSessions) { | 171 TEST_F(MidiManagerTest, StartMultipleSessions) { |
158 scoped_ptr<FakeMidiManagerClient> client1; | 172 scoped_ptr<FakeMidiManagerClient> client1; |
159 scoped_ptr<FakeMidiManagerClient> client2; | 173 scoped_ptr<FakeMidiManagerClient> client2; |
160 client1.reset(new FakeMidiManagerClient(0)); | 174 client1.reset(new FakeMidiManagerClient(0)); |
161 client2.reset(new FakeMidiManagerClient(1)); | 175 client2.reset(new FakeMidiManagerClient(1)); |
162 | 176 |
163 StartTheFirstSession(client1.get()); | 177 StartTheFirstSession(client1.get()); |
164 StartTheSecondSession(client2.get()); | 178 StartTheNthSession(client2.get(), 2); |
165 CompleteInitialization(MIDI_OK); | 179 CompleteInitialization(MIDI_OK); |
166 EXPECT_EQ(MIDI_OK, client1->WaitForResult()); | 180 EXPECT_EQ(MIDI_OK, client1->WaitForResult()); |
167 EXPECT_EQ(MIDI_OK, client2->WaitForResult()); | 181 EXPECT_EQ(MIDI_OK, client2->WaitForResult()); |
168 EndSession(client1.get(), 2U, 1U); | 182 EndSession(client1.get(), 2U, 1U); |
169 EndSession(client2.get(), 1U, 0U); | 183 EndSession(client2.get(), 1U, 0U); |
170 } | 184 } |
171 | 185 |
| 186 TEST_F(MidiManagerTest, TooManyPendingSessions) { |
| 187 // Push as many client requests for starting session as possible. |
| 188 ScopedVector<FakeMidiManagerClient> many_existing_clients; |
| 189 many_existing_clients.resize(MidiManager::kMaxPendingClientCount); |
| 190 for (size_t i = 0; i < MidiManager::kMaxPendingClientCount; ++i) { |
| 191 many_existing_clients[i] = new FakeMidiManagerClient(i); |
| 192 StartTheNthSession(many_existing_clients[i], i + 1); |
| 193 } |
| 194 |
| 195 // Push the last client that should be rejected for too many pending requests. |
| 196 scoped_ptr<FakeMidiManagerClient> additional_client( |
| 197 new FakeMidiManagerClient(MidiManager::kMaxPendingClientCount)); |
| 198 manager_->start_initialization_is_called_ = false; |
| 199 manager_->StartSession(additional_client.get(), |
| 200 additional_client->get_client_id()); |
| 201 EXPECT_FALSE(manager_->start_initialization_is_called_); |
| 202 EXPECT_EQ(MIDI_INITIALIZATION_ERROR, additional_client->get_result()); |
| 203 |
| 204 // Other clients still should not receive a result. |
| 205 RunLoopUntilIdle(); |
| 206 for (size_t i = 0; i < many_existing_clients.size(); ++i) |
| 207 EXPECT_EQ(MIDI_NOT_SUPPORTED, many_existing_clients[i]->get_result()); |
| 208 |
| 209 // The result MIDI_OK should be distributed to other clients. |
| 210 CompleteInitialization(MIDI_OK); |
| 211 for (size_t i = 0; i < many_existing_clients.size(); ++i) |
| 212 EXPECT_EQ(MIDI_OK, many_existing_clients[i]->WaitForResult()); |
| 213 |
| 214 // Close all successful sessions in FIFO order. |
| 215 size_t sessions = many_existing_clients.size(); |
| 216 for (size_t i = 0; i < many_existing_clients.size(); ++i, --sessions) |
| 217 EndSession(many_existing_clients[i], sessions, sessions - 1); |
| 218 } |
| 219 |
172 TEST_F(MidiManagerTest, CreateMidiManager) { | 220 TEST_F(MidiManagerTest, CreateMidiManager) { |
173 scoped_ptr<FakeMidiManagerClient> client; | 221 scoped_ptr<FakeMidiManagerClient> client; |
174 client.reset(new FakeMidiManagerClient(0)); | 222 client.reset(new FakeMidiManagerClient(0)); |
175 | 223 |
176 scoped_ptr<MidiManager> manager(MidiManager::Create()); | 224 scoped_ptr<MidiManager> manager(MidiManager::Create()); |
177 manager->StartSession(client.get(), client->get_client_id()); | 225 manager->StartSession(client.get(), client->get_client_id()); |
178 EXPECT_EQ(MIDI_OK, client->WaitForResult()); | 226 EXPECT_EQ(MIDI_OK, client->WaitForResult()); |
179 } | 227 } |
180 | 228 |
181 } // namespace | 229 } // namespace |
182 | 230 |
183 } // namespace media | 231 } // namespace media |
OLD | NEW |