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#ifndef CONTENT_SHELL_RENDERER_TEST_RUNNER_GAMEPAD_CONTROLLER_H_
6#define CONTENT_SHELL_RENDERER_TEST_RUNNER_GAMEPAD_CONTROLLER_H_
7
8#include <map>
9
10#include "base/memory/weak_ptr.h"
11#include "content/public/renderer/renderer_gamepad_provider.h"
12#include "third_party/WebKit/public/platform/WebGamepads.h"
13
14namespace blink {
15class WebFrame;
16class WebGamepadListener;
17}
18
19namespace content {
20
21class WebTestDelegate;
22
23class GamepadController
24    : public base::SupportsWeakPtr<GamepadController>,
25      public RendererGamepadProvider {
26 public:
27  static base::WeakPtr<GamepadController> Create(WebTestDelegate* delegate);
28  virtual ~GamepadController();
29
30  void Reset();
31  void Install(blink::WebFrame* frame);
32
33  // RendererGamepadProvider implementation.
34  virtual void SampleGamepads(
35      blink::WebGamepads& gamepads) OVERRIDE;
36  virtual bool OnControlMessageReceived(const IPC::Message& msg) OVERRIDE;
37  virtual void SendStartMessage() OVERRIDE;
38  virtual void SendStopMessage() OVERRIDE;
39
40 private:
41  friend class GamepadControllerBindings;
42  GamepadController();
43
44  // TODO(b.kelemen): for historical reasons Connect just initializes the
45  // object. The 'gamepadconnected' event will be dispatched via
46  // DispatchConnected. Tests for connected events need to first connect(),
47  // then set the gamepad data and finally call dispatchConnected().
48  // We should consider renaming Connect to Init and DispatchConnected to
49  // Connect and at the same time updating all the gamepad tests.
50  void Connect(int index);
51  void DispatchConnected(int index);
52
53  void Disconnect(int index);
54  void SetId(int index, const std::string& src);
55  void SetButtonCount(int index, int buttons);
56  void SetButtonData(int index, int button, double data);
57  void SetAxisCount(int index, int axes);
58  void SetAxisData(int index, int axis, double data);
59
60  blink::WebGamepads gamepads_;
61
62  // Mapping from gamepad index to connection state.
63  std::map<int, bool> pending_changes_;
64
65  base::WeakPtrFactory<GamepadController> weak_factory_;
66
67  DISALLOW_COPY_AND_ASSIGN(GamepadController);
68};
69
70}  // namespace content
71
72#endif  // CONTENT_SHELL_RENDERER_TEST_RUNNER_GAMEPAD_CONTROLLER_H_
73