Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "components/test_runner/mock_web_midi_accessor.h" | 5 #include "components/test_runner/mock_web_midi_accessor.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/strings/stringprintf.h" | |
| 10 #include "components/test_runner/test_interfaces.h" | 11 #include "components/test_runner/test_interfaces.h" |
| 11 #include "components/test_runner/test_runner.h" | 12 #include "components/test_runner/test_runner.h" |
| 12 #include "components/test_runner/web_test_delegate.h" | 13 #include "components/test_runner/web_test_delegate.h" |
| 13 #include "components/test_runner/web_test_runner.h" | 14 #include "components/test_runner/web_test_runner.h" |
| 14 #include "third_party/WebKit/public/platform/WebString.h" | 15 #include "third_party/WebKit/public/platform/WebString.h" |
| 15 #include "third_party/WebKit/public/platform/modules/webmidi/WebMIDIAccessorClie nt.h" | 16 #include "third_party/WebKit/public/platform/modules/webmidi/WebMIDIAccessorClie nt.h" |
| 16 | 17 |
| 17 using midi::mojom::PortState; | 18 using midi::mojom::PortState; |
| 18 using midi::mojom::Result; | 19 using midi::mojom::Result; |
| 19 | 20 |
| 20 namespace test_runner { | 21 namespace test_runner { |
| 21 | 22 |
| 23 namespace { | |
| 24 | |
| 25 const unsigned char kSysexHeader[] = {0xf0, 0x00, 0x02, 0x0d, 0x7f}; | |
|
yhirano
2016/11/14 04:32:54
constexpr
ditto below
Takashi Toyoshima
2016/11/14 06:25:28
Done.
| |
| 26 const unsigned char kSysexFooter = 0xf7; | |
| 27 const size_t kSysexMinimumLength = | |
| 28 arraysize(kSysexHeader) + sizeof(kSysexFooter) + 1; | |
| 29 | |
| 30 bool isSysexForTesting(const unsigned char* data, size_t length) { | |
| 31 // It should have five bytes header, one byte footer, and at least one byte | |
| 32 // payload. | |
| 33 if (length < kSysexMinimumLength) | |
| 34 return false; | |
| 35 if (memcmp(data, kSysexHeader, arraysize(kSysexHeader))) | |
| 36 return false; | |
| 37 return data[length - 1] == kSysexFooter; | |
| 38 } | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 22 MockWebMIDIAccessor::MockWebMIDIAccessor(blink::WebMIDIAccessorClient* client, | 42 MockWebMIDIAccessor::MockWebMIDIAccessor(blink::WebMIDIAccessorClient* client, |
| 23 TestInterfaces* interfaces) | 43 TestInterfaces* interfaces) |
| 24 : client_(client), interfaces_(interfaces), weak_factory_(this) {} | 44 : client_(client), |
| 45 interfaces_(interfaces), | |
| 46 max_input_port_index_(0), | |
| 47 max_output_port_index_(0), | |
| 48 weak_factory_(this) {} | |
| 25 | 49 |
| 26 MockWebMIDIAccessor::~MockWebMIDIAccessor() { | 50 MockWebMIDIAccessor::~MockWebMIDIAccessor() { |
| 27 } | 51 } |
| 28 | 52 |
| 29 void MockWebMIDIAccessor::startSession() { | 53 void MockWebMIDIAccessor::startSession() { |
| 30 // Add a mock input and output port. | 54 // Add a mock input and output port. |
| 31 client_->didAddInputPort("MockInputID", "MockInputManufacturer", | 55 addInputPort(PortState::CONNECTED); |
| 32 "MockInputName", "MockInputVersion", | 56 addOutputPort(PortState::CONNECTED); |
| 33 PortState::CONNECTED); | |
| 34 client_->didAddOutputPort("MockOutputID", "MockOutputManufacturer", | |
| 35 "MockOutputName", "MockOutputVersion", | |
| 36 PortState::CONNECTED); | |
| 37 interfaces_->GetDelegate()->PostTask(base::Bind( | 57 interfaces_->GetDelegate()->PostTask(base::Bind( |
| 38 &MockWebMIDIAccessor::ReportStartedSession, weak_factory_.GetWeakPtr(), | 58 &MockWebMIDIAccessor::reportStartedSession, weak_factory_.GetWeakPtr(), |
| 39 interfaces_->GetTestRunner()->midiAccessorResult())); | 59 interfaces_->GetTestRunner()->midiAccessorResult())); |
| 40 } | 60 } |
| 41 | 61 |
| 42 void MockWebMIDIAccessor::ReportStartedSession(Result result) { | |
| 43 client_->didStartSession(result); | |
| 44 } | |
| 45 | |
| 46 void MockWebMIDIAccessor::sendMIDIData(unsigned port_index, | 62 void MockWebMIDIAccessor::sendMIDIData(unsigned port_index, |
| 47 const unsigned char* data, | 63 const unsigned char* data, |
| 48 size_t length, | 64 size_t length, |
| 49 double timestamp) { | 65 double timestamp) { |
| 50 // Emulate a loopback device for testing. | 66 // Emulate a loopback device for testing. |
| 51 client_->didReceiveMIDIData(port_index, data, length, timestamp); | 67 if (port_index <= max_output_port_index_) |
|
yhirano
2016/11/14 04:32:54
Is there any chance this condition doesn't hold?
yhirano
2016/11/14 04:32:54
Shouldn't this '<=' be '<'?
Takashi Toyoshima
2016/11/14 06:25:27
If it's only for this CL, I can change sysex comma
| |
| 68 client_->didReceiveMIDIData(port_index, data, length, timestamp); | |
| 69 | |
| 70 // Handle special sysex messages for testing. | |
| 71 // A special sequence is [0xf0, 0x00, 0x02, 0x0d, 0x7f, <function>, 0xf7]. | |
| 72 // <function> should be one of following sequences. | |
| 73 // - [0x00, 0x00]: Add an input port as connected. | |
| 74 // - [0x00, 0x01]: Add an output port as connected. | |
| 75 // - [0x00, 0x02]: Add an input port as opened. | |
| 76 // - [0x00, 0x03]: Add an output port as opened. | |
| 77 if (!isSysexForTesting(data, length)) | |
| 78 return; | |
| 79 size_t offset = arraysize(kSysexHeader); | |
| 80 if (data[offset++] != 0) | |
| 81 return; | |
| 82 switch (data[offset]) { | |
| 83 case 0: | |
| 84 addInputPort(PortState::CONNECTED); | |
| 85 break; | |
| 86 case 1: | |
| 87 addOutputPort(PortState::CONNECTED); | |
| 88 break; | |
| 89 case 2: | |
| 90 addInputPort(PortState::OPENED); | |
| 91 break; | |
| 92 case 3: | |
| 93 addOutputPort(PortState::OPENED); | |
| 94 break; | |
|
yhirano
2016/11/14 04:32:54
default: break;
Takashi Toyoshima
2016/11/14 06:25:28
Done.
| |
| 95 } | |
| 96 } | |
| 97 | |
| 98 void MockWebMIDIAccessor::addInputPort(PortState state) { | |
| 99 std::string id = | |
| 100 base::StringPrintf("MockInputID-%d", max_input_port_index_++); | |
| 101 client_->didAddInputPort(blink::WebString::fromUTF8(id), | |
| 102 "MockInputManufacturer", "MockInputName", | |
| 103 "MockInputVersion", state); | |
| 104 } | |
| 105 | |
| 106 void MockWebMIDIAccessor::addOutputPort(PortState state) { | |
| 107 std::string id = | |
| 108 base::StringPrintf("MockOutputID-%d", max_output_port_index_++); | |
| 109 client_->didAddOutputPort(blink::WebString::fromUTF8(id), | |
| 110 "MockOutputManufacturer", "MockOutputName", | |
| 111 "MockOutputVersion", state); | |
| 112 } | |
| 113 | |
| 114 void MockWebMIDIAccessor::reportStartedSession(Result result) { | |
| 115 client_->didStartSession(result); | |
| 52 } | 116 } |
| 53 | 117 |
| 54 } // namespace test_runner | 118 } // namespace test_runner |
| OLD | NEW |