blob: 11ddba4c60235b3e7811b20b28836670ac6092fe [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 {
17
18namespace {
19
20void Noop(const MIDIPacketList*, void*, void*) {}
21
22class FakeMidiManagerClient : public MidiManagerClient {
23 public:
24 FakeMidiManagerClient()
25 : result_(MIDI_NOT_SUPPORTED),
26 wait_for_result_(true),
toyoshim4f728352015-03-03 02:30:54 -080027 wait_for_port_(true),
28 unexpected_callback_(false) {}
toyoshimf2197522015-02-19 20:53:55 -080029
30 // MidiManagerClient implementation.
31 void AddInputPort(const MidiPortInfo& info) override {}
32 void AddOutputPort(const MidiPortInfo& info) override {
toyoshim4f728352015-03-03 02:30:54 -080033 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.
37 if (wait_for_result_)
38 return;
39
40 // Check if this is the first call after CompleteStartSession(), and
41 // the case should not happen twice.
42 if (!wait_for_port_)
43 unexpected_callback_ = true;
44
toyoshimf2197522015-02-19 20:53:55 -080045 info_ = info;
46 wait_for_port_ = false;
47 }
48 void SetInputPortState(uint32 port_index, MidiPortState state) override {}
49 void SetOutputPortState(uint32 port_index, MidiPortState state) override {}
50
51 void CompleteStartSession(MidiResult result) override {
toyoshim4f728352015-03-03 02:30:54 -080052 base::AutoLock lock(lock_);
53 if (!wait_for_result_)
54 unexpected_callback_ = true;
55
toyoshimf2197522015-02-19 20:53:55 -080056 result_ = result;
57 wait_for_result_ = false;
58 }
59
60 void ReceiveMidiData(uint32 port_index, const uint8* data, size_t size,
61 double timestamp) override {}
62 void AccumulateMidiBytesSent(size_t size) override {}
63
toyoshim4f728352015-03-03 02:30:54 -080064 bool GetWaitForResult() {
65 base::AutoLock lock(lock_);
66 return wait_for_result_;
67 }
68
69 bool GetWaitForPort() {
70 base::AutoLock lock(lock_);
71 return wait_for_port_;
72 }
73
toyoshimf2197522015-02-19 20:53:55 -080074 MidiResult WaitForResult() {
toyoshim4f728352015-03-03 02:30:54 -080075 while (GetWaitForResult()) {
toyoshimf2197522015-02-19 20:53:55 -080076 base::RunLoop run_loop;
77 run_loop.RunUntilIdle();
78 }
toyoshim4f728352015-03-03 02:30:54 -080079 EXPECT_FALSE(unexpected_callback_);
toyoshimf2197522015-02-19 20:53:55 -080080 return result_;
81 }
82 MidiPortInfo WaitForPort() {
toyoshim4f728352015-03-03 02:30:54 -080083 while (GetWaitForPort()) {
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 info_;
89 }
90
91 private:
toyoshim4f728352015-03-03 02:30:54 -080092 base::Lock lock_;
toyoshimf2197522015-02-19 20:53:55 -080093 MidiResult result_;
94 bool wait_for_result_;
95 MidiPortInfo info_;
96 bool wait_for_port_;
toyoshim4f728352015-03-03 02:30:54 -080097 bool unexpected_callback_;
toyoshimf2197522015-02-19 20:53:55 -080098
99 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient);
100};
101
102class MidiManagerMacTest : public ::testing::Test {
103 public:
104 MidiManagerMacTest()
105 : manager_(new MidiManagerMac),
106 message_loop_(new base::MessageLoop) {}
107
108 protected:
109 void StartSession(MidiManagerClient* client) {
110 manager_->StartSession(client);
111 }
112 void EndSession(MidiManagerClient* client) {
113 manager_->EndSession(client);
114 }
115
116 private:
117 scoped_ptr<MidiManager> manager_;
118 scoped_ptr<base::MessageLoop> message_loop_;
119
120 DISALLOW_COPY_AND_ASSIGN(MidiManagerMacTest);
121};
122
123
124TEST_F(MidiManagerMacTest, MidiNotification) {
125 scoped_ptr<FakeMidiManagerClient> client(new FakeMidiManagerClient);
126 StartSession(client.get());
127
128 MidiResult result = client->WaitForResult();
129 EXPECT_EQ(MIDI_OK, result);
130
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);
137
138 MIDIEndpointRef ep = 0;
139 status = MIDIDestinationCreate(
140 midi_client, CFSTR("DestinationTest"), Noop, nullptr, &ep);
141 EXPECT_EQ(noErr, status);
toyoshimc9746982015-04-03 05:32:55 -0700142 SInt32 id;
143 status = MIDIObjectGetIntegerProperty(ep, kMIDIPropertyUniqueID, &id);
144 EXPECT_EQ(noErr, status);
145 EXPECT_NE(0, id);
toyoshimf2197522015-02-19 20:53:55 -0800146
147 // Wait until the created device is notified to MidiManagerMac.
148 MidiPortInfo info = client->WaitForPort();
149 EXPECT_EQ("DestinationTest", info.name);
150
151 EndSession(client.get());
152 if (ep)
153 MIDIEndpointDispose(ep);
154 if (midi_client)
155 MIDIClientDispose(midi_client);
156}
157
158} // namespace
159
160} // namespace media