Remove INJECT_EVENTS permissions from test APKs.
[chromium-blink-merge.git] / media / midi / midi_manager_mac_unittest.cc
blob8bd75f2d650aa6bca46b9fb0b771c1f98ea1896c
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 {
17 namespace midi {
19 namespace {
21 void Noop(const MIDIPacketList*, void*, void*) {}
23 class FakeMidiManagerClient : public MidiManagerClient {
24 public:
25 FakeMidiManagerClient()
26 : result_(MIDI_NOT_SUPPORTED),
27 wait_for_result_(true),
28 wait_for_port_(true),
29 unexpected_callback_(false) {}
31 // MidiManagerClient implementation.
32 void AddInputPort(const MidiPortInfo& info) override {}
33 void AddOutputPort(const MidiPortInfo& info) override {
34 base::AutoLock lock(lock_);
35 // AddOutputPort may be called before CompleteStartSession() is invoked
36 // if one or more MIDI devices including virtual ports are connected.
37 // Just ignore in such cases.
38 if (wait_for_result_)
39 return;
41 // Check if this is the first call after CompleteStartSession(), and
42 // the case should not happen twice.
43 if (!wait_for_port_)
44 unexpected_callback_ = true;
46 info_ = info;
47 wait_for_port_ = false;
49 void SetInputPortState(uint32 port_index, MidiPortState state) override {}
50 void SetOutputPortState(uint32 port_index, MidiPortState state) override {}
52 void CompleteStartSession(MidiResult result) override {
53 base::AutoLock lock(lock_);
54 if (!wait_for_result_)
55 unexpected_callback_ = true;
57 result_ = result;
58 wait_for_result_ = false;
61 void ReceiveMidiData(uint32 port_index, const uint8* data, size_t size,
62 double timestamp) override {}
63 void AccumulateMidiBytesSent(size_t size) override {}
65 bool GetWaitForResult() {
66 base::AutoLock lock(lock_);
67 return wait_for_result_;
70 bool GetWaitForPort() {
71 base::AutoLock lock(lock_);
72 return wait_for_port_;
75 MidiResult WaitForResult() {
76 while (GetWaitForResult()) {
77 base::RunLoop run_loop;
78 run_loop.RunUntilIdle();
80 EXPECT_FALSE(unexpected_callback_);
81 return result_;
83 MidiPortInfo WaitForPort() {
84 while (GetWaitForPort()) {
85 base::RunLoop run_loop;
86 run_loop.RunUntilIdle();
88 EXPECT_FALSE(unexpected_callback_);
89 return info_;
92 private:
93 base::Lock lock_;
94 MidiResult result_;
95 bool wait_for_result_;
96 MidiPortInfo info_;
97 bool wait_for_port_;
98 bool unexpected_callback_;
100 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient);
103 class MidiManagerMacTest : public ::testing::Test {
104 public:
105 MidiManagerMacTest()
106 : manager_(new MidiManagerMac),
107 message_loop_(new base::MessageLoop) {}
109 protected:
110 void StartSession(MidiManagerClient* client) {
111 manager_->StartSession(client);
113 void EndSession(MidiManagerClient* client) {
114 manager_->EndSession(client);
117 private:
118 scoped_ptr<MidiManager> manager_;
119 scoped_ptr<base::MessageLoop> message_loop_;
121 DISALLOW_COPY_AND_ASSIGN(MidiManagerMacTest);
125 TEST_F(MidiManagerMacTest, MidiNotification) {
126 scoped_ptr<FakeMidiManagerClient> client(new FakeMidiManagerClient);
127 StartSession(client.get());
129 MidiResult result = client->WaitForResult();
130 EXPECT_EQ(MIDI_OK, result);
132 // Create MIDIClient, and MIDIEndpoint as a MIDIDestination. This should
133 // notify MIDIManagerMac as a MIDIObjectAddRemoveNotification.
134 MIDIClientRef midi_client = 0;
135 OSStatus status = MIDIClientCreate(
136 CFSTR("MidiManagerMacTest"), nullptr, nullptr, &midi_client);
137 EXPECT_EQ(noErr, status);
139 MIDIEndpointRef ep = 0;
140 status = MIDIDestinationCreate(
141 midi_client, CFSTR("DestinationTest"), Noop, nullptr, &ep);
142 EXPECT_EQ(noErr, status);
143 SInt32 id;
144 status = MIDIObjectGetIntegerProperty(ep, kMIDIPropertyUniqueID, &id);
145 EXPECT_EQ(noErr, status);
146 EXPECT_NE(0, id);
148 // Wait until the created device is notified to MidiManagerMac.
149 MidiPortInfo info = client->WaitForPort();
150 EXPECT_EQ("DestinationTest", info.name);
152 EndSession(client.get());
153 if (ep)
154 MIDIEndpointDispose(ep);
155 if (midi_client)
156 MIDIClientDispose(midi_client);
159 } // namespace
161 } // namespace midi
162 } // namespace media