Commit ecd097ee authored by Tommy Steimel's avatar Tommy Steimel Committed by Commit Bot

Idle: Add ScopedSetIdleState to make ui/base/idle more test-friendly

This CL adds a new ScopedSetIdleState class for testing that sets an
overriding idle state in ui/base/idle so that classes depending on
ui/base/idle can be more easily tested.

Bug: 951560
Change-Id: I4234af0783c7fc053620803b74f0ef6b0fc0ce4c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1643414Reviewed-by: default avatarScott Violet <sky@chromium.org>
Commit-Queue: Tommy Steimel <steimel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#666444}
parent af90fcdc
...@@ -13,6 +13,13 @@ component("idle") { ...@@ -13,6 +13,13 @@ component("idle") {
defines = [ "IS_UI_BASE_IDLE_IMPL" ] defines = [ "IS_UI_BASE_IDLE_IMPL" ]
# All targets in this file are allowed to access any of the headers.
friend = [ ":*" ]
public = [
"idle.h",
]
deps = [ deps = [
"//base", "//base",
"//ui/base", "//ui/base",
...@@ -26,6 +33,8 @@ component("idle") { ...@@ -26,6 +33,8 @@ component("idle") {
"idle.cc", "idle.cc",
"idle.h", "idle.h",
"idle_chromeos.cc", "idle_chromeos.cc",
"idle_internal.cc",
"idle_internal.h",
"idle_mac.mm", "idle_mac.mm",
"idle_win.cc", "idle_win.cc",
] ]
...@@ -66,3 +75,17 @@ component("idle") { ...@@ -66,3 +75,17 @@ component("idle") {
] ]
} }
} }
static_library("test_support") {
testonly = true
sources = [
"scoped_set_idle_state.cc",
"scoped_set_idle_state.h",
]
deps = [
":idle",
"//base",
]
}
...@@ -4,9 +4,14 @@ ...@@ -4,9 +4,14 @@
#include "ui/base/idle/idle.h" #include "ui/base/idle/idle.h"
#include "ui/base/idle/idle_internal.h"
namespace ui { namespace ui {
IdleState CalculateIdleState(int idle_threshold) { IdleState CalculateIdleState(int idle_threshold) {
if (IdleStateForTesting().has_value())
return IdleStateForTesting().value();
if (CheckIdleStateIsLocked()) if (CheckIdleStateIsLocked())
return IDLE_STATE_LOCKED; return IDLE_STATE_LOCKED;
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/singleton.h" #include "base/memory/singleton.h"
#include "jni/IdleDetector_jni.h" #include "jni/IdleDetector_jni.h"
#include "ui/base/idle/idle_internal.h"
using base::android::AttachCurrentThread; using base::android::AttachCurrentThread;
using base::android::ConvertJavaStringToUTF8; using base::android::ConvertJavaStringToUTF8;
...@@ -71,6 +72,9 @@ int CalculateIdleTime() { ...@@ -71,6 +72,9 @@ int CalculateIdleTime() {
} }
bool CheckIdleStateIsLocked() { bool CheckIdleStateIsLocked() {
if (IdleStateForTesting().has_value())
return IdleStateForTesting().value() == IDLE_STATE_LOCKED;
return AndroidIdleMonitor::GetInstance()->CheckIdleStateIsLocked(); return AndroidIdleMonitor::GetInstance()->CheckIdleStateIsLocked();
} }
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "base/time/time.h" #include "base/time/time.h"
#include "chromeos/dbus/session_manager/session_manager_client.h" #include "chromeos/dbus/session_manager/session_manager_client.h"
#include "ui/base/idle/idle_internal.h"
#include "ui/base/user_activity/user_activity_detector.h" #include "ui/base/user_activity/user_activity_detector.h"
namespace ui { namespace ui {
...@@ -17,6 +18,9 @@ int CalculateIdleTime() { ...@@ -17,6 +18,9 @@ int CalculateIdleTime() {
} }
bool CheckIdleStateIsLocked() { bool CheckIdleStateIsLocked() {
if (IdleStateForTesting().has_value())
return IdleStateForTesting().value() == IDLE_STATE_LOCKED;
return chromeos::SessionManagerClient::Get()->IsScreenLocked(); return chromeos::SessionManagerClient::Get()->IsScreenLocked();
} }
......
// Copyright 2019 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 "ui/base/idle/idle_internal.h"
#include "base/no_destructor.h"
namespace ui {
base::Optional<IdleState>& IdleStateForTesting() {
static base::NoDestructor<base::Optional<IdleState>> idle_state;
return *idle_state;
}
} // namespace ui
// Copyright 2019 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.
#ifndef UI_BASE_IDLE_IDLE_INTERNAL_H_
#define UI_BASE_IDLE_IDLE_INTERNAL_H_
#include "base/component_export.h"
#include "base/optional.h"
#include "ui/base/idle/idle.h"
namespace ui {
// An optional idle state set by tests via a ScopedSetIdleState to override the
// actual idle state of the system.
COMPONENT_EXPORT(UI_BASE_IDLE) base::Optional<IdleState>& IdleStateForTesting();
} // namespace ui
#endif // UI_BASE_IDLE_IDLE_INTERNAL_H_
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "ui/base/idle/idle.h" #include "ui/base/idle/idle.h"
#include "ui/base/idle/idle_internal.h"
#if defined(USE_X11) #if defined(USE_X11)
#include "ui/base/idle/idle_query_x11.h" #include "ui/base/idle/idle_query_x11.h"
#include "ui/base/idle/screensaver_window_finder_x11.h" #include "ui/base/idle/screensaver_window_finder_x11.h"
...@@ -21,6 +23,9 @@ int CalculateIdleTime() { ...@@ -21,6 +23,9 @@ int CalculateIdleTime() {
} }
bool CheckIdleStateIsLocked() { bool CheckIdleStateIsLocked() {
if (IdleStateForTesting().has_value())
return IdleStateForTesting().value() == IDLE_STATE_LOCKED;
#if defined(USE_X11) #if defined(USE_X11)
// Usually the screensaver is used to lock the screen. // Usually the screensaver is used to lock the screen.
return ScreensaverWindowFinder::ScreensaverWindowExists(); return ScreensaverWindowFinder::ScreensaverWindowExists();
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
#include <ApplicationServices/ApplicationServices.h> #include <ApplicationServices/ApplicationServices.h>
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#include "ui/base/idle/idle_internal.h"
@interface MacScreenMonitor : NSObject { @interface MacScreenMonitor : NSObject {
@private @private
BOOL screensaverRunning_; BOOL screensaverRunning_;
...@@ -92,6 +94,9 @@ int CalculateIdleTime() { ...@@ -92,6 +94,9 @@ int CalculateIdleTime() {
} }
bool CheckIdleStateIsLocked() { bool CheckIdleStateIsLocked() {
if (IdleStateForTesting().has_value())
return IdleStateForTesting().value() == IDLE_STATE_LOCKED;
return [g_screenMonitor isScreensaverRunning] || return [g_screenMonitor isScreensaverRunning] ||
[g_screenMonitor isScreenLocked]; [g_screenMonitor isScreenLocked];
} }
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <limits.h> #include <limits.h>
#include <windows.h> #include <windows.h>
#include "ui/base/idle/idle_internal.h"
#include "ui/base/win/lock_state.h" #include "ui/base/win/lock_state.h"
namespace ui { namespace ui {
...@@ -51,6 +52,9 @@ int CalculateIdleTime() { ...@@ -51,6 +52,9 @@ int CalculateIdleTime() {
} }
bool CheckIdleStateIsLocked() { bool CheckIdleStateIsLocked() {
if (IdleStateForTesting().has_value())
return IdleStateForTesting().value() == IDLE_STATE_LOCKED;
return ui::IsWorkstationLocked() || IsScreensaverRunning(); return ui::IsWorkstationLocked() || IsScreensaverRunning();
} }
......
// Copyright 2019 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 "ui/base/idle/scoped_set_idle_state.h"
#include "ui/base/idle/idle_internal.h"
namespace ui {
ScopedSetIdleState::ScopedSetIdleState(IdleState state)
: previous_state_(IdleStateForTesting()) {
IdleStateForTesting() = state;
}
ScopedSetIdleState::~ScopedSetIdleState() {
IdleStateForTesting() = previous_state_;
}
} // namespace ui
// Copyright 2019 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.
#ifndef UI_BASE_IDLE_SCOPED_SET_IDLE_STATE_H_
#define UI_BASE_IDLE_SCOPED_SET_IDLE_STATE_H_
#include "base/optional.h"
#include "ui/base/idle/idle.h"
namespace ui {
class ScopedSetIdleState {
public:
explicit ScopedSetIdleState(IdleState state);
~ScopedSetIdleState();
private:
base::Optional<IdleState> previous_state_;
DISALLOW_COPY_AND_ASSIGN(ScopedSetIdleState);
};
} // namespace ui
#endif // UI_BASE_IDLE_SCOPED_SET_IDLE_STATE_H_
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment