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

Unified Diff: athena/input/input_manager_unittest.cc

Issue 654343002: Intorduce PowerButtonObserver API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: speculative fix Created 6 years, 2 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 | « athena/input/input_manager_impl.cc ('k') | athena/input/power_button_controller.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: athena/input/input_manager_unittest.cc
diff --git a/athena/input/input_manager_unittest.cc b/athena/input/input_manager_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..acc541adc831eab94f7308a7b6af4fb8a1f7f2a0
--- /dev/null
+++ b/athena/input/input_manager_unittest.cc
@@ -0,0 +1,105 @@
+// Copyright 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 "athena/input/input_manager_impl.h"
+
+#include "athena/input/public/accelerator_manager.h"
+#include "athena/test/base/athena_test_base.h"
+#include "base/run_loop.h"
+#include "ui/events/test/event_generator.h"
+
+namespace athena {
+namespace {
+
+class TestPowerButtonObserver : public PowerButtonObserver {
+ public:
+ TestPowerButtonObserver() : count_(0), state_(RELEASED) {
+ InputManager::Get()->AddPowerButtonObserver(this);
+ }
+ virtual ~TestPowerButtonObserver() {
+ InputManager::Get()->RemovePowerButtonObserver(this);
+ }
+
+ int count() const { return count_; }
+ State state() const { return state_; }
+
+ bool WaitForLongPress() {
+ run_loop_.Run();
+ return state_ == LONG_PRESSED;
+ }
+
+ private:
+ virtual void OnPowerButtonStateChanged(
+ PowerButtonObserver::State state) override {
+ state_ = state;
+ count_++;
+ if (state == LONG_PRESSED) {
+ DCHECK(run_loop_.running());
+ run_loop_.Quit();
+ }
+ }
+ base::RunLoop run_loop_;
+ int count_;
+ State state_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestPowerButtonObserver);
+};
+
+} // namespace
+
+namespace test {
+
+class ScopedPowerButtonTimeoutShortener {
+ public:
+ ScopedPowerButtonTimeoutShortener()
+ : original_timeout_(
+ GetInputManagerImpl()->SetPowerButtonTimeoutMsForTest(1)) {}
+ ~ScopedPowerButtonTimeoutShortener() {
+ GetInputManagerImpl()->SetPowerButtonTimeoutMsForTest(original_timeout_);
+ }
+
+ private:
+ InputManagerImpl* GetInputManagerImpl() {
+ return static_cast<InputManagerImpl*>(InputManager::Get());
+ }
+
+ int original_timeout_;
+ DISALLOW_COPY_AND_ASSIGN(ScopedPowerButtonTimeoutShortener);
+};
+
+} // namespace test
+
+typedef test::AthenaTestBase InputManagerTest;
+
+TEST_F(InputManagerTest, PowerButton) {
+ test::ScopedPowerButtonTimeoutShortener shortener;
+ TestPowerButtonObserver observer;
+
+ ui::test::EventGenerator generator(root_window());
+ generator.PressKey(ui::VKEY_P, ui::EF_NONE);
+ EXPECT_EQ(0, observer.count());
+
+ // Test short press.
+ generator.PressKey(ui::VKEY_P, ui::EF_ALT_DOWN);
+ EXPECT_EQ(1, observer.count());
+ EXPECT_EQ(PowerButtonObserver::PRESSED, observer.state());
+ generator.ReleaseKey(ui::VKEY_P, ui::EF_ALT_DOWN);
+ EXPECT_EQ(2, observer.count());
+ EXPECT_EQ(PowerButtonObserver::RELEASED, observer.state());
+
+ // Test long press.
+ generator.PressKey(ui::VKEY_P, ui::EF_ALT_DOWN);
+ EXPECT_EQ(3, observer.count());
+ EXPECT_EQ(PowerButtonObserver::PRESSED, observer.state());
+
+ EXPECT_TRUE(observer.WaitForLongPress());
+ EXPECT_EQ(4, observer.count());
+ EXPECT_EQ(PowerButtonObserver::LONG_PRESSED, observer.state());
+
+ generator.ReleaseKey(ui::VKEY_P, ui::EF_ALT_DOWN);
+ EXPECT_EQ(5, observer.count());
+ EXPECT_EQ(PowerButtonObserver::RELEASED, observer.state());
+}
+
+} // namespace athena
« no previous file with comments | « athena/input/input_manager_impl.cc ('k') | athena/input/power_button_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698