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> |
avi | 793390d | 2015-12-22 22:22:36 -0800 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 10 | |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 11 | #include <memory> |
| 12 | |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 13 | #include "base/run_loop.h" |
toyoshim | 4f72835 | 2015-03-03 02:30:54 -0800 | [diff] [blame] | 14 | #include "base/synchronization/lock.h" |
Carlos Caballero | 10135c2 | 2019-12-02 08:40:23 +0000 | [diff] [blame] | 15 | #include "base/test/task_environment.h" |
Takashi Toyoshima | 143f0fd | 2017-08-01 16:15:33 +0000 | [diff] [blame] | 16 | #include "media/midi/midi_service.h" |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 19 | namespace midi { |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 20 | |
| 21 | namespace { |
| 22 | |
toyoshim | ec2570a | 2016-10-21 02:15:27 -0700 | [diff] [blame] | 23 | using mojom::PortState; |
toyoshim | 2f3a48f | 2016-10-17 01:54:13 -0700 | [diff] [blame] | 24 | using mojom::Result; |
| 25 | |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 26 | void Noop(const MIDIPacketList*, void*, void*) {} |
| 27 | |
| 28 | class FakeMidiManagerClient : public MidiManagerClient { |
| 29 | public: |
| 30 | FakeMidiManagerClient() |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 31 | : result_(Result::NOT_SUPPORTED), |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 32 | wait_for_result_(true), |
toyoshim | 4f72835 | 2015-03-03 02:30:54 -0800 | [diff] [blame] | 33 | wait_for_port_(true), |
| 34 | unexpected_callback_(false) {} |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 35 | |
Peter Boström | 5e5c4fa | 2021-10-15 21:43:24 +0000 | [diff] [blame] | 36 | FakeMidiManagerClient(const FakeMidiManagerClient&) = delete; |
| 37 | FakeMidiManagerClient& operator=(const FakeMidiManagerClient&) = delete; |
| 38 | |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 39 | // MidiManagerClient implementation. |
Adithya Srinivasan | 3325273 | 2018-10-17 15:59:40 +0000 | [diff] [blame] | 40 | void AddInputPort(const mojom::PortInfo& info) override {} |
| 41 | void AddOutputPort(const mojom::PortInfo& info) override { |
toyoshim | 4f72835 | 2015-03-03 02:30:54 -0800 | [diff] [blame] | 42 | base::AutoLock lock(lock_); |
| 43 | // AddOutputPort may be called before CompleteStartSession() is invoked |
| 44 | // if one or more MIDI devices including virtual ports are connected. |
| 45 | // Just ignore in such cases. |
| 46 | if (wait_for_result_) |
| 47 | return; |
| 48 | |
| 49 | // Check if this is the first call after CompleteStartSession(), and |
| 50 | // the case should not happen twice. |
| 51 | if (!wait_for_port_) |
| 52 | unexpected_callback_ = true; |
| 53 | |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 54 | info_ = info; |
| 55 | wait_for_port_ = false; |
| 56 | } |
toyoshim | ec2570a | 2016-10-21 02:15:27 -0700 | [diff] [blame] | 57 | void SetInputPortState(uint32_t port_index, PortState state) override {} |
| 58 | void SetOutputPortState(uint32_t port_index, PortState state) override {} |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 59 | |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 60 | void CompleteStartSession(Result result) override { |
toyoshim | 4f72835 | 2015-03-03 02:30:54 -0800 | [diff] [blame] | 61 | base::AutoLock lock(lock_); |
| 62 | if (!wait_for_result_) |
| 63 | unexpected_callback_ = true; |
| 64 | |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 65 | result_ = result; |
| 66 | wait_for_result_ = false; |
| 67 | } |
| 68 | |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 69 | void ReceiveMidiData(uint32_t port_index, |
| 70 | const uint8_t* data, |
| 71 | size_t size, |
tzik | 925e2c6 | 2018-02-02 07:39:45 +0000 | [diff] [blame] | 72 | base::TimeTicks timestamp) override {} |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 73 | void AccumulateMidiBytesSent(size_t size) override {} |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 74 | void Detach() override {} |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 75 | |
toyoshim | 4f72835 | 2015-03-03 02:30:54 -0800 | [diff] [blame] | 76 | bool GetWaitForResult() { |
| 77 | base::AutoLock lock(lock_); |
| 78 | return wait_for_result_; |
| 79 | } |
| 80 | |
| 81 | bool GetWaitForPort() { |
| 82 | base::AutoLock lock(lock_); |
| 83 | return wait_for_port_; |
| 84 | } |
| 85 | |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 86 | Result WaitForResult() { |
toyoshim | 4f72835 | 2015-03-03 02:30:54 -0800 | [diff] [blame] | 87 | while (GetWaitForResult()) { |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 88 | base::RunLoop run_loop; |
| 89 | run_loop.RunUntilIdle(); |
| 90 | } |
toyoshim | 4f72835 | 2015-03-03 02:30:54 -0800 | [diff] [blame] | 91 | EXPECT_FALSE(unexpected_callback_); |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 92 | return result_; |
| 93 | } |
Adithya Srinivasan | 3325273 | 2018-10-17 15:59:40 +0000 | [diff] [blame] | 94 | mojom::PortInfo WaitForPort() { |
toyoshim | 4f72835 | 2015-03-03 02:30:54 -0800 | [diff] [blame] | 95 | while (GetWaitForPort()) { |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 96 | base::RunLoop run_loop; |
| 97 | run_loop.RunUntilIdle(); |
| 98 | } |
toyoshim | 4f72835 | 2015-03-03 02:30:54 -0800 | [diff] [blame] | 99 | EXPECT_FALSE(unexpected_callback_); |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 100 | return info_; |
| 101 | } |
| 102 | |
| 103 | private: |
toyoshim | 4f72835 | 2015-03-03 02:30:54 -0800 | [diff] [blame] | 104 | base::Lock lock_; |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 105 | Result result_; |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 106 | bool wait_for_result_; |
Adithya Srinivasan | 3325273 | 2018-10-17 15:59:40 +0000 | [diff] [blame] | 107 | mojom::PortInfo info_; |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 108 | bool wait_for_port_; |
toyoshim | 4f72835 | 2015-03-03 02:30:54 -0800 | [diff] [blame] | 109 | bool unexpected_callback_; |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | class MidiManagerMacTest : public ::testing::Test { |
| 113 | public: |
Carlos Caballero | 10135c2 | 2019-12-02 08:40:23 +0000 | [diff] [blame] | 114 | MidiManagerMacTest() : service_(std::make_unique<MidiService>()) {} |
Peter Boström | 5363403 | 2021-09-22 20:24:34 +0000 | [diff] [blame] | 115 | |
| 116 | MidiManagerMacTest(const MidiManagerMacTest&) = delete; |
| 117 | MidiManagerMacTest& operator=(const MidiManagerMacTest&) = delete; |
| 118 | |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 119 | ~MidiManagerMacTest() override { |
Takashi Toyoshima | 143f0fd | 2017-08-01 16:15:33 +0000 | [diff] [blame] | 120 | service_->Shutdown(); |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 121 | base::RunLoop run_loop; |
| 122 | run_loop.RunUntilIdle(); |
| 123 | } |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 124 | |
| 125 | protected: |
| 126 | void StartSession(MidiManagerClient* client) { |
Takashi Toyoshima | 143f0fd | 2017-08-01 16:15:33 +0000 | [diff] [blame] | 127 | service_->StartSession(client); |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 128 | } |
Takashi Toyoshima | 143f0fd | 2017-08-01 16:15:33 +0000 | [diff] [blame] | 129 | void EndSession(MidiManagerClient* client) { service_->EndSession(client); } |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 130 | |
| 131 | private: |
Takashi Toyoshima | 143f0fd | 2017-08-01 16:15:33 +0000 | [diff] [blame] | 132 | std::unique_ptr<MidiService> service_; |
Carlos Caballero | 10135c2 | 2019-12-02 08:40:23 +0000 | [diff] [blame] | 133 | base::test::SingleThreadTaskEnvironment task_environment_; |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | |
| 137 | TEST_F(MidiManagerMacTest, MidiNotification) { |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 138 | std::unique_ptr<FakeMidiManagerClient> client(new FakeMidiManagerClient); |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 139 | StartSession(client.get()); |
| 140 | |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 141 | Result result = client->WaitForResult(); |
| 142 | EXPECT_EQ(Result::OK, result); |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 143 | |
| 144 | // Create MIDIClient, and MIDIEndpoint as a MIDIDestination. This should |
| 145 | // notify MIDIManagerMac as a MIDIObjectAddRemoveNotification. |
| 146 | MIDIClientRef midi_client = 0; |
| 147 | OSStatus status = MIDIClientCreate( |
| 148 | CFSTR("MidiManagerMacTest"), nullptr, nullptr, &midi_client); |
| 149 | EXPECT_EQ(noErr, status); |
| 150 | |
| 151 | MIDIEndpointRef ep = 0; |
| 152 | status = MIDIDestinationCreate( |
| 153 | midi_client, CFSTR("DestinationTest"), Noop, nullptr, &ep); |
| 154 | EXPECT_EQ(noErr, status); |
toyoshim | c974698 | 2015-04-03 05:32:55 -0700 | [diff] [blame] | 155 | SInt32 id; |
| 156 | status = MIDIObjectGetIntegerProperty(ep, kMIDIPropertyUniqueID, &id); |
| 157 | EXPECT_EQ(noErr, status); |
| 158 | EXPECT_NE(0, id); |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 159 | |
| 160 | // Wait until the created device is notified to MidiManagerMac. |
Adithya Srinivasan | 3325273 | 2018-10-17 15:59:40 +0000 | [diff] [blame] | 161 | mojom::PortInfo info = client->WaitForPort(); |
toyoshim | f219752 | 2015-02-19 20:53:55 -0800 | [diff] [blame] | 162 | EXPECT_EQ("DestinationTest", info.name); |
| 163 | |
| 164 | EndSession(client.get()); |
| 165 | if (ep) |
| 166 | MIDIEndpointDispose(ep); |
| 167 | if (midi_client) |
| 168 | MIDIClientDispose(midi_client); |
| 169 | } |
| 170 | |
| 171 | } // namespace |
| 172 | |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 173 | } // namespace midi |