Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(328)

Unified Diff: content/browser/gamepad/gamepad_service_unittest.cc

Issue 362123002: Gamepad: don't notify about connected pads twice (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: more build fix (kNumberOfGamepads) Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/browser/gamepad/gamepad_service.cc ('k') | content/browser/gamepad/gamepad_test_helpers.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/browser/gamepad/gamepad_service_unittest.cc
diff --git a/content/browser/gamepad/gamepad_service_unittest.cc b/content/browser/gamepad/gamepad_service_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..c32989fc8345fbdc6400b6c8e77619a927ede249
--- /dev/null
+++ b/content/browser/gamepad/gamepad_service_unittest.cc
@@ -0,0 +1,132 @@
+// Copyright (c) 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/memory/scoped_ptr.h"
+#include "base/run_loop.h"
+#include "content/browser/gamepad/gamepad_consumer.h"
+#include "content/browser/gamepad/gamepad_service.h"
+#include "content/browser/gamepad/gamepad_test_helpers.h"
+#include "content/public/test/test_browser_thread_bundle.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace content {
+
+namespace {
+static const int kNumberOfGamepads = blink::WebGamepads::itemsLengthCap;
+}
+
+using blink::WebGamepads;
+
+class ConnectionListener : public GamepadConsumer {
+ public:
+ ConnectionListener() {
+ ClearCounters();
+ }
+
+ virtual void OnGamepadConnected(
+ unsigned index,
+ const blink::WebGamepad& gamepad) OVERRIDE {
+ connected_counter_++;
+ }
+ virtual void OnGamepadDisconnected(
+ unsigned index,
+ const blink::WebGamepad& gamepad) OVERRIDE {
+ disconnected_counter_++;
+ }
+
+ void ClearCounters() {
+ connected_counter_ = 0;
+ disconnected_counter_ = 0;
+ }
+
+ int connected_counter() const { return connected_counter_; }
+ int disconnected_counter() const { return disconnected_counter_; }
+
+ private:
+ int connected_counter_;
+ int disconnected_counter_;
+};
+
+class GamepadServiceTest : public testing::Test {
+ protected:
+ GamepadServiceTest();
+ virtual ~GamepadServiceTest() OVERRIDE;
+
+ void SetPadsConnected(bool connected);
+ void WaitForData();
+
+ int GetConnectedCounter() const {
+ return connection_listener_->connected_counter();
+ }
+ int GetDisconnectedCounter() const {
+ return connection_listener_->disconnected_counter();
+ }
+
+ virtual void SetUp() OVERRIDE;
+
+ private:
+ MockGamepadDataFetcher* fetcher_;
+ GamepadService* service_;
+ scoped_ptr<ConnectionListener> connection_listener_;
+ TestBrowserThreadBundle browser_thread_;
+ WebGamepads test_data_;
+
+ DISALLOW_COPY_AND_ASSIGN(GamepadServiceTest);
+};
+
+GamepadServiceTest::GamepadServiceTest()
+ : browser_thread_(TestBrowserThreadBundle::IO_MAINLOOP) {
+ memset(&test_data_, 0, sizeof(test_data_));
+
+ // Set it so that we have user gesture.
+ test_data_.items[0].buttonsLength = 1;
+ test_data_.items[0].buttons[0].value = 1.f;
+ test_data_.items[0].buttons[0].pressed = true;
+}
+
+GamepadServiceTest::~GamepadServiceTest() {
+ delete service_;
+}
+
+void GamepadServiceTest::SetUp() {
+ fetcher_ = new MockGamepadDataFetcher(test_data_);
+ service_ = new GamepadService(scoped_ptr<GamepadDataFetcher>(fetcher_));
+ connection_listener_.reset((new ConnectionListener));
+ service_->ConsumerBecameActive(connection_listener_.get());
+}
+
+void GamepadServiceTest::SetPadsConnected(bool connected) {
+ for (int i = 0; i < kNumberOfGamepads; ++i) {
+ test_data_.items[i].connected = connected;
+ }
+ fetcher_->SetTestData(test_data_);
+}
+
+void GamepadServiceTest::WaitForData() {
+ connection_listener_->ClearCounters();
+ fetcher_->WaitForDataReadAndCallbacksIssued();
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(GamepadServiceTest, ConnectionsTest) {
+ WaitForData();
+ EXPECT_EQ(0, GetConnectedCounter());
+ EXPECT_EQ(0, GetDisconnectedCounter());
+
+ SetPadsConnected(true);
+ WaitForData();
+ EXPECT_EQ(kNumberOfGamepads, GetConnectedCounter());
+ EXPECT_EQ(0, GetDisconnectedCounter());
+
+ SetPadsConnected(false);
+ WaitForData();
+ EXPECT_EQ(0, GetConnectedCounter());
+ EXPECT_EQ(kNumberOfGamepads, GetDisconnectedCounter());
+
+ WaitForData();
+ EXPECT_EQ(0, GetConnectedCounter());
+ EXPECT_EQ(0, GetDisconnectedCounter());
+}
+
+} // namespace content
« no previous file with comments | « content/browser/gamepad/gamepad_service.cc ('k') | content/browser/gamepad/gamepad_test_helpers.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698