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

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

Powered by Google App Engine
This is Rietveld 408576698