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