Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / media / midi / midi_manager_mac_unittest.cc
blob11ddba4c60235b3e7811b20b28836670ac6092fe
1 // 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.
5 #include "media/midi/midi_manager_mac.h"
7 #include <CoreMIDI/MIDIServices.h>
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"
13 #include "base/synchronization/lock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace media {
18 namespace {
20 void Noop(const MIDIPacketList*, void*, void*) {}
22 class FakeMidiManagerClient : public MidiManagerClient {
23 public:
24 FakeMidiManagerClient()
25 : result_(MIDI_NOT_SUPPORTED),
26 wait_for_result_(true),
27 wait_for_port_(true),
28 unexpected_callback_(false) {}
30 // MidiManagerClient implementation.
31 void AddInputPort(const MidiPortInfo& info) override {}
32 void AddOutputPort(const MidiPortInfo& info) override {
33 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;
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;
45 info_ = info;
46 wait_for_port_ = false;
48 void SetInputPortState(uint32 port_index, MidiPortState state) override {}
49 void SetOutputPortState(uint32 port_index, MidiPortState state) override {}
51 void CompleteStartSession(MidiResult result) override {
52 base::AutoLock lock(lock_);
53 if (!wait_for_result_)
54 unexpected_callback_ = true;
56 result_ = result;
57 wait_for_result_ = false;
60 void ReceiveMidiData(uint32 port_index, const uint8* data, size_t size,
61 double timestamp) override {}
62 void AccumulateMidiBytesSent(size_t size) override {}
64 bool GetWaitForResult() {
65 base::AutoLock lock(lock_);
66 return wait_for_result_;
69 bool GetWaitForPort() {
70 base::AutoLock lock(lock_);
71 return wait_for_port_;
74 MidiResult WaitForResult() {
75 while (GetWaitForResult()) {
76 base::RunLoop run_loop;
77 run_loop.RunUntilIdle();
79 EXPECT_FALSE(unexpected_callback_);
80 return result_;
82 MidiPortInfo WaitForPort() {
83 while (GetWaitForPort()) {
84 base::RunLoop run_loop;
85 run_loop.RunUntilIdle();
87 EXPECT_FALSE(unexpected_callback_);
88 return info_;
91 private:
92 base::Lock lock_;
93 MidiResult result_;
94 bool wait_for_result_;
95 MidiPortInfo info_;
96 bool wait_for_port_;
97 bool unexpected_callback_;
99 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient);
102 class MidiManagerMacTest : public ::testing::Test {
103 public:
104 MidiManagerMacTest()
105 : manager_(new MidiManagerMac),
106 message_loop_(new base::MessageLoop) {}
108 protected:
109 void StartSession(MidiManagerClient* client) {
110 manager_->StartSession(client);
112 void EndSession(MidiManagerClient* client) {
113 manager_->EndSession(client);
116 private:
117 scoped_ptr<MidiManager> manager_;
118 scoped_ptr<base::MessageLoop> message_loop_;
120 DISALLOW_COPY_AND_ASSIGN(MidiManagerMacTest);
124 TEST_F(MidiManagerMacTest, MidiNotification) {
125 scoped_ptr<FakeMidiManagerClient> client(new FakeMidiManagerClient);
126 StartSession(client.get());
128 MidiResult result = client->WaitForResult();
129 EXPECT_EQ(MIDI_OK, result);
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);
138 MIDIEndpointRef ep = 0;
139 status = MIDIDestinationCreate(
140 midi_client, CFSTR("DestinationTest"), Noop, nullptr, &ep);
141 EXPECT_EQ(noErr, status);
142 SInt32 id;
143 status = MIDIObjectGetIntegerProperty(ep, kMIDIPropertyUniqueID, &id);
144 EXPECT_EQ(noErr, status);
145 EXPECT_NE(0, id);
147 // Wait until the created device is notified to MidiManagerMac.
148 MidiPortInfo info = client->WaitForPort();
149 EXPECT_EQ("DestinationTest", info.name);
151 EndSession(client.get());
152 if (ep)
153 MIDIEndpointDispose(ep);
154 if (midi_client)
155 MIDIClientDispose(midi_client);
158 } // namespace
160 } // namespace media