blob: c896afc2149b9af499edbe6fae0fa9552e03ff61 [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"
Takashi Toyoshima143f0fd2017-08-01 16:15:33 +000018#include "media/midi/midi_service.h"
toyoshimf2197522015-02-19 20:53:55 -080019#include "testing/gtest/include/gtest/gtest.h"
20
toyoshime147c5e2015-05-07 21:58:31 -070021namespace midi {
toyoshimf2197522015-02-19 20:53:55 -080022
23namespace {
24
toyoshimec2570a2016-10-21 02:15:27 -070025using mojom::PortState;
toyoshim2f3a48f2016-10-17 01:54:13 -070026using mojom::Result;
27
toyoshimf2197522015-02-19 20:53:55 -080028void Noop(const MIDIPacketList*, void*, void*) {}
29
30class FakeMidiManagerClient : public MidiManagerClient {
31 public:
32 FakeMidiManagerClient()
toyoshimf1b88962015-07-09 14:14:51 -070033 : result_(Result::NOT_SUPPORTED),
toyoshimf2197522015-02-19 20:53:55 -080034 wait_for_result_(true),
toyoshim4f728352015-03-03 02:30:54 -080035 wait_for_port_(true),
36 unexpected_callback_(false) {}
toyoshimf2197522015-02-19 20:53:55 -080037
38 // MidiManagerClient implementation.
Adithya Srinivasan33252732018-10-17 15:59:40 +000039 void AddInputPort(const mojom::PortInfo& info) override {}
40 void AddOutputPort(const mojom::PortInfo& info) override {
toyoshim4f728352015-03-03 02:30:54 -080041 base::AutoLock lock(lock_);
42 // AddOutputPort may be called before CompleteStartSession() is invoked
43 // if one or more MIDI devices including virtual ports are connected.
44 // Just ignore in such cases.
45 if (wait_for_result_)
46 return;
47
48 // Check if this is the first call after CompleteStartSession(), and
49 // the case should not happen twice.
50 if (!wait_for_port_)
51 unexpected_callback_ = true;
52
toyoshimf2197522015-02-19 20:53:55 -080053 info_ = info;
54 wait_for_port_ = false;
55 }
toyoshimec2570a2016-10-21 02:15:27 -070056 void SetInputPortState(uint32_t port_index, PortState state) override {}
57 void SetOutputPortState(uint32_t port_index, PortState state) override {}
toyoshimf2197522015-02-19 20:53:55 -080058
toyoshimf1b88962015-07-09 14:14:51 -070059 void CompleteStartSession(Result result) override {
toyoshim4f728352015-03-03 02:30:54 -080060 base::AutoLock lock(lock_);
61 if (!wait_for_result_)
62 unexpected_callback_ = true;
63
toyoshimf2197522015-02-19 20:53:55 -080064 result_ = result;
65 wait_for_result_ = false;
66 }
67
Avi Drissman3528fd02015-12-18 20:11:31 -050068 void ReceiveMidiData(uint32_t port_index,
69 const uint8_t* data,
70 size_t size,
tzik925e2c62018-02-02 07:39:45 +000071 base::TimeTicks timestamp) override {}
toyoshimf2197522015-02-19 20:53:55 -080072 void AccumulateMidiBytesSent(size_t size) override {}
toyoshim7a809662015-10-06 00:54:21 -070073 void Detach() override {}
toyoshimf2197522015-02-19 20:53:55 -080074
toyoshim4f728352015-03-03 02:30:54 -080075 bool GetWaitForResult() {
76 base::AutoLock lock(lock_);
77 return wait_for_result_;
78 }
79
80 bool GetWaitForPort() {
81 base::AutoLock lock(lock_);
82 return wait_for_port_;
83 }
84
toyoshimf1b88962015-07-09 14:14:51 -070085 Result WaitForResult() {
toyoshim4f728352015-03-03 02:30:54 -080086 while (GetWaitForResult()) {
toyoshimf2197522015-02-19 20:53:55 -080087 base::RunLoop run_loop;
88 run_loop.RunUntilIdle();
89 }
toyoshim4f728352015-03-03 02:30:54 -080090 EXPECT_FALSE(unexpected_callback_);
toyoshimf2197522015-02-19 20:53:55 -080091 return result_;
92 }
Adithya Srinivasan33252732018-10-17 15:59:40 +000093 mojom::PortInfo WaitForPort() {
toyoshim4f728352015-03-03 02:30:54 -080094 while (GetWaitForPort()) {
toyoshimf2197522015-02-19 20:53:55 -080095 base::RunLoop run_loop;
96 run_loop.RunUntilIdle();
97 }
toyoshim4f728352015-03-03 02:30:54 -080098 EXPECT_FALSE(unexpected_callback_);
toyoshimf2197522015-02-19 20:53:55 -080099 return info_;
100 }
101
102 private:
toyoshim4f728352015-03-03 02:30:54 -0800103 base::Lock lock_;
toyoshimf1b88962015-07-09 14:14:51 -0700104 Result result_;
toyoshimf2197522015-02-19 20:53:55 -0800105 bool wait_for_result_;
Adithya Srinivasan33252732018-10-17 15:59:40 +0000106 mojom::PortInfo info_;
toyoshimf2197522015-02-19 20:53:55 -0800107 bool wait_for_port_;
toyoshim4f728352015-03-03 02:30:54 -0800108 bool unexpected_callback_;
toyoshimf2197522015-02-19 20:53:55 -0800109
110 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient);
111};
112
113class MidiManagerMacTest : public ::testing::Test {
114 public:
115 MidiManagerMacTest()
Gyuyoung Kim34e191a2018-01-10 09:48:42 +0000116 : service_(std::make_unique<MidiService>()),
117 message_loop_(std::make_unique<base::MessageLoop>()) {}
toyoshim7a809662015-10-06 00:54:21 -0700118 ~MidiManagerMacTest() override {
Takashi Toyoshima143f0fd2017-08-01 16:15:33 +0000119 service_->Shutdown();
toyoshim7a809662015-10-06 00:54:21 -0700120 base::RunLoop run_loop;
121 run_loop.RunUntilIdle();
122 }
toyoshimf2197522015-02-19 20:53:55 -0800123
124 protected:
125 void StartSession(MidiManagerClient* client) {
Takashi Toyoshima143f0fd2017-08-01 16:15:33 +0000126 service_->StartSession(client);
toyoshimf2197522015-02-19 20:53:55 -0800127 }
Takashi Toyoshima143f0fd2017-08-01 16:15:33 +0000128 void EndSession(MidiManagerClient* client) { service_->EndSession(client); }
toyoshimf2197522015-02-19 20:53:55 -0800129
130 private:
Takashi Toyoshima143f0fd2017-08-01 16:15:33 +0000131 std::unique_ptr<MidiService> service_;
danakj75afea02016-04-25 20:36:04 -0700132 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.
Adithya Srinivasan33252732018-10-17 15:59:40 +0000162 mojom::PortInfo info = client->WaitForPort();
toyoshimf2197522015-02-19 20:53:55 -0800163 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