Commit a3014e00 authored by Jeffrey Young's avatar Jeffrey Young Committed by Commit Bot

ambient: make ui timeouts configurable by prefs

Used for automating ambient mode tast tests.
Add prefs for configuring lock screen inactivity timeout
and background lock screen timeout after ambient mode starts.

BUG=b:169579102

Cq-Include-Trybots: luci.chrome.try:linux-chromeos-chrome
Change-Id: I2040ea5a0519b985ad246132925815d87754f7e0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2485754
Commit-Queue: Jeffrey Young <cowmoo@chromium.org>
Reviewed-by: default avatarTao Wu <wutao@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Reviewed-by: default avatarXiaohui Chen <xiaohuic@chromium.org>
Cr-Commit-Position: refs/heads/master@{#822714}
parent fb59d309
......@@ -32,9 +32,6 @@ constexpr base::TimeDelta kBackupPhotoRefreshDelay =
constexpr base::TimeDelta kWeatherRefreshInterval =
base::TimeDelta::FromMinutes(5);
// The delay between ambient mode starts and enabling lock screen.
constexpr base::TimeDelta kLockScreenDelay = base::TimeDelta::FromSeconds(5);
// The batch size of topics to fetch in one request.
constexpr int kTopicsBatchSize = 100;
......
......@@ -38,6 +38,7 @@
#include "chromeos/dbus/power_manager/backlight.pb.h"
#include "chromeos/dbus/power_manager/idle.pb.h"
#include "chromeos/services/assistant/public/cpp/assistant_service.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "ui/aura/client/aura_constants.h"
......@@ -108,13 +109,17 @@ bool IsUiHidden(AmbientUiVisibility visibility) {
return visibility == AmbientUiVisibility::kHidden;
}
PrefService* GetPrimaryUserPrefService() {
return Shell::Get()->session_controller()->GetPrimaryUserPrefService();
}
bool IsAmbientModeEnabled() {
if (!AmbientClient::Get()->IsAmbientModeAllowed())
return false;
PrefService* prefs =
Shell::Get()->session_controller()->GetPrimaryUserPrefService();
return prefs && prefs->GetBoolean(ambient::prefs::kAmbientModeEnabled);
auto* pref_service = GetPrimaryUserPrefService();
return pref_service &&
pref_service->GetBoolean(ambient::prefs::kAmbientModeEnabled);
}
class AmbientWidgetDelegate : public views::WidgetDelegate {
......@@ -141,6 +146,20 @@ void AmbientController::RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterIntegerPref(
ash::ambient::prefs::kAmbientModePhotoSourcePref,
static_cast<int>(ash::ambient::AmbientModePhotoSource::kUnset));
// Used to control the number of seconds of inactivity on lock screen before
// showing Ambient mode. This pref is not displayed to the user. Registered
// as integer rather than TimeDelta to work with prefs_util.
registry->RegisterIntegerPref(
ambient::prefs::kAmbientModeLockScreenInactivityTimeoutSeconds,
kLockScreenInactivityTimeout.InSeconds());
// Used to control the number of seconds to lock the session after starting
// Ambient mode. This pref is not displayed to the user. Registered as
// integer rather than TimeDelta to work with prefs_util.
registry->RegisterIntegerPref(
ambient::prefs::kAmbientModeLockScreenBackgroundTimeoutSeconds,
kLockScreenBackgroundTimeout.InSeconds());
}
}
......@@ -242,7 +261,7 @@ void AmbientController::OnAmbientUiVisibilityChanged(
// Start timer to show ambient mode.
inactivity_timer_.Start(
FROM_HERE, kAutoShowWaitTimeInterval,
FROM_HERE, ambient_ui_model_.lock_screen_inactivity_timeout(),
base::BindOnce(&AmbientController::OnAutoShowTimeOut,
weak_ptr_factory_.GetWeakPtr()));
}
......@@ -318,6 +337,31 @@ void AmbientController::OnFirstSessionStarted() {
ambient_photo_controller_.ScheduleFetchBackupImages();
}
void AmbientController::OnActiveUserPrefServiceChanged(
PrefService* pref_service) {
if (!IsAmbientModeEnabled() || GetPrimaryUserPrefService() != pref_service)
return;
pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>();
pref_change_registrar_->Init(pref_service);
pref_change_registrar_->Add(
ambient::prefs::kAmbientModeLockScreenInactivityTimeoutSeconds,
base::BindRepeating(
&AmbientController::OnLockScreenInactivityTimeoutPrefChanged,
weak_ptr_factory_.GetWeakPtr()));
pref_change_registrar_->Add(
ambient::prefs::kAmbientModeLockScreenBackgroundTimeoutSeconds,
base::BindRepeating(
&AmbientController::OnLockScreenBackgroundTimeoutPrefChanged,
weak_ptr_factory_.GetWeakPtr()));
// Trigger the callbacks manually the first time to init AmbientUiModel.
OnLockScreenInactivityTimeoutPrefChanged();
OnLockScreenBackgroundTimeoutPrefChanged();
}
void AmbientController::OnPowerStatusChanged() {
if (ambient_ui_model_.ui_visibility() != AmbientUiVisibility::kShown) {
// No action needed if ambient screen is not shown.
......@@ -484,9 +528,9 @@ void AmbientController::AcquireWakeLock() {
if (!session_controller->IsScreenLocked() &&
!delayed_lock_timer_.IsRunning()) {
delayed_lock_timer_.Start(
FROM_HERE, kLockScreenDelay, base::BindOnce([]() {
Shell::Get()->session_controller()->LockScreen();
}));
FROM_HERE, ambient_ui_model_.background_lock_screen_timeout(),
base::BindOnce(
[]() { Shell::Get()->session_controller()->LockScreen(); }));
}
}
}
......@@ -513,6 +557,26 @@ void AmbientController::CloseWidget(bool immediately) {
container_view_ = nullptr;
}
void AmbientController::OnLockScreenInactivityTimeoutPrefChanged() {
auto* pref_service = GetPrimaryUserPrefService();
if (!pref_service)
return;
ambient_ui_model_.SetLockScreenInactivityTimeout(
base::TimeDelta::FromSeconds(pref_service->GetInteger(
ambient::prefs::kAmbientModeLockScreenInactivityTimeoutSeconds)));
}
void AmbientController::OnLockScreenBackgroundTimeoutPrefChanged() {
auto* pref_service = GetPrimaryUserPrefService();
if (!pref_service)
return;
ambient_ui_model_.SetBackgroundLockScreenTimeout(
base::TimeDelta::FromSeconds(pref_service->GetInteger(
ambient::prefs::kAmbientModeLockScreenBackgroundTimeoutSeconds)));
}
void AmbientController::RequestAccessToken(
AmbientAccessTokenController::AccessTokenCallback callback,
bool may_refresh_token_on_lock) {
......@@ -602,5 +666,4 @@ void AmbientController::set_backend_controller_for_testing(
ambient_backend_controller_ = std::move(backend_controller);
}
constexpr base::TimeDelta AmbientController::kAutoShowWaitTimeInterval;
} // namespace ash
......@@ -52,9 +52,6 @@ class ASH_EXPORT AmbientController
public device::mojom::FingerprintObserver,
public ui::UserActivityObserver {
public:
static constexpr base::TimeDelta kAutoShowWaitTimeInterval =
base::TimeDelta::FromSeconds(7);
static void RegisterProfilePrefs(PrefRegistrySimple* registry);
explicit AmbientController(
......@@ -67,6 +64,7 @@ class ASH_EXPORT AmbientController
// SessionObserver:
void OnLockStateChanged(bool locked) override;
void OnFirstSessionStarted() override;
void OnActiveUserPrefServiceChanged(PrefService* pref_service) override;
// PowerStatus::Observer:
void OnPowerStatusChanged() override;
......@@ -159,8 +157,9 @@ class ASH_EXPORT AmbientController
void CloseWidget(bool immediately);
// Invoked when the |kAmbientModeEnabled| pref state changed.
void OnEnabledStateChanged();
// Invoked when the Ambient mode prefs state changes.
void OnLockScreenInactivityTimeoutPrefChanged();
void OnLockScreenBackgroundTimeoutPrefChanged();
AmbientContainerView* get_container_view_for_testing() {
return container_view_;
......
......@@ -48,7 +48,7 @@ TEST_F(AmbientControllerTest, ShowAmbientScreenUponLock) {
// Ambient mode will show after inacivity and successfully loading first
// image.
FastForwardToInactivity();
FastForwardToLockScreenTimeout();
FastForwardTiny();
EXPECT_TRUE(container_view());
......@@ -70,7 +70,7 @@ TEST_F(AmbientControllerTest, NotShowAmbientWhenPrefNotEnabled) {
// Ambient mode will not show after inacivity and successfully loading first
// image.
FastForwardToInactivity();
FastForwardToLockScreenTimeout();
FastForwardTiny();
EXPECT_FALSE(container_view());
......@@ -85,7 +85,7 @@ TEST_F(AmbientControllerTest, NotShowAmbientWhenPrefNotEnabled) {
TEST_F(AmbientControllerTest, HideAmbientScreen) {
LockScreen();
FastForwardToInactivity();
FastForwardToLockScreenTimeout();
FastForwardTiny();
EXPECT_TRUE(container_view());
......@@ -106,7 +106,7 @@ TEST_F(AmbientControllerTest, HideAmbientScreen) {
TEST_F(AmbientControllerTest, CloseAmbientScreenUponUnlock) {
LockScreen();
FastForwardToInactivity();
FastForwardToLockScreenTimeout();
FastForwardTiny();
EXPECT_TRUE(container_view());
......@@ -130,7 +130,7 @@ TEST_F(AmbientControllerTest, CloseAmbientScreenUponUnlockSecondaryUser) {
SetAmbientModeEnabled(true);
LockScreen();
FastForwardToInactivity();
FastForwardToLockScreenTimeout();
FastForwardTiny();
EXPECT_TRUE(container_view());
......@@ -145,7 +145,7 @@ TEST_F(AmbientControllerTest, CloseAmbientScreenUponUnlockSecondaryUser) {
// The view should be destroyed along the widget.
EXPECT_FALSE(container_view());
FastForwardToInactivity();
FastForwardToLockScreenTimeout();
FastForwardTiny();
EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
AmbientUiVisibility::kClosed);
......@@ -161,7 +161,7 @@ TEST_F(AmbientControllerTest, NotShowAmbientWhenLockSecondaryUser) {
SetAmbientModeEnabled(true);
LockScreen();
FastForwardToInactivity();
FastForwardToLockScreenTimeout();
FastForwardTiny();
EXPECT_TRUE(container_view());
......@@ -181,7 +181,7 @@ TEST_F(AmbientControllerTest, NotShowAmbientWhenLockSecondaryUser) {
EXPECT_FALSE(container_view());
LockScreen();
FastForwardToInactivity();
FastForwardToLockScreenTimeout();
FastForwardTiny();
EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
......@@ -373,7 +373,7 @@ TEST_F(AmbientControllerTest,
// Lock screen to start ambient mode, and flush the loop to ensure
// the acquire wake lock request has reached the wake lock provider.
LockScreen();
FastForwardToInactivity();
FastForwardToLockScreenTimeout();
FastForwardTiny();
EXPECT_EQ(1, GetNumOfActiveWakeLocks(
......@@ -386,7 +386,7 @@ TEST_F(AmbientControllerTest,
device::mojom::WakeLockType::kPreventDisplaySleep));
// Ambient screen showup again after inactivity.
FastForwardToInactivity();
FastForwardToLockScreenTimeout();
EXPECT_EQ(1, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
......@@ -404,7 +404,7 @@ TEST_F(AmbientControllerTest,
SetPowerStateDischarging();
// Lock screen to start ambient mode.
LockScreen();
FastForwardToInactivity();
FastForwardToLockScreenTimeout();
FastForwardTiny();
EXPECT_TRUE(ambient_controller()->IsShown());
......@@ -485,7 +485,7 @@ TEST_F(AmbientControllerTest, ShouldDismissContainerViewOnEvents) {
TEST_F(AmbientControllerTest, ShouldDismissAndThenComesBack) {
LockScreen();
FastForwardToInactivity();
FastForwardToLockScreenTimeout();
FastForwardTiny();
EXPECT_TRUE(container_view()->GetWidget()->IsVisible());
......@@ -494,7 +494,7 @@ TEST_F(AmbientControllerTest, ShouldDismissAndThenComesBack) {
ambient_controller()->OnUserActivity(&key_event);
EXPECT_FALSE(container_view());
FastForwardToInactivity();
FastForwardToLockScreenTimeout();
FastForwardTiny();
EXPECT_TRUE(container_view()->GetWidget()->IsVisible());
}
......@@ -567,7 +567,7 @@ TEST_F(AmbientControllerTest,
EXPECT_FALSE(IsLocked());
EXPECT_FALSE(ambient_controller()->IsShown());
FastForwardToInactivity();
FastForwardToLockScreenTimeout();
FastForwardTiny();
EXPECT_TRUE(ambient_controller()->IsShown());
......@@ -636,7 +636,7 @@ TEST_F(AmbientControllerTest,
SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
EXPECT_FALSE(IsLocked());
FastForwardToInactivity();
FastForwardToLockScreenTimeout();
FastForwardTiny();
EXPECT_TRUE(ambient_controller()->IsShown());
......@@ -646,7 +646,7 @@ TEST_F(AmbientControllerTest,
// Should dismiss ambient mode screen.
SetScreenBrightnessAndWait(/*percent=*/0);
SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/true);
FastForwardToInactivity();
FastForwardToLockScreenTimeout();
FastForwardTiny();
EXPECT_FALSE(ambient_controller()->IsShown());
......@@ -657,7 +657,7 @@ TEST_F(AmbientControllerTest,
EXPECT_TRUE(IsLocked());
EXPECT_FALSE(ambient_controller()->IsShown());
FastForwardToInactivity();
FastForwardToLockScreenTimeout();
FastForwardTiny();
EXPECT_TRUE(ambient_controller()->IsShown());
}
......@@ -669,7 +669,7 @@ TEST_F(AmbientControllerTest, HideCursor) {
cursor_manager->ShowCursor();
EXPECT_TRUE(cursor_manager->IsCursorVisible());
FastForwardToInactivity();
FastForwardToLockScreenTimeout();
FastForwardTiny();
EXPECT_TRUE(container_view());
......
......@@ -17,6 +17,7 @@
#include "ash/ambient/ui/media_string_view.h"
#include "ash/ambient/ui/photo_view.h"
#include "ash/public/cpp/ambient/ambient_prefs.h"
#include "ash/public/cpp/ambient/ambient_ui_model.h"
#include "ash/public/cpp/ambient/fake_ambient_backend_controller_impl.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
......@@ -274,13 +275,17 @@ MediaStringView* AmbientAshTestBase::GetMediaStringView() {
container_view()->GetViewByID(AmbientViewID::kAmbientMediaStringView));
}
void AmbientAshTestBase::FastForwardToInactivity() {
task_environment()->FastForwardBy(
kFastForwardFactor * AmbientController::kAutoShowWaitTimeInterval);
void AmbientAshTestBase::FastForwardToLockScreenTimeout() {
task_environment()->FastForwardBy(kFastForwardFactor *
ambient_controller()
->ambient_ui_model()
->lock_screen_inactivity_timeout());
}
void AmbientAshTestBase::FastForwardToNextImage() {
task_environment()->FastForwardBy(kFastForwardFactor * kPhotoRefreshInterval);
task_environment()->FastForwardBy(
kFastForwardFactor *
ambient_controller()->GetAmbientBackendModel()->photo_refresh_interval_);
}
void AmbientAshTestBase::FastForwardTiny() {
......@@ -290,12 +295,17 @@ void AmbientAshTestBase::FastForwardTiny() {
}
void AmbientAshTestBase::FastForwardToLockScreen() {
task_environment()->FastForwardBy(kFastForwardFactor * kLockScreenDelay);
task_environment()->FastForwardBy(kFastForwardFactor *
ambient_controller()
->ambient_ui_model()
->background_lock_screen_timeout());
}
void AmbientAshTestBase::FastForwardHalfLockScreenDelay() {
task_environment()->FastForwardBy(0.5 * kFastForwardFactor *
kLockScreenDelay);
ambient_controller()
->ambient_ui_model()
->background_lock_screen_timeout());
}
void AmbientAshTestBase::SetPowerStateCharging() {
......
......@@ -90,8 +90,9 @@ class AmbientAshTestBase : public AshTestBase {
// Set the size of the next image that will be loaded.
void SetPhotoViewImageSize(int width, int height);
// Advance the task environment timer to expire the inactivity monitor.
void FastForwardToInactivity();
// Advance the task environment timer to expire the lock screen inactivity
// timer.
void FastForwardToLockScreenTimeout();
// Advance the task environment timer to load the next photo.
void FastForwardToNextImage();
......
......@@ -370,7 +370,7 @@ TEST_F(MediaStringViewTest, DoNotShowOnLockScreenIfPrefIsDisabled) {
pref->SetBoolean(prefs::kLockScreenMediaControlsEnabled, false);
// Simulates Ambient Mode shown on lock-screen.
LockScreen();
FastForwardToInactivity();
FastForwardToLockScreenTimeout();
FastForwardTiny();
// Simulates active and playing media session.
......
......@@ -17,6 +17,12 @@ constexpr char kAmbientModeEnabled[] = "settings.ambient_mode.enabled";
constexpr char kAmbientModePhotoSourcePref[] =
"settings.ambient_mode.photo_source_enum";
constexpr char kAmbientModeLockScreenInactivityTimeoutSeconds[] =
"ash.ambient.lock_screen_idle_timeout";
constexpr char kAmbientModeLockScreenBackgroundTimeoutSeconds[] =
"ash.ambient.lock_screen_background_timeout";
} // namespace prefs
} // namespace ambient
} // namespace ash
......@@ -21,6 +21,17 @@ ASH_PUBLIC_EXPORT extern const char kAmbientModeEnabled[];
// |Ash.AmbientMode.PhotoSource|. Not displayed to the user in settings.
ASH_PUBLIC_EXPORT extern const char kAmbientModePhotoSourcePref[];
// Integer pref for the number of seconds to wait before starting Ambient mode
// on lock screen. Not displayed to the user in settings.
ASH_PUBLIC_EXPORT extern const char
kAmbientModeLockScreenInactivityTimeoutSeconds[];
// Integer pref for the number of seconds to wait before locking the screen in
// the background after Ambient mode has started. Not displayed to the user in
// settings.
ASH_PUBLIC_EXPORT extern const char
kAmbientModeLockScreenBackgroundTimeoutSeconds[];
} // namespace prefs
} // namespace ambient
} // namespace ash
......
......@@ -44,11 +44,41 @@ void AmbientUiModel::SetUiVisibility(AmbientUiVisibility visibility) {
NotifyAmbientUiVisibilityChanged();
}
void AmbientUiModel::SetLockScreenInactivityTimeout(base::TimeDelta timeout) {
if (timeout == lock_screen_inactivity_timeout_)
return;
lock_screen_inactivity_timeout_ = timeout;
NotifyLockScreenInactivityTimeoutChanged();
}
void AmbientUiModel::SetBackgroundLockScreenTimeout(base::TimeDelta timeout) {
if (timeout == background_lock_screen_timeout_)
return;
background_lock_screen_timeout_ = timeout;
NotifyBackgroundLockScreenTimeoutChanged();
}
void AmbientUiModel::NotifyAmbientUiVisibilityChanged() {
for (auto& observer : observers_)
observer.OnAmbientUiVisibilityChanged(ui_visibility_);
}
void AmbientUiModel::NotifyLockScreenInactivityTimeoutChanged() {
for (auto& observer : observers_) {
observer.OnLockScreenInactivityTimeoutChanged(
lock_screen_inactivity_timeout_);
}
}
void AmbientUiModel::NotifyBackgroundLockScreenTimeoutChanged() {
for (auto& observer : observers_) {
observer.OnBackgroundLockScreenTimeoutChanged(
background_lock_screen_timeout_);
}
}
std::ostream& operator<<(std::ostream& out, AmbientUiMode mode) {
switch (mode) {
case AmbientUiMode::kLockScreenUi:
......
......@@ -9,6 +9,7 @@
#include "base/macros.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "base/time/time.h"
namespace ash {
......@@ -27,12 +28,24 @@ enum class AmbientUiMode {
kMaxValue = kInSessionUi,
};
// The default time before starting Ambient mode on lock screen.
constexpr base::TimeDelta kLockScreenInactivityTimeout =
base::TimeDelta::FromSeconds(7);
// The default time to lock screen in the background after Ambient mode begins.
constexpr base::TimeDelta kLockScreenBackgroundTimeout =
base::TimeDelta::FromSeconds(5);
// A checked observer which receives notification of changes to the Ambient Mode
// UI model.
class ASH_PUBLIC_EXPORT AmbientUiModelObserver : public base::CheckedObserver {
public:
// Invoked when the Ambient Mode UI visibility changed.
virtual void OnAmbientUiVisibilityChanged(AmbientUiVisibility visibility) = 0;
virtual void OnAmbientUiVisibilityChanged(AmbientUiVisibility visibility) {}
virtual void OnLockScreenInactivityTimeoutChanged(base::TimeDelta timeout) {}
virtual void OnBackgroundLockScreenTimeoutChanged(base::TimeDelta timeout) {}
};
// Models the Ambient Mode UI.
......@@ -53,11 +66,35 @@ class ASH_PUBLIC_EXPORT AmbientUiModel {
AmbientUiVisibility ui_visibility() const { return ui_visibility_; }
void SetLockScreenInactivityTimeout(base::TimeDelta timeout);
base::TimeDelta lock_screen_inactivity_timeout() const {
return lock_screen_inactivity_timeout_;
}
void SetBackgroundLockScreenTimeout(base::TimeDelta timeout);
base::TimeDelta background_lock_screen_timeout() const {
return background_lock_screen_timeout_;
}
private:
void NotifyAmbientUiVisibilityChanged();
void NotifyLockScreenInactivityTimeoutChanged();
void NotifyBackgroundLockScreenTimeoutChanged();
AmbientUiVisibility ui_visibility_ = AmbientUiVisibility::kClosed;
// The delay to show ambient mode after lock screen is activated.
base::TimeDelta lock_screen_inactivity_timeout_ =
kLockScreenInactivityTimeout;
// The delay between ambient mode starts and enabling lock screen.
base::TimeDelta background_lock_screen_timeout_ =
kLockScreenBackgroundTimeout;
base::ObserverList<AmbientUiModelObserver> observers_;
};
......
......@@ -523,6 +523,14 @@ const PrefsUtil::TypedPrefMap& PrefsUtil::GetAllowlistedKeys() {
// Ambient Mode.
(*s_allowlist)[ash::ambient::prefs::kAmbientModeEnabled] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
// The following two prefs are not displayed to the user but are configurable
// to speed up automated testing of Ambient mode.
(*s_allowlist)
[ash::ambient::prefs::kAmbientModeLockScreenInactivityTimeoutSeconds] =
settings_api::PrefType::PREF_TYPE_NUMBER;
(*s_allowlist)
[ash::ambient::prefs::kAmbientModeLockScreenBackgroundTimeoutSeconds] =
settings_api::PrefType::PREF_TYPE_NUMBER;
// Google Assistant.
(*s_allowlist)[chromeos::assistant::prefs::kAssistantConsentStatus] =
......
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