toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame^] | 1 | // Copyright 2015 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_mac.h" |
| 6 | |
| 7 | #include <CoreMIDI/MIDIServices.h> |
| 8 | |
| 9 | #include "base/logging.h" |
| 10 | #include "base/memory/scoped_ptr.h" |
| 11 | #include "base/message_loop/message_loop.h" |
| 12 | #include "base/run_loop.h" |
| 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
| 15 | namespace media { |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | void Noop(const MIDIPacketList*, void*, void*) {} |
| 20 | |
| 21 | class FakeMidiManagerClient : public MidiManagerClient { |
| 22 | public: |
| 23 | FakeMidiManagerClient() |
| 24 | : result_(MIDI_NOT_SUPPORTED), |
| 25 | wait_for_result_(true), |
| 26 | wait_for_port_(true) {} |
| 27 | |
| 28 | // MidiManagerClient implementation. |
| 29 | void AddInputPort(const MidiPortInfo& info) override {} |
| 30 | void AddOutputPort(const MidiPortInfo& info) override { |
| 31 | CHECK(!wait_for_result_); |
| 32 | info_ = info; |
| 33 | wait_for_port_ = false; |
| 34 | } |
| 35 | void SetInputPortState(uint32 port_index, MidiPortState state) override {} |
| 36 | void SetOutputPortState(uint32 port_index, MidiPortState state) override {} |
| 37 | |
| 38 | void CompleteStartSession(MidiResult result) override { |
| 39 | EXPECT_TRUE(wait_for_result_); |
| 40 | result_ = result; |
| 41 | wait_for_result_ = false; |
| 42 | } |
| 43 | |
| 44 | void ReceiveMidiData(uint32 port_index, const uint8* data, size_t size, |
| 45 | double timestamp) override {} |
| 46 | void AccumulateMidiBytesSent(size_t size) override {} |
| 47 | |
| 48 | MidiResult WaitForResult() { |
| 49 | while (wait_for_result_) { |
| 50 | base::RunLoop run_loop; |
| 51 | run_loop.RunUntilIdle(); |
| 52 | } |
| 53 | return result_; |
| 54 | } |
| 55 | MidiPortInfo WaitForPort() { |
| 56 | while (wait_for_port_) { |
| 57 | base::RunLoop run_loop; |
| 58 | run_loop.RunUntilIdle(); |
| 59 | } |
| 60 | return info_; |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | MidiResult result_; |
| 65 | bool wait_for_result_; |
| 66 | MidiPortInfo info_; |
| 67 | bool wait_for_port_; |
| 68 | |
| 69 | DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient); |
| 70 | }; |
| 71 | |
| 72 | class MidiManagerMacTest : public ::testing::Test { |
| 73 | public: |
| 74 | MidiManagerMacTest() |
| 75 | : manager_(new MidiManagerMac), |
| 76 | message_loop_(new base::MessageLoop) {} |
| 77 | |
| 78 | protected: |
| 79 | void StartSession(MidiManagerClient* client) { |
| 80 | manager_->StartSession(client); |
| 81 | } |
| 82 | void EndSession(MidiManagerClient* client) { |
| 83 | manager_->EndSession(client); |
| 84 | } |
| 85 | |
| 86 | private: |
| 87 | scoped_ptr<MidiManager> manager_; |
| 88 | scoped_ptr<base::MessageLoop> message_loop_; |
| 89 | |
| 90 | DISALLOW_COPY_AND_ASSIGN(MidiManagerMacTest); |
| 91 | }; |
| 92 | |
| 93 | |
| 94 | TEST_F(MidiManagerMacTest, MidiNotification) { |
| 95 | scoped_ptr<FakeMidiManagerClient> client(new FakeMidiManagerClient); |
| 96 | StartSession(client.get()); |
| 97 | |
| 98 | MidiResult result = client->WaitForResult(); |
| 99 | EXPECT_EQ(MIDI_OK, result); |
| 100 | |
| 101 | // Create MIDIClient, and MIDIEndpoint as a MIDIDestination. This should |
| 102 | // notify MIDIManagerMac as a MIDIObjectAddRemoveNotification. |
| 103 | MIDIClientRef midi_client = 0; |
| 104 | OSStatus status = MIDIClientCreate( |
| 105 | CFSTR("MidiManagerMacTest"), nullptr, nullptr, &midi_client); |
| 106 | EXPECT_EQ(noErr, status); |
| 107 | |
| 108 | MIDIEndpointRef ep = 0; |
| 109 | status = MIDIDestinationCreate( |
| 110 | midi_client, CFSTR("DestinationTest"), Noop, nullptr, &ep); |
| 111 | EXPECT_EQ(noErr, status); |
| 112 | |
| 113 | // Wait until the created device is notified to MidiManagerMac. |
| 114 | MidiPortInfo info = client->WaitForPort(); |
| 115 | EXPECT_EQ("DestinationTest", info.name); |
| 116 | |
| 117 | EndSession(client.get()); |
| 118 | if (ep) |
| 119 | MIDIEndpointDispose(ep); |
| 120 | if (midi_client) |
| 121 | MIDIClientDispose(midi_client); |
| 122 | } |
| 123 | |
| 124 | } // namespace |
| 125 | |
| 126 | } // namespace media |