Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / gamepad / gamepad_test_helpers.h
blobdbaa96649122b91b9df37d40eefdda819c393e1d
1 // Copyright (c) 2012 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.
5 #ifndef CONTENT_BROWSER_GAMEPAD_GAMEPAD_TEST_HELPERS_H_
6 #define CONTENT_BROWSER_GAMEPAD_GAMEPAD_TEST_HELPERS_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/synchronization/lock.h"
11 #include "base/synchronization/waitable_event.h"
12 #include "content/browser/gamepad/gamepad_data_fetcher.h"
13 #include "third_party/WebKit/public/platform/WebGamepads.h"
15 namespace content {
17 class GamepadService;
19 // Data fetcher that returns canned data for the gamepad provider.
20 class MockGamepadDataFetcher : public GamepadDataFetcher {
21 public:
22 // Initializes the fetcher with the given gamepad data, which will be
23 // returned when the provider queries us.
24 explicit MockGamepadDataFetcher(const blink::WebGamepads& test_data);
26 ~MockGamepadDataFetcher() override;
28 // GamepadDataFetcher.
29 void GetGamepadData(blink::WebGamepads* pads,
30 bool devices_changed_hint) override;
32 // Blocks the current thread until the GamepadProvider reads from this
33 // fetcher on the background thread.
34 void WaitForDataRead();
36 // Blocks the current thread until the GamepadProvider reads from this
37 // fetcher on the background thread and issued all callbacks related to the
38 // read on the client's thread.
39 void WaitForDataReadAndCallbacksIssued();
41 // Updates the test data.
42 void SetTestData(const blink::WebGamepads& new_data);
44 private:
45 base::Lock lock_;
46 blink::WebGamepads test_data_;
47 base::WaitableEvent read_data_;
49 DISALLOW_COPY_AND_ASSIGN(MockGamepadDataFetcher);
52 // Base class for the other test helpers. This just sets up the system monitor.
53 class GamepadTestHelper {
54 public:
55 GamepadTestHelper();
56 virtual ~GamepadTestHelper();
58 base::MessageLoop& message_loop() { return message_loop_; }
60 private:
61 // This must be constructed before the system monitor.
62 base::MessageLoop message_loop_;
64 DISALLOW_COPY_AND_ASSIGN(GamepadTestHelper);
67 // Constructs a GamepadService with a mock data source. This bypasses the
68 // global singleton for the gamepad service.
69 class GamepadServiceTestConstructor : public GamepadTestHelper {
70 public:
71 explicit GamepadServiceTestConstructor(const blink::WebGamepads& test_data);
72 ~GamepadServiceTestConstructor() override;
74 GamepadService* gamepad_service() { return gamepad_service_; }
75 MockGamepadDataFetcher* data_fetcher() { return data_fetcher_; }
77 private:
78 // Owning pointer (can't be a scoped_ptr due to private destructor).
79 GamepadService* gamepad_service_;
81 // Pointer owned by the provider (which is owned by the gamepad service).
82 MockGamepadDataFetcher* data_fetcher_;
84 DISALLOW_COPY_AND_ASSIGN(GamepadServiceTestConstructor);
87 } // namespace content
89 #endif // CONTENT_BROWSER_GAMEPAD_GAMEPAD_TEST_HELPERS_H_