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" |
toyoshim | 4f72835 | 2015-03-03 02:30:54 -0800 | [diff] [blame] | 13 | #include "base/synchronization/lock.h" |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
| 16 | namespace media { |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | void Noop(const MIDIPacketList*, void*, void*) {} |
| 21 | |
| 22 | class FakeMidiManagerClient : public MidiManagerClient { |
| 23 | public: |
| 24 | FakeMidiManagerClient() |
| 25 | : result_(MIDI_NOT_SUPPORTED), |
| 26 | wait_for_result_(true), |
toyoshim | 4f72835 | 2015-03-03 02:30:54 -0800 | [diff] [blame] | 27 | wait_for_port_(true), |
| 28 | unexpected_callback_(false) {} |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 29 | |
| 30 | // MidiManagerClient implementation. |
| 31 | void AddInputPort(const MidiPortInfo& info) override {} |
| 32 | void AddOutputPort(const MidiPortInfo& info) override { |
toyoshim | 4f72835 | 2015-03-03 02:30:54 -0800 | [diff] [blame] | 33 | base::AutoLock lock(lock_); |
| 34 | // AddOutputPort may be called before CompleteStartSession() is invoked |
| 35 | // if one or more MIDI devices including virtual ports are connected. |
| 36 | // Just ignore in such cases. |
| 37 | if (wait_for_result_) |
| 38 | return; |
| 39 | |
| 40 | // Check if this is the first call after CompleteStartSession(), and |
| 41 | // the case should not happen twice. |
| 42 | if (!wait_for_port_) |
| 43 | unexpected_callback_ = true; |
| 44 | |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 45 | info_ = info; |
| 46 | wait_for_port_ = false; |
| 47 | } |
| 48 | void SetInputPortState(uint32 port_index, MidiPortState state) override {} |
| 49 | void SetOutputPortState(uint32 port_index, MidiPortState state) override {} |
| 50 | |
| 51 | void CompleteStartSession(MidiResult result) override { |
toyoshim | 4f72835 | 2015-03-03 02:30:54 -0800 | [diff] [blame] | 52 | base::AutoLock lock(lock_); |
| 53 | if (!wait_for_result_) |
| 54 | unexpected_callback_ = true; |
| 55 | |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 56 | result_ = result; |
| 57 | wait_for_result_ = false; |
| 58 | } |
| 59 | |
| 60 | void ReceiveMidiData(uint32 port_index, const uint8* data, size_t size, |
| 61 | double timestamp) override {} |
| 62 | void AccumulateMidiBytesSent(size_t size) override {} |
| 63 | |
toyoshim | 4f72835 | 2015-03-03 02:30:54 -0800 | [diff] [blame] | 64 | bool GetWaitForResult() { |
| 65 | base::AutoLock lock(lock_); |
| 66 | return wait_for_result_; |
| 67 | } |
| 68 | |
| 69 | bool GetWaitForPort() { |
| 70 | base::AutoLock lock(lock_); |
| 71 | return wait_for_port_; |
| 72 | } |
| 73 | |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 74 | MidiResult WaitForResult() { |
toyoshim | 4f72835 | 2015-03-03 02:30:54 -0800 | [diff] [blame] | 75 | while (GetWaitForResult()) { |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 76 | base::RunLoop run_loop; |
| 77 | run_loop.RunUntilIdle(); |
| 78 | } |
toyoshim | 4f72835 | 2015-03-03 02:30:54 -0800 | [diff] [blame] | 79 | EXPECT_FALSE(unexpected_callback_); |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 80 | return result_; |
| 81 | } |
| 82 | MidiPortInfo WaitForPort() { |
toyoshim | 4f72835 | 2015-03-03 02:30:54 -0800 | [diff] [blame] | 83 | while (GetWaitForPort()) { |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 84 | base::RunLoop run_loop; |
| 85 | run_loop.RunUntilIdle(); |
| 86 | } |
toyoshim | 4f72835 | 2015-03-03 02:30:54 -0800 | [diff] [blame] | 87 | EXPECT_FALSE(unexpected_callback_); |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 88 | return info_; |
| 89 | } |
| 90 | |
| 91 | private: |
toyoshim | 4f72835 | 2015-03-03 02:30:54 -0800 | [diff] [blame] | 92 | base::Lock lock_; |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 93 | MidiResult result_; |
| 94 | bool wait_for_result_; |
| 95 | MidiPortInfo info_; |
| 96 | bool wait_for_port_; |
toyoshim | 4f72835 | 2015-03-03 02:30:54 -0800 | [diff] [blame] | 97 | bool unexpected_callback_; |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 98 | |
| 99 | DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient); |
| 100 | }; |
| 101 | |
| 102 | class MidiManagerMacTest : public ::testing::Test { |
| 103 | public: |
| 104 | MidiManagerMacTest() |
| 105 | : manager_(new MidiManagerMac), |
| 106 | message_loop_(new base::MessageLoop) {} |
| 107 | |
| 108 | protected: |
| 109 | void StartSession(MidiManagerClient* client) { |
| 110 | manager_->StartSession(client); |
| 111 | } |
| 112 | void EndSession(MidiManagerClient* client) { |
| 113 | manager_->EndSession(client); |
| 114 | } |
| 115 | |
| 116 | private: |
| 117 | scoped_ptr<MidiManager> manager_; |
| 118 | scoped_ptr<base::MessageLoop> message_loop_; |
| 119 | |
| 120 | DISALLOW_COPY_AND_ASSIGN(MidiManagerMacTest); |
| 121 | }; |
| 122 | |
| 123 | |
| 124 | TEST_F(MidiManagerMacTest, MidiNotification) { |
| 125 | scoped_ptr<FakeMidiManagerClient> client(new FakeMidiManagerClient); |
| 126 | StartSession(client.get()); |
| 127 | |
| 128 | MidiResult result = client->WaitForResult(); |
| 129 | EXPECT_EQ(MIDI_OK, result); |
| 130 | |
| 131 | // Create MIDIClient, and MIDIEndpoint as a MIDIDestination. This should |
| 132 | // notify MIDIManagerMac as a MIDIObjectAddRemoveNotification. |
| 133 | MIDIClientRef midi_client = 0; |
| 134 | OSStatus status = MIDIClientCreate( |
| 135 | CFSTR("MidiManagerMacTest"), nullptr, nullptr, &midi_client); |
| 136 | EXPECT_EQ(noErr, status); |
| 137 | |
| 138 | MIDIEndpointRef ep = 0; |
| 139 | status = MIDIDestinationCreate( |
| 140 | midi_client, CFSTR("DestinationTest"), Noop, nullptr, &ep); |
| 141 | EXPECT_EQ(noErr, status); |
toyoshim | c974698 | 2015-04-03 05:32:55 -0700 | [diff] [blame^] | 142 | SInt32 id; |
| 143 | status = MIDIObjectGetIntegerProperty(ep, kMIDIPropertyUniqueID, &id); |
| 144 | EXPECT_EQ(noErr, status); |
| 145 | EXPECT_NE(0, id); |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 146 | |
| 147 | // Wait until the created device is notified to MidiManagerMac. |
| 148 | MidiPortInfo info = client->WaitForPort(); |
| 149 | EXPECT_EQ("DestinationTest", info.name); |
| 150 | |
| 151 | EndSession(client.get()); |
| 152 | if (ep) |
| 153 | MIDIEndpointDispose(ep); |
| 154 | if (midi_client) |
| 155 | MIDIClientDispose(midi_client); |
| 156 | } |
| 157 | |
| 158 | } // namespace |
| 159 | |
| 160 | } // namespace media |