blob: acee1af90cbc83d6e8d87ab51a2f9d9b7d6adf96 [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/run_loop.h"
toyoshim4f728352015-03-03 02:30:54 -080014#include "base/synchronization/lock.h"
Carlos Caballero10135c22019-12-02 08:40:23 +000015#include "base/test/task_environment.h"
Takashi Toyoshima143f0fd2017-08-01 16:15:33 +000016#include "media/midi/midi_service.h"
toyoshimf2197522015-02-19 20:53:55 -080017#include "testing/gtest/include/gtest/gtest.h"
18
toyoshime147c5e2015-05-07 21:58:31 -070019namespace midi {
toyoshimf2197522015-02-19 20:53:55 -080020
21namespace {
22
toyoshimec2570a2016-10-21 02:15:27 -070023using mojom::PortState;
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
Peter Boström5e5c4fa2021-10-15 21:43:24 +000036 FakeMidiManagerClient(const FakeMidiManagerClient&) = delete;
37 FakeMidiManagerClient& operator=(const FakeMidiManagerClient&) = delete;
38
toyoshimf2197522015-02-19 20:53:55 -080039 // MidiManagerClient implementation.
Adithya Srinivasan33252732018-10-17 15:59:40 +000040 void AddInputPort(const mojom::PortInfo& info) override {}
41 void AddOutputPort(const mojom::PortInfo& info) override {
toyoshim4f728352015-03-03 02:30:54 -080042 base::AutoLock lock(lock_);
43 // AddOutputPort may be called before CompleteStartSession() is invoked
44 // if one or more MIDI devices including virtual ports are connected.
45 // Just ignore in such cases.
46 if (wait_for_result_)
47 return;
48
49 // Check if this is the first call after CompleteStartSession(), and
50 // the case should not happen twice.
51 if (!wait_for_port_)
52 unexpected_callback_ = true;
53
toyoshimf2197522015-02-19 20:53:55 -080054 info_ = info;
55 wait_for_port_ = false;
56 }
toyoshimec2570a2016-10-21 02:15:27 -070057 void SetInputPortState(uint32_t port_index, PortState state) override {}
58 void SetOutputPortState(uint32_t port_index, PortState state) override {}
toyoshimf2197522015-02-19 20:53:55 -080059
toyoshimf1b88962015-07-09 14:14:51 -070060 void CompleteStartSession(Result result) override {
toyoshim4f728352015-03-03 02:30:54 -080061 base::AutoLock lock(lock_);
62 if (!wait_for_result_)
63 unexpected_callback_ = true;
64
toyoshimf2197522015-02-19 20:53:55 -080065 result_ = result;
66 wait_for_result_ = false;
67 }
68
Avi Drissman3528fd02015-12-18 20:11:31 -050069 void ReceiveMidiData(uint32_t port_index,
70 const uint8_t* data,
71 size_t size,
tzik925e2c62018-02-02 07:39:45 +000072 base::TimeTicks timestamp) override {}
toyoshimf2197522015-02-19 20:53:55 -080073 void AccumulateMidiBytesSent(size_t size) override {}
toyoshim7a809662015-10-06 00:54:21 -070074 void Detach() override {}
toyoshimf2197522015-02-19 20:53:55 -080075
toyoshim4f728352015-03-03 02:30:54 -080076 bool GetWaitForResult() {
77 base::AutoLock lock(lock_);
78 return wait_for_result_;
79 }
80
81 bool GetWaitForPort() {
82 base::AutoLock lock(lock_);
83 return wait_for_port_;
84 }
85
toyoshimf1b88962015-07-09 14:14:51 -070086 Result WaitForResult() {
toyoshim4f728352015-03-03 02:30:54 -080087 while (GetWaitForResult()) {
toyoshimf2197522015-02-19 20:53:55 -080088 base::RunLoop run_loop;
89 run_loop.RunUntilIdle();
90 }
toyoshim4f728352015-03-03 02:30:54 -080091 EXPECT_FALSE(unexpected_callback_);
toyoshimf2197522015-02-19 20:53:55 -080092 return result_;
93 }
Adithya Srinivasan33252732018-10-17 15:59:40 +000094 mojom::PortInfo WaitForPort() {
toyoshim4f728352015-03-03 02:30:54 -080095 while (GetWaitForPort()) {
toyoshimf2197522015-02-19 20:53:55 -080096 base::RunLoop run_loop;
97 run_loop.RunUntilIdle();
98 }
toyoshim4f728352015-03-03 02:30:54 -080099 EXPECT_FALSE(unexpected_callback_);
toyoshimf2197522015-02-19 20:53:55 -0800100 return info_;
101 }
102
103 private:
toyoshim4f728352015-03-03 02:30:54 -0800104 base::Lock lock_;
toyoshimf1b88962015-07-09 14:14:51 -0700105 Result result_;
toyoshimf2197522015-02-19 20:53:55 -0800106 bool wait_for_result_;
Adithya Srinivasan33252732018-10-17 15:59:40 +0000107 mojom::PortInfo info_;
toyoshimf2197522015-02-19 20:53:55 -0800108 bool wait_for_port_;
toyoshim4f728352015-03-03 02:30:54 -0800109 bool unexpected_callback_;
toyoshimf2197522015-02-19 20:53:55 -0800110};
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