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