blob: 7aadceb70ad3a3b63ee62f572e8ab1df3a46a0d4 [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
Peter Boström5e5c4fa2021-10-15 21:43:24 +000037 FakeMidiManagerClient(const FakeMidiManagerClient&) = delete;
38 FakeMidiManagerClient& operator=(const FakeMidiManagerClient&) = delete;
39
toyoshimf2197522015-02-19 20:53:55 -080040 // MidiManagerClient implementation.
Adithya Srinivasan33252732018-10-17 15:59:40 +000041 void AddInputPort(const mojom::PortInfo& info) override {}
42 void AddOutputPort(const mojom::PortInfo& info) override {
toyoshim4f728352015-03-03 02:30:54 -080043 base::AutoLock lock(lock_);
44 // AddOutputPort may be called before CompleteStartSession() is invoked
45 // if one or more MIDI devices including virtual ports are connected.
46 // Just ignore in such cases.
47 if (wait_for_result_)
48 return;
49
50 // Check if this is the first call after CompleteStartSession(), and
51 // the case should not happen twice.
52 if (!wait_for_port_)
53 unexpected_callback_ = true;
54
toyoshimf2197522015-02-19 20:53:55 -080055 info_ = info;
56 wait_for_port_ = false;
57 }
toyoshimec2570a2016-10-21 02:15:27 -070058 void SetInputPortState(uint32_t port_index, PortState state) override {}
59 void SetOutputPortState(uint32_t port_index, PortState state) override {}
toyoshimf2197522015-02-19 20:53:55 -080060
toyoshimf1b88962015-07-09 14:14:51 -070061 void CompleteStartSession(Result result) override {
toyoshim4f728352015-03-03 02:30:54 -080062 base::AutoLock lock(lock_);
63 if (!wait_for_result_)
64 unexpected_callback_ = true;
65
toyoshimf2197522015-02-19 20:53:55 -080066 result_ = result;
67 wait_for_result_ = false;
68 }
69
Avi Drissman3528fd02015-12-18 20:11:31 -050070 void ReceiveMidiData(uint32_t port_index,
71 const uint8_t* data,
72 size_t size,
tzik925e2c62018-02-02 07:39:45 +000073 base::TimeTicks timestamp) override {}
toyoshimf2197522015-02-19 20:53:55 -080074 void AccumulateMidiBytesSent(size_t size) override {}
toyoshim7a809662015-10-06 00:54:21 -070075 void Detach() override {}
toyoshimf2197522015-02-19 20:53:55 -080076
toyoshim4f728352015-03-03 02:30:54 -080077 bool GetWaitForResult() {
78 base::AutoLock lock(lock_);
79 return wait_for_result_;
80 }
81
82 bool GetWaitForPort() {
83 base::AutoLock lock(lock_);
84 return wait_for_port_;
85 }
86
toyoshimf1b88962015-07-09 14:14:51 -070087 Result WaitForResult() {
toyoshim4f728352015-03-03 02:30:54 -080088 while (GetWaitForResult()) {
toyoshimf2197522015-02-19 20:53:55 -080089 base::RunLoop run_loop;
90 run_loop.RunUntilIdle();
91 }
toyoshim4f728352015-03-03 02:30:54 -080092 EXPECT_FALSE(unexpected_callback_);
toyoshimf2197522015-02-19 20:53:55 -080093 return result_;
94 }
Adithya Srinivasan33252732018-10-17 15:59:40 +000095 mojom::PortInfo WaitForPort() {
toyoshim4f728352015-03-03 02:30:54 -080096 while (GetWaitForPort()) {
toyoshimf2197522015-02-19 20:53:55 -080097 base::RunLoop run_loop;
98 run_loop.RunUntilIdle();
99 }
toyoshim4f728352015-03-03 02:30:54 -0800100 EXPECT_FALSE(unexpected_callback_);
toyoshimf2197522015-02-19 20:53:55 -0800101 return info_;
102 }
103
104 private:
toyoshim4f728352015-03-03 02:30:54 -0800105 base::Lock lock_;
toyoshimf1b88962015-07-09 14:14:51 -0700106 Result result_;
toyoshimf2197522015-02-19 20:53:55 -0800107 bool wait_for_result_;
Adithya Srinivasan33252732018-10-17 15:59:40 +0000108 mojom::PortInfo info_;
toyoshimf2197522015-02-19 20:53:55 -0800109 bool wait_for_port_;
toyoshim4f728352015-03-03 02:30:54 -0800110 bool unexpected_callback_;
toyoshimf2197522015-02-19 20:53:55 -0800111};
112
113class MidiManagerMacTest : public ::testing::Test {
114 public:
Carlos Caballero10135c22019-12-02 08:40:23 +0000115 MidiManagerMacTest() : service_(std::make_unique<MidiService>()) {}
Peter Boström53634032021-09-22 20:24:34 +0000116
117 MidiManagerMacTest(const MidiManagerMacTest&) = delete;
118 MidiManagerMacTest& operator=(const MidiManagerMacTest&) = delete;
119
toyoshim7a809662015-10-06 00:54:21 -0700120 ~MidiManagerMacTest() override {
Takashi Toyoshima143f0fd2017-08-01 16:15:33 +0000121 service_->Shutdown();
toyoshim7a809662015-10-06 00:54:21 -0700122 base::RunLoop run_loop;
123 run_loop.RunUntilIdle();
124 }
toyoshimf2197522015-02-19 20:53:55 -0800125
126 protected:
127 void StartSession(MidiManagerClient* client) {
Takashi Toyoshima143f0fd2017-08-01 16:15:33 +0000128 service_->StartSession(client);
toyoshimf2197522015-02-19 20:53:55 -0800129 }
Takashi Toyoshima143f0fd2017-08-01 16:15:33 +0000130 void EndSession(MidiManagerClient* client) { service_->EndSession(client); }
toyoshimf2197522015-02-19 20:53:55 -0800131
132 private:
Takashi Toyoshima143f0fd2017-08-01 16:15:33 +0000133 std::unique_ptr<MidiService> service_;
Carlos Caballero10135c22019-12-02 08:40:23 +0000134 base::test::SingleThreadTaskEnvironment task_environment_;
toyoshimf2197522015-02-19 20:53:55 -0800135};
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