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