OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/midi/midi_manager_mac.h" | 5 #include "media/midi/midi_manager_mac.h" |
6 | 6 |
7 #include <CoreMIDI/MIDIServices.h> | 7 #include <CoreMIDI/MIDIServices.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <memory> | 11 #include <memory> |
12 | 12 |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
17 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 | 19 |
20 namespace midi { | 20 namespace midi { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 void Noop(const MIDIPacketList*, void*, void*) {} | 24 void Noop(const MIDIPacketList*, void*, void*) {} |
25 | 25 |
26 class FakeMidiManagerClient : public MidiManagerClient { | 26 class FakeMidiManagerClient : public MidiManagerClient { |
27 public: | 27 public: |
28 FakeMidiManagerClient() | 28 FakeMidiManagerClient() |
29 : result_(Result::NOT_SUPPORTED), | 29 : result_(mojom::Result::NOT_SUPPORTED), |
30 wait_for_result_(true), | 30 wait_for_result_(true), |
31 wait_for_port_(true), | 31 wait_for_port_(true), |
32 unexpected_callback_(false) {} | 32 unexpected_callback_(false) {} |
33 | 33 |
34 // MidiManagerClient implementation. | 34 // MidiManagerClient implementation. |
35 void AddInputPort(const MidiPortInfo& info) override {} | 35 void AddInputPort(const MidiPortInfo& info) override {} |
36 void AddOutputPort(const MidiPortInfo& info) override { | 36 void AddOutputPort(const MidiPortInfo& info) override { |
37 base::AutoLock lock(lock_); | 37 base::AutoLock lock(lock_); |
38 // AddOutputPort may be called before CompleteStartSession() is invoked | 38 // AddOutputPort may be called before CompleteStartSession() is invoked |
39 // if one or more MIDI devices including virtual ports are connected. | 39 // if one or more MIDI devices including virtual ports are connected. |
40 // Just ignore in such cases. | 40 // Just ignore in such cases. |
41 if (wait_for_result_) | 41 if (wait_for_result_) |
42 return; | 42 return; |
43 | 43 |
44 // Check if this is the first call after CompleteStartSession(), and | 44 // Check if this is the first call after CompleteStartSession(), and |
45 // the case should not happen twice. | 45 // the case should not happen twice. |
46 if (!wait_for_port_) | 46 if (!wait_for_port_) |
47 unexpected_callback_ = true; | 47 unexpected_callback_ = true; |
48 | 48 |
49 info_ = info; | 49 info_ = info; |
50 wait_for_port_ = false; | 50 wait_for_port_ = false; |
51 } | 51 } |
52 void SetInputPortState(uint32_t port_index, MidiPortState state) override {} | 52 void SetInputPortState(uint32_t port_index, MidiPortState state) override {} |
53 void SetOutputPortState(uint32_t port_index, MidiPortState state) override {} | 53 void SetOutputPortState(uint32_t port_index, MidiPortState state) override {} |
54 | 54 |
55 void CompleteStartSession(Result result) override { | 55 void CompleteStartSession(mojom::Result result) override { |
56 base::AutoLock lock(lock_); | 56 base::AutoLock lock(lock_); |
57 if (!wait_for_result_) | 57 if (!wait_for_result_) |
58 unexpected_callback_ = true; | 58 unexpected_callback_ = true; |
59 | 59 |
60 result_ = result; | 60 result_ = result; |
61 wait_for_result_ = false; | 61 wait_for_result_ = false; |
62 } | 62 } |
63 | 63 |
64 void ReceiveMidiData(uint32_t port_index, | 64 void ReceiveMidiData(uint32_t port_index, |
65 const uint8_t* data, | 65 const uint8_t* data, |
(...skipping 24 matching lines...) Expand all Loading... |
90 while (GetWaitForPort()) { | 90 while (GetWaitForPort()) { |
91 base::RunLoop run_loop; | 91 base::RunLoop run_loop; |
92 run_loop.RunUntilIdle(); | 92 run_loop.RunUntilIdle(); |
93 } | 93 } |
94 EXPECT_FALSE(unexpected_callback_); | 94 EXPECT_FALSE(unexpected_callback_); |
95 return info_; | 95 return info_; |
96 } | 96 } |
97 | 97 |
98 private: | 98 private: |
99 base::Lock lock_; | 99 base::Lock lock_; |
100 Result result_; | 100 mojom::Result result_; |
101 bool wait_for_result_; | 101 bool wait_for_result_; |
102 MidiPortInfo info_; | 102 MidiPortInfo info_; |
103 bool wait_for_port_; | 103 bool wait_for_port_; |
104 bool unexpected_callback_; | 104 bool unexpected_callback_; |
105 | 105 |
106 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient); | 106 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient); |
107 }; | 107 }; |
108 | 108 |
109 class MidiManagerMacTest : public ::testing::Test { | 109 class MidiManagerMacTest : public ::testing::Test { |
110 public: | 110 public: |
(...skipping 19 matching lines...) Expand all Loading... |
130 std::unique_ptr<base::MessageLoop> message_loop_; | 130 std::unique_ptr<base::MessageLoop> message_loop_; |
131 | 131 |
132 DISALLOW_COPY_AND_ASSIGN(MidiManagerMacTest); | 132 DISALLOW_COPY_AND_ASSIGN(MidiManagerMacTest); |
133 }; | 133 }; |
134 | 134 |
135 | 135 |
136 TEST_F(MidiManagerMacTest, MidiNotification) { | 136 TEST_F(MidiManagerMacTest, MidiNotification) { |
137 std::unique_ptr<FakeMidiManagerClient> client(new FakeMidiManagerClient); | 137 std::unique_ptr<FakeMidiManagerClient> client(new FakeMidiManagerClient); |
138 StartSession(client.get()); | 138 StartSession(client.get()); |
139 | 139 |
140 Result result = client->WaitForResult(); | 140 mojom::Result result = client->WaitForResult(); |
141 EXPECT_EQ(Result::OK, result); | 141 EXPECT_EQ(mojom::Result::OK, result); |
142 | 142 |
143 // Create MIDIClient, and MIDIEndpoint as a MIDIDestination. This should | 143 // Create MIDIClient, and MIDIEndpoint as a MIDIDestination. This should |
144 // notify MIDIManagerMac as a MIDIObjectAddRemoveNotification. | 144 // notify MIDIManagerMac as a MIDIObjectAddRemoveNotification. |
145 MIDIClientRef midi_client = 0; | 145 MIDIClientRef midi_client = 0; |
146 OSStatus status = MIDIClientCreate( | 146 OSStatus status = MIDIClientCreate( |
147 CFSTR("MidiManagerMacTest"), nullptr, nullptr, &midi_client); | 147 CFSTR("MidiManagerMacTest"), nullptr, nullptr, &midi_client); |
148 EXPECT_EQ(noErr, status); | 148 EXPECT_EQ(noErr, status); |
149 | 149 |
150 MIDIEndpointRef ep = 0; | 150 MIDIEndpointRef ep = 0; |
151 status = MIDIDestinationCreate( | 151 status = MIDIDestinationCreate( |
(...skipping 11 matching lines...) Expand all Loading... |
163 EndSession(client.get()); | 163 EndSession(client.get()); |
164 if (ep) | 164 if (ep) |
165 MIDIEndpointDispose(ep); | 165 MIDIEndpointDispose(ep); |
166 if (midi_client) | 166 if (midi_client) |
167 MIDIClientDispose(midi_client); | 167 MIDIClientDispose(midi_client); |
168 } | 168 } |
169 | 169 |
170 } // namespace | 170 } // namespace |
171 | 171 |
172 } // namespace midi | 172 } // namespace midi |
OLD | NEW |