Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: media/midi/midi_manager_unittest.cc

Issue 662853003: Manage MIDI related objects and sessions' lifecycles correctly (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address review comments at #7 Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/bind.h" 9 #include "base/bind.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 } 47 }
48 48
49 bool start_initialization_is_called_; 49 bool start_initialization_is_called_;
50 50
51 private: 51 private:
52 DISALLOW_COPY_AND_ASSIGN(FakeMidiManager); 52 DISALLOW_COPY_AND_ASSIGN(FakeMidiManager);
53 }; 53 };
54 54
55 class FakeMidiManagerClient : public MidiManagerClient { 55 class FakeMidiManagerClient : public MidiManagerClient {
56 public: 56 public:
57 explicit FakeMidiManagerClient(int client_id) 57 FakeMidiManagerClient()
58 : client_id_(client_id), 58 : result_(MIDI_NOT_SUPPORTED),
59 result_(MIDI_NOT_SUPPORTED),
60 wait_for_result_(true) {} 59 wait_for_result_(true) {}
61 virtual ~FakeMidiManagerClient() {} 60 virtual ~FakeMidiManagerClient() {}
62 61
63 // MidiManagerClient implementation. 62 // MidiManagerClient implementation.
64 virtual void CompleteStartSession(int client_id, MidiResult result) override { 63 virtual void CompleteStartSession(MidiResult result) override {
65 EXPECT_TRUE(wait_for_result_); 64 EXPECT_TRUE(wait_for_result_);
66 CHECK_EQ(client_id_, client_id);
67 result_ = result; 65 result_ = result;
68 wait_for_result_ = false; 66 wait_for_result_ = false;
69 } 67 }
70 68
71 virtual void ReceiveMidiData(uint32 port_index, const uint8* data, 69 virtual void ReceiveMidiData(uint32 port_index, const uint8* data,
72 size_t size, double timestamp) override {} 70 size_t size, double timestamp) override {}
73 virtual void AccumulateMidiBytesSent(size_t size) override {} 71 virtual void AccumulateMidiBytesSent(size_t size) override {}
74 72
75 int client_id() const { return client_id_; }
76 MidiResult result() const { return result_; } 73 MidiResult result() const { return result_; }
77 74
78 MidiResult WaitForResult() { 75 MidiResult WaitForResult() {
79 while (wait_for_result_) { 76 while (wait_for_result_) {
80 base::RunLoop run_loop; 77 base::RunLoop run_loop;
81 run_loop.RunUntilIdle(); 78 run_loop.RunUntilIdle();
82 } 79 }
83 return result(); 80 return result();
84 } 81 }
85 82
86 private: 83 private:
87 int client_id_;
88 MidiResult result_; 84 MidiResult result_;
89 bool wait_for_result_; 85 bool wait_for_result_;
90 86
91 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient); 87 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient);
92 }; 88 };
93 89
94 class MidiManagerTest : public ::testing::Test { 90 class MidiManagerTest : public ::testing::Test {
95 public: 91 public:
96 MidiManagerTest() 92 MidiManagerTest()
97 : manager_(new FakeMidiManager), 93 : manager_(new FakeMidiManager),
98 message_loop_(new base::MessageLoop) {} 94 message_loop_(new base::MessageLoop) {}
99 virtual ~MidiManagerTest() {} 95 virtual ~MidiManagerTest() {}
100 96
101 protected: 97 protected:
102 void StartTheFirstSession(FakeMidiManagerClient* client) { 98 void StartTheFirstSession(FakeMidiManagerClient* client) {
103 EXPECT_FALSE(manager_->start_initialization_is_called_); 99 EXPECT_FALSE(manager_->start_initialization_is_called_);
104 EXPECT_EQ(0U, manager_->GetClientCount()); 100 EXPECT_EQ(0U, manager_->GetClientCount());
105 EXPECT_EQ(0U, manager_->GetPendingClientCount()); 101 EXPECT_EQ(0U, manager_->GetPendingClientCount());
106 manager_->StartSession(client, client->client_id()); 102 manager_->StartSession(client);
107 EXPECT_EQ(0U, manager_->GetClientCount()); 103 EXPECT_EQ(0U, manager_->GetClientCount());
108 EXPECT_EQ(1U, manager_->GetPendingClientCount()); 104 EXPECT_EQ(1U, manager_->GetPendingClientCount());
109 EXPECT_TRUE(manager_->start_initialization_is_called_); 105 EXPECT_TRUE(manager_->start_initialization_is_called_);
110 EXPECT_EQ(0U, manager_->GetClientCount()); 106 EXPECT_EQ(0U, manager_->GetClientCount());
111 EXPECT_EQ(1U, manager_->GetPendingClientCount()); 107 EXPECT_EQ(1U, manager_->GetPendingClientCount());
112 EXPECT_TRUE(manager_->start_initialization_is_called_); 108 EXPECT_TRUE(manager_->start_initialization_is_called_);
113 } 109 }
114 110
115 void StartTheNthSession(FakeMidiManagerClient* client, size_t nth) { 111 void StartTheNthSession(FakeMidiManagerClient* client, size_t nth) {
116 EXPECT_EQ(nth != 1, manager_->start_initialization_is_called_); 112 EXPECT_EQ(nth != 1, manager_->start_initialization_is_called_);
117 EXPECT_EQ(0U, manager_->GetClientCount()); 113 EXPECT_EQ(0U, manager_->GetClientCount());
118 EXPECT_EQ(nth - 1, manager_->GetPendingClientCount()); 114 EXPECT_EQ(nth - 1, manager_->GetPendingClientCount());
119 115
120 // StartInitialization() should not be called for the second and later 116 // StartInitialization() should not be called for the second and later
121 // sessions. 117 // sessions.
122 manager_->start_initialization_is_called_ = false; 118 manager_->start_initialization_is_called_ = false;
123 manager_->StartSession(client, client->client_id()); 119 manager_->StartSession(client);
124 EXPECT_EQ(nth == 1, manager_->start_initialization_is_called_); 120 EXPECT_EQ(nth == 1, manager_->start_initialization_is_called_);
125 manager_->start_initialization_is_called_ = true; 121 manager_->start_initialization_is_called_ = true;
126 } 122 }
127 123
128 void EndSession(FakeMidiManagerClient* client, size_t before, size_t after) { 124 void EndSession(FakeMidiManagerClient* client, size_t before, size_t after) {
129 EXPECT_EQ(before, manager_->GetClientCount()); 125 EXPECT_EQ(before, manager_->GetClientCount());
130 manager_->EndSession(client); 126 manager_->EndSession(client);
131 EXPECT_EQ(after, manager_->GetClientCount()); 127 EXPECT_EQ(after, manager_->GetClientCount());
132 } 128 }
133 129
(...skipping 10 matching lines...) Expand all
144 scoped_ptr<FakeMidiManager> manager_; 140 scoped_ptr<FakeMidiManager> manager_;
145 141
146 private: 142 private:
147 scoped_ptr<base::MessageLoop> message_loop_; 143 scoped_ptr<base::MessageLoop> message_loop_;
148 144
149 DISALLOW_COPY_AND_ASSIGN(MidiManagerTest); 145 DISALLOW_COPY_AND_ASSIGN(MidiManagerTest);
150 }; 146 };
151 147
152 TEST_F(MidiManagerTest, StartAndEndSession) { 148 TEST_F(MidiManagerTest, StartAndEndSession) {
153 scoped_ptr<FakeMidiManagerClient> client; 149 scoped_ptr<FakeMidiManagerClient> client;
154 client.reset(new FakeMidiManagerClient(0)); 150 client.reset(new FakeMidiManagerClient);
155 151
156 StartTheFirstSession(client.get()); 152 StartTheFirstSession(client.get());
157 CompleteInitialization(MIDI_OK); 153 CompleteInitialization(MIDI_OK);
158 EXPECT_EQ(MIDI_OK, client->WaitForResult()); 154 EXPECT_EQ(MIDI_OK, client->WaitForResult());
159 EndSession(client.get(), 1U, 0U); 155 EndSession(client.get(), 1U, 0U);
160 } 156 }
161 157
162 TEST_F(MidiManagerTest, StartAndEndSessionWithError) { 158 TEST_F(MidiManagerTest, StartAndEndSessionWithError) {
163 scoped_ptr<FakeMidiManagerClient> client; 159 scoped_ptr<FakeMidiManagerClient> client;
164 client.reset(new FakeMidiManagerClient(1)); 160 client.reset(new FakeMidiManagerClient);
165 161
166 StartTheFirstSession(client.get()); 162 StartTheFirstSession(client.get());
167 CompleteInitialization(MIDI_INITIALIZATION_ERROR); 163 CompleteInitialization(MIDI_INITIALIZATION_ERROR);
168 EXPECT_EQ(MIDI_INITIALIZATION_ERROR, client->WaitForResult()); 164 EXPECT_EQ(MIDI_INITIALIZATION_ERROR, client->WaitForResult());
169 EndSession(client.get(), 0U, 0U); 165 EndSession(client.get(), 0U, 0U);
170 } 166 }
171 167
172 TEST_F(MidiManagerTest, StartMultipleSessions) { 168 TEST_F(MidiManagerTest, StartMultipleSessions) {
173 scoped_ptr<FakeMidiManagerClient> client1; 169 scoped_ptr<FakeMidiManagerClient> client1;
174 scoped_ptr<FakeMidiManagerClient> client2; 170 scoped_ptr<FakeMidiManagerClient> client2;
175 scoped_ptr<FakeMidiManagerClient> client3; 171 scoped_ptr<FakeMidiManagerClient> client3;
176 client1.reset(new FakeMidiManagerClient(0)); 172 client1.reset(new FakeMidiManagerClient);
177 client2.reset(new FakeMidiManagerClient(1)); 173 client2.reset(new FakeMidiManagerClient);
178 client3.reset(new FakeMidiManagerClient(1)); 174 client3.reset(new FakeMidiManagerClient);
179 175
180 StartTheFirstSession(client1.get()); 176 StartTheFirstSession(client1.get());
181 StartTheNthSession(client2.get(), 2); 177 StartTheNthSession(client2.get(), 2);
182 StartTheNthSession(client3.get(), 3); 178 StartTheNthSession(client3.get(), 3);
183 CompleteInitialization(MIDI_OK); 179 CompleteInitialization(MIDI_OK);
184 EXPECT_EQ(MIDI_OK, client1->WaitForResult()); 180 EXPECT_EQ(MIDI_OK, client1->WaitForResult());
185 EXPECT_EQ(MIDI_OK, client2->WaitForResult()); 181 EXPECT_EQ(MIDI_OK, client2->WaitForResult());
186 EXPECT_EQ(MIDI_OK, client3->WaitForResult()); 182 EXPECT_EQ(MIDI_OK, client3->WaitForResult());
187 EndSession(client1.get(), 3U, 2U); 183 EndSession(client1.get(), 3U, 2U);
188 EndSession(client2.get(), 2U, 1U); 184 EndSession(client2.get(), 2U, 1U);
189 EndSession(client3.get(), 1U, 0U); 185 EndSession(client3.get(), 1U, 0U);
190 } 186 }
191 187
192 // TODO(toyoshim): Add a test for a MidiManagerClient that has multiple 188 // TODO(toyoshim): Add a test for a MidiManagerClient that has multiple
193 // sessions with multiple client_id. 189 // sessions with multiple client_id.
194 190
195 TEST_F(MidiManagerTest, TooManyPendingSessions) { 191 TEST_F(MidiManagerTest, TooManyPendingSessions) {
196 // Push as many client requests for starting session as possible. 192 // Push as many client requests for starting session as possible.
197 ScopedVector<FakeMidiManagerClient> many_existing_clients; 193 ScopedVector<FakeMidiManagerClient> many_existing_clients;
198 many_existing_clients.resize(MidiManager::kMaxPendingClientCount); 194 many_existing_clients.resize(MidiManager::kMaxPendingClientCount);
199 for (size_t i = 0; i < MidiManager::kMaxPendingClientCount; ++i) { 195 for (size_t i = 0; i < MidiManager::kMaxPendingClientCount; ++i) {
200 many_existing_clients[i] = new FakeMidiManagerClient(i); 196 many_existing_clients[i] = new FakeMidiManagerClient;
201 StartTheNthSession(many_existing_clients[i], i + 1); 197 StartTheNthSession(many_existing_clients[i], i + 1);
202 } 198 }
203 199
204 // Push the last client that should be rejected for too many pending requests. 200 // Push the last client that should be rejected for too many pending requests.
205 scoped_ptr<FakeMidiManagerClient> additional_client( 201 scoped_ptr<FakeMidiManagerClient> additional_client(
206 new FakeMidiManagerClient(MidiManager::kMaxPendingClientCount)); 202 new FakeMidiManagerClient);
207 manager_->start_initialization_is_called_ = false; 203 manager_->start_initialization_is_called_ = false;
208 manager_->StartSession(additional_client.get(), 204 manager_->StartSession(additional_client.get());
209 additional_client->client_id());
210 EXPECT_FALSE(manager_->start_initialization_is_called_); 205 EXPECT_FALSE(manager_->start_initialization_is_called_);
211 EXPECT_EQ(MIDI_INITIALIZATION_ERROR, additional_client->result()); 206 EXPECT_EQ(MIDI_INITIALIZATION_ERROR, additional_client->result());
212 207
213 // Other clients still should not receive a result. 208 // Other clients still should not receive a result.
214 RunLoopUntilIdle(); 209 RunLoopUntilIdle();
215 for (size_t i = 0; i < many_existing_clients.size(); ++i) 210 for (size_t i = 0; i < many_existing_clients.size(); ++i)
216 EXPECT_EQ(MIDI_NOT_SUPPORTED, many_existing_clients[i]->result()); 211 EXPECT_EQ(MIDI_NOT_SUPPORTED, many_existing_clients[i]->result());
217 212
218 // The result MIDI_OK should be distributed to other clients. 213 // The result MIDI_OK should be distributed to other clients.
219 CompleteInitialization(MIDI_OK); 214 CompleteInitialization(MIDI_OK);
220 for (size_t i = 0; i < many_existing_clients.size(); ++i) 215 for (size_t i = 0; i < many_existing_clients.size(); ++i)
221 EXPECT_EQ(MIDI_OK, many_existing_clients[i]->WaitForResult()); 216 EXPECT_EQ(MIDI_OK, many_existing_clients[i]->WaitForResult());
222 217
223 // Close all successful sessions in FIFO order. 218 // Close all successful sessions in FIFO order.
224 size_t sessions = many_existing_clients.size(); 219 size_t sessions = many_existing_clients.size();
225 for (size_t i = 0; i < many_existing_clients.size(); ++i, --sessions) 220 for (size_t i = 0; i < many_existing_clients.size(); ++i, --sessions)
226 EndSession(many_existing_clients[i], sessions, sessions - 1); 221 EndSession(many_existing_clients[i], sessions, sessions - 1);
227 } 222 }
228 223
229 TEST_F(MidiManagerTest, AbortSession) { 224 TEST_F(MidiManagerTest, AbortSession) {
230 // A client starting a session can be destructed while an asynchronous 225 // A client starting a session can be destructed while an asynchronous
231 // initialization is performed. 226 // initialization is performed.
232 scoped_ptr<FakeMidiManagerClient> client; 227 scoped_ptr<FakeMidiManagerClient> client;
233 client.reset(new FakeMidiManagerClient(0)); 228 client.reset(new FakeMidiManagerClient);
234 229
235 StartTheFirstSession(client.get()); 230 StartTheFirstSession(client.get());
236 EndSession(client.get(), 0, 0); 231 EndSession(client.get(), 0, 0);
237 client.reset(); 232 client.reset();
238 233
239 // Following function should not call the destructed |client| function. 234 // Following function should not call the destructed |client| function.
240 CompleteInitialization(MIDI_OK); 235 CompleteInitialization(MIDI_OK);
241 base::RunLoop run_loop; 236 base::RunLoop run_loop;
242 run_loop.RunUntilIdle(); 237 run_loop.RunUntilIdle();
243 } 238 }
244 239
245 TEST_F(MidiManagerTest, CreateMidiManager) { 240 TEST_F(MidiManagerTest, CreateMidiManager) {
246 scoped_ptr<FakeMidiManagerClient> client; 241 scoped_ptr<FakeMidiManagerClient> client;
247 client.reset(new FakeMidiManagerClient(0)); 242 client.reset(new FakeMidiManagerClient);
248 243
249 scoped_ptr<MidiManager> manager(MidiManager::Create()); 244 scoped_ptr<MidiManager> manager(MidiManager::Create());
250 manager->StartSession(client.get(), client->client_id()); 245 manager->StartSession(client.get());
251 246
252 MidiResult result = client->WaitForResult(); 247 MidiResult result = client->WaitForResult();
253 // This #ifdef needs to be identical to the one in media/midi/midi_manager.cc. 248 // This #ifdef needs to be identical to the one in media/midi/midi_manager.cc.
254 // Do not change the condition for disabling this test. 249 // Do not change the condition for disabling this test.
255 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(USE_ALSA) && \ 250 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(USE_ALSA) && \
256 !defined(OS_ANDROID) && !defined(OS_CHROMEOS) 251 !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
257 EXPECT_EQ(MIDI_NOT_SUPPORTED, result); 252 EXPECT_EQ(MIDI_NOT_SUPPORTED, result);
258 #elif defined(USE_ALSA) 253 #elif defined(USE_ALSA)
259 // Temporary until http://crbug.com/371230 is resolved. 254 // Temporary until http://crbug.com/371230 is resolved.
260 EXPECT_TRUE((result == MIDI_OK) || (result == MIDI_INITIALIZATION_ERROR)); 255 EXPECT_TRUE((result == MIDI_OK) || (result == MIDI_INITIALIZATION_ERROR));
261 #else 256 #else
262 EXPECT_EQ(MIDI_OK, result); 257 EXPECT_EQ(MIDI_OK, result);
263 #endif 258 #endif
264 } 259 }
265 260
266 } // namespace 261 } // namespace
267 262
268 } // namespace media 263 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698