OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 /* |
| 6 * Copyright (C) 2011 Google Inc. All rights reserved. |
| 7 * |
| 8 * Redistribution and use in source and binary forms, with or without |
| 9 * modification, are permitted provided that the following conditions are |
| 10 * met: |
| 11 * |
| 12 * * Redistributions of source code must retain the above copyright |
| 13 * notice, this list of conditions and the following disclaimer. |
| 14 * * Redistributions in binary form must reproduce the above |
| 15 * copyright notice, this list of conditions and the following disclaimer |
| 16 * in the documentation and/or other materials provided with the |
| 17 * distribution. |
| 18 * * Neither the name of Google Inc. nor the names of its |
| 19 * contributors may be used to endorse or promote products derived from |
| 20 * this software without specific prior written permission. |
| 21 * |
| 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 */ |
| 34 |
| 35 #include "content/test/layout_tests/runner/GamepadController.h" |
| 36 |
| 37 #include "content/public/test/layout_tests/WebTestDelegate.h" |
| 38 |
| 39 using namespace blink; |
| 40 |
| 41 namespace WebTestRunner { |
| 42 |
| 43 GamepadController::GamepadController() |
| 44 { |
| 45 bindMethod("connect", &GamepadController::connect); |
| 46 bindMethod("disconnect", &GamepadController::disconnect); |
| 47 bindMethod("setId", &GamepadController::setId); |
| 48 bindMethod("setButtonCount", &GamepadController::setButtonCount); |
| 49 bindMethod("setButtonData", &GamepadController::setButtonData); |
| 50 bindMethod("setAxisCount", &GamepadController::setAxisCount); |
| 51 bindMethod("setAxisData", &GamepadController::setAxisData); |
| 52 |
| 53 bindFallbackMethod(&GamepadController::fallbackCallback); |
| 54 |
| 55 reset(); |
| 56 } |
| 57 |
| 58 void GamepadController::bindToJavascript(WebFrame* frame, const WebString& class
name) |
| 59 { |
| 60 CppBoundClass::bindToJavascript(frame, classname); |
| 61 } |
| 62 |
| 63 void GamepadController::setDelegate(WebTestDelegate* delegate) |
| 64 { |
| 65 m_delegate = delegate; |
| 66 } |
| 67 |
| 68 void GamepadController::reset() |
| 69 { |
| 70 memset(&m_gamepads, 0, sizeof(m_gamepads)); |
| 71 } |
| 72 |
| 73 void GamepadController::connect(const CppArgumentList& args, CppVariant* result) |
| 74 { |
| 75 if (args.size() < 1) { |
| 76 m_delegate->printMessage("Invalid args"); |
| 77 return; |
| 78 } |
| 79 int index = args[0].toInt32(); |
| 80 if (index < 0 || index >= static_cast<int>(blink::WebGamepads::itemsLengthCa
p)) |
| 81 return; |
| 82 m_gamepads.items[index].connected = true; |
| 83 m_gamepads.length = 0; |
| 84 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) |
| 85 if (m_gamepads.items[i].connected) |
| 86 m_gamepads.length = i + 1; |
| 87 m_delegate->setGamepadData(m_gamepads); |
| 88 result->setNull(); |
| 89 } |
| 90 |
| 91 void GamepadController::disconnect(const CppArgumentList& args, CppVariant* resu
lt) |
| 92 { |
| 93 if (args.size() < 1) { |
| 94 m_delegate->printMessage("Invalid args"); |
| 95 return; |
| 96 } |
| 97 int index = args[0].toInt32(); |
| 98 if (index < 0 || index >= static_cast<int>(blink::WebGamepads::itemsLengthCa
p)) |
| 99 return; |
| 100 m_gamepads.items[index].connected = false; |
| 101 m_gamepads.length = 0; |
| 102 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) |
| 103 if (m_gamepads.items[i].connected) |
| 104 m_gamepads.length = i + 1; |
| 105 m_delegate->setGamepadData(m_gamepads); |
| 106 result->setNull(); |
| 107 } |
| 108 |
| 109 void GamepadController::setId(const CppArgumentList& args, CppVariant* result) |
| 110 { |
| 111 if (args.size() < 2) { |
| 112 m_delegate->printMessage("Invalid args"); |
| 113 return; |
| 114 } |
| 115 int index = args[0].toInt32(); |
| 116 if (index < 0 || index >= static_cast<int>(blink::WebGamepads::itemsLengthCa
p)) |
| 117 return; |
| 118 std::string src = args[1].toString(); |
| 119 const char* p = src.c_str(); |
| 120 memset(m_gamepads.items[index].id, 0, sizeof(m_gamepads.items[index].id)); |
| 121 for (unsigned i = 0; *p && i < blink::WebGamepad::idLengthCap - 1; ++i) |
| 122 m_gamepads.items[index].id[i] = *p++; |
| 123 m_delegate->setGamepadData(m_gamepads); |
| 124 result->setNull(); |
| 125 } |
| 126 |
| 127 void GamepadController::setButtonCount(const CppArgumentList& args, CppVariant*
result) |
| 128 { |
| 129 if (args.size() < 2) { |
| 130 m_delegate->printMessage("Invalid args"); |
| 131 return; |
| 132 } |
| 133 int index = args[0].toInt32(); |
| 134 if (index < 0 || index >= static_cast<int>(blink::WebGamepads::itemsLengthCa
p)) |
| 135 return; |
| 136 int buttons = args[1].toInt32(); |
| 137 if (buttons < 0 || buttons >= static_cast<int>(blink::WebGamepad::buttonsLen
gthCap)) |
| 138 return; |
| 139 m_gamepads.items[index].buttonsLength = buttons; |
| 140 m_delegate->setGamepadData(m_gamepads); |
| 141 result->setNull(); |
| 142 } |
| 143 |
| 144 void GamepadController::setButtonData(const CppArgumentList& args, CppVariant* r
esult) |
| 145 { |
| 146 if (args.size() < 3) { |
| 147 m_delegate->printMessage("Invalid args"); |
| 148 return; |
| 149 } |
| 150 int index = args[0].toInt32(); |
| 151 if (index < 0 || index >= static_cast<int>(blink::WebGamepads::itemsLengthCa
p)) |
| 152 return; |
| 153 int button = args[1].toInt32(); |
| 154 if (button < 0 || button >= static_cast<int>(blink::WebGamepad::buttonsLengt
hCap)) |
| 155 return; |
| 156 double data = args[2].toDouble(); |
| 157 m_gamepads.items[index].buttons[button] = data; |
| 158 m_delegate->setGamepadData(m_gamepads); |
| 159 result->setNull(); |
| 160 } |
| 161 |
| 162 void GamepadController::setAxisCount(const CppArgumentList& args, CppVariant* re
sult) |
| 163 { |
| 164 if (args.size() < 2) { |
| 165 m_delegate->printMessage("Invalid args"); |
| 166 return; |
| 167 } |
| 168 int index = args[0].toInt32(); |
| 169 if (index < 0 || index >= static_cast<int>(blink::WebGamepads::itemsLengthCa
p)) |
| 170 return; |
| 171 int axes = args[1].toInt32(); |
| 172 if (axes < 0 || axes >= static_cast<int>(blink::WebGamepad::axesLengthCap)) |
| 173 return; |
| 174 m_gamepads.items[index].axesLength = axes; |
| 175 m_delegate->setGamepadData(m_gamepads); |
| 176 result->setNull(); |
| 177 } |
| 178 |
| 179 void GamepadController::setAxisData(const CppArgumentList& args, CppVariant* res
ult) |
| 180 { |
| 181 if (args.size() < 3) { |
| 182 m_delegate->printMessage("Invalid args"); |
| 183 return; |
| 184 } |
| 185 int index = args[0].toInt32(); |
| 186 if (index < 0 || index >= static_cast<int>(blink::WebGamepads::itemsLengthCa
p)) |
| 187 return; |
| 188 int axis = args[1].toInt32(); |
| 189 if (axis < 0 || axis >= static_cast<int>(blink::WebGamepad::axesLengthCap)) |
| 190 return; |
| 191 double data = args[2].toDouble(); |
| 192 m_gamepads.items[index].axes[axis] = data; |
| 193 m_delegate->setGamepadData(m_gamepads); |
| 194 result->setNull(); |
| 195 } |
| 196 |
| 197 void GamepadController::fallbackCallback(const CppArgumentList&, CppVariant* res
ult) |
| 198 { |
| 199 m_delegate->printMessage("CONSOLE MESSAGE: JavaScript ERROR: unknown method
called on GamepadController\n"); |
| 200 result->setNull(); |
| 201 } |
| 202 |
| 203 } |
OLD | NEW |