blob: 2175f2a2cc5fd2988464f0e52b55168a12f6c806 [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
avi793390d2015-12-22 22:22:36 -080013#include "base/macros.h"
toyoshimf2197522015-02-19 20:53:55 -080014#include "base/run_loop.h"
toyoshim4f728352015-03-03 02:30:54 -080015#include "base/synchronization/lock.h"
Carlos Caballero10135c22019-12-02 08:40:23 +000016#include "base/test/task_environment.h"
Takashi Toyoshima143f0fd2017-08-01 16:15:33 +000017#include "media/midi/midi_service.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
toyoshimec2570a2016-10-21 02:15:27 -070024using mojom::PortState;
toyoshim2f3a48f2016-10-17 01:54:13 -070025using mojom::Result;
26
toyoshimf2197522015-02-19 20:53:55 -080027void Noop(const MIDIPacketList*, void*, void*) {}
28
29class FakeMidiManagerClient : public MidiManagerClient {
30 public:
31 FakeMidiManagerClient()
toyoshimf1b88962015-07-09 14:14:51 -070032 : result_(Result::NOT_SUPPORTED),
toyoshimf2197522015-02-19 20:53:55 -080033 wait_for_result_(true),
toyoshim4f728352015-03-03 02:30:54 -080034 wait_for_port_(true),
35 unexpected_callback_(false) {}
toyoshimf2197522015-02-19 20:53:55 -080036
37 // MidiManagerClient implementation.
Adithya Srinivasan33252732018-10-17 15:59:40 +000038 void AddInputPort(const mojom::PortInfo& info) override {}
39 void AddOutputPort(const mojom::PortInfo& info) override {
toyoshim4f728352015-03-03 02:30:54 -080040 base::AutoLock lock(lock_);
41 // AddOutputPort may be called before CompleteStartSession() is invoked
42 // if one or more MIDI devices including virtual ports are connected.
43 // Just ignore in such cases.
44 if (wait_for_result_)
45 return;
46
47 // Check if this is the first call after CompleteStartSession(), and
48 // the case should not happen twice.
49 if (!wait_for_port_)
50 unexpected_callback_ = true;
51
toyoshimf2197522015-02-19 20:53:55 -080052 info_ = info;
53 wait_for_port_ = false;
54 }
toyoshimec2570a2016-10-21 02:15:27 -070055 void SetInputPortState(uint32_t port_index, PortState state) override {}
56 void SetOutputPortState(uint32_t port_index, PortState state) override {}
toyoshimf2197522015-02-19 20:53:55 -080057
toyoshimf1b88962015-07-09 14:14:51 -070058 void CompleteStartSession(Result result) override {
toyoshim4f728352015-03-03 02:30:54 -080059 base::AutoLock lock(lock_);
60 if (!wait_for_result_)
61 unexpected_callback_ = true;
62
toyoshimf2197522015-02-19 20:53:55 -080063 result_ = result;
64 wait_for_result_ = false;
65 }
66
Avi Drissman3528fd02015-12-18 20:11:31 -050067 void ReceiveMidiData(uint32_t port_index,
68 const uint8_t* data,
69 size_t size,
tzik925e2c62018-02-02 07:39:45 +000070 base::TimeTicks timestamp) override {}
toyoshimf2197522015-02-19 20:53:55 -080071 void AccumulateMidiBytesSent(size_t size) override {}
toyoshim7a809662015-10-06 00:54:21 -070072 void Detach() override {}
toyoshimf2197522015-02-19 20:53:55 -080073
toyoshim4f728352015-03-03 02:30:54 -080074 bool GetWaitForResult() {
75 base::AutoLock lock(lock_);
76 return wait_for_result_;
77 }
78
79 bool GetWaitForPort() {
80 base::AutoLock lock(lock_);
81 return wait_for_port_;
82 }
83
toyoshimf1b88962015-07-09 14:14:51 -070084 Result WaitForResult() {
toyoshim4f728352015-03-03 02:30:54 -080085 while (GetWaitForResult()) {
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 result_;
91 }
Adithya Srinivasan33252732018-10-17 15:59:40 +000092 mojom::PortInfo WaitForPort() {
toyoshim4f728352015-03-03 02:30:54 -080093 while (GetWaitForPort()) {
toyoshimf2197522015-02-19 20:53:55 -080094 base::RunLoop run_loop;
95 run_loop.RunUntilIdle();
96 }
toyoshim4f728352015-03-03 02:30:54 -080097 EXPECT_FALSE(unexpected_callback_);
toyoshimf2197522015-02-19 20:53:55 -080098 return info_;
99 }
100
101 private:
toyoshim4f728352015-03-03 02:30:54 -0800102 base::Lock lock_;
toyoshimf1b88962015-07-09 14:14:51 -0700103 Result result_;
toyoshimf2197522015-02-19 20:53:55 -0800104 bool wait_for_result_;
Adithya Srinivasan33252732018-10-17 15:59:40 +0000105 mojom::PortInfo info_;
toyoshimf2197522015-02-19 20:53:55 -0800106 bool wait_for_port_;
toyoshim4f728352015-03-03 02:30:54 -0800107 bool unexpected_callback_;
toyoshimf2197522015-02-19 20:53:55 -0800108
109 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient);
110};
111
112class MidiManagerMacTest : public ::testing::Test {
113 public:
Carlos Caballero10135c22019-12-02 08:40:23 +0000114 MidiManagerMacTest() : service_(std::make_unique<MidiService>()) {}
Peter Boström53634032021-09-22 20:24:34 +0000115
116 MidiManagerMacTest(const MidiManagerMacTest&) = delete;
117 MidiManagerMacTest& operator=(const MidiManagerMacTest&) = delete;
118
toyoshim7a809662015-10-06 00:54:21 -0700119 ~MidiManagerMacTest() override {
Takashi Toyoshima143f0fd2017-08-01 16:15:33 +0000120 service_->Shutdown();
toyoshim7a809662015-10-06 00:54:21 -0700121 base::RunLoop run_loop;
122 run_loop.RunUntilIdle();
123 }
toyoshimf2197522015-02-19 20:53:55 -0800124
125 protected:
126 void StartSession(MidiManagerClient* client) {
Takashi Toyoshima143f0fd2017-08-01 16:15:33 +0000127 service_->StartSession(client);
toyoshimf2197522015-02-19 20:53:55 -0800128 }
Takashi Toyoshima143f0fd2017-08-01 16:15:33 +0000129 void EndSession(MidiManagerClient* client) { service_->EndSession(client); }
toyoshimf2197522015-02-19 20:53:55 -0800130
131 private:
Takashi Toyoshima143f0fd2017-08-01 16:15:33 +0000132 std::unique_ptr<MidiService> service_;
Carlos Caballero10135c22019-12-02 08:40:23 +0000133 base::test::SingleThreadTaskEnvironment task_environment_;
toyoshimf2197522015-02-19 20:53:55 -0800134};
135
136
137TEST_F(MidiManagerMacTest, MidiNotification) {
danakj75afea02016-04-25 20:36:04 -0700138 std::unique_ptr<FakeMidiManagerClient> client(new FakeMidiManagerClient);
toyoshimf2197522015-02-19 20:53:55 -0800139 StartSession(client.get());
140
toyoshimf1b88962015-07-09 14:14:51 -0700141 Result result = client->WaitForResult();
142 EXPECT_EQ(Result::OK, result);
toyoshimf2197522015-02-19 20:53:55 -0800143
144 // Create MIDIClient, and MIDIEndpoint as a MIDIDestination. This should
145 // notify MIDIManagerMac as a MIDIObjectAddRemoveNotification.
146 MIDIClientRef midi_client = 0;
147 OSStatus status = MIDIClientCreate(
148 CFSTR("MidiManagerMacTest"), nullptr, nullptr, &midi_client);
149 EXPECT_EQ(noErr, status);
150
151 MIDIEndpointRef ep = 0;
152 status = MIDIDestinationCreate(
153 midi_client, CFSTR("DestinationTest"), Noop, nullptr, &ep);
154 EXPECT_EQ(noErr, status);
toyoshimc9746982015-04-03 05:32:55 -0700155 SInt32 id;
156 status = MIDIObjectGetIntegerProperty(ep, kMIDIPropertyUniqueID, &id);
157 EXPECT_EQ(noErr, status);
158 EXPECT_NE(0, id);
toyoshimf2197522015-02-19 20:53:55 -0800159
160 // Wait until the created device is notified to MidiManagerMac.
Adithya Srinivasan33252732018-10-17 15:59:40 +0000161 mojom::PortInfo info = client->WaitForPort();
toyoshimf2197522015-02-19 20:53:55 -0800162 EXPECT_EQ("DestinationTest", info.name);
163
164 EndSession(client.get());
165 if (ep)
166 MIDIEndpointDispose(ep);
167 if (midi_client)
168 MIDIClientDispose(midi_client);
169}
170
171} // namespace
172
toyoshime147c5e2015-05-07 21:58:31 -0700173} // namespace midi