blob: 9bd01c3f23516abfe291b58c758a3d2aac107723 [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>
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"
toyoshim4f728352015-03-03 02:30:54 -080013#include "base/synchronization/lock.h"
toyoshimf2197522015-02-19 20:53:55 -080014#include "testing/gtest/include/gtest/gtest.h"
15
16namespace media {
toyoshime147c5e2015-05-07 21:58:31 -070017namespace midi {
toyoshimf2197522015-02-19 20:53:55 -080018
19namespace {
20
21void Noop(const MIDIPacketList*, void*, void*) {}
22
23class FakeMidiManagerClient : public MidiManagerClient {
24 public:
25 FakeMidiManagerClient()
toyoshimf1b88962015-07-09 14:14:51 -070026 : result_(Result::NOT_SUPPORTED),
toyoshimf2197522015-02-19 20:53:55 -080027 wait_for_result_(true),
toyoshim4f728352015-03-03 02:30:54 -080028 wait_for_port_(true),
29 unexpected_callback_(false) {}
toyoshimf2197522015-02-19 20:53:55 -080030
31 // MidiManagerClient implementation.
32 void AddInputPort(const MidiPortInfo& info) override {}
33 void AddOutputPort(const MidiPortInfo& info) override {
toyoshim4f728352015-03-03 02:30:54 -080034 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
toyoshimf2197522015-02-19 20:53:55 -080046 info_ = info;
47 wait_for_port_ = false;
48 }
Avi Drissman3528fd02015-12-18 20:11:31 -050049 void SetInputPortState(uint32_t port_index, MidiPortState state) override {}
50 void SetOutputPortState(uint32_t port_index, MidiPortState state) override {}
toyoshimf2197522015-02-19 20:53:55 -080051
toyoshimf1b88962015-07-09 14:14:51 -070052 void CompleteStartSession(Result result) override {
toyoshim4f728352015-03-03 02:30:54 -080053 base::AutoLock lock(lock_);
54 if (!wait_for_result_)
55 unexpected_callback_ = true;
56
toyoshimf2197522015-02-19 20:53:55 -080057 result_ = result;
58 wait_for_result_ = false;
59 }
60
Avi Drissman3528fd02015-12-18 20:11:31 -050061 void ReceiveMidiData(uint32_t port_index,
62 const uint8_t* data,
63 size_t size,
toyoshimf2197522015-02-19 20:53:55 -080064 double timestamp) override {}
65 void AccumulateMidiBytesSent(size_t size) override {}
toyoshim7a809662015-10-06 00:54:21 -070066 void Detach() override {}
toyoshimf2197522015-02-19 20:53:55 -080067
toyoshim4f728352015-03-03 02:30:54 -080068 bool GetWaitForResult() {
69 base::AutoLock lock(lock_);
70 return wait_for_result_;
71 }
72
73 bool GetWaitForPort() {
74 base::AutoLock lock(lock_);
75 return wait_for_port_;
76 }
77
toyoshimf1b88962015-07-09 14:14:51 -070078 Result WaitForResult() {
toyoshim4f728352015-03-03 02:30:54 -080079 while (GetWaitForResult()) {
toyoshimf2197522015-02-19 20:53:55 -080080 base::RunLoop run_loop;
81 run_loop.RunUntilIdle();
82 }
toyoshim4f728352015-03-03 02:30:54 -080083 EXPECT_FALSE(unexpected_callback_);
toyoshimf2197522015-02-19 20:53:55 -080084 return result_;
85 }
86 MidiPortInfo WaitForPort() {
toyoshim4f728352015-03-03 02:30:54 -080087 while (GetWaitForPort()) {
toyoshimf2197522015-02-19 20:53:55 -080088 base::RunLoop run_loop;
89 run_loop.RunUntilIdle();
90 }
toyoshim4f728352015-03-03 02:30:54 -080091 EXPECT_FALSE(unexpected_callback_);
toyoshimf2197522015-02-19 20:53:55 -080092 return info_;
93 }
94
95 private:
toyoshim4f728352015-03-03 02:30:54 -080096 base::Lock lock_;
toyoshimf1b88962015-07-09 14:14:51 -070097 Result result_;
toyoshimf2197522015-02-19 20:53:55 -080098 bool wait_for_result_;
99 MidiPortInfo info_;
100 bool wait_for_port_;
toyoshim4f728352015-03-03 02:30:54 -0800101 bool unexpected_callback_;
toyoshimf2197522015-02-19 20:53:55 -0800102
103 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient);
104};
105
106class MidiManagerMacTest : public ::testing::Test {
107 public:
108 MidiManagerMacTest()
109 : manager_(new MidiManagerMac),
110 message_loop_(new base::MessageLoop) {}
toyoshim7a809662015-10-06 00:54:21 -0700111 ~MidiManagerMacTest() override {
112 manager_->Shutdown();
113 base::RunLoop run_loop;
114 run_loop.RunUntilIdle();
115 }
toyoshimf2197522015-02-19 20:53:55 -0800116
117 protected:
118 void StartSession(MidiManagerClient* client) {
119 manager_->StartSession(client);
120 }
121 void EndSession(MidiManagerClient* client) {
122 manager_->EndSession(client);
123 }
124
125 private:
126 scoped_ptr<MidiManager> manager_;
127 scoped_ptr<base::MessageLoop> message_loop_;
128
129 DISALLOW_COPY_AND_ASSIGN(MidiManagerMacTest);
130};
131
132
133TEST_F(MidiManagerMacTest, MidiNotification) {
134 scoped_ptr<FakeMidiManagerClient> client(new FakeMidiManagerClient);
135 StartSession(client.get());
136
toyoshimf1b88962015-07-09 14:14:51 -0700137 Result result = client->WaitForResult();
138 EXPECT_EQ(Result::OK, result);
toyoshimf2197522015-02-19 20:53:55 -0800139
140 // Create MIDIClient, and MIDIEndpoint as a MIDIDestination. This should
141 // notify MIDIManagerMac as a MIDIObjectAddRemoveNotification.
142 MIDIClientRef midi_client = 0;
143 OSStatus status = MIDIClientCreate(
144 CFSTR("MidiManagerMacTest"), nullptr, nullptr, &midi_client);
145 EXPECT_EQ(noErr, status);
146
147 MIDIEndpointRef ep = 0;
148 status = MIDIDestinationCreate(
149 midi_client, CFSTR("DestinationTest"), Noop, nullptr, &ep);
150 EXPECT_EQ(noErr, status);
toyoshimc9746982015-04-03 05:32:55 -0700151 SInt32 id;
152 status = MIDIObjectGetIntegerProperty(ep, kMIDIPropertyUniqueID, &id);
153 EXPECT_EQ(noErr, status);
154 EXPECT_NE(0, id);
toyoshimf2197522015-02-19 20:53:55 -0800155
156 // Wait until the created device is notified to MidiManagerMac.
157 MidiPortInfo info = client->WaitForPort();
158 EXPECT_EQ("DestinationTest", info.name);
159
160 EndSession(client.get());
161 if (ep)
162 MIDIEndpointDispose(ep);
163 if (midi_client)
164 MIDIClientDispose(midi_client);
165}
166
167} // namespace
168
toyoshime147c5e2015-05-07 21:58:31 -0700169} // namespace midi
toyoshimf2197522015-02-19 20:53:55 -0800170} // namespace media