blob: 244576ef53b4493974efb6d01d681ee4696aede4 [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 }
49 void SetInputPortState(uint32 port_index, MidiPortState state) override {}
50 void SetOutputPortState(uint32 port_index, MidiPortState state) override {}
51
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
61 void ReceiveMidiData(uint32 port_index, const uint8* data, size_t size,
62 double timestamp) override {}
63 void AccumulateMidiBytesSent(size_t size) override {}
toyoshim7a809662015-10-06 00:54:21 -070064 void Detach() override {}
toyoshimf2197522015-02-19 20:53:55 -080065
toyoshim4f728352015-03-03 02:30:54 -080066 bool GetWaitForResult() {
67 base::AutoLock lock(lock_);
68 return wait_for_result_;
69 }
70
71 bool GetWaitForPort() {
72 base::AutoLock lock(lock_);
73 return wait_for_port_;
74 }
75
toyoshimf1b88962015-07-09 14:14:51 -070076 Result WaitForResult() {
toyoshim4f728352015-03-03 02:30:54 -080077 while (GetWaitForResult()) {
toyoshimf2197522015-02-19 20:53:55 -080078 base::RunLoop run_loop;
79 run_loop.RunUntilIdle();
80 }
toyoshim4f728352015-03-03 02:30:54 -080081 EXPECT_FALSE(unexpected_callback_);
toyoshimf2197522015-02-19 20:53:55 -080082 return result_;
83 }
84 MidiPortInfo WaitForPort() {
toyoshim4f728352015-03-03 02:30:54 -080085 while (GetWaitForPort()) {
toyoshimf2197522015-02-19 20:53:55 -080086 base::RunLoop run_loop;
87 run_loop.RunUntilIdle();
88 }
toyoshim4f728352015-03-03 02:30:54 -080089 EXPECT_FALSE(unexpected_callback_);
toyoshimf2197522015-02-19 20:53:55 -080090 return info_;
91 }
92
93 private:
toyoshim4f728352015-03-03 02:30:54 -080094 base::Lock lock_;
toyoshimf1b88962015-07-09 14:14:51 -070095 Result result_;
toyoshimf2197522015-02-19 20:53:55 -080096 bool wait_for_result_;
97 MidiPortInfo info_;
98 bool wait_for_port_;
toyoshim4f728352015-03-03 02:30:54 -080099 bool unexpected_callback_;
toyoshimf2197522015-02-19 20:53:55 -0800100
101 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient);
102};
103
104class MidiManagerMacTest : public ::testing::Test {
105 public:
106 MidiManagerMacTest()
107 : manager_(new MidiManagerMac),
108 message_loop_(new base::MessageLoop) {}
toyoshim7a809662015-10-06 00:54:21 -0700109 ~MidiManagerMacTest() override {
110 manager_->Shutdown();
111 base::RunLoop run_loop;
112 run_loop.RunUntilIdle();
113 }
toyoshimf2197522015-02-19 20:53:55 -0800114
115 protected:
116 void StartSession(MidiManagerClient* client) {
117 manager_->StartSession(client);
118 }
119 void EndSession(MidiManagerClient* client) {
120 manager_->EndSession(client);
121 }
122
123 private:
124 scoped_ptr<MidiManager> manager_;
125 scoped_ptr<base::MessageLoop> message_loop_;
126
127 DISALLOW_COPY_AND_ASSIGN(MidiManagerMacTest);
128};
129
130
131TEST_F(MidiManagerMacTest, MidiNotification) {
132 scoped_ptr<FakeMidiManagerClient> client(new FakeMidiManagerClient);
133 StartSession(client.get());
134
toyoshimf1b88962015-07-09 14:14:51 -0700135 Result result = client->WaitForResult();
136 EXPECT_EQ(Result::OK, result);
toyoshimf2197522015-02-19 20:53:55 -0800137
138 // Create MIDIClient, and MIDIEndpoint as a MIDIDestination. This should
139 // notify MIDIManagerMac as a MIDIObjectAddRemoveNotification.
140 MIDIClientRef midi_client = 0;
141 OSStatus status = MIDIClientCreate(
142 CFSTR("MidiManagerMacTest"), nullptr, nullptr, &midi_client);
143 EXPECT_EQ(noErr, status);
144
145 MIDIEndpointRef ep = 0;
146 status = MIDIDestinationCreate(
147 midi_client, CFSTR("DestinationTest"), Noop, nullptr, &ep);
148 EXPECT_EQ(noErr, status);
toyoshimc9746982015-04-03 05:32:55 -0700149 SInt32 id;
150 status = MIDIObjectGetIntegerProperty(ep, kMIDIPropertyUniqueID, &id);
151 EXPECT_EQ(noErr, status);
152 EXPECT_NE(0, id);
toyoshimf2197522015-02-19 20:53:55 -0800153
154 // Wait until the created device is notified to MidiManagerMac.
155 MidiPortInfo info = client->WaitForPort();
156 EXPECT_EQ("DestinationTest", info.name);
157
158 EndSession(client.get());
159 if (ep)
160 MIDIEndpointDispose(ep);
161 if (midi_client)
162 MIDIClientDispose(midi_client);
163}
164
165} // namespace
166
toyoshime147c5e2015-05-07 21:58:31 -0700167} // namespace midi
toyoshimf2197522015-02-19 20:53:55 -0800168} // namespace media