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

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

Issue 264053002: Web MIDI: introduce pending client count limit to start sessions (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: one more fix Created 6 years, 7 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 | Annotate | Revision Log
« no previous file with comments | « media/midi/midi_manager.cc ('k') | no next file » | 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>
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
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 explicit 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_);
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
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
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<scoped_ptr<FakeMidiManagerClient> > clients;
yukawa 2014/05/04 23:45:55 Note: ScopedVector (base/memory/scoped_vector.h) m
Takashi Toyoshima 2014/05/05 00:59:06 Oh, it will help me. I'll try it.
199 clients.resize(MidiManager::kMaxPendingClientCount);
200 for (size_t i = 0; i < MidiManager::kMaxPendingClientCount; ++i) {
201 clients[i].reset(new FakeMidiManagerClient(i));
202 StartTheNthSession(clients[i].get(), i + 1);
203 }
204
205 // Push the last client that should be rejected for too many pending requests.
206 scoped_ptr<FakeMidiManagerClient> client(
207 new FakeMidiManagerClient(MidiManager::kMaxPendingClientCount));
208 manager_->start_initialization_is_called_ = false;
209 manager_->StartSession(client.get(), client->GetClientId());
210 EXPECT_FALSE(manager_->start_initialization_is_called_);
211 EXPECT_EQ(MIDI_INITIALIZATION_ERROR, client->GetResult());
212
213 // Other clients still should not receive a result.
214 for (size_t i = 0; i < clients.size(); ++i)
215 EXPECT_EQ(MIDI_NOT_SUPPORTED, clients[i]->GetResult());
216
217 // The result MIDI_OK should be distributed to other clients.
218 CompleteInitialization(MIDI_OK);
219 for (size_t i = 0; i < clients.size(); ++i)
220 EXPECT_EQ(MIDI_OK, clients[i]->GetResult());
221
222 // Close all successful sessions in FIFO order.
223 size_t sessions = clients.size();
224 for (size_t i = 0; i < clients.size(); ++i, --sessions)
225 EndSession(clients[i].get(), sessions, sessions - 1);
226 }
227
185 } // namespace 228 } // namespace
186 229
187 } // namespace media 230 } // namespace media
OLDNEW
« no previous file with comments | « media/midi/midi_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698