File: mock_web_midi_accessor.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (121 lines) | stat: -rw-r--r-- 4,172 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/test_runner/mock_web_midi_accessor.h"

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/macros.h"
#include "base/strings/stringprintf.h"
#include "components/test_runner/test_interfaces.h"
#include "components/test_runner/test_runner.h"
#include "components/test_runner/web_test_delegate.h"
#include "components/test_runner/web_test_runner.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/platform/modules/webmidi/WebMIDIAccessorClient.h"

using midi::mojom::PortState;
using midi::mojom::Result;

namespace test_runner {

namespace {

constexpr unsigned char kSysexHeader[] = {0xf0, 0x00, 0x02, 0x0d, 0x7f};
constexpr unsigned char kSysexFooter = 0xf7;
constexpr size_t kSysexMinimumLength =
    arraysize(kSysexHeader) + sizeof(kSysexFooter) + 1;

bool isSysexForTesting(const unsigned char* data, size_t length) {
  // It should have five bytes header, one byte footer, and at least one byte
  // payload.
  if (length < kSysexMinimumLength)
    return false;
  if (memcmp(data, kSysexHeader, arraysize(kSysexHeader)))
    return false;
  return data[length - 1] == kSysexFooter;
}

}  // namespace

MockWebMIDIAccessor::MockWebMIDIAccessor(blink::WebMIDIAccessorClient* client,
                                         TestInterfaces* interfaces)
    : client_(client),
      interfaces_(interfaces),
      next_input_port_index_(0),
      next_output_port_index_(0),
      weak_factory_(this) {}

MockWebMIDIAccessor::~MockWebMIDIAccessor() {
}

void MockWebMIDIAccessor::startSession() {
  // Add a mock input and output port.
  addInputPort(PortState::CONNECTED);
  addOutputPort(PortState::CONNECTED);
  interfaces_->GetDelegate()->PostTask(base::Bind(
      &MockWebMIDIAccessor::reportStartedSession, weak_factory_.GetWeakPtr(),
      interfaces_->GetTestRunner()->midiAccessorResult()));
}

void MockWebMIDIAccessor::sendMIDIData(unsigned port_index,
                                       const unsigned char* data,
                                       size_t length,
                                       double timestamp) {
  // Emulate a loopback device for testing. Make sure if an input port that has
  // the same index exists.
  if (port_index < next_input_port_index_)
    client_->didReceiveMIDIData(port_index, data, length, timestamp);

  // Handle special sysex messages for testing.
  // A special sequence is [0xf0, 0x00, 0x02, 0x0d, 0x7f, <function>, 0xf7].
  // <function> should be one of following sequences.
  //  - [0x00, 0x00]: Add an input port as connected.
  //  - [0x00, 0x01]: Add an output port as connected.
  //  - [0x00, 0x02]: Add an input port as opened.
  //  - [0x00, 0x03]: Add an output port as opened.
  if (!isSysexForTesting(data, length))
    return;
  size_t offset = arraysize(kSysexHeader);
  if (data[offset++] != 0)
    return;
  switch (data[offset]) {
    case 0:
      addInputPort(PortState::CONNECTED);
      break;
    case 1:
      addOutputPort(PortState::CONNECTED);
      break;
    case 2:
      addInputPort(PortState::OPENED);
      break;
    case 3:
      addOutputPort(PortState::OPENED);
      break;
    default:
      break;
  }
}

void MockWebMIDIAccessor::addInputPort(PortState state) {
  std::string id =
      base::StringPrintf("MockInputID-%d", next_input_port_index_++);
  client_->didAddInputPort(blink::WebString::fromUTF8(id),
                           "MockInputManufacturer", "MockInputName",
                           "MockInputVersion", state);
}

void MockWebMIDIAccessor::addOutputPort(PortState state) {
  std::string id =
      base::StringPrintf("MockOutputID-%d", next_output_port_index_++);
  client_->didAddOutputPort(blink::WebString::fromUTF8(id),
                            "MockOutputManufacturer", "MockOutputName",
                            "MockOutputVersion", state);
}

void MockWebMIDIAccessor::reportStartedSession(Result result) {
  client_->didStartSession(result);
}

}  // namespace test_runner