OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef DEVICE_GAMEPAD_GAMEPAD_TEST_HELPERS_H_ | 5 #ifndef DEVICE_GAMEPAD_GAMEPAD_TEST_HELPERS_H_ |
6 #define DEVICE_GAMEPAD_GAMEPAD_TEST_HELPERS_H_ | 6 #define DEVICE_GAMEPAD_GAMEPAD_TEST_HELPERS_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
13 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
14 #include "device/gamepad/gamepad_data_fetcher.h" | 14 #include "device/gamepad/gamepad_data_fetcher.h" |
15 #include "device/gamepad/gamepad_service.h" | 15 #include "device/gamepad/gamepad_service.h" |
16 #include "device/gamepad/gamepad_shared_buffer.h" | 16 #include "device/gamepad/gamepad_shared_buffer.h" |
17 #include "third_party/WebKit/public/platform/WebGamepads.h" | 17 #include "device/gamepad/public/cpp/gamepads.h" |
18 | 18 |
19 namespace device { | 19 namespace device { |
20 | 20 |
21 // Data fetcher that returns canned data for the gamepad provider. | 21 // Data fetcher that returns canned data for the gamepad provider. |
22 class MockGamepadDataFetcher : public GamepadDataFetcher { | 22 class MockGamepadDataFetcher : public GamepadDataFetcher { |
23 public: | 23 public: |
24 // Initializes the fetcher with the given gamepad data, which will be | 24 // Initializes the fetcher with the given gamepad data, which will be |
25 // returned when the provider queries us. | 25 // returned when the provider queries us. |
26 explicit MockGamepadDataFetcher(const blink::WebGamepads& test_data); | 26 explicit MockGamepadDataFetcher(const Gamepads& test_data); |
27 | 27 |
28 ~MockGamepadDataFetcher() override; | 28 ~MockGamepadDataFetcher() override; |
29 | 29 |
30 GamepadSource source() override; | 30 GamepadSource source() override; |
31 | 31 |
32 // GamepadDataFetcher. | 32 // GamepadDataFetcher. |
33 void GetGamepadData(bool devices_changed_hint) override; | 33 void GetGamepadData(bool devices_changed_hint) override; |
34 | 34 |
35 // Blocks the current thread until the GamepadProvider reads from this | 35 // Blocks the current thread until the GamepadProvider reads from this |
36 // fetcher on the background thread. | 36 // fetcher on the background thread. |
37 void WaitForDataRead(); | 37 void WaitForDataRead(); |
38 | 38 |
39 // Blocks the current thread until the GamepadProvider reads from this | 39 // Blocks the current thread until the GamepadProvider reads from this |
40 // fetcher on the background thread and issued all callbacks related to the | 40 // fetcher on the background thread and issued all callbacks related to the |
41 // read on the client's thread. | 41 // read on the client's thread. |
42 void WaitForDataReadAndCallbacksIssued(); | 42 void WaitForDataReadAndCallbacksIssued(); |
43 | 43 |
44 // Updates the test data. | 44 // Updates the test data. |
45 void SetTestData(const blink::WebGamepads& new_data); | 45 void SetTestData(const Gamepads& new_data); |
46 | 46 |
47 private: | 47 private: |
48 base::Lock lock_; | 48 base::Lock lock_; |
49 blink::WebGamepads test_data_; | 49 Gamepads test_data_; |
50 base::WaitableEvent read_data_; | 50 base::WaitableEvent read_data_; |
51 | 51 |
52 DISALLOW_COPY_AND_ASSIGN(MockGamepadDataFetcher); | 52 DISALLOW_COPY_AND_ASSIGN(MockGamepadDataFetcher); |
53 }; | 53 }; |
54 | 54 |
55 // Base class for the other test helpers. This just sets up the system monitor. | 55 // Base class for the other test helpers. This just sets up the system monitor. |
56 class GamepadTestHelper { | 56 class GamepadTestHelper { |
57 public: | 57 public: |
58 GamepadTestHelper(); | 58 GamepadTestHelper(); |
59 virtual ~GamepadTestHelper(); | 59 virtual ~GamepadTestHelper(); |
60 | 60 |
61 base::MessageLoop& message_loop() { return message_loop_; } | 61 base::MessageLoop& message_loop() { return message_loop_; } |
62 | 62 |
63 private: | 63 private: |
64 // This must be constructed before the system monitor. | 64 // This must be constructed before the system monitor. |
65 base::MessageLoop message_loop_; | 65 base::MessageLoop message_loop_; |
66 | 66 |
67 DISALLOW_COPY_AND_ASSIGN(GamepadTestHelper); | 67 DISALLOW_COPY_AND_ASSIGN(GamepadTestHelper); |
68 }; | 68 }; |
69 | 69 |
70 // Constructs a GamepadService with a mock data source. This bypasses the | 70 // Constructs a GamepadService with a mock data source. This bypasses the |
71 // global singleton for the gamepad service. | 71 // global singleton for the gamepad service. |
72 class GamepadServiceTestConstructor : public GamepadTestHelper { | 72 class GamepadServiceTestConstructor : public GamepadTestHelper { |
73 public: | 73 public: |
74 explicit GamepadServiceTestConstructor(const blink::WebGamepads& test_data); | 74 explicit GamepadServiceTestConstructor(const Gamepads& test_data); |
75 ~GamepadServiceTestConstructor() override; | 75 ~GamepadServiceTestConstructor() override; |
76 | 76 |
77 GamepadService* gamepad_service() { return gamepad_service_; } | 77 GamepadService* gamepad_service() { return gamepad_service_; } |
78 MockGamepadDataFetcher* data_fetcher() { return data_fetcher_; } | 78 MockGamepadDataFetcher* data_fetcher() { return data_fetcher_; } |
79 | 79 |
80 private: | 80 private: |
81 // Owning pointer (can't be a scoped_ptr due to private destructor). | 81 // Owning pointer (can't be a scoped_ptr due to private destructor). |
82 GamepadService* gamepad_service_; | 82 GamepadService* gamepad_service_; |
83 | 83 |
84 // Pointer owned by the provider (which is owned by the gamepad service). | 84 // Pointer owned by the provider (which is owned by the gamepad service). |
85 MockGamepadDataFetcher* data_fetcher_; | 85 MockGamepadDataFetcher* data_fetcher_; |
86 | 86 |
87 DISALLOW_COPY_AND_ASSIGN(GamepadServiceTestConstructor); | 87 DISALLOW_COPY_AND_ASSIGN(GamepadServiceTestConstructor); |
88 }; | 88 }; |
89 | 89 |
90 } // namespace device | 90 } // namespace device |
91 | 91 |
92 #endif // DEVICE_GAMEPAD_GAMEPAD_TEST_HELPERS_H_ | 92 #endif // DEVICE_GAMEPAD_GAMEPAD_TEST_HELPERS_H_ |
OLD | NEW |