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