toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame^] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "media/midi/midi_manager.h" |
| 6 | |
| 7 | #include "base/logging.h" |
| 8 | #include "base/memory/scoped_ptr.h" |
| 9 | #include "testing/gtest/include/gtest/gtest.h" |
| 10 | |
| 11 | namespace media { |
| 12 | |
| 13 | namespace { |
| 14 | |
| 15 | class FakeMidiManager : public MidiManager { |
| 16 | public: |
| 17 | FakeMidiManager() |
| 18 | : start_initialization_is_called_(false), |
| 19 | complete_initialization_synchronously_(false) {} |
| 20 | virtual ~FakeMidiManager() {} |
| 21 | |
| 22 | // MidiManager implementation. |
| 23 | virtual void StartInitialization() OVERRIDE { |
| 24 | start_initialization_is_called_ = true; |
| 25 | if (complete_initialization_synchronously_) |
| 26 | CompleteInitialization(MIDI_OK); |
| 27 | } |
| 28 | |
| 29 | virtual void DispatchSendMidiData(MidiManagerClient* client, |
| 30 | uint32 port_index, |
| 31 | const std::vector<uint8>& data, |
| 32 | double timestamp) OVERRIDE {} |
| 33 | |
| 34 | // Utility functions for testing. |
| 35 | void CallCompleteInitialization(MidiResult result) { |
| 36 | CompleteInitialization(result); |
| 37 | } |
| 38 | |
| 39 | size_t GetClientCount() { |
| 40 | return clients_.size(); |
| 41 | } |
| 42 | |
| 43 | size_t GetPendingClientCount() { |
| 44 | return pending_clients_.size(); |
| 45 | } |
| 46 | |
| 47 | bool start_initialization_is_called_; |
| 48 | bool complete_initialization_synchronously_; |
| 49 | |
| 50 | private: |
| 51 | DISALLOW_COPY_AND_ASSIGN(FakeMidiManager); |
| 52 | }; |
| 53 | |
| 54 | class FakeMidiManagerClient : public MidiManagerClient { |
| 55 | public: |
| 56 | FakeMidiManagerClient(int client_id) : client_id_(client_id) {} |
| 57 | virtual ~FakeMidiManagerClient() {} |
| 58 | |
| 59 | // MidiManagerClient implementation. |
| 60 | virtual void CompleteStartSession(int client_id, MidiResult result) OVERRIDE { |
| 61 | DCHECK_EQ(client_id_, client_id); |
| 62 | result_ = result; |
| 63 | } |
| 64 | |
| 65 | virtual void ReceiveMidiData(uint32 port_index, const uint8* data, |
| 66 | size_t size, double timestamp) OVERRIDE {} |
| 67 | virtual void AccumulateMidiBytesSent(size_t size) OVERRIDE {} |
| 68 | |
| 69 | int GetClientId() { |
| 70 | return client_id_; |
| 71 | } |
| 72 | |
| 73 | MidiResult GetResult() { |
| 74 | return result_; |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | int client_id_; |
| 79 | MidiResult result_; |
| 80 | |
| 81 | DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient); |
| 82 | }; |
| 83 | |
| 84 | class MidiManagerTest : public ::testing::Test { |
| 85 | public: |
| 86 | MidiManagerTest() : manager_(new FakeMidiManager) {} |
| 87 | virtual ~MidiManagerTest() {} |
| 88 | |
| 89 | protected: |
| 90 | void StartTheFirstSession(FakeMidiManagerClient* client, |
| 91 | bool complete_initialization_synchronously) { |
| 92 | manager_->complete_initialization_synchronously_ = |
| 93 | complete_initialization_synchronously; |
| 94 | EXPECT_FALSE(manager_->start_initialization_is_called_); |
| 95 | EXPECT_EQ(0U, manager_->GetClientCount()); |
| 96 | EXPECT_EQ(0U, manager_->GetPendingClientCount()); |
| 97 | manager_->StartSession(client, client->GetClientId()); |
| 98 | if (complete_initialization_synchronously) { |
| 99 | EXPECT_EQ(1U, manager_->GetClientCount()); |
| 100 | EXPECT_EQ(0U, manager_->GetPendingClientCount()); |
| 101 | EXPECT_TRUE(manager_->start_initialization_is_called_); |
| 102 | EXPECT_EQ(MIDI_OK, client->GetResult()); |
| 103 | } else { |
| 104 | EXPECT_EQ(0U, manager_->GetClientCount()); |
| 105 | EXPECT_EQ(1U, manager_->GetPendingClientCount()); |
| 106 | EXPECT_TRUE(manager_->start_initialization_is_called_); |
| 107 | EXPECT_EQ(0U, manager_->GetClientCount()); |
| 108 | EXPECT_EQ(1U, manager_->GetPendingClientCount()); |
| 109 | EXPECT_TRUE(manager_->start_initialization_is_called_); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void StartTheSecondSession(FakeMidiManagerClient* client) { |
| 114 | EXPECT_TRUE(manager_->start_initialization_is_called_); |
| 115 | EXPECT_EQ(0U, manager_->GetClientCount()); |
| 116 | EXPECT_EQ(1U, manager_->GetPendingClientCount()); |
| 117 | |
| 118 | // StartInitialization() should not be called for the second session. |
| 119 | manager_->start_initialization_is_called_ = false; |
| 120 | manager_->StartSession(client, client->GetClientId()); |
| 121 | EXPECT_FALSE(manager_->start_initialization_is_called_); |
| 122 | } |
| 123 | |
| 124 | void EndSession(FakeMidiManagerClient* client, size_t before, size_t after) { |
| 125 | EXPECT_EQ(before, manager_->GetClientCount()); |
| 126 | manager_->EndSession(client); |
| 127 | EXPECT_EQ(after, manager_->GetClientCount()); |
| 128 | } |
| 129 | |
| 130 | void CompleteInitialization(MidiResult result) { |
| 131 | manager_->CallCompleteInitialization(result); |
| 132 | } |
| 133 | |
| 134 | private: |
| 135 | scoped_ptr<FakeMidiManager> manager_; |
| 136 | |
| 137 | DISALLOW_COPY_AND_ASSIGN(MidiManagerTest); |
| 138 | }; |
| 139 | |
| 140 | // Check if calling CompleteInitialization() does not acquire the same lock |
| 141 | // on the same thread. |
| 142 | TEST_F(MidiManagerTest, StartAndEndSessionSynchronously) { |
| 143 | scoped_ptr<FakeMidiManagerClient> client; |
| 144 | client.reset(new FakeMidiManagerClient(0)); |
| 145 | |
| 146 | StartTheFirstSession(client.get(), true); |
| 147 | EndSession(client.get(), 1U, 0U); |
| 148 | } |
| 149 | |
| 150 | TEST_F(MidiManagerTest, StartAndEndSession) { |
| 151 | scoped_ptr<FakeMidiManagerClient> client; |
| 152 | client.reset(new FakeMidiManagerClient(0)); |
| 153 | |
| 154 | StartTheFirstSession(client.get(), false); |
| 155 | CompleteInitialization(MIDI_OK); |
| 156 | EXPECT_EQ(MIDI_OK, client->GetResult()); |
| 157 | EndSession(client.get(), 1U, 0U); |
| 158 | } |
| 159 | |
| 160 | TEST_F(MidiManagerTest, StartAndEndSessionWithError) { |
| 161 | scoped_ptr<FakeMidiManagerClient> client; |
| 162 | client.reset(new FakeMidiManagerClient(1)); |
| 163 | |
| 164 | StartTheFirstSession(client.get(), false); |
| 165 | CompleteInitialization(MIDI_INITIALIZATION_ERROR); |
| 166 | EXPECT_EQ(MIDI_INITIALIZATION_ERROR, client->GetResult()); |
| 167 | EndSession(client.get(), 0U, 0U); |
| 168 | } |
| 169 | |
| 170 | TEST_F(MidiManagerTest, StartMultipleSessions) { |
| 171 | scoped_ptr<FakeMidiManagerClient> client1; |
| 172 | scoped_ptr<FakeMidiManagerClient> client2; |
| 173 | client1.reset(new FakeMidiManagerClient(0)); |
| 174 | client2.reset(new FakeMidiManagerClient(1)); |
| 175 | |
| 176 | StartTheFirstSession(client1.get(), false); |
| 177 | StartTheSecondSession(client2.get()); |
| 178 | CompleteInitialization(MIDI_OK); |
| 179 | EXPECT_EQ(MIDI_OK, client1->GetResult()); |
| 180 | EXPECT_EQ(MIDI_OK, client2->GetResult()); |
| 181 | EndSession(client1.get(), 2U, 1U); |
| 182 | EndSession(client2.get(), 1U, 0U); |
| 183 | } |
| 184 | |
| 185 | } // namespace |
| 186 | |
| 187 | } // namespace media |