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

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

Issue 2707183003: Move //components/test_runner back into //content/shell (Closed)
Patch Set: Trim DEPS Created 3 years, 10 months 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
(Empty)
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 "components/test_runner/mock_web_midi_accessor.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/macros.h"
10 #include "base/strings/stringprintf.h"
11 #include "components/test_runner/test_interfaces.h"
12 #include "components/test_runner/test_runner.h"
13 #include "components/test_runner/web_test_delegate.h"
14 #include "components/test_runner/web_test_runner.h"
15 #include "third_party/WebKit/public/platform/WebString.h"
16 #include "third_party/WebKit/public/platform/modules/webmidi/WebMIDIAccessorClie nt.h"
17
18 using midi::mojom::PortState;
19 using midi::mojom::Result;
20
21 namespace test_runner {
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
42 MockWebMIDIAccessor::MockWebMIDIAccessor(blink::WebMIDIAccessorClient* client,
43 TestInterfaces* interfaces)
44 : client_(client),
45 interfaces_(interfaces),
46 next_input_port_index_(0),
47 next_output_port_index_(0),
48 weak_factory_(this) {}
49
50 MockWebMIDIAccessor::~MockWebMIDIAccessor() {
51 }
52
53 void MockWebMIDIAccessor::startSession() {
54 // Add a mock input and output port.
55 addInputPort(PortState::CONNECTED);
56 addOutputPort(PortState::CONNECTED);
57 interfaces_->GetDelegate()->PostTask(base::Bind(
58 &MockWebMIDIAccessor::reportStartedSession, weak_factory_.GetWeakPtr(),
59 interfaces_->GetTestRunner()->midiAccessorResult()));
60 }
61
62 void MockWebMIDIAccessor::sendMIDIData(unsigned port_index,
63 const unsigned char* data,
64 size_t length,
65 double timestamp) {
66 // Emulate a loopback device for testing. Make sure if an input port that has
67 // the same index exists.
68 if (port_index < next_input_port_index_)
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);
119 }
120
121 } // namespace test_runner
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698