summaryrefslogtreecommitdiffstats
path: root/content/browser/gamepad/gamepad_test_helpers.h
blob: 9abba01fd5fcbae611df7b22190481985791fec0 (plain)
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
// Copyright (c) 2012 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.

#ifndef CONTENT_BROWSER_GAMEPAD_GAMEPAD_TEST_HELPERS_H_
#define CONTENT_BROWSER_GAMEPAD_GAMEPAD_TEST_HELPERS_H_

#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "base/synchronization/lock.h"
#include "base/synchronization/waitable_event.h"
#include "content/browser/gamepad/gamepad_data_fetcher.h"
#include "third_party/WebKit/Source/Platform/chromium/public/WebGamepads.h"

namespace base {
class SystemMonitor;
}

namespace content {

class GamepadService;

// Data fetcher that returns canned data for the gamepad provider.
class MockGamepadDataFetcher : public GamepadDataFetcher {
 public:
  // Initializes the fetcher with the given gamepad data, which will be
  // returned when the provider queries us.
  explicit MockGamepadDataFetcher(const WebKit::WebGamepads& test_data);

  virtual ~MockGamepadDataFetcher();

  // GamepadDataFetcher.
  virtual void GetGamepadData(WebKit::WebGamepads* pads,
                              bool devices_changed_hint) OVERRIDE;

  // Blocks the current thread until the GamepadProvider reads from this
  // fetcher on the background thread.
  void WaitForDataRead();

  // Updates the test data.
  void SetTestData(const WebKit::WebGamepads& new_data);

 private:
  base::Lock lock_;
  WebKit::WebGamepads test_data_;
  base::WaitableEvent read_data_;

  DISALLOW_COPY_AND_ASSIGN(MockGamepadDataFetcher);
};

// Base class for the other test helpers. This just sets up the system monitor.
class GamepadTestHelper {
 public:
  GamepadTestHelper();
  virtual ~GamepadTestHelper();

  MessageLoop& message_loop() { return message_loop_; }

 private:
  // This must be constructed before the system monitor.
  MessageLoop message_loop_;

  scoped_ptr<base::SystemMonitor> system_monitor_;

  DISALLOW_COPY_AND_ASSIGN(GamepadTestHelper);
};

// Constructs a GamepadService with a mock data source. This bypasses the
// global singleton for the gamepad service.
class GamepadServiceTestConstructor : public GamepadTestHelper {
 public:
  explicit GamepadServiceTestConstructor(const WebKit::WebGamepads& test_data);
  virtual ~GamepadServiceTestConstructor();

  GamepadService* gamepad_service() { return gamepad_service_; }
  MockGamepadDataFetcher* data_fetcher() { return data_fetcher_; }

 private:
  // Owning pointer (can't be a scoped_ptr due to private destructor).
  GamepadService* gamepad_service_;

  // Pointer owned by the provider (which is owned by the gamepad service).
  MockGamepadDataFetcher* data_fetcher_;

  DISALLOW_COPY_AND_ASSIGN(GamepadServiceTestConstructor);
};

}  // namespace content

#endif  // CONTENT_BROWSER_GAMEPAD_GAMEPAD_TEST_HELPERS_H_