Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: components/test_runner/mock_web_midi_accessor.cc

Issue 2487113002: Web MIDI: fix a regression of r430234 (Closed)
Patch Set: fix a bug that was discussed offline Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 constexpr unsigned char kSysexHeader[] = {0xf0, 0x00, 0x02, 0x0d, 0x7f};
26 constexpr unsigned char kSysexFooter = 0xf7;
27 constexpr 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 next_input_port_index_(0),
47 next_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. Make sure if an input port that has
51 client_->didReceiveMIDIData(port_index, data, length, timestamp); 67 // the same index exists.
68 if (port_index < next_input_port_index_)
Takashi Toyoshima 2016/11/14 09:12:30 fixed
69 client_->didReceiveMIDIData(port_index, data, length, timestamp);
70
71 // Handle special sysex messages for testing.
72 // A special sequence is [0xf0, 0x00, 0x02, 0x0d, 0x7f, <function>, 0xf7].
73 // <function> should be one of following sequences.
74 // - [0x00, 0x00]: Add an input port as connected.
75 // - [0x00, 0x01]: Add an output port as connected.
76 // - [0x00, 0x02]: Add an input port as opened.
77 // - [0x00, 0x03]: Add an output port as opened.
78 if (!isSysexForTesting(data, length))
79 return;
80 size_t offset = arraysize(kSysexHeader);
81 if (data[offset++] != 0)
82 return;
83 switch (data[offset]) {
84 case 0:
85 addInputPort(PortState::CONNECTED);
86 break;
87 case 1:
88 addOutputPort(PortState::CONNECTED);
89 break;
90 case 2:
91 addInputPort(PortState::OPENED);
92 break;
93 case 3:
94 addOutputPort(PortState::OPENED);
95 break;
96 default:
97 break;
98 }
99 }
100
101 void MockWebMIDIAccessor::addInputPort(PortState state) {
102 std::string id =
103 base::StringPrintf("MockInputID-%d", next_input_port_index_++);
104 client_->didAddInputPort(blink::WebString::fromUTF8(id),
105 "MockInputManufacturer", "MockInputName",
106 "MockInputVersion", state);
107 }
108
109 void MockWebMIDIAccessor::addOutputPort(PortState state) {
110 std::string id =
111 base::StringPrintf("MockOutputID-%d", next_output_port_index_++);
112 client_->didAddOutputPort(blink::WebString::fromUTF8(id),
113 "MockOutputManufacturer", "MockOutputName",
114 "MockOutputVersion", state);
115 }
116
117 void MockWebMIDIAccessor::reportStartedSession(Result result) {
118 client_->didStartSession(result);
52 } 119 }
53 120
54 } // namespace test_runner 121 } // namespace test_runner
OLDNEW
« no previous file with comments | « components/test_runner/mock_web_midi_accessor.h ('k') | third_party/WebKit/LayoutTests/webmidi/add-port.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698