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.
5 #include "media/midi/midi_manager_mac.h"
7 #include <CoreMIDI/MIDIServices.h>
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"
13 #include "base/synchronization/lock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
20 void Noop(const MIDIPacketList
*, void*, void*) {}
22 class FakeMidiManagerClient
: public MidiManagerClient
{
24 FakeMidiManagerClient()
25 : result_(MIDI_NOT_SUPPORTED
),
26 wait_for_result_(true),
28 unexpected_callback_(false) {}
30 // MidiManagerClient implementation.
31 void AddInputPort(const MidiPortInfo
& info
) override
{}
32 void AddOutputPort(const MidiPortInfo
& info
) override
{
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.
40 // Check if this is the first call after CompleteStartSession(), and
41 // the case should not happen twice.
43 unexpected_callback_
= true;
46 wait_for_port_
= false;
48 void SetInputPortState(uint32 port_index
, MidiPortState state
) override
{}
49 void SetOutputPortState(uint32 port_index
, MidiPortState state
) override
{}
51 void CompleteStartSession(MidiResult result
) override
{
52 base::AutoLock
lock(lock_
);
53 if (!wait_for_result_
)
54 unexpected_callback_
= true;
57 wait_for_result_
= false;
60 void ReceiveMidiData(uint32 port_index
, const uint8
* data
, size_t size
,
61 double timestamp
) override
{}
62 void AccumulateMidiBytesSent(size_t size
) override
{}
64 bool GetWaitForResult() {
65 base::AutoLock
lock(lock_
);
66 return wait_for_result_
;
69 bool GetWaitForPort() {
70 base::AutoLock
lock(lock_
);
71 return wait_for_port_
;
74 MidiResult
WaitForResult() {
75 while (GetWaitForResult()) {
76 base::RunLoop run_loop
;
77 run_loop
.RunUntilIdle();
79 EXPECT_FALSE(unexpected_callback_
);
82 MidiPortInfo
WaitForPort() {
83 while (GetWaitForPort()) {
84 base::RunLoop run_loop
;
85 run_loop
.RunUntilIdle();
87 EXPECT_FALSE(unexpected_callback_
);
94 bool wait_for_result_
;
97 bool unexpected_callback_
;
99 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient
);
102 class MidiManagerMacTest
: public ::testing::Test
{
105 : manager_(new MidiManagerMac
),
106 message_loop_(new base::MessageLoop
) {}
109 void StartSession(MidiManagerClient
* client
) {
110 manager_
->StartSession(client
);
112 void EndSession(MidiManagerClient
* client
) {
113 manager_
->EndSession(client
);
117 scoped_ptr
<MidiManager
> manager_
;
118 scoped_ptr
<base::MessageLoop
> message_loop_
;
120 DISALLOW_COPY_AND_ASSIGN(MidiManagerMacTest
);
124 TEST_F(MidiManagerMacTest
, MidiNotification
) {
125 scoped_ptr
<FakeMidiManagerClient
> client(new FakeMidiManagerClient
);
126 StartSession(client
.get());
128 MidiResult result
= client
->WaitForResult();
129 EXPECT_EQ(MIDI_OK
, result
);
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
);
138 MIDIEndpointRef ep
= 0;
139 status
= MIDIDestinationCreate(
140 midi_client
, CFSTR("DestinationTest"), Noop
, nullptr, &ep
);
141 EXPECT_EQ(noErr
, status
);
143 status
= MIDIObjectGetIntegerProperty(ep
, kMIDIPropertyUniqueID
, &id
);
144 EXPECT_EQ(noErr
, status
);
147 // Wait until the created device is notified to MidiManagerMac.
148 MidiPortInfo info
= client
->WaitForPort();
149 EXPECT_EQ("DestinationTest", info
.name
);
151 EndSession(client
.get());
153 MIDIEndpointDispose(ep
);
155 MIDIClientDispose(midi_client
);