| 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 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 namespace media { | 16 namespace media { |
| 17 namespace midi { | 17 namespace midi { |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 void Noop(const MIDIPacketList*, void*, void*) {} | 21 void Noop(const MIDIPacketList*, void*, void*) {} |
| 22 | 22 |
| 23 class FakeMidiManagerClient : public MidiManagerClient { | 23 class FakeMidiManagerClient : public MidiManagerClient { |
| 24 public: | 24 public: |
| 25 FakeMidiManagerClient() | 25 FakeMidiManagerClient() |
| 26 : result_(MIDI_NOT_SUPPORTED), | 26 : result_(Result::NOT_SUPPORTED), |
| 27 wait_for_result_(true), | 27 wait_for_result_(true), |
| 28 wait_for_port_(true), | 28 wait_for_port_(true), |
| 29 unexpected_callback_(false) {} | 29 unexpected_callback_(false) {} |
| 30 | 30 |
| 31 // MidiManagerClient implementation. | 31 // MidiManagerClient implementation. |
| 32 void AddInputPort(const MidiPortInfo& info) override {} | 32 void AddInputPort(const MidiPortInfo& info) override {} |
| 33 void AddOutputPort(const MidiPortInfo& info) override { | 33 void AddOutputPort(const MidiPortInfo& info) override { |
| 34 base::AutoLock lock(lock_); | 34 base::AutoLock lock(lock_); |
| 35 // AddOutputPort may be called before CompleteStartSession() is invoked | 35 // AddOutputPort may be called before CompleteStartSession() is invoked |
| 36 // if one or more MIDI devices including virtual ports are connected. | 36 // if one or more MIDI devices including virtual ports are connected. |
| 37 // Just ignore in such cases. | 37 // Just ignore in such cases. |
| 38 if (wait_for_result_) | 38 if (wait_for_result_) |
| 39 return; | 39 return; |
| 40 | 40 |
| 41 // Check if this is the first call after CompleteStartSession(), and | 41 // Check if this is the first call after CompleteStartSession(), and |
| 42 // the case should not happen twice. | 42 // the case should not happen twice. |
| 43 if (!wait_for_port_) | 43 if (!wait_for_port_) |
| 44 unexpected_callback_ = true; | 44 unexpected_callback_ = true; |
| 45 | 45 |
| 46 info_ = info; | 46 info_ = info; |
| 47 wait_for_port_ = false; | 47 wait_for_port_ = false; |
| 48 } | 48 } |
| 49 void SetInputPortState(uint32 port_index, MidiPortState state) override {} | 49 void SetInputPortState(uint32 port_index, MidiPortState state) override {} |
| 50 void SetOutputPortState(uint32 port_index, MidiPortState state) override {} | 50 void SetOutputPortState(uint32 port_index, MidiPortState state) override {} |
| 51 | 51 |
| 52 void CompleteStartSession(MidiResult result) override { | 52 void CompleteStartSession(Result result) override { |
| 53 base::AutoLock lock(lock_); | 53 base::AutoLock lock(lock_); |
| 54 if (!wait_for_result_) | 54 if (!wait_for_result_) |
| 55 unexpected_callback_ = true; | 55 unexpected_callback_ = true; |
| 56 | 56 |
| 57 result_ = result; | 57 result_ = result; |
| 58 wait_for_result_ = false; | 58 wait_for_result_ = false; |
| 59 } | 59 } |
| 60 | 60 |
| 61 void ReceiveMidiData(uint32 port_index, const uint8* data, size_t size, | 61 void ReceiveMidiData(uint32 port_index, const uint8* data, size_t size, |
| 62 double timestamp) override {} | 62 double timestamp) override {} |
| 63 void AccumulateMidiBytesSent(size_t size) override {} | 63 void AccumulateMidiBytesSent(size_t size) override {} |
| 64 | 64 |
| 65 bool GetWaitForResult() { | 65 bool GetWaitForResult() { |
| 66 base::AutoLock lock(lock_); | 66 base::AutoLock lock(lock_); |
| 67 return wait_for_result_; | 67 return wait_for_result_; |
| 68 } | 68 } |
| 69 | 69 |
| 70 bool GetWaitForPort() { | 70 bool GetWaitForPort() { |
| 71 base::AutoLock lock(lock_); | 71 base::AutoLock lock(lock_); |
| 72 return wait_for_port_; | 72 return wait_for_port_; |
| 73 } | 73 } |
| 74 | 74 |
| 75 MidiResult WaitForResult() { | 75 Result WaitForResult() { |
| 76 while (GetWaitForResult()) { | 76 while (GetWaitForResult()) { |
| 77 base::RunLoop run_loop; | 77 base::RunLoop run_loop; |
| 78 run_loop.RunUntilIdle(); | 78 run_loop.RunUntilIdle(); |
| 79 } | 79 } |
| 80 EXPECT_FALSE(unexpected_callback_); | 80 EXPECT_FALSE(unexpected_callback_); |
| 81 return result_; | 81 return result_; |
| 82 } | 82 } |
| 83 MidiPortInfo WaitForPort() { | 83 MidiPortInfo WaitForPort() { |
| 84 while (GetWaitForPort()) { | 84 while (GetWaitForPort()) { |
| 85 base::RunLoop run_loop; | 85 base::RunLoop run_loop; |
| 86 run_loop.RunUntilIdle(); | 86 run_loop.RunUntilIdle(); |
| 87 } | 87 } |
| 88 EXPECT_FALSE(unexpected_callback_); | 88 EXPECT_FALSE(unexpected_callback_); |
| 89 return info_; | 89 return info_; |
| 90 } | 90 } |
| 91 | 91 |
| 92 private: | 92 private: |
| 93 base::Lock lock_; | 93 base::Lock lock_; |
| 94 MidiResult result_; | 94 Result result_; |
| 95 bool wait_for_result_; | 95 bool wait_for_result_; |
| 96 MidiPortInfo info_; | 96 MidiPortInfo info_; |
| 97 bool wait_for_port_; | 97 bool wait_for_port_; |
| 98 bool unexpected_callback_; | 98 bool unexpected_callback_; |
| 99 | 99 |
| 100 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient); | 100 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient); |
| 101 }; | 101 }; |
| 102 | 102 |
| 103 class MidiManagerMacTest : public ::testing::Test { | 103 class MidiManagerMacTest : public ::testing::Test { |
| 104 public: | 104 public: |
| (...skipping 14 matching lines...) Expand all Loading... |
| 119 scoped_ptr<base::MessageLoop> message_loop_; | 119 scoped_ptr<base::MessageLoop> message_loop_; |
| 120 | 120 |
| 121 DISALLOW_COPY_AND_ASSIGN(MidiManagerMacTest); | 121 DISALLOW_COPY_AND_ASSIGN(MidiManagerMacTest); |
| 122 }; | 122 }; |
| 123 | 123 |
| 124 | 124 |
| 125 TEST_F(MidiManagerMacTest, MidiNotification) { | 125 TEST_F(MidiManagerMacTest, MidiNotification) { |
| 126 scoped_ptr<FakeMidiManagerClient> client(new FakeMidiManagerClient); | 126 scoped_ptr<FakeMidiManagerClient> client(new FakeMidiManagerClient); |
| 127 StartSession(client.get()); | 127 StartSession(client.get()); |
| 128 | 128 |
| 129 MidiResult result = client->WaitForResult(); | 129 Result result = client->WaitForResult(); |
| 130 EXPECT_EQ(MIDI_OK, result); | 130 EXPECT_EQ(Result::OK, result); |
| 131 | 131 |
| 132 // Create MIDIClient, and MIDIEndpoint as a MIDIDestination. This should | 132 // Create MIDIClient, and MIDIEndpoint as a MIDIDestination. This should |
| 133 // notify MIDIManagerMac as a MIDIObjectAddRemoveNotification. | 133 // notify MIDIManagerMac as a MIDIObjectAddRemoveNotification. |
| 134 MIDIClientRef midi_client = 0; | 134 MIDIClientRef midi_client = 0; |
| 135 OSStatus status = MIDIClientCreate( | 135 OSStatus status = MIDIClientCreate( |
| 136 CFSTR("MidiManagerMacTest"), nullptr, nullptr, &midi_client); | 136 CFSTR("MidiManagerMacTest"), nullptr, nullptr, &midi_client); |
| 137 EXPECT_EQ(noErr, status); | 137 EXPECT_EQ(noErr, status); |
| 138 | 138 |
| 139 MIDIEndpointRef ep = 0; | 139 MIDIEndpointRef ep = 0; |
| 140 status = MIDIDestinationCreate( | 140 status = MIDIDestinationCreate( |
| (...skipping 12 matching lines...) Expand all Loading... |
| 153 if (ep) | 153 if (ep) |
| 154 MIDIEndpointDispose(ep); | 154 MIDIEndpointDispose(ep); |
| 155 if (midi_client) | 155 if (midi_client) |
| 156 MIDIClientDispose(midi_client); | 156 MIDIClientDispose(midi_client); |
| 157 } | 157 } |
| 158 | 158 |
| 159 } // namespace | 159 } // namespace |
| 160 | 160 |
| 161 } // namespace midi | 161 } // namespace midi |
| 162 } // namespace media | 162 } // namespace media |
| OLD | NEW |