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

Unified 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: renames by #8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « media/midi/midi_manager.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: media/midi/midi_manager_unittest.cc
diff --git a/media/midi/midi_manager_unittest.cc b/media/midi/midi_manager_unittest.cc
index 456c2b0988ebcadf5931dcd11cc9b23231eb887e..f967332d2bb8c1a45b79d95cd92fe5e44d463809 100644
--- a/media/midi/midi_manager_unittest.cc
+++ b/media/midi/midi_manager_unittest.cc
@@ -4,8 +4,11 @@
#include "media/midi/midi_manager.h"
+#include <vector>
+
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
+#include "base/memory/scoped_vector.h"
#include "base/run_loop.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -57,6 +60,7 @@ class FakeMidiManagerClient : public MidiManagerClient {
// MidiManagerClient implementation.
virtual void CompleteStartSession(int client_id, MidiResult result) OVERRIDE {
+ EXPECT_TRUE(wait_for_result_);
CHECK_EQ(client_id_, client_id);
result_ = result;
wait_for_result_ = false;
@@ -89,7 +93,8 @@ class FakeMidiManagerClient : public MidiManagerClient {
class MidiManagerTest : public ::testing::Test {
public:
MidiManagerTest()
- : message_loop_(new base::MessageLoop), manager_(new FakeMidiManager) {}
+ : manager_(new FakeMidiManager),
+ message_loop_(new base::MessageLoop) {}
virtual ~MidiManagerTest() {}
protected:
@@ -106,15 +111,17 @@ class MidiManagerTest : public ::testing::Test {
EXPECT_TRUE(manager_->start_initialization_is_called_);
}
- void StartTheSecondSession(FakeMidiManagerClient* client) {
- EXPECT_TRUE(manager_->start_initialization_is_called_);
+ void StartTheNthSession(FakeMidiManagerClient* client, size_t nth) {
+ EXPECT_EQ(nth != 1, manager_->start_initialization_is_called_);
EXPECT_EQ(0U, manager_->GetClientCount());
- EXPECT_EQ(1U, manager_->GetPendingClientCount());
+ EXPECT_EQ(nth - 1, manager_->GetPendingClientCount());
- // StartInitialization() should not be called for the second session.
+ // StartInitialization() should not be called for the second and later
+ // sessions.
manager_->start_initialization_is_called_ = false;
manager_->StartSession(client, client->get_client_id());
- EXPECT_FALSE(manager_->start_initialization_is_called_);
+ EXPECT_EQ(nth == 1, manager_->start_initialization_is_called_);
+ manager_->start_initialization_is_called_ = true;
}
void EndSession(FakeMidiManagerClient* client, size_t before, size_t after) {
@@ -127,9 +134,16 @@ class MidiManagerTest : public ::testing::Test {
manager_->CallCompleteInitialization(result);
}
+ void RunLoopUntilIdle() {
+ base::RunLoop run_loop;
+ run_loop.RunUntilIdle();
+ }
+
+ protected:
+ scoped_ptr<FakeMidiManager> manager_;
+
private:
scoped_ptr<base::MessageLoop> message_loop_;
- scoped_ptr<FakeMidiManager> manager_;
DISALLOW_COPY_AND_ASSIGN(MidiManagerTest);
};
@@ -161,7 +175,7 @@ TEST_F(MidiManagerTest, StartMultipleSessions) {
client2.reset(new FakeMidiManagerClient(1));
StartTheFirstSession(client1.get());
- StartTheSecondSession(client2.get());
+ StartTheNthSession(client2.get(), 2);
CompleteInitialization(MIDI_OK);
EXPECT_EQ(MIDI_OK, client1->WaitForResult());
EXPECT_EQ(MIDI_OK, client2->WaitForResult());
@@ -169,6 +183,40 @@ TEST_F(MidiManagerTest, StartMultipleSessions) {
EndSession(client2.get(), 1U, 0U);
}
+TEST_F(MidiManagerTest, TooManyPendingSessions) {
+ // Push as many client requests for starting session as possible.
+ ScopedVector<FakeMidiManagerClient> many_existing_clients;
+ many_existing_clients.resize(MidiManager::kMaxPendingClientCount);
+ for (size_t i = 0; i < MidiManager::kMaxPendingClientCount; ++i) {
+ many_existing_clients[i] = new FakeMidiManagerClient(i);
+ StartTheNthSession(many_existing_clients[i], i + 1);
+ }
+
+ // Push the last client that should be rejected for too many pending requests.
+ scoped_ptr<FakeMidiManagerClient> additional_client(
+ new FakeMidiManagerClient(MidiManager::kMaxPendingClientCount));
+ manager_->start_initialization_is_called_ = false;
+ manager_->StartSession(additional_client.get(),
+ additional_client->get_client_id());
+ EXPECT_FALSE(manager_->start_initialization_is_called_);
+ EXPECT_EQ(MIDI_INITIALIZATION_ERROR, additional_client->get_result());
+
+ // Other clients still should not receive a result.
+ RunLoopUntilIdle();
+ for (size_t i = 0; i < many_existing_clients.size(); ++i)
+ EXPECT_EQ(MIDI_NOT_SUPPORTED, many_existing_clients[i]->get_result());
+
+ // The result MIDI_OK should be distributed to other clients.
+ CompleteInitialization(MIDI_OK);
+ for (size_t i = 0; i < many_existing_clients.size(); ++i)
+ EXPECT_EQ(MIDI_OK, many_existing_clients[i]->WaitForResult());
+
+ // Close all successful sessions in FIFO order.
+ size_t sessions = many_existing_clients.size();
+ for (size_t i = 0; i < many_existing_clients.size(); ++i, --sessions)
+ EndSession(many_existing_clients[i], sessions, sessions - 1);
+}
+
TEST_F(MidiManagerTest, CreateMidiManager) {
scoped_ptr<FakeMidiManagerClient> client;
client.reset(new FakeMidiManagerClient(0));
« 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