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  GamepadController();
28  virtual ~GamepadController();
29
30  void Reset();
31  void Install(blink::WebFrame* frame);
32  void SetDelegate(WebTestDelegate* delegate);
33
34  // RendererGamepadProvider implementation.
35  virtual void SampleGamepads(
36      blink::WebGamepads& gamepads) OVERRIDE;
37  virtual void SetGamepadListener(
38      blink::WebGamepadListener* listener) OVERRIDE;
39
40 private:
41  friend class GamepadControllerBindings;
42
43  // TODO(b.kelemen): for historical reasons Connect just initializes the
44  // object. The 'gamepadconnected' event will be dispatched via
45  // DispatchConnected. Tests for connected events need to first connect(),
46  // then set the gamepad data and finally call dispatchConnected().
47  // We should consider renaming Connect to Init and DispatchConnected to
48  // Connect and at the same time updating all the gamepad tests.
49  void Connect(int index);
50  void DispatchConnected(int index);
51
52  void Disconnect(int index);
53  void SetId(int index, const std::string& src);
54  void SetButtonCount(int index, int buttons);
55  void SetButtonData(int index, int button, double data);
56  void SetAxisCount(int index, int axes);
57  void SetAxisData(int index, int axis, double data);
58
59  blink::WebGamepads gamepads_;
60
61  blink::WebGamepadListener* listener_;
62
63  // Mapping from gamepad index to connection state.
64  std::map<int, bool> pending_changes_;
65
66  base::WeakPtrFactory<GamepadController> weak_factory_;
67
68  DISALLOW_COPY_AND_ASSIGN(GamepadController);
69};
70
71}  // namespace content
72
73#endif  // CONTENT_SHELL_RENDERER_TEST_RUNNER_GAMEPAD_CONTROLLER_H_
74