• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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 #include "base/memory/scoped_ptr.h"
6 #include "base/run_loop.h"
7 #include "content/browser/gamepad/gamepad_consumer.h"
8 #include "content/browser/gamepad/gamepad_service.h"
9 #include "content/browser/gamepad/gamepad_test_helpers.h"
10 #include "content/public/test/test_browser_thread_bundle.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 
13 namespace content {
14 
15 namespace {
16 static const int kNumberOfGamepads = blink::WebGamepads::itemsLengthCap;
17 }
18 
19 using blink::WebGamepads;
20 
21 class ConnectionListener : public GamepadConsumer {
22  public:
ConnectionListener()23   ConnectionListener() {
24     ClearCounters();
25   }
26 
OnGamepadConnected(unsigned index,const blink::WebGamepad & gamepad)27   virtual void OnGamepadConnected(
28       unsigned index,
29       const blink::WebGamepad& gamepad) OVERRIDE {
30     connected_counter_++;
31   }
OnGamepadDisconnected(unsigned index,const blink::WebGamepad & gamepad)32   virtual void OnGamepadDisconnected(
33       unsigned index,
34       const blink::WebGamepad& gamepad) OVERRIDE {
35     disconnected_counter_++;
36   }
37 
ClearCounters()38   void ClearCounters() {
39     connected_counter_ = 0;
40     disconnected_counter_ = 0;
41   }
42 
connected_counter() const43   int connected_counter() const { return connected_counter_; }
disconnected_counter() const44   int disconnected_counter() const { return disconnected_counter_; }
45 
46  private:
47   int connected_counter_;
48   int disconnected_counter_;
49 };
50 
51 class GamepadServiceTest : public testing::Test {
52  protected:
53   GamepadServiceTest();
54   virtual ~GamepadServiceTest() OVERRIDE;
55 
56   void SetPadsConnected(bool connected);
57   void WaitForData();
58 
GetConnectedCounter() const59   int GetConnectedCounter() const {
60     return connection_listener_->connected_counter();
61   }
GetDisconnectedCounter() const62   int GetDisconnectedCounter() const {
63     return connection_listener_->disconnected_counter();
64   }
65 
66   virtual void SetUp() OVERRIDE;
67 
68  private:
69   MockGamepadDataFetcher* fetcher_;
70   GamepadService* service_;
71   scoped_ptr<ConnectionListener> connection_listener_;
72   TestBrowserThreadBundle browser_thread_;
73   WebGamepads test_data_;
74 
75   DISALLOW_COPY_AND_ASSIGN(GamepadServiceTest);
76 };
77 
GamepadServiceTest()78 GamepadServiceTest::GamepadServiceTest()
79     : browser_thread_(TestBrowserThreadBundle::IO_MAINLOOP) {
80   memset(&test_data_, 0, sizeof(test_data_));
81 
82   // Set it so that we have user gesture.
83   test_data_.items[0].buttonsLength = 1;
84   test_data_.items[0].buttons[0].value = 1.f;
85   test_data_.items[0].buttons[0].pressed = true;
86 }
87 
~GamepadServiceTest()88 GamepadServiceTest::~GamepadServiceTest() {
89   delete service_;
90 }
91 
SetUp()92 void GamepadServiceTest::SetUp() {
93   fetcher_ = new MockGamepadDataFetcher(test_data_);
94   service_ = new GamepadService(scoped_ptr<GamepadDataFetcher>(fetcher_));
95   connection_listener_.reset((new ConnectionListener));
96   service_->ConsumerBecameActive(connection_listener_.get());
97 }
98 
SetPadsConnected(bool connected)99 void GamepadServiceTest::SetPadsConnected(bool connected) {
100   for (int i = 0; i < kNumberOfGamepads; ++i) {
101     test_data_.items[i].connected = connected;
102   }
103   fetcher_->SetTestData(test_data_);
104 }
105 
WaitForData()106 void GamepadServiceTest::WaitForData() {
107   connection_listener_->ClearCounters();
108   fetcher_->WaitForDataReadAndCallbacksIssued();
109   base::RunLoop().RunUntilIdle();
110 }
111 
TEST_F(GamepadServiceTest,ConnectionsTest)112 TEST_F(GamepadServiceTest, ConnectionsTest) {
113   WaitForData();
114   EXPECT_EQ(0, GetConnectedCounter());
115   EXPECT_EQ(0, GetDisconnectedCounter());
116 
117   SetPadsConnected(true);
118   WaitForData();
119   EXPECT_EQ(kNumberOfGamepads, GetConnectedCounter());
120   EXPECT_EQ(0, GetDisconnectedCounter());
121 
122   SetPadsConnected(false);
123   WaitForData();
124   EXPECT_EQ(0, GetConnectedCounter());
125   EXPECT_EQ(kNumberOfGamepads, GetDisconnectedCounter());
126 
127   WaitForData();
128   EXPECT_EQ(0, GetConnectedCounter());
129   EXPECT_EQ(0, GetDisconnectedCounter());
130 }
131 
132 }  // namespace content
133