blob: f0871f7d715cecf3205a63fc6680a5ea7e3db1aa [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
toyoshim2f3a48f2016-10-17 01:54:13 -070024using mojom::Result;
25
toyoshimf2197522015-02-19 20:53:55 -080026void Noop(const MIDIPacketList*, void*, void*) {}
27
28class FakeMidiManagerClient : public MidiManagerClient {
29 public:
30 FakeMidiManagerClient()
toyoshimf1b88962015-07-09 14:14:51 -070031 : result_(Result::NOT_SUPPORTED),
toyoshimf2197522015-02-19 20:53:55 -080032 wait_for_result_(true),
toyoshim4f728352015-03-03 02:30:54 -080033 wait_for_port_(true),
34 unexpected_callback_(false) {}
toyoshimf2197522015-02-19 20:53:55 -080035
36 // MidiManagerClient implementation.
37 void AddInputPort(const MidiPortInfo& info) override {}
38 void AddOutputPort(const MidiPortInfo& info) override {
toyoshim4f728352015-03-03 02:30:54 -080039 base::AutoLock lock(lock_);
40 // AddOutputPort may be called before CompleteStartSession() is invoked
41 // if one or more MIDI devices including virtual ports are connected.
42 // Just ignore in such cases.
43 if (wait_for_result_)
44 return;
45
46 // Check if this is the first call after CompleteStartSession(), and
47 // the case should not happen twice.
48 if (!wait_for_port_)
49 unexpected_callback_ = true;
50
toyoshimf2197522015-02-19 20:53:55 -080051 info_ = info;
52 wait_for_port_ = false;
53 }
Avi Drissman3528fd02015-12-18 20:11:31 -050054 void SetInputPortState(uint32_t port_index, MidiPortState state) override {}
55 void SetOutputPortState(uint32_t port_index, MidiPortState state) override {}
toyoshimf2197522015-02-19 20:53:55 -080056
toyoshimf1b88962015-07-09 14:14:51 -070057 void CompleteStartSession(Result result) override {
toyoshim4f728352015-03-03 02:30:54 -080058 base::AutoLock lock(lock_);
59 if (!wait_for_result_)
60 unexpected_callback_ = true;
61
toyoshimf2197522015-02-19 20:53:55 -080062 result_ = result;
63 wait_for_result_ = false;
64 }
65
Avi Drissman3528fd02015-12-18 20:11:31 -050066 void ReceiveMidiData(uint32_t port_index,
67 const uint8_t* data,
68 size_t size,
toyoshimf2197522015-02-19 20:53:55 -080069 double timestamp) override {}
70 void AccumulateMidiBytesSent(size_t size) override {}
toyoshim7a809662015-10-06 00:54:21 -070071 void Detach() override {}
toyoshimf2197522015-02-19 20:53:55 -080072
toyoshim4f728352015-03-03 02:30:54 -080073 bool GetWaitForResult() {
74 base::AutoLock lock(lock_);
75 return wait_for_result_;
76 }
77
78 bool GetWaitForPort() {
79 base::AutoLock lock(lock_);
80 return wait_for_port_;
81 }
82
toyoshimf1b88962015-07-09 14:14:51 -070083 Result WaitForResult() {
toyoshim4f728352015-03-03 02:30:54 -080084 while (GetWaitForResult()) {
toyoshimf2197522015-02-19 20:53:55 -080085 base::RunLoop run_loop;
86 run_loop.RunUntilIdle();
87 }
toyoshim4f728352015-03-03 02:30:54 -080088 EXPECT_FALSE(unexpected_callback_);
toyoshimf2197522015-02-19 20:53:55 -080089 return result_;
90 }
91 MidiPortInfo WaitForPort() {
toyoshim4f728352015-03-03 02:30:54 -080092 while (GetWaitForPort()) {
toyoshimf2197522015-02-19 20:53:55 -080093 base::RunLoop run_loop;
94 run_loop.RunUntilIdle();
95 }
toyoshim4f728352015-03-03 02:30:54 -080096 EXPECT_FALSE(unexpected_callback_);
toyoshimf2197522015-02-19 20:53:55 -080097 return info_;
98 }
99
100 private:
toyoshim4f728352015-03-03 02:30:54 -0800101 base::Lock lock_;
toyoshimf1b88962015-07-09 14:14:51 -0700102 Result result_;
toyoshimf2197522015-02-19 20:53:55 -0800103 bool wait_for_result_;
104 MidiPortInfo info_;
105 bool wait_for_port_;
toyoshim4f728352015-03-03 02:30:54 -0800106 bool unexpected_callback_;
toyoshimf2197522015-02-19 20:53:55 -0800107
108 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient);
109};
110
111class MidiManagerMacTest : public ::testing::Test {
112 public:
113 MidiManagerMacTest()
114 : manager_(new MidiManagerMac),
115 message_loop_(new base::MessageLoop) {}
toyoshim7a809662015-10-06 00:54:21 -0700116 ~MidiManagerMacTest() override {
117 manager_->Shutdown();
118 base::RunLoop run_loop;
119 run_loop.RunUntilIdle();
120 }
toyoshimf2197522015-02-19 20:53:55 -0800121
122 protected:
123 void StartSession(MidiManagerClient* client) {
124 manager_->StartSession(client);
125 }
126 void EndSession(MidiManagerClient* client) {
127 manager_->EndSession(client);
128 }
129
130 private:
danakj75afea02016-04-25 20:36:04 -0700131 std::unique_ptr<MidiManager> manager_;
132 std::unique_ptr<base::MessageLoop> message_loop_;
toyoshimf2197522015-02-19 20:53:55 -0800133
134 DISALLOW_COPY_AND_ASSIGN(MidiManagerMacTest);
135};
136
137
138TEST_F(MidiManagerMacTest, MidiNotification) {
danakj75afea02016-04-25 20:36:04 -0700139 std::unique_ptr<FakeMidiManagerClient> client(new FakeMidiManagerClient);
toyoshimf2197522015-02-19 20:53:55 -0800140 StartSession(client.get());
141
toyoshimf1b88962015-07-09 14:14:51 -0700142 Result result = client->WaitForResult();
143 EXPECT_EQ(Result::OK, result);
toyoshimf2197522015-02-19 20:53:55 -0800144
145 // Create MIDIClient, and MIDIEndpoint as a MIDIDestination. This should
146 // notify MIDIManagerMac as a MIDIObjectAddRemoveNotification.
147 MIDIClientRef midi_client = 0;
148 OSStatus status = MIDIClientCreate(
149 CFSTR("MidiManagerMacTest"), nullptr, nullptr, &midi_client);
150 EXPECT_EQ(noErr, status);
151
152 MIDIEndpointRef ep = 0;
153 status = MIDIDestinationCreate(
154 midi_client, CFSTR("DestinationTest"), Noop, nullptr, &ep);
155 EXPECT_EQ(noErr, status);
toyoshimc9746982015-04-03 05:32:55 -0700156 SInt32 id;
157 status = MIDIObjectGetIntegerProperty(ep, kMIDIPropertyUniqueID, &id);
158 EXPECT_EQ(noErr, status);
159 EXPECT_NE(0, id);
toyoshimf2197522015-02-19 20:53:55 -0800160
161 // Wait until the created device is notified to MidiManagerMac.
162 MidiPortInfo info = client->WaitForPort();
163 EXPECT_EQ("DestinationTest", info.name);
164
165 EndSession(client.get());
166 if (ep)
167 MIDIEndpointDispose(ep);
168 if (midi_client)
169 MIDIClientDispose(midi_client);
170}
171
172} // namespace
173
toyoshime147c5e2015-05-07 21:58:31 -0700174} // namespace midi