yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 1 | // Copyright 2014 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_usb.h" |
| 6 | |
avi | 793390d | 2015-12-22 22:22:36 -0800 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 9 | #include <memory> |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 10 | #include <string> |
dcheng | c2aeece | 2015-12-27 00:54:00 -0800 | [diff] [blame] | 11 | #include <utility> |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 12 | |
Lei Zhang | 21a4453 | 2021-05-26 19:26:48 +0000 | [diff] [blame] | 13 | #include "base/cxx17_backports.h" |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 14 | #include "base/run_loop.h" |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 15 | #include "base/strings/stringprintf.h" |
Carlos Caballero | 10135c2 | 2019-12-02 08:40:23 +0000 | [diff] [blame] | 16 | #include "base/test/task_environment.h" |
yhirano@chromium.org | cfa642c | 2014-05-01 08:54:41 +0000 | [diff] [blame] | 17 | #include "base/time/time.h" |
Takashi Toyoshima | f436ca3 | 2017-09-11 10:09:44 +0000 | [diff] [blame] | 18 | #include "media/midi/midi_service.h" |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 19 | #include "media/midi/usb_midi_device.h" |
| 20 | #include "testing/gtest/include/gtest/gtest.h" |
| 21 | |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 22 | namespace midi { |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 23 | |
| 24 | namespace { |
| 25 | |
toyoshim | ec2570a | 2016-10-21 02:15:27 -0700 | [diff] [blame] | 26 | using mojom::PortState; |
toyoshim | 2f3a48f | 2016-10-17 01:54:13 -0700 | [diff] [blame] | 27 | using mojom::Result; |
| 28 | |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 29 | template<typename T, size_t N> |
| 30 | std::vector<T> ToVector(const T (&array)[N]) { |
| 31 | return std::vector<T>(array, array + N); |
| 32 | } |
| 33 | |
| 34 | class Logger { |
| 35 | public: |
Chris Watkins | c6cbcf6 | 2017-12-01 03:08:01 +0000 | [diff] [blame] | 36 | Logger() = default; |
Peter Boström | 5363403 | 2021-09-22 20:24:34 +0000 | [diff] [blame] | 37 | |
| 38 | Logger(const Logger&) = delete; |
| 39 | Logger& operator=(const Logger&) = delete; |
| 40 | |
Chris Watkins | c6cbcf6 | 2017-12-01 03:08:01 +0000 | [diff] [blame] | 41 | ~Logger() = default; |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 42 | |
| 43 | void AddLog(const std::string& message) { log_ += message; } |
| 44 | std::string TakeLog() { |
| 45 | std::string result; |
| 46 | result.swap(log_); |
| 47 | return result; |
| 48 | } |
| 49 | |
| 50 | private: |
| 51 | std::string log_; |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | class FakeUsbMidiDevice : public UsbMidiDevice { |
| 55 | public: |
| 56 | explicit FakeUsbMidiDevice(Logger* logger) : logger_(logger) {} |
Peter Boström | 5363403 | 2021-09-22 20:24:34 +0000 | [diff] [blame] | 57 | |
| 58 | FakeUsbMidiDevice(const FakeUsbMidiDevice&) = delete; |
| 59 | FakeUsbMidiDevice& operator=(const FakeUsbMidiDevice&) = delete; |
| 60 | |
Chris Watkins | c6cbcf6 | 2017-12-01 03:08:01 +0000 | [diff] [blame] | 61 | ~FakeUsbMidiDevice() override = default; |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 62 | |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 63 | std::vector<uint8_t> GetDescriptors() override { |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 64 | logger_->AddLog("UsbMidiDevice::GetDescriptors\n"); |
| 65 | return descriptors_; |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 66 | } |
| 67 | |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 68 | std::string GetManufacturer() override { return manufacturer_; } |
| 69 | std::string GetProductName() override { return product_name_; } |
| 70 | std::string GetDeviceVersion() override { return device_version_; } |
| 71 | |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 72 | void Send(int endpoint_number, const std::vector<uint8_t>& data) override { |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 73 | logger_->AddLog("UsbMidiDevice::Send "); |
| 74 | logger_->AddLog(base::StringPrintf("endpoint = %d data =", |
| 75 | endpoint_number)); |
| 76 | for (size_t i = 0; i < data.size(); ++i) |
| 77 | logger_->AddLog(base::StringPrintf(" 0x%02x", data[i])); |
| 78 | logger_->AddLog("\n"); |
| 79 | } |
| 80 | |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 81 | void SetDescriptors(const std::vector<uint8_t> descriptors) { |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 82 | descriptors_ = descriptors; |
| 83 | } |
| 84 | void SetManufacturer(const std::string& manufacturer) { |
| 85 | manufacturer_ = manufacturer; |
| 86 | } |
| 87 | void SetProductName(const std::string& product_name) { |
| 88 | product_name_ = product_name; |
| 89 | } |
| 90 | void SetDeviceVersion(const std::string& device_version) { |
| 91 | device_version_ = device_version; |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | private: |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 95 | std::vector<uint8_t> descriptors_; |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 96 | std::string manufacturer_; |
| 97 | std::string product_name_; |
| 98 | std::string device_version_; |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 99 | Logger* logger_; |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 102 | class FakeMidiManagerClient : public MidiManagerClient { |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 103 | public: |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 104 | explicit FakeMidiManagerClient(Logger* logger) |
| 105 | : complete_start_session_(false), |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 106 | result_(Result::NOT_SUPPORTED), |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 107 | logger_(logger) {} |
Peter Boström | 5363403 | 2021-09-22 20:24:34 +0000 | [diff] [blame] | 108 | |
| 109 | FakeMidiManagerClient(const FakeMidiManagerClient&) = delete; |
| 110 | FakeMidiManagerClient& operator=(const FakeMidiManagerClient&) = delete; |
| 111 | |
Chris Watkins | c6cbcf6 | 2017-12-01 03:08:01 +0000 | [diff] [blame] | 112 | ~FakeMidiManagerClient() override = default; |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 113 | |
Adithya Srinivasan | 3325273 | 2018-10-17 15:59:40 +0000 | [diff] [blame] | 114 | void AddInputPort(const mojom::PortInfo& info) override { |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 115 | input_ports_.push_back(info); |
| 116 | } |
| 117 | |
Adithya Srinivasan | 3325273 | 2018-10-17 15:59:40 +0000 | [diff] [blame] | 118 | void AddOutputPort(const mojom::PortInfo& info) override { |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 119 | output_ports_.push_back(info); |
| 120 | } |
| 121 | |
toyoshim | ec2570a | 2016-10-21 02:15:27 -0700 | [diff] [blame] | 122 | void SetInputPortState(uint32_t port_index, PortState state) override {} |
toyoshim | 5c6fe4b | 2015-02-18 23:28:09 -0800 | [diff] [blame] | 123 | |
toyoshim | ec2570a | 2016-10-21 02:15:27 -0700 | [diff] [blame] | 124 | void SetOutputPortState(uint32_t port_index, PortState state) override {} |
toyoshim | 5c6fe4b | 2015-02-18 23:28:09 -0800 | [diff] [blame] | 125 | |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 126 | void CompleteStartSession(Result result) override { |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 127 | complete_start_session_ = true; |
| 128 | result_ = result; |
toyoshim@chromium.org | f77a1e6 | 2014-04-11 13:16:24 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 131 | void ReceiveMidiData(uint32_t port_index, |
| 132 | const uint8_t* data, |
dcheng | 63ccbc3 | 2014-10-21 05:23:27 -0700 | [diff] [blame] | 133 | size_t size, |
tzik | 925e2c6 | 2018-02-02 07:39:45 +0000 | [diff] [blame] | 134 | base::TimeTicks timestamp) override { |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 135 | logger_->AddLog("MidiManagerClient::ReceiveMidiData "); |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 136 | logger_->AddLog( |
| 137 | base::StringPrintf("usb:port_index = %d data =", port_index)); |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 138 | for (size_t i = 0; i < size; ++i) |
| 139 | logger_->AddLog(base::StringPrintf(" 0x%02x", data[i])); |
| 140 | logger_->AddLog("\n"); |
| 141 | } |
| 142 | |
dcheng | 63ccbc3 | 2014-10-21 05:23:27 -0700 | [diff] [blame] | 143 | void AccumulateMidiBytesSent(size_t size) override { |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 144 | logger_->AddLog("MidiManagerClient::AccumulateMidiBytesSent "); |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 145 | // Windows has no "%zu". |
| 146 | logger_->AddLog(base::StringPrintf("size = %u\n", |
| 147 | static_cast<unsigned>(size))); |
| 148 | } |
| 149 | |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 150 | void Detach() override {} |
| 151 | |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 152 | bool complete_start_session_; |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 153 | Result result_; |
Adithya Srinivasan | 3325273 | 2018-10-17 15:59:40 +0000 | [diff] [blame] | 154 | std::vector<mojom::PortInfo> input_ports_; |
| 155 | std::vector<mojom::PortInfo> output_ports_; |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 156 | |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 157 | private: |
| 158 | Logger* logger_; |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | class TestUsbMidiDeviceFactory : public UsbMidiDevice::Factory { |
| 162 | public: |
Chris Watkins | c6cbcf6 | 2017-12-01 03:08:01 +0000 | [diff] [blame] | 163 | TestUsbMidiDeviceFactory() = default; |
Peter Boström | 5363403 | 2021-09-22 20:24:34 +0000 | [diff] [blame] | 164 | |
| 165 | TestUsbMidiDeviceFactory(const TestUsbMidiDeviceFactory&) = delete; |
| 166 | TestUsbMidiDeviceFactory& operator=(const TestUsbMidiDeviceFactory&) = delete; |
| 167 | |
Chris Watkins | c6cbcf6 | 2017-12-01 03:08:01 +0000 | [diff] [blame] | 168 | ~TestUsbMidiDeviceFactory() override = default; |
dcheng | 63ccbc3 | 2014-10-21 05:23:27 -0700 | [diff] [blame] | 169 | void EnumerateDevices(UsbMidiDeviceDelegate* device, |
| 170 | Callback callback) override { |
Takashi Toyoshima | d2bdc59 | 2017-09-13 10:02:54 +0000 | [diff] [blame] | 171 | callback_ = std::move(callback); |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | Callback callback_; |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 177 | class MidiManagerUsbForTesting : public MidiManagerUsb { |
| 178 | public: |
Takashi Toyoshima | f436ca3 | 2017-09-11 10:09:44 +0000 | [diff] [blame] | 179 | MidiManagerUsbForTesting( |
| 180 | std::unique_ptr<UsbMidiDevice::Factory> device_factory, |
| 181 | MidiService* service) |
| 182 | : MidiManagerUsb(service, std::move(device_factory)) {} |
Peter Boström | 5363403 | 2021-09-22 20:24:34 +0000 | [diff] [blame] | 183 | |
| 184 | MidiManagerUsbForTesting(const MidiManagerUsbForTesting&) = delete; |
| 185 | MidiManagerUsbForTesting& operator=(const MidiManagerUsbForTesting&) = delete; |
| 186 | |
Chris Watkins | c6cbcf6 | 2017-12-01 03:08:01 +0000 | [diff] [blame] | 187 | ~MidiManagerUsbForTesting() override = default; |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 188 | |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 189 | void CallCompleteInitialization(Result result) { |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 190 | CompleteInitialization(result); |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 191 | base::RunLoop run_loop; |
| 192 | run_loop.RunUntilIdle(); |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 193 | } |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 194 | }; |
| 195 | |
Takashi Toyoshima | f436ca3 | 2017-09-11 10:09:44 +0000 | [diff] [blame] | 196 | class MidiManagerFactoryForTesting : public midi::MidiService::ManagerFactory { |
| 197 | public: |
| 198 | MidiManagerFactoryForTesting() = default; |
| 199 | ~MidiManagerFactoryForTesting() override = default; |
| 200 | std::unique_ptr<midi::MidiManager> Create( |
| 201 | midi::MidiService* service) override { |
| 202 | std::unique_ptr<TestUsbMidiDeviceFactory> device_factory = |
| 203 | std::make_unique<TestUsbMidiDeviceFactory>(); |
| 204 | device_factory_ = device_factory.get(); |
| 205 | std::unique_ptr<MidiManagerUsbForTesting> manager = |
| 206 | std::make_unique<MidiManagerUsbForTesting>(std::move(device_factory), |
| 207 | service); |
| 208 | // |manager| will be owned by the caller MidiService instance, and valid |
| 209 | // while the MidiService instance is running. |
| 210 | // MidiService::Shutdown() or destructor will destruct it, and |manager_| |
| 211 | // get to be invalid after that. |
| 212 | manager_ = manager.get(); |
| 213 | return manager; |
| 214 | } |
| 215 | MidiManagerUsb* manager() { |
| 216 | DCHECK(manager_); |
| 217 | return manager_; |
| 218 | } |
| 219 | TestUsbMidiDeviceFactory* device_factory() { |
| 220 | DCHECK(device_factory_); |
| 221 | return device_factory_; |
| 222 | } |
| 223 | |
| 224 | private: |
| 225 | TestUsbMidiDeviceFactory* device_factory_ = nullptr; |
| 226 | MidiManagerUsbForTesting* manager_ = nullptr; |
| 227 | }; |
| 228 | |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 229 | class MidiManagerUsbTest : public ::testing::Test { |
| 230 | public: |
Carlos Caballero | 10135c2 | 2019-12-02 08:40:23 +0000 | [diff] [blame] | 231 | MidiManagerUsbTest() { |
Takashi Toyoshima | f436ca3 | 2017-09-11 10:09:44 +0000 | [diff] [blame] | 232 | std::unique_ptr<MidiManagerFactoryForTesting> factory = |
| 233 | std::make_unique<MidiManagerFactoryForTesting>(); |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 234 | factory_ = factory.get(); |
Takashi Toyoshima | f436ca3 | 2017-09-11 10:09:44 +0000 | [diff] [blame] | 235 | service_ = std::make_unique<MidiService>(std::move(factory)); |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 236 | } |
Peter Boström | 5363403 | 2021-09-22 20:24:34 +0000 | [diff] [blame] | 237 | |
| 238 | MidiManagerUsbTest(const MidiManagerUsbTest&) = delete; |
| 239 | MidiManagerUsbTest& operator=(const MidiManagerUsbTest&) = delete; |
| 240 | |
dcheng | 9e8524d | 2014-10-27 15:18:50 -0700 | [diff] [blame] | 241 | ~MidiManagerUsbTest() override { |
Takashi Toyoshima | bc959ee | 2018-01-09 16:04:04 +0900 | [diff] [blame] | 242 | service_->Shutdown(); |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 243 | base::RunLoop run_loop; |
| 244 | run_loop.RunUntilIdle(); |
| 245 | |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 246 | std::string leftover_logs = logger_.TakeLog(); |
| 247 | if (!leftover_logs.empty()) { |
| 248 | ADD_FAILURE() << "Log should be empty: " << leftover_logs; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | protected: |
| 253 | void Initialize() { |
Peter Boström | 78053ff | 2021-04-02 23:09:28 +0000 | [diff] [blame] | 254 | client_ = std::make_unique<FakeMidiManagerClient>(&logger_); |
Takashi Toyoshima | bc959ee | 2018-01-09 16:04:04 +0900 | [diff] [blame] | 255 | service_->StartSession(client_.get()); |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Takashi Toyoshima | bc959ee | 2018-01-09 16:04:04 +0900 | [diff] [blame] | 258 | void Finalize() { service_->EndSession(client_.get()); } |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 259 | |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 260 | bool IsInitializationCallbackInvoked() { |
| 261 | return client_->complete_start_session_; |
| 262 | } |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 263 | |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 264 | Result GetInitializationResult() { return client_->result_; } |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 265 | |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 266 | void RunCallbackUntilCallbackInvoked( |
| 267 | bool result, UsbMidiDevice::Devices* devices) { |
Takashi Toyoshima | f436ca3 | 2017-09-11 10:09:44 +0000 | [diff] [blame] | 268 | std::move(factory_->device_factory()->callback_).Run(result, devices); |
toyoshim | 11f7d57 | 2014-10-20 02:37:10 -0700 | [diff] [blame] | 269 | while (!client_->complete_start_session_) { |
| 270 | base::RunLoop run_loop; |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 271 | run_loop.RunUntilIdle(); |
toyoshim | 11f7d57 | 2014-10-20 02:37:10 -0700 | [diff] [blame] | 272 | } |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Adithya Srinivasan | 3325273 | 2018-10-17 15:59:40 +0000 | [diff] [blame] | 275 | const std::vector<mojom::PortInfo>& input_ports() { |
| 276 | return client_->input_ports_; |
| 277 | } |
| 278 | const std::vector<mojom::PortInfo>& output_ports() { |
| 279 | return client_->output_ports_; |
| 280 | } |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 281 | |
Takashi Toyoshima | f436ca3 | 2017-09-11 10:09:44 +0000 | [diff] [blame] | 282 | MidiManagerUsb* manager() { return factory_->manager(); } |
| 283 | |
| 284 | MidiManagerFactoryForTesting* factory_; |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 285 | std::unique_ptr<FakeMidiManagerClient> client_; |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 286 | Logger logger_; |
| 287 | |
| 288 | private: |
Takashi Toyoshima | f436ca3 | 2017-09-11 10:09:44 +0000 | [diff] [blame] | 289 | std::unique_ptr<MidiService> service_; |
Carlos Caballero | 10135c2 | 2019-12-02 08:40:23 +0000 | [diff] [blame] | 290 | base::test::SingleThreadTaskEnvironment task_environment_; |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | |
| 294 | TEST_F(MidiManagerUsbTest, Initialize) { |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 295 | std::unique_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_)); |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 296 | uint8_t descriptors[] = { |
| 297 | 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a, 0x2d, 0x75, |
| 298 | 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02, 0x75, 0x00, 0x02, 0x01, |
| 299 | 0x00, 0x80, 0x30, 0x09, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, |
| 300 | 0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, |
| 301 | 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51, |
| 302 | 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02, 0x01, 0x03, |
| 303 | 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09, 0x24, 0x03, 0x01, 0x07, |
| 304 | 0x01, 0x06, 0x01, 0x00, 0x09, 0x24, 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, |
| 305 | 0x00, 0x09, 0x24, 0x03, 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, |
| 306 | 0x02, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02, |
| 307 | 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, 0x05, 0x25, |
| 308 | 0x01, 0x01, 0x07, |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 309 | }; |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 310 | device->SetDescriptors(ToVector(descriptors)); |
| 311 | device->SetManufacturer("vendor1"); |
| 312 | device->SetProductName("device1"); |
| 313 | device->SetDeviceVersion("1.02"); |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 314 | |
| 315 | Initialize(); |
xiaofeng.zhang | 06152d8 | 2017-05-21 04:39:34 -0700 | [diff] [blame] | 316 | UsbMidiDevice::Devices devices; |
dcheng | c2aeece | 2015-12-27 00:54:00 -0800 | [diff] [blame] | 317 | devices.push_back(std::move(device)); |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 318 | EXPECT_FALSE(IsInitializationCallbackInvoked()); |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 319 | RunCallbackUntilCallbackInvoked(true, &devices); |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 320 | EXPECT_EQ(Result::OK, GetInitializationResult()); |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 321 | |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 322 | ASSERT_EQ(1u, input_ports().size()); |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 323 | EXPECT_EQ("usb:port-0-2", input_ports()[0].id); |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 324 | EXPECT_EQ("vendor1", input_ports()[0].manufacturer); |
| 325 | EXPECT_EQ("device1", input_ports()[0].name); |
| 326 | EXPECT_EQ("1.02", input_ports()[0].version); |
| 327 | |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 328 | ASSERT_EQ(2u, output_ports().size()); |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 329 | EXPECT_EQ("usb:port-0-0", output_ports()[0].id); |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 330 | EXPECT_EQ("vendor1", output_ports()[0].manufacturer); |
| 331 | EXPECT_EQ("device1", output_ports()[0].name); |
| 332 | EXPECT_EQ("1.02", output_ports()[0].version); |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 333 | EXPECT_EQ("usb:port-0-1", output_ports()[1].id); |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 334 | EXPECT_EQ("vendor1", output_ports()[1].manufacturer); |
| 335 | EXPECT_EQ("device1", output_ports()[1].name); |
| 336 | EXPECT_EQ("1.02", output_ports()[1].version); |
| 337 | |
Takashi Toyoshima | f436ca3 | 2017-09-11 10:09:44 +0000 | [diff] [blame] | 338 | ASSERT_TRUE(manager()->input_stream()); |
| 339 | std::vector<UsbMidiJack> jacks = manager()->input_stream()->jacks(); |
| 340 | ASSERT_EQ(2u, manager()->output_streams().size()); |
| 341 | EXPECT_EQ(2u, manager()->output_streams()[0]->jack().jack_id); |
| 342 | EXPECT_EQ(3u, manager()->output_streams()[1]->jack().jack_id); |
yhirano | 807f97f | 2015-02-26 18:44:10 -0800 | [diff] [blame] | 343 | ASSERT_EQ(1u, jacks.size()); |
| 344 | EXPECT_EQ(2, jacks[0].endpoint_number()); |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 345 | |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 346 | EXPECT_EQ("UsbMidiDevice::GetDescriptors\n", logger_.TakeLog()); |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 347 | } |
| 348 | |
yhirano | 78a04ac | 2015-02-25 07:32:38 -0800 | [diff] [blame] | 349 | TEST_F(MidiManagerUsbTest, InitializeMultipleDevices) { |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 350 | std::unique_ptr<FakeUsbMidiDevice> device1(new FakeUsbMidiDevice(&logger_)); |
| 351 | std::unique_ptr<FakeUsbMidiDevice> device2(new FakeUsbMidiDevice(&logger_)); |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 352 | uint8_t descriptors[] = { |
yhirano | 78a04ac | 2015-02-25 07:32:38 -0800 | [diff] [blame] | 353 | 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a, 0x2d, 0x75, |
| 354 | 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02, 0x75, 0x00, 0x02, 0x01, |
| 355 | 0x00, 0x80, 0x30, 0x09, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, |
| 356 | 0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, |
| 357 | 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51, |
| 358 | 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02, 0x01, 0x03, |
| 359 | 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09, 0x24, 0x03, 0x01, 0x07, |
| 360 | 0x01, 0x06, 0x01, 0x00, 0x09, 0x24, 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, |
| 361 | 0x00, 0x09, 0x24, 0x03, 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, |
| 362 | 0x02, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02, |
| 363 | 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, 0x05, 0x25, |
| 364 | 0x01, 0x01, 0x07, |
| 365 | }; |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 366 | device1->SetDescriptors(ToVector(descriptors)); |
| 367 | device1->SetManufacturer("vendor1"); |
| 368 | device1->SetProductName("device1"); |
| 369 | device1->SetDeviceVersion("1.02"); |
| 370 | device2->SetDescriptors(ToVector(descriptors)); |
| 371 | device2->SetManufacturer("vendor2"); |
| 372 | device2->SetProductName("device2"); |
| 373 | device2->SetDeviceVersion("98.76"); |
yhirano | 78a04ac | 2015-02-25 07:32:38 -0800 | [diff] [blame] | 374 | |
| 375 | Initialize(); |
xiaofeng.zhang | 06152d8 | 2017-05-21 04:39:34 -0700 | [diff] [blame] | 376 | UsbMidiDevice::Devices devices; |
dcheng | c2aeece | 2015-12-27 00:54:00 -0800 | [diff] [blame] | 377 | devices.push_back(std::move(device1)); |
| 378 | devices.push_back(std::move(device2)); |
yhirano | 78a04ac | 2015-02-25 07:32:38 -0800 | [diff] [blame] | 379 | EXPECT_FALSE(IsInitializationCallbackInvoked()); |
| 380 | RunCallbackUntilCallbackInvoked(true, &devices); |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 381 | EXPECT_EQ(Result::OK, GetInitializationResult()); |
yhirano | 78a04ac | 2015-02-25 07:32:38 -0800 | [diff] [blame] | 382 | |
| 383 | ASSERT_EQ(2u, input_ports().size()); |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 384 | EXPECT_EQ("usb:port-0-2", input_ports()[0].id); |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 385 | EXPECT_EQ("vendor1", input_ports()[0].manufacturer); |
| 386 | EXPECT_EQ("device1", input_ports()[0].name); |
| 387 | EXPECT_EQ("1.02", input_ports()[0].version); |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 388 | EXPECT_EQ("usb:port-1-2", input_ports()[1].id); |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 389 | EXPECT_EQ("vendor2", input_ports()[1].manufacturer); |
| 390 | EXPECT_EQ("device2", input_ports()[1].name); |
| 391 | EXPECT_EQ("98.76", input_ports()[1].version); |
| 392 | |
yhirano | 78a04ac | 2015-02-25 07:32:38 -0800 | [diff] [blame] | 393 | ASSERT_EQ(4u, output_ports().size()); |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 394 | EXPECT_EQ("usb:port-0-0", output_ports()[0].id); |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 395 | EXPECT_EQ("vendor1", output_ports()[0].manufacturer); |
| 396 | EXPECT_EQ("device1", output_ports()[0].name); |
| 397 | EXPECT_EQ("1.02", output_ports()[0].version); |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 398 | EXPECT_EQ("usb:port-0-1", output_ports()[1].id); |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 399 | EXPECT_EQ("vendor1", output_ports()[1].manufacturer); |
| 400 | EXPECT_EQ("device1", output_ports()[1].name); |
| 401 | EXPECT_EQ("1.02", output_ports()[1].version); |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 402 | EXPECT_EQ("usb:port-1-0", output_ports()[2].id); |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 403 | EXPECT_EQ("vendor2", output_ports()[2].manufacturer); |
| 404 | EXPECT_EQ("device2", output_ports()[2].name); |
| 405 | EXPECT_EQ("98.76", output_ports()[2].version); |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 406 | EXPECT_EQ("usb:port-1-1", output_ports()[3].id); |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 407 | EXPECT_EQ("vendor2", output_ports()[3].manufacturer); |
| 408 | EXPECT_EQ("device2", output_ports()[3].name); |
| 409 | EXPECT_EQ("98.76", output_ports()[3].version); |
| 410 | |
Takashi Toyoshima | f436ca3 | 2017-09-11 10:09:44 +0000 | [diff] [blame] | 411 | ASSERT_TRUE(manager()->input_stream()); |
| 412 | std::vector<UsbMidiJack> jacks = manager()->input_stream()->jacks(); |
| 413 | ASSERT_EQ(4u, manager()->output_streams().size()); |
| 414 | EXPECT_EQ(2u, manager()->output_streams()[0]->jack().jack_id); |
| 415 | EXPECT_EQ(3u, manager()->output_streams()[1]->jack().jack_id); |
yhirano | 807f97f | 2015-02-26 18:44:10 -0800 | [diff] [blame] | 416 | ASSERT_EQ(2u, jacks.size()); |
| 417 | EXPECT_EQ(2, jacks[0].endpoint_number()); |
yhirano | 78a04ac | 2015-02-25 07:32:38 -0800 | [diff] [blame] | 418 | |
| 419 | EXPECT_EQ( |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 420 | "UsbMidiDevice::GetDescriptors\n" |
| 421 | "UsbMidiDevice::GetDescriptors\n", |
yhirano | 78a04ac | 2015-02-25 07:32:38 -0800 | [diff] [blame] | 422 | logger_.TakeLog()); |
| 423 | } |
| 424 | |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 425 | TEST_F(MidiManagerUsbTest, InitializeFail) { |
| 426 | Initialize(); |
| 427 | |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 428 | EXPECT_FALSE(IsInitializationCallbackInvoked()); |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 429 | RunCallbackUntilCallbackInvoked(false, NULL); |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 430 | EXPECT_EQ(Result::INITIALIZATION_ERROR, GetInitializationResult()); |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 431 | } |
| 432 | |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 433 | TEST_F(MidiManagerUsbTest, InitializeFailBecauseOfInvalidDescriptors) { |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 434 | std::unique_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_)); |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 435 | uint8_t descriptors[] = {0x04}; |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 436 | device->SetDescriptors(ToVector(descriptors)); |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 437 | |
| 438 | Initialize(); |
xiaofeng.zhang | 06152d8 | 2017-05-21 04:39:34 -0700 | [diff] [blame] | 439 | UsbMidiDevice::Devices devices; |
dcheng | c2aeece | 2015-12-27 00:54:00 -0800 | [diff] [blame] | 440 | devices.push_back(std::move(device)); |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 441 | EXPECT_FALSE(IsInitializationCallbackInvoked()); |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 442 | RunCallbackUntilCallbackInvoked(true, &devices); |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 443 | EXPECT_EQ(Result::INITIALIZATION_ERROR, GetInitializationResult()); |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 444 | EXPECT_EQ("UsbMidiDevice::GetDescriptors\n", logger_.TakeLog()); |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | TEST_F(MidiManagerUsbTest, Send) { |
toyoshim | 7dd1248 | 2015-04-07 07:02:49 -0700 | [diff] [blame] | 448 | Initialize(); |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 449 | std::unique_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_)); |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 450 | uint8_t descriptors[] = { |
| 451 | 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a, 0x2d, 0x75, |
| 452 | 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02, 0x75, 0x00, 0x02, 0x01, |
| 453 | 0x00, 0x80, 0x30, 0x09, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, |
| 454 | 0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, |
| 455 | 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51, |
| 456 | 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02, 0x01, 0x03, |
| 457 | 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09, 0x24, 0x03, 0x01, 0x07, |
| 458 | 0x01, 0x06, 0x01, 0x00, 0x09, 0x24, 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, |
| 459 | 0x00, 0x09, 0x24, 0x03, 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, |
| 460 | 0x02, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02, |
| 461 | 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, 0x05, 0x25, |
| 462 | 0x01, 0x01, 0x07, |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 463 | }; |
| 464 | |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 465 | device->SetDescriptors(ToVector(descriptors)); |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 466 | uint8_t data[] = { |
| 467 | 0x90, 0x45, 0x7f, 0xf0, 0x00, 0x01, 0xf7, |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 468 | }; |
| 469 | |
xiaofeng.zhang | 06152d8 | 2017-05-21 04:39:34 -0700 | [diff] [blame] | 470 | UsbMidiDevice::Devices devices; |
dcheng | c2aeece | 2015-12-27 00:54:00 -0800 | [diff] [blame] | 471 | devices.push_back(std::move(device)); |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 472 | EXPECT_FALSE(IsInitializationCallbackInvoked()); |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 473 | RunCallbackUntilCallbackInvoked(true, &devices); |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 474 | EXPECT_EQ(Result::OK, GetInitializationResult()); |
Takashi Toyoshima | f436ca3 | 2017-09-11 10:09:44 +0000 | [diff] [blame] | 475 | ASSERT_EQ(2u, manager()->output_streams().size()); |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 476 | |
tzik | 925e2c6 | 2018-02-02 07:39:45 +0000 | [diff] [blame] | 477 | manager()->DispatchSendMidiData(client_.get(), 1, ToVector(data), |
| 478 | base::TimeTicks()); |
toyoshim | ec5518e | 2015-04-03 00:53:39 -0700 | [diff] [blame] | 479 | // Since UsbMidiDevice::Send is posted as a task, RunLoop should run to |
| 480 | // invoke the task. |
toyoshim | ec5518e | 2015-04-03 00:53:39 -0700 | [diff] [blame] | 481 | base::RunLoop run_loop; |
| 482 | run_loop.RunUntilIdle(); |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 483 | EXPECT_EQ("UsbMidiDevice::GetDescriptors\n" |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 484 | "UsbMidiDevice::Send endpoint = 2 data = " |
| 485 | "0x19 0x90 0x45 0x7f " |
| 486 | "0x14 0xf0 0x00 0x01 " |
toyoshim | 7dd1248 | 2015-04-07 07:02:49 -0700 | [diff] [blame] | 487 | "0x15 0xf7 0x00 0x00\n" |
| 488 | "MidiManagerClient::AccumulateMidiBytesSent size = 7\n", |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 489 | logger_.TakeLog()); |
| 490 | } |
| 491 | |
yhirano | 86da2d8 | 2015-02-09 07:13:28 -0800 | [diff] [blame] | 492 | TEST_F(MidiManagerUsbTest, SendFromCompromizedRenderer) { |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 493 | std::unique_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_)); |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 494 | uint8_t descriptors[] = { |
| 495 | 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a, 0x2d, 0x75, |
| 496 | 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02, 0x75, 0x00, 0x02, 0x01, |
| 497 | 0x00, 0x80, 0x30, 0x09, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, |
| 498 | 0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, |
| 499 | 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51, |
| 500 | 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02, 0x01, 0x03, |
| 501 | 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09, 0x24, 0x03, 0x01, 0x07, |
| 502 | 0x01, 0x06, 0x01, 0x00, 0x09, 0x24, 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, |
| 503 | 0x00, 0x09, 0x24, 0x03, 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, |
| 504 | 0x02, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02, |
| 505 | 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, 0x05, 0x25, |
| 506 | 0x01, 0x01, 0x07, |
yhirano | 86da2d8 | 2015-02-09 07:13:28 -0800 | [diff] [blame] | 507 | }; |
| 508 | |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 509 | device->SetDescriptors(ToVector(descriptors)); |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 510 | uint8_t data[] = { |
| 511 | 0x90, 0x45, 0x7f, 0xf0, 0x00, 0x01, 0xf7, |
yhirano | 86da2d8 | 2015-02-09 07:13:28 -0800 | [diff] [blame] | 512 | }; |
| 513 | |
| 514 | Initialize(); |
xiaofeng.zhang | 06152d8 | 2017-05-21 04:39:34 -0700 | [diff] [blame] | 515 | UsbMidiDevice::Devices devices; |
dcheng | c2aeece | 2015-12-27 00:54:00 -0800 | [diff] [blame] | 516 | devices.push_back(std::move(device)); |
yhirano | 86da2d8 | 2015-02-09 07:13:28 -0800 | [diff] [blame] | 517 | EXPECT_FALSE(IsInitializationCallbackInvoked()); |
| 518 | RunCallbackUntilCallbackInvoked(true, &devices); |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 519 | EXPECT_EQ(Result::OK, GetInitializationResult()); |
Takashi Toyoshima | f436ca3 | 2017-09-11 10:09:44 +0000 | [diff] [blame] | 520 | ASSERT_EQ(2u, manager()->output_streams().size()); |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 521 | EXPECT_EQ("UsbMidiDevice::GetDescriptors\n", logger_.TakeLog()); |
yhirano | 86da2d8 | 2015-02-09 07:13:28 -0800 | [diff] [blame] | 522 | |
| 523 | // The specified port index is invalid. The manager must ignore the request. |
tzik | 925e2c6 | 2018-02-02 07:39:45 +0000 | [diff] [blame] | 524 | manager()->DispatchSendMidiData(client_.get(), 99, ToVector(data), |
| 525 | base::TimeTicks()); |
yhirano | 86da2d8 | 2015-02-09 07:13:28 -0800 | [diff] [blame] | 526 | EXPECT_EQ("", logger_.TakeLog()); |
| 527 | |
| 528 | // The specified port index is invalid. The manager must ignore the request. |
tzik | 925e2c6 | 2018-02-02 07:39:45 +0000 | [diff] [blame] | 529 | manager()->DispatchSendMidiData(client_.get(), 2, ToVector(data), |
| 530 | base::TimeTicks()); |
yhirano | 86da2d8 | 2015-02-09 07:13:28 -0800 | [diff] [blame] | 531 | EXPECT_EQ("", logger_.TakeLog()); |
| 532 | } |
| 533 | |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 534 | TEST_F(MidiManagerUsbTest, Receive) { |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 535 | std::unique_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_)); |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 536 | uint8_t descriptors[] = { |
| 537 | 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a, 0x2d, 0x75, |
| 538 | 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02, 0x75, 0x00, 0x02, 0x01, |
| 539 | 0x00, 0x80, 0x30, 0x09, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, |
| 540 | 0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, |
| 541 | 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51, |
| 542 | 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02, 0x01, 0x03, |
| 543 | 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09, 0x24, 0x03, 0x01, 0x07, |
| 544 | 0x01, 0x06, 0x01, 0x00, 0x09, 0x24, 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, |
| 545 | 0x00, 0x09, 0x24, 0x03, 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, |
| 546 | 0x02, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02, |
| 547 | 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, 0x05, 0x25, |
| 548 | 0x01, 0x01, 0x07, |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 549 | }; |
| 550 | |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 551 | device->SetDescriptors(ToVector(descriptors)); |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 552 | uint8_t data[] = { |
| 553 | 0x09, 0x90, 0x45, 0x7f, 0x04, 0xf0, 0x00, |
| 554 | 0x01, 0x49, 0x90, 0x88, 0x99, // This data should be ignored (CN = 4). |
| 555 | 0x05, 0xf7, 0x00, 0x00, |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 556 | }; |
| 557 | |
| 558 | Initialize(); |
xiaofeng.zhang | 06152d8 | 2017-05-21 04:39:34 -0700 | [diff] [blame] | 559 | UsbMidiDevice::Devices devices; |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 560 | UsbMidiDevice* device_raw = device.get(); |
dcheng | c2aeece | 2015-12-27 00:54:00 -0800 | [diff] [blame] | 561 | devices.push_back(std::move(device)); |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 562 | EXPECT_FALSE(IsInitializationCallbackInvoked()); |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 563 | RunCallbackUntilCallbackInvoked(true, &devices); |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 564 | EXPECT_EQ(Result::OK, GetInitializationResult()); |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 565 | |
Avi Drissman | 2c63719 | 2018-12-25 20:26:39 +0000 | [diff] [blame] | 566 | manager()->ReceiveUsbMidiData(device_raw, 2, data, base::size(data), |
Takashi Toyoshima | f436ca3 | 2017-09-11 10:09:44 +0000 | [diff] [blame] | 567 | base::TimeTicks()); |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 568 | Finalize(); |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 569 | |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 570 | EXPECT_EQ( |
| 571 | "UsbMidiDevice::GetDescriptors\n" |
| 572 | "MidiManagerClient::ReceiveMidiData usb:port_index = 0 " |
| 573 | "data = 0x90 0x45 0x7f\n" |
| 574 | "MidiManagerClient::ReceiveMidiData usb:port_index = 0 " |
| 575 | "data = 0xf0 0x00 0x01\n" |
| 576 | "MidiManagerClient::ReceiveMidiData usb:port_index = 0 data = 0xf7\n", |
| 577 | logger_.TakeLog()); |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 578 | } |
| 579 | |
yhirano | 33315c6 | 2015-02-26 17:01:15 -0800 | [diff] [blame] | 580 | TEST_F(MidiManagerUsbTest, AttachDevice) { |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 581 | uint8_t descriptors[] = { |
| 582 | 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a, 0x2d, 0x75, |
| 583 | 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02, 0x75, 0x00, 0x02, 0x01, |
| 584 | 0x00, 0x80, 0x30, 0x09, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, |
| 585 | 0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, |
| 586 | 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51, |
| 587 | 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02, 0x01, 0x03, |
| 588 | 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09, 0x24, 0x03, 0x01, 0x07, |
| 589 | 0x01, 0x06, 0x01, 0x00, 0x09, 0x24, 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, |
| 590 | 0x00, 0x09, 0x24, 0x03, 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, |
| 591 | 0x02, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02, |
| 592 | 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, 0x05, 0x25, |
| 593 | 0x01, 0x01, 0x07, |
yhirano | 33315c6 | 2015-02-26 17:01:15 -0800 | [diff] [blame] | 594 | }; |
| 595 | |
| 596 | Initialize(); |
xiaofeng.zhang | 06152d8 | 2017-05-21 04:39:34 -0700 | [diff] [blame] | 597 | UsbMidiDevice::Devices devices; |
yhirano | 33315c6 | 2015-02-26 17:01:15 -0800 | [diff] [blame] | 598 | EXPECT_FALSE(IsInitializationCallbackInvoked()); |
| 599 | RunCallbackUntilCallbackInvoked(true, &devices); |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 600 | EXPECT_EQ(Result::OK, GetInitializationResult()); |
yhirano | 33315c6 | 2015-02-26 17:01:15 -0800 | [diff] [blame] | 601 | |
| 602 | ASSERT_EQ(0u, input_ports().size()); |
| 603 | ASSERT_EQ(0u, output_ports().size()); |
Takashi Toyoshima | f436ca3 | 2017-09-11 10:09:44 +0000 | [diff] [blame] | 604 | ASSERT_TRUE(manager()->input_stream()); |
| 605 | std::vector<UsbMidiJack> jacks = manager()->input_stream()->jacks(); |
| 606 | ASSERT_EQ(0u, manager()->output_streams().size()); |
yhirano | 807f97f | 2015-02-26 18:44:10 -0800 | [diff] [blame] | 607 | ASSERT_EQ(0u, jacks.size()); |
yhirano | 33315c6 | 2015-02-26 17:01:15 -0800 | [diff] [blame] | 608 | EXPECT_EQ("", logger_.TakeLog()); |
| 609 | |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 610 | std::unique_ptr<FakeUsbMidiDevice> new_device( |
| 611 | new FakeUsbMidiDevice(&logger_)); |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 612 | new_device->SetDescriptors(ToVector(descriptors)); |
Takashi Toyoshima | f436ca3 | 2017-09-11 10:09:44 +0000 | [diff] [blame] | 613 | manager()->OnDeviceAttached(std::move(new_device)); |
yhirano | 33315c6 | 2015-02-26 17:01:15 -0800 | [diff] [blame] | 614 | |
| 615 | ASSERT_EQ(1u, input_ports().size()); |
| 616 | ASSERT_EQ(2u, output_ports().size()); |
Takashi Toyoshima | f436ca3 | 2017-09-11 10:09:44 +0000 | [diff] [blame] | 617 | ASSERT_TRUE(manager()->input_stream()); |
| 618 | jacks = manager()->input_stream()->jacks(); |
| 619 | ASSERT_EQ(2u, manager()->output_streams().size()); |
| 620 | EXPECT_EQ(2u, manager()->output_streams()[0]->jack().jack_id); |
| 621 | EXPECT_EQ(3u, manager()->output_streams()[1]->jack().jack_id); |
yhirano | 807f97f | 2015-02-26 18:44:10 -0800 | [diff] [blame] | 622 | ASSERT_EQ(1u, jacks.size()); |
| 623 | EXPECT_EQ(2, jacks[0].endpoint_number()); |
yhirano | 8e5f374 | 2015-04-23 23:06:15 -0700 | [diff] [blame] | 624 | EXPECT_EQ("UsbMidiDevice::GetDescriptors\n", logger_.TakeLog()); |
yhirano | 33315c6 | 2015-02-26 17:01:15 -0800 | [diff] [blame] | 625 | } |
| 626 | |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 627 | } // namespace |
| 628 | |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 629 | } // namespace midi |