blob: c8d80e5773ecc11eaacbc03068251703a190c759 [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
20namespace media {
toyoshime147c5e2015-05-07 21:58:31 -070021namespace midi {
toyoshimf2197522015-02-19 20:53:55 -080022
23namespace {
24
25void Noop(const MIDIPacketList*, void*, void*) {}
26
27class FakeMidiManagerClient : public MidiManagerClient {
28 public:
29 FakeMidiManagerClient()
toyoshimf1b88962015-07-09 14:14:51 -070030 : result_(Result::NOT_SUPPORTED),
toyoshimf2197522015-02-19 20:53:55 -080031 wait_for_result_(true),
toyoshim4f728352015-03-03 02:30:54 -080032 wait_for_port_(true),
33 unexpected_callback_(false) {}
toyoshimf2197522015-02-19 20:53:55 -080034
35 // MidiManagerClient implementation.
36 void AddInputPort(const MidiPortInfo& info) override {}
37 void AddOutputPort(const MidiPortInfo& info) override {
toyoshim4f728352015-03-03 02:30:54 -080038 base::AutoLock lock(lock_);
39 // AddOutputPort may be called before CompleteStartSession() is invoked
40 // if one or more MIDI devices including virtual ports are connected.
41 // Just ignore in such cases.
42 if (wait_for_result_)
43 return;
44
45 // Check if this is the first call after CompleteStartSession(), and
46 // the case should not happen twice.
47 if (!wait_for_port_)
48 unexpected_callback_ = true;
49
toyoshimf2197522015-02-19 20:53:55 -080050 info_ = info;
51 wait_for_port_ = false;
52 }
Avi Drissman3528fd02015-12-18 20:11:31 -050053 void SetInputPortState(uint32_t port_index, MidiPortState state) override {}
54 void SetOutputPortState(uint32_t port_index, MidiPortState state) override {}
toyoshimf2197522015-02-19 20:53:55 -080055
toyoshimf1b88962015-07-09 14:14:51 -070056 void CompleteStartSession(Result result) override {
toyoshim4f728352015-03-03 02:30:54 -080057 base::AutoLock lock(lock_);
58 if (!wait_for_result_)
59 unexpected_callback_ = true;
60
toyoshimf2197522015-02-19 20:53:55 -080061 result_ = result;
62 wait_for_result_ = false;
63 }
64
Avi Drissman3528fd02015-12-18 20:11:31 -050065 void ReceiveMidiData(uint32_t port_index,
66 const uint8_t* data,
67 size_t size,
toyoshimf2197522015-02-19 20:53:55 -080068 double timestamp) override {}
69 void AccumulateMidiBytesSent(size_t size) override {}
toyoshim7a809662015-10-06 00:54:21 -070070 void Detach() override {}
toyoshimf2197522015-02-19 20:53:55 -080071
toyoshim4f728352015-03-03 02:30:54 -080072 bool GetWaitForResult() {
73 base::AutoLock lock(lock_);
74 return wait_for_result_;
75 }
76
77 bool GetWaitForPort() {
78 base::AutoLock lock(lock_);
79 return wait_for_port_;
80 }
81
toyoshimf1b88962015-07-09 14:14:51 -070082 Result WaitForResult() {
toyoshim4f728352015-03-03 02:30:54 -080083 while (GetWaitForResult()) {
toyoshimf2197522015-02-19 20:53:55 -080084 base::RunLoop run_loop;
85 run_loop.RunUntilIdle();
86 }
toyoshim4f728352015-03-03 02:30:54 -080087 EXPECT_FALSE(unexpected_callback_);
toyoshimf2197522015-02-19 20:53:55 -080088 return result_;
89 }
90 MidiPortInfo WaitForPort() {
toyoshim4f728352015-03-03 02:30:54 -080091 while (GetWaitForPort()) {
toyoshimf2197522015-02-19 20:53:55 -080092 base::RunLoop run_loop;
93 run_loop.RunUntilIdle();
94 }
toyoshim4f728352015-03-03 02:30:54 -080095 EXPECT_FALSE(unexpected_callback_);
toyoshimf2197522015-02-19 20:53:55 -080096 return info_;
97 }
98
99 private:
toyoshim4f728352015-03-03 02:30:54 -0800100 base::Lock lock_;
toyoshimf1b88962015-07-09 14:14:51 -0700101 Result result_;
toyoshimf2197522015-02-19 20:53:55 -0800102 bool wait_for_result_;
103 MidiPortInfo info_;
104 bool wait_for_port_;
toyoshim4f728352015-03-03 02:30:54 -0800105 bool unexpected_callback_;
toyoshimf2197522015-02-19 20:53:55 -0800106
107 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient);
108};
109
110class MidiManagerMacTest : public ::testing::Test {
111 public:
112 MidiManagerMacTest()
113 : manager_(new MidiManagerMac),
114 message_loop_(new base::MessageLoop) {}
toyoshim7a809662015-10-06 00:54:21 -0700115 ~MidiManagerMacTest() override {
116 manager_->Shutdown();
117 base::RunLoop run_loop;
118 run_loop.RunUntilIdle();
119 }
toyoshimf2197522015-02-19 20:53:55 -0800120
121 protected:
122 void StartSession(MidiManagerClient* client) {
123 manager_->StartSession(client);
124 }
125 void EndSession(MidiManagerClient* client) {
126 manager_->EndSession(client);
127 }
128
129 private:
danakj75afea02016-04-25 20:36:04 -0700130 std::unique_ptr<MidiManager> manager_;
131 std::unique_ptr<base::MessageLoop> message_loop_;
toyoshimf2197522015-02-19 20:53:55 -0800132
133 DISALLOW_COPY_AND_ASSIGN(MidiManagerMacTest);
134};
135
136
137TEST_F(MidiManagerMacTest, MidiNotification) {
danakj75afea02016-04-25 20:36:04 -0700138 std::unique_ptr<FakeMidiManagerClient> client(new FakeMidiManagerClient);
toyoshimf2197522015-02-19 20:53:55 -0800139 StartSession(client.get());
140
toyoshimf1b88962015-07-09 14:14:51 -0700141 Result result = client->WaitForResult();
142 EXPECT_EQ(Result::OK, result);
toyoshimf2197522015-02-19 20:53:55 -0800143
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);
toyoshimc9746982015-04-03 05:32:55 -0700155 SInt32 id;
156 status = MIDIObjectGetIntegerProperty(ep, kMIDIPropertyUniqueID, &id);
157 EXPECT_EQ(noErr, status);
158 EXPECT_NE(0, id);
toyoshimf2197522015-02-19 20:53:55 -0800159
160 // Wait until the created device is notified to MidiManagerMac.
161 MidiPortInfo info = client->WaitForPort();
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
toyoshime147c5e2015-05-07 21:58:31 -0700173} // namespace midi
toyoshimf2197522015-02-19 20:53:55 -0800174} // namespace media