blob: 4ee525e93b6534561f32e07781866801b590c114 [file] [log] [blame]
toyoshimf2197522015-02-19 20:53:55 -08001// 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>
avi793390d2015-12-22 22:22:36 -08008#include <stddef.h>
9#include <stdint.h>
toyoshimf2197522015-02-19 20:53:55 -080010
danakj75afea02016-04-25 20:36:04 -070011#include <memory>
12
toyoshimf2197522015-02-19 20:53:55 -080013#include "base/logging.h"
avi793390d2015-12-22 22:22:36 -080014#include "base/macros.h"
toyoshimf2197522015-02-19 20:53:55 -080015#include "base/message_loop/message_loop.h"
16#include "base/run_loop.h"
toyoshim4f728352015-03-03 02:30:54 -080017#include "base/synchronization/lock.h"
toyoshimf2197522015-02-19 20:53:55 -080018#include "testing/gtest/include/gtest/gtest.h"
19
toyoshime147c5e2015-05-07 21:58:31 -070020namespace midi {
toyoshimf2197522015-02-19 20:53:55 -080021
22namespace {
23
24void Noop(const MIDIPacketList*, void*, void*) {}
25
26class FakeMidiManagerClient : public MidiManagerClient {
27 public:
28 FakeMidiManagerClient()
toyoshimf1b88962015-07-09 14:14:51 -070029 : result_(Result::NOT_SUPPORTED),
toyoshimf2197522015-02-19 20:53:55 -080030 wait_for_result_(true),
toyoshim4f728352015-03-03 02:30:54 -080031 wait_for_port_(true),
32 unexpected_callback_(false) {}
toyoshimf2197522015-02-19 20:53:55 -080033
34 // MidiManagerClient implementation.
35 void AddInputPort(const MidiPortInfo& info) override {}
36 void AddOutputPort(const MidiPortInfo& info) override {
toyoshim4f728352015-03-03 02:30:54 -080037 base::AutoLock lock(lock_);
38 // AddOutputPort may be called before CompleteStartSession() is invoked
39 // if one or more MIDI devices including virtual ports are connected.
40 // Just ignore in such cases.
41 if (wait_for_result_)
42 return;
43
44 // Check if this is the first call after CompleteStartSession(), and
45 // the case should not happen twice.
46 if (!wait_for_port_)
47 unexpected_callback_ = true;
48
toyoshimf2197522015-02-19 20:53:55 -080049 info_ = info;
50 wait_for_port_ = false;
51 }
Avi Drissman3528fd02015-12-18 20:11:31 -050052 void SetInputPortState(uint32_t port_index, MidiPortState state) override {}
53 void SetOutputPortState(uint32_t port_index, MidiPortState state) override {}
toyoshimf2197522015-02-19 20:53:55 -080054
toyoshimf1b88962015-07-09 14:14:51 -070055 void CompleteStartSession(Result result) override {
toyoshim4f728352015-03-03 02:30:54 -080056 base::AutoLock lock(lock_);
57 if (!wait_for_result_)
58 unexpected_callback_ = true;
59
toyoshimf2197522015-02-19 20:53:55 -080060 result_ = result;
61 wait_for_result_ = false;
62 }
63
Avi Drissman3528fd02015-12-18 20:11:31 -050064 void ReceiveMidiData(uint32_t port_index,
65 const uint8_t* data,
66 size_t size,
toyoshimf2197522015-02-19 20:53:55 -080067 double timestamp) override {}
68 void AccumulateMidiBytesSent(size_t size) override {}
toyoshim7a809662015-10-06 00:54:21 -070069 void Detach() override {}
toyoshimf2197522015-02-19 20:53:55 -080070
toyoshim4f728352015-03-03 02:30:54 -080071 bool GetWaitForResult() {
72 base::AutoLock lock(lock_);
73 return wait_for_result_;
74 }
75
76 bool GetWaitForPort() {
77 base::AutoLock lock(lock_);
78 return wait_for_port_;
79 }
80
toyoshimf1b88962015-07-09 14:14:51 -070081 Result WaitForResult() {
toyoshim4f728352015-03-03 02:30:54 -080082 while (GetWaitForResult()) {
toyoshimf2197522015-02-19 20:53:55 -080083 base::RunLoop run_loop;
84 run_loop.RunUntilIdle();
85 }
toyoshim4f728352015-03-03 02:30:54 -080086 EXPECT_FALSE(unexpected_callback_);
toyoshimf2197522015-02-19 20:53:55 -080087 return result_;
88 }
89 MidiPortInfo WaitForPort() {
toyoshim4f728352015-03-03 02:30:54 -080090 while (GetWaitForPort()) {
toyoshimf2197522015-02-19 20:53:55 -080091 base::RunLoop run_loop;
92 run_loop.RunUntilIdle();
93 }
toyoshim4f728352015-03-03 02:30:54 -080094 EXPECT_FALSE(unexpected_callback_);
toyoshimf2197522015-02-19 20:53:55 -080095 return info_;
96 }
97
98 private:
toyoshim4f728352015-03-03 02:30:54 -080099 base::Lock lock_;
toyoshimf1b88962015-07-09 14:14:51 -0700100 Result result_;
toyoshimf2197522015-02-19 20:53:55 -0800101 bool wait_for_result_;
102 MidiPortInfo info_;
103 bool wait_for_port_;
toyoshim4f728352015-03-03 02:30:54 -0800104 bool unexpected_callback_;
toyoshimf2197522015-02-19 20:53:55 -0800105
106 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient);
107};
108
109class MidiManagerMacTest : public ::testing::Test {
110 public:
111 MidiManagerMacTest()
112 : manager_(new MidiManagerMac),
113 message_loop_(new base::MessageLoop) {}
toyoshim7a809662015-10-06 00:54:21 -0700114 ~MidiManagerMacTest() override {
115 manager_->Shutdown();
116 base::RunLoop run_loop;
117 run_loop.RunUntilIdle();
118 }
toyoshimf2197522015-02-19 20:53:55 -0800119
120 protected:
121 void StartSession(MidiManagerClient* client) {
122 manager_->StartSession(client);
123 }
124 void EndSession(MidiManagerClient* client) {
125 manager_->EndSession(client);
126 }
127
128 private:
danakj75afea02016-04-25 20:36:04 -0700129 std::unique_ptr<MidiManager> manager_;
130 std::unique_ptr<base::MessageLoop> message_loop_;
toyoshimf2197522015-02-19 20:53:55 -0800131
132 DISALLOW_COPY_AND_ASSIGN(MidiManagerMacTest);
133};
134
135
136TEST_F(MidiManagerMacTest, MidiNotification) {
danakj75afea02016-04-25 20:36:04 -0700137 std::unique_ptr<FakeMidiManagerClient> client(new FakeMidiManagerClient);
toyoshimf2197522015-02-19 20:53:55 -0800138 StartSession(client.get());
139
toyoshimf1b88962015-07-09 14:14:51 -0700140 Result result = client->WaitForResult();
141 EXPECT_EQ(Result::OK, result);
toyoshimf2197522015-02-19 20:53:55 -0800142
143 // Create MIDIClient, and MIDIEndpoint as a MIDIDestination. This should
144 // notify MIDIManagerMac as a MIDIObjectAddRemoveNotification.
145 MIDIClientRef midi_client = 0;
146 OSStatus status = MIDIClientCreate(
147 CFSTR("MidiManagerMacTest"), nullptr, nullptr, &midi_client);
148 EXPECT_EQ(noErr, status);
149
150 MIDIEndpointRef ep = 0;
151 status = MIDIDestinationCreate(
152 midi_client, CFSTR("DestinationTest"), Noop, nullptr, &ep);
153 EXPECT_EQ(noErr, status);
toyoshimc9746982015-04-03 05:32:55 -0700154 SInt32 id;
155 status = MIDIObjectGetIntegerProperty(ep, kMIDIPropertyUniqueID, &id);
156 EXPECT_EQ(noErr, status);
157 EXPECT_NE(0, id);
toyoshimf2197522015-02-19 20:53:55 -0800158
159 // Wait until the created device is notified to MidiManagerMac.
160 MidiPortInfo info = client->WaitForPort();
161 EXPECT_EQ("DestinationTest", info.name);
162
163 EndSession(client.get());
164 if (ep)
165 MIDIEndpointDispose(ep);
166 if (midi_client)
167 MIDIClientDispose(midi_client);
168}
169
170} // namespace
171
toyoshime147c5e2015-05-07 21:58:31 -0700172} // namespace midi