12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400 |
- // Copyright 2020 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 "ash/ambient/ambient_controller.h"
- #include <memory>
- #include <string>
- #include <utility>
- #include "ash/ambient/test/ambient_ash_test_base.h"
- #include "ash/ambient/ui/ambient_container_view.h"
- #include "ash/ambient/ui/ambient_view_ids.h"
- #include "ash/assistant/assistant_interaction_controller_impl.h"
- #include "ash/assistant/model/assistant_interaction_model.h"
- #include "ash/constants/ambient_animation_theme.h"
- #include "ash/constants/ash_features.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/public/cpp/assistant/controller/assistant_interaction_controller.h"
- #include "ash/root_window_controller.h"
- #include "ash/shell.h"
- #include "ash/system/power/power_status.h"
- #include "ash/wm/tablet_mode/tablet_mode_controller.h"
- #include "base/callback.h"
- #include "base/location.h"
- #include "base/run_loop.h"
- #include "base/scoped_observation.h"
- #include "base/strings/strcat.h"
- #include "base/test/bind.h"
- #include "base/test/metrics/histogram_tester.h"
- #include "base/test/scoped_feature_list.h"
- #include "base/test/scoped_run_loop_timeout.h"
- #include "base/threading/sequenced_task_runner_handle.h"
- #include "base/time/time.h"
- #include "build/buildflag.h"
- #include "chromeos/ash/components/assistant/buildflags.h"
- #include "chromeos/dbus/power/fake_power_manager_client.h"
- #include "chromeos/dbus/power/power_manager_client.h"
- #include "chromeos/dbus/power_manager/power_supply_properties.pb.h"
- #include "chromeos/dbus/power_manager/suspend.pb.h"
- #include "chromeos/services/libassistant/public/cpp/assistant_interaction_metadata.h"
- #include "ui/events/event.h"
- #include "ui/events/keycodes/keyboard_codes_posix.h"
- #include "ui/events/platform/platform_event_source.h"
- #include "ui/events/pointer_details.h"
- #include "ui/events/types/event_type.h"
- namespace ash {
- namespace {
- using chromeos::assistant::AssistantInteractionMetadata;
- constexpr char kUser1[] = "user1@gmail.com";
- constexpr char kUser2[] = "user2@gmail.com";
- class AmbientUiVisibilityBarrier : public AmbientUiModelObserver {
- public:
- explicit AmbientUiVisibilityBarrier(AmbientUiVisibility target_visibility)
- : target_visibility_(target_visibility) {
- observation_.Observe(AmbientUiModel::Get());
- }
- AmbientUiVisibilityBarrier(const AmbientUiVisibilityBarrier&) = delete;
- AmbientUiVisibilityBarrier& operator=(const AmbientUiVisibilityBarrier&) =
- delete;
- ~AmbientUiVisibilityBarrier() override = default;
- void WaitWithTimeout(base::TimeDelta timeout) {
- if (AmbientUiModel::Get()->ui_visibility() == target_visibility_)
- return;
- base::test::ScopedRunLoopTimeout run_loop_timeout(FROM_HERE, timeout);
- base::RunLoop run_loop;
- run_loop_quit_closure_ = run_loop.QuitClosure();
- run_loop.Run();
- }
- private:
- void OnAmbientUiVisibilityChanged(AmbientUiVisibility visibility) override {
- if (visibility == target_visibility_ && run_loop_quit_closure_) {
- // Post task so that any existing tasks get run before WaitWithTimeout()
- // completes.
- base::SequencedTaskRunnerHandle::Get()->PostTask(
- FROM_HERE, std::move(run_loop_quit_closure_));
- }
- }
- const AmbientUiVisibility target_visibility_;
- base::ScopedObservation<AmbientUiModel, AmbientUiModelObserver> observation_{
- this};
- base::RepeatingClosure run_loop_quit_closure_;
- };
- } // namespace
- class AmbientControllerTest : public AmbientAshTestBase {
- public:
- AmbientControllerTest() = default;
- ~AmbientControllerTest() override = default;
- // AmbientAshTestBase:
- void SetUp() override {
- AmbientAshTestBase::SetUp();
- GetSessionControllerClient()->set_show_lock_screen_views(true);
- }
- bool IsPrefObserved(const std::string& pref_name) {
- auto* pref_change_registrar =
- ambient_controller()->pref_change_registrar_.get();
- DCHECK(pref_change_registrar);
- return pref_change_registrar->IsObserved(pref_name);
- }
- bool WidgetsVisible() {
- const auto& views = GetContainerViews();
- return views.size() > 0 &&
- std::all_of(views.cbegin(), views.cend(), [](const auto* view) {
- return view->GetWidget()->IsVisible();
- });
- }
- bool AreSessionSpecificObserversBound() {
- auto* ctrl = ambient_controller();
- bool ui_model_bound = ctrl->ambient_ui_model_observer_.IsObserving();
- bool backend_model_bound =
- ctrl->ambient_backend_model_observer_.IsObserving();
- bool power_manager_bound =
- ctrl->power_manager_client_observer_.IsObserving();
- bool fingerprint_bound = ctrl->fingerprint_observer_receiver_.is_bound();
- EXPECT_EQ(ui_model_bound, backend_model_bound)
- << "observers should all have the same state";
- EXPECT_EQ(ui_model_bound, power_manager_bound)
- << "observers should all have the same state";
- EXPECT_EQ(ui_model_bound, fingerprint_bound)
- << "observers should all have the same state";
- return ui_model_bound;
- }
- };
- // Tests for behavior that are agnostic to the AmbientAnimationTheme selected by
- // the user should use this test harness.
- //
- // Currently there are test cases that actually fall under this category but
- // do not use this test fixture. This is done purely for time constraint reasons
- // (it takes a lot of compute time to repeat every single one of these test
- // cases).
- class AmbientControllerTestForAnyTheme
- : public AmbientControllerTest,
- public ::testing::WithParamInterface<AmbientAnimationTheme> {
- protected:
- void SetUp() override {
- AmbientControllerTest::SetUp();
- SetAmbientAnimationTheme(GetParam());
- }
- };
- INSTANTIATE_TEST_SUITE_P(
- SlideshowAndAnimationTheme,
- AmbientControllerTestForAnyTheme,
- // Only one lottie-animated theme is sufficient here. The main goal here is
- // to make sure that fundamental behavior holds for both the slideshow and
- // lottie-animated codepaths.
- testing::Values(AmbientAnimationTheme::kSlideshow
- #if BUILDFLAG(HAS_ASH_AMBIENT_ANIMATION_RESOURCES)
- ,
- AmbientAnimationTheme::kFeelTheBreeze
- #endif // BUILDFLAG(HAS_ASH_AMBIENT_ANIMATION_RESOURCES)
- ));
- TEST_P(AmbientControllerTestForAnyTheme, ShowAmbientScreenUponLock) {
- LockScreen();
- // Lockscreen will not immediately show Ambient mode.
- EXPECT_FALSE(ambient_controller()->IsShown());
- // Ambient mode will show after inacivity and successfully loading first
- // image.
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- EXPECT_FALSE(GetContainerViews().empty());
- EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
- AmbientUiVisibility::kShown);
- EXPECT_TRUE(ambient_controller()->IsShown());
- // Clean up.
- UnlockScreen();
- EXPECT_FALSE(ambient_controller()->IsShown());
- }
- TEST_P(AmbientControllerTestForAnyTheme, NotShowAmbientWhenPrefNotEnabled) {
- SetAmbientModeEnabled(false);
- LockScreen();
- // Lockscreen will not immediately show Ambient mode.
- EXPECT_FALSE(ambient_controller()->IsShown());
- // Ambient mode will not show after inacivity and successfully loading first
- // image.
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- EXPECT_TRUE(GetContainerViews().empty());
- EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
- AmbientUiVisibility::kClosed);
- EXPECT_FALSE(ambient_controller()->IsShown());
- // Clean up.
- UnlockScreen();
- EXPECT_FALSE(ambient_controller()->IsShown());
- }
- TEST_P(AmbientControllerTestForAnyTheme, HideAmbientScreen) {
- LockScreen();
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- EXPECT_FALSE(GetContainerViews().empty());
- EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
- AmbientUiVisibility::kShown);
- EXPECT_TRUE(ambient_controller()->IsShown());
- HideAmbientScreen();
- FastForwardTiny();
- EXPECT_TRUE(GetContainerViews().empty());
- EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
- AmbientUiVisibility::kHidden);
- // Clean up.
- UnlockScreen();
- EXPECT_FALSE(ambient_controller()->IsShown());
- }
- TEST_P(AmbientControllerTestForAnyTheme, CloseAmbientScreenUponUnlock) {
- LockScreen();
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- EXPECT_FALSE(GetContainerViews().empty());
- EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
- AmbientUiVisibility::kShown);
- EXPECT_TRUE(ambient_controller()->IsShown());
- UnlockScreen();
- EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
- AmbientUiVisibility::kClosed);
- EXPECT_FALSE(ambient_controller()->IsShown());
- // The view should be destroyed along the widget.
- FastForwardTiny();
- EXPECT_TRUE(GetContainerViews().empty());
- }
- TEST_P(AmbientControllerTestForAnyTheme,
- CloseAmbientScreenUponUnlockSecondaryUser) {
- // Simulate the login screen.
- ClearLogin();
- SimulateUserLogin(kUser1);
- SetAmbientModeEnabled(true);
- LockScreen();
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- EXPECT_FALSE(GetContainerViews().empty());
- EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
- AmbientUiVisibility::kShown);
- EXPECT_TRUE(ambient_controller()->IsShown());
- SimulateUserLogin(kUser2);
- EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
- AmbientUiVisibility::kClosed);
- EXPECT_FALSE(ambient_controller()->IsShown());
- // The view should be destroyed along the widget.
- FastForwardTiny();
- EXPECT_TRUE(GetContainerViews().empty());
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
- AmbientUiVisibility::kClosed);
- EXPECT_FALSE(ambient_controller()->IsShown());
- // The view should be destroyed along the widget.
- FastForwardTiny();
- EXPECT_TRUE(GetContainerViews().empty());
- }
- TEST_F(AmbientControllerTest, NotShowAmbientWhenLockSecondaryUser) {
- // Simulate the login screen.
- ClearLogin();
- SimulateUserLogin(kUser1);
- SetAmbientModeEnabled(true);
- LockScreen();
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- EXPECT_FALSE(GetContainerViews().empty());
- EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
- AmbientUiVisibility::kShown);
- EXPECT_TRUE(ambient_controller()->IsShown());
- SimulateUserLogin(kUser2);
- SetAmbientModeEnabled(true);
- // Ambient mode should not show for second user even if that user has the pref
- // turned on.
- EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
- AmbientUiVisibility::kClosed);
- EXPECT_FALSE(ambient_controller()->IsShown());
- // The view should be destroyed along the widget.
- FastForwardTiny();
- EXPECT_TRUE(GetContainerViews().empty());
- LockScreen();
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
- AmbientUiVisibility::kClosed);
- EXPECT_FALSE(ambient_controller()->IsShown());
- // The view should be destroyed along the widget.
- EXPECT_TRUE(GetContainerViews().empty());
- }
- TEST_P(AmbientControllerTestForAnyTheme,
- ShouldRequestAccessTokenWhenLockingScreen) {
- EXPECT_FALSE(IsAccessTokenRequestPending());
- // Lock the screen will request a token.
- LockScreen();
- EXPECT_TRUE(IsAccessTokenRequestPending());
- IssueAccessToken(/*is_empty=*/false);
- EXPECT_FALSE(IsAccessTokenRequestPending());
- // Should close ambient widget already when unlocking screen.
- UnlockScreen();
- EXPECT_FALSE(IsAccessTokenRequestPending());
- }
- TEST_F(AmbientControllerTest, ShouldNotRequestAccessTokenWhenPrefNotEnabled) {
- SetAmbientModeEnabled(false);
- EXPECT_FALSE(IsAccessTokenRequestPending());
- // Lock the screen will not request a token.
- LockScreen();
- EXPECT_FALSE(IsAccessTokenRequestPending());
- UnlockScreen();
- EXPECT_FALSE(IsAccessTokenRequestPending());
- }
- TEST_P(AmbientControllerTestForAnyTheme, ShouldReturnCachedAccessToken) {
- EXPECT_FALSE(IsAccessTokenRequestPending());
- // Lock the screen will request a token.
- LockScreen();
- EXPECT_TRUE(IsAccessTokenRequestPending());
- IssueAccessToken(/*is_empty=*/false);
- EXPECT_FALSE(IsAccessTokenRequestPending());
- // Another token request will return cached token.
- base::OnceClosure closure = base::MakeExpectedRunClosure(FROM_HERE);
- base::RunLoop run_loop;
- ambient_controller()->RequestAccessToken(base::BindLambdaForTesting(
- [&](const std::string& gaia_id, const std::string& access_token_fetched) {
- EXPECT_EQ(access_token_fetched, TestAmbientClient::kTestAccessToken);
- std::move(closure).Run();
- run_loop.Quit();
- }));
- EXPECT_FALSE(IsAccessTokenRequestPending());
- run_loop.Run();
- // Clean up.
- CloseAmbientScreen();
- }
- TEST_F(AmbientControllerTest, ShouldReturnEmptyAccessToken) {
- EXPECT_FALSE(IsAccessTokenRequestPending());
- // Lock the screen will request a token.
- LockScreen();
- EXPECT_TRUE(IsAccessTokenRequestPending());
- IssueAccessToken(/*is_empty=*/false);
- EXPECT_FALSE(IsAccessTokenRequestPending());
- // Another token request will return cached token.
- base::OnceClosure closure = base::MakeExpectedRunClosure(FROM_HERE);
- base::RunLoop run_loop_1;
- ambient_controller()->RequestAccessToken(base::BindLambdaForTesting(
- [&](const std::string& gaia_id, const std::string& access_token_fetched) {
- EXPECT_EQ(access_token_fetched, TestAmbientClient::kTestAccessToken);
- std::move(closure).Run();
- run_loop_1.Quit();
- }));
- EXPECT_FALSE(IsAccessTokenRequestPending());
- run_loop_1.Run();
- base::RunLoop run_loop_2;
- // When token expired, another token request will get empty token.
- constexpr base::TimeDelta kTokenRefreshDelay = base::Seconds(60);
- task_environment()->FastForwardBy(kTokenRefreshDelay);
- closure = base::MakeExpectedRunClosure(FROM_HERE);
- ambient_controller()->RequestAccessToken(base::BindLambdaForTesting(
- [&](const std::string& gaia_id, const std::string& access_token_fetched) {
- EXPECT_TRUE(access_token_fetched.empty());
- std::move(closure).Run();
- run_loop_2.Quit();
- }));
- EXPECT_FALSE(IsAccessTokenRequestPending());
- run_loop_2.Run();
- // Clean up.
- CloseAmbientScreen();
- }
- TEST_F(AmbientControllerTest, ShouldRetryRefreshAccessTokenAfterFailure) {
- EXPECT_FALSE(IsAccessTokenRequestPending());
- // Lock the screen will request a token.
- LockScreen();
- EXPECT_TRUE(IsAccessTokenRequestPending());
- IssueAccessToken(/*is_empty=*/true);
- EXPECT_FALSE(IsAccessTokenRequestPending());
- // Token request automatically retry.
- task_environment()->FastForwardBy(GetRefreshTokenDelay() * 1.1);
- EXPECT_TRUE(IsAccessTokenRequestPending());
- // Clean up.
- CloseAmbientScreen();
- }
- TEST_F(AmbientControllerTest, ShouldRetryRefreshAccessTokenWithBackoffPolicy) {
- EXPECT_FALSE(IsAccessTokenRequestPending());
- // Lock the screen will request a token.
- LockScreen();
- EXPECT_TRUE(IsAccessTokenRequestPending());
- IssueAccessToken(/*is_empty=*/true);
- EXPECT_FALSE(IsAccessTokenRequestPending());
- base::TimeDelta delay1 = GetRefreshTokenDelay();
- task_environment()->FastForwardBy(delay1 * 1.1);
- EXPECT_TRUE(IsAccessTokenRequestPending());
- IssueAccessToken(/*is_empty=*/true);
- EXPECT_FALSE(IsAccessTokenRequestPending());
- base::TimeDelta delay2 = GetRefreshTokenDelay();
- EXPECT_GT(delay2, delay1);
- task_environment()->FastForwardBy(delay2 * 1.1);
- EXPECT_TRUE(IsAccessTokenRequestPending());
- // Clean up.
- CloseAmbientScreen();
- }
- TEST_F(AmbientControllerTest, ShouldRetryRefreshAccessTokenOnlyThreeTimes) {
- EXPECT_FALSE(IsAccessTokenRequestPending());
- // Lock the screen will request a token.
- LockScreen();
- EXPECT_TRUE(IsAccessTokenRequestPending());
- IssueAccessToken(/*is_empty=*/true);
- EXPECT_FALSE(IsAccessTokenRequestPending());
- // 1st retry.
- task_environment()->FastForwardBy(GetRefreshTokenDelay() * 1.1);
- EXPECT_TRUE(IsAccessTokenRequestPending());
- IssueAccessToken(/*is_empty=*/true);
- EXPECT_FALSE(IsAccessTokenRequestPending());
- // 2nd retry.
- task_environment()->FastForwardBy(GetRefreshTokenDelay() * 1.1);
- EXPECT_TRUE(IsAccessTokenRequestPending());
- IssueAccessToken(/*is_empty=*/true);
- EXPECT_FALSE(IsAccessTokenRequestPending());
- // 3rd retry.
- task_environment()->FastForwardBy(GetRefreshTokenDelay() * 1.1);
- EXPECT_TRUE(IsAccessTokenRequestPending());
- IssueAccessToken(/*is_empty=*/true);
- EXPECT_FALSE(IsAccessTokenRequestPending());
- // Will not retry.
- task_environment()->FastForwardBy(GetRefreshTokenDelay() * 1.1);
- EXPECT_FALSE(IsAccessTokenRequestPending());
- CloseAmbientScreen();
- }
- TEST_F(AmbientControllerTest,
- CheckAcquireAndReleaseWakeLockWhenBatteryIsCharging) {
- // Simulate a device being connected to a charger initially.
- SetPowerStateCharging();
- // Lock screen to start ambient mode, and flush the loop to ensure
- // the acquire wake lock request has reached the wake lock provider.
- LockScreen();
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- EXPECT_EQ(1, GetNumOfActiveWakeLocks(
- device::mojom::WakeLockType::kPreventDisplaySleep));
- HideAmbientScreen();
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(0, GetNumOfActiveWakeLocks(
- device::mojom::WakeLockType::kPreventDisplaySleep));
- // Ambient screen showup again after inactivity.
- FastForwardToLockScreenTimeout();
- EXPECT_EQ(1, GetNumOfActiveWakeLocks(
- device::mojom::WakeLockType::kPreventDisplaySleep));
- // Unlock screen to exit ambient mode.
- UnlockScreen();
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(0, GetNumOfActiveWakeLocks(
- device::mojom::WakeLockType::kPreventDisplaySleep));
- }
- TEST_F(AmbientControllerTest,
- CheckAcquireAndReleaseWakeLockWhenBatteryBatteryIsFullAndDischarging) {
- SetPowerStateDischarging();
- SetBatteryPercent(100.f);
- SetExternalPowerConnected();
- // Lock screen to start ambient mode, and flush the loop to ensure
- // the acquire wake lock request has reached the wake lock provider.
- LockScreen();
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- EXPECT_EQ(1, GetNumOfActiveWakeLocks(
- device::mojom::WakeLockType::kPreventDisplaySleep));
- HideAmbientScreen();
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(0, GetNumOfActiveWakeLocks(
- device::mojom::WakeLockType::kPreventDisplaySleep));
- // Ambient screen showup again after inactivity.
- FastForwardToLockScreenTimeout();
- EXPECT_EQ(1, GetNumOfActiveWakeLocks(
- device::mojom::WakeLockType::kPreventDisplaySleep));
- // Unlock screen to exit ambient mode.
- UnlockScreen();
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(0, GetNumOfActiveWakeLocks(
- device::mojom::WakeLockType::kPreventDisplaySleep));
- }
- TEST_F(AmbientControllerTest,
- CheckAcquireAndReleaseWakeLockWhenBatteryStateChanged) {
- SetPowerStateDischarging();
- SetExternalPowerConnected();
- SetBatteryPercent(50.f);
- // Lock screen to start ambient mode.
- LockScreen();
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- EXPECT_TRUE(ambient_controller()->IsShown());
- // Should not acquire wake lock when device is not charging and with low
- // battery.
- EXPECT_EQ(0, GetNumOfActiveWakeLocks(
- device::mojom::WakeLockType::kPreventDisplaySleep));
- // Connect the device with a charger.
- SetPowerStateCharging();
- base::RunLoop().RunUntilIdle();
- // Should acquire the wake lock when battery is charging.
- EXPECT_EQ(1, GetNumOfActiveWakeLocks(
- device::mojom::WakeLockType::kPreventDisplaySleep));
- // Simulates a full battery.
- SetBatteryPercent(100.f);
- // Should keep the wake lock as the charger is still connected.
- EXPECT_EQ(1, GetNumOfActiveWakeLocks(
- device::mojom::WakeLockType::kPreventDisplaySleep));
- // Disconnects the charger again.
- SetPowerStateDischarging();
- base::RunLoop().RunUntilIdle();
- // Should keep the wake lock when battery is high.
- EXPECT_EQ(1, GetNumOfActiveWakeLocks(
- device::mojom::WakeLockType::kPreventDisplaySleep));
- SetBatteryPercent(50.f);
- base::RunLoop().RunUntilIdle();
- // Should release the wake lock when battery is not charging and low.
- EXPECT_EQ(0, GetNumOfActiveWakeLocks(
- device::mojom::WakeLockType::kPreventDisplaySleep));
- SetBatteryPercent(100.f);
- base::RunLoop().RunUntilIdle();
- // Should take the wake lock when battery is not charging and high.
- EXPECT_EQ(1, GetNumOfActiveWakeLocks(
- device::mojom::WakeLockType::kPreventDisplaySleep));
- SetExternalPowerDisconnected();
- base::RunLoop().RunUntilIdle();
- // Should release the wake lock when power is not connected.
- EXPECT_EQ(0, GetNumOfActiveWakeLocks(
- device::mojom::WakeLockType::kPreventDisplaySleep));
- // An unbalanced release should do nothing.
- UnlockScreen();
- EXPECT_EQ(0, GetNumOfActiveWakeLocks(
- device::mojom::WakeLockType::kPreventDisplaySleep));
- }
- // TODO(cowmoo): find a way to simulate events to trigger |UserActivityDetector|
- TEST_P(AmbientControllerTestForAnyTheme, ShouldDismissContainerViewOnEvents) {
- std::vector<std::unique_ptr<ui::Event>> events;
- for (auto mouse_event_type : {ui::ET_MOUSE_PRESSED, ui::ET_MOUSE_MOVED}) {
- events.emplace_back(std::make_unique<ui::MouseEvent>(
- mouse_event_type, gfx::Point(), gfx::Point(), base::TimeTicks(),
- ui::EF_NONE, ui::EF_NONE));
- }
- events.emplace_back(std::make_unique<ui::MouseWheelEvent>(
- gfx::Vector2d(), gfx::PointF(), gfx::PointF(), base::TimeTicks(),
- ui::EF_NONE, ui::EF_NONE));
- events.emplace_back(std::make_unique<ui::ScrollEvent>(
- ui::ET_SCROLL, gfx::PointF(), gfx::PointF(), base::TimeTicks(),
- ui::EF_NONE, /*x_offset=*/0.0f,
- /*y_offset=*/0.0f,
- /*x_offset_ordinal=*/0.0f,
- /*x_offset_ordinal=*/0.0f, /*finger_count=*/2));
- events.emplace_back(std::make_unique<ui::TouchEvent>(
- ui::ET_TOUCH_PRESSED, gfx::PointF(), gfx::PointF(), base::TimeTicks(),
- ui::PointerDetails()));
- for (const auto& event : events) {
- ShowAmbientScreen();
- FastForwardTiny();
- EXPECT_TRUE(WidgetsVisible());
- ambient_controller()->OnUserActivity(event.get());
- FastForwardTiny();
- EXPECT_TRUE(GetContainerViews().empty());
- // Clean up.
- CloseAmbientScreen();
- }
- }
- TEST_P(AmbientControllerTestForAnyTheme, ShouldDismissAndThenComesBack) {
- LockScreen();
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- EXPECT_TRUE(WidgetsVisible());
- ui::MouseEvent mouse_event(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(),
- base::TimeTicks(), ui::EF_NONE, ui::EF_NONE);
- ambient_controller()->OnUserActivity(&mouse_event);
- FastForwardTiny();
- EXPECT_TRUE(GetContainerViews().empty());
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- EXPECT_TRUE(WidgetsVisible());
- }
- TEST_P(AmbientControllerTestForAnyTheme, ShouldDismissContainerViewOnKeyEvent) {
- // Without user interaction, should show ambient mode.
- ShowAmbientScreen();
- EXPECT_TRUE(WidgetsVisible());
- CloseAmbientScreen();
- // When ambient is shown, OnUserActivity() should ignore key event.
- ambient_controller()->ShowUi();
- EXPECT_TRUE(ambient_controller()->IsShown());
- // General key press will exit ambient mode.
- // Simulate key press to close the widget.
- PressAndReleaseKey(ui::VKEY_A);
- EXPECT_FALSE(ambient_controller()->IsShown());
- }
- TEST_F(AmbientControllerTest,
- ShouldDismissContainerViewOnKeyEventWhenLockScreenInBackground) {
- GetSessionControllerClient()->SetShouldLockScreenAutomatically(true);
- SetPowerStateCharging();
- EXPECT_FALSE(ambient_controller()->IsShown());
- // Should not lock the device and enter ambient mode when the screen is
- // dimmed.
- SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
- EXPECT_FALSE(IsLocked());
- EXPECT_TRUE(ambient_controller()->IsShown());
- FastForwardToBackgroundLockScreenTimeout();
- EXPECT_TRUE(IsLocked());
- // Should not disrupt ongoing ambient mode.
- EXPECT_TRUE(ambient_controller()->IsShown());
- // General key press will exit ambient mode.
- // Simulate key press to close the widget.
- PressAndReleaseKey(ui::VKEY_A);
- EXPECT_FALSE(ambient_controller()->IsShown());
- }
- TEST_F(AmbientControllerTest,
- ShouldShowAmbientScreenWithLockscreenWhenScreenIsDimmed) {
- GetSessionControllerClient()->SetShouldLockScreenAutomatically(true);
- SetPowerStateCharging();
- EXPECT_FALSE(ambient_controller()->IsShown());
- // Should enter ambient mode when the screen is dimmed.
- SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
- EXPECT_FALSE(IsLocked());
- EXPECT_TRUE(ambient_controller()->IsShown());
- FastForwardToBackgroundLockScreenTimeout();
- EXPECT_TRUE(IsLocked());
- // Should not disrupt ongoing ambient mode.
- EXPECT_TRUE(ambient_controller()->IsShown());
- // Closes ambient for clean-up.
- UnlockScreen();
- EXPECT_FALSE(ambient_controller()->IsShown());
- }
- TEST_F(AmbientControllerTest,
- ShouldShowAmbientScreenWithLockscreenWithNoisyPowerEvents) {
- GetSessionControllerClient()->SetShouldLockScreenAutomatically(true);
- SetPowerStateCharging();
- EXPECT_FALSE(ambient_controller()->IsShown());
- // Should enter ambient mode when the screen is dimmed.
- SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
- EXPECT_FALSE(IsLocked());
- FastForwardTiny();
- EXPECT_TRUE(ambient_controller()->IsShown());
- FastForwardHalfLockScreenDelay();
- SetPowerStateCharging();
- FastForwardHalfLockScreenDelay();
- SetPowerStateCharging();
- EXPECT_TRUE(IsLocked());
- // Should not disrupt ongoing ambient mode.
- EXPECT_TRUE(ambient_controller()->IsShown());
- // Closes ambient for clean-up.
- UnlockScreen();
- EXPECT_FALSE(ambient_controller()->IsShown());
- }
- TEST_F(AmbientControllerTest,
- ShouldShowAmbientScreenWithoutLockscreenWhenScreenIsDimmed) {
- GetSessionControllerClient()->SetShouldLockScreenAutomatically(true);
- // When power is discharging, we do not lock the screen with ambient
- // mode since we do not prevent the device go to sleep which will natually
- // lock the device.
- SetPowerStateDischarging();
- EXPECT_FALSE(ambient_controller()->IsShown());
- // Should not lock the device but still enter ambient mode when the screen is
- // dimmed.
- SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
- EXPECT_FALSE(IsLocked());
- EXPECT_TRUE(ambient_controller()->IsShown());
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- EXPECT_TRUE(ambient_controller()->IsShown());
- FastForwardToBackgroundLockScreenTimeout();
- EXPECT_FALSE(IsLocked());
- // Closes ambient for clean-up.
- CloseAmbientScreen();
- }
- TEST_F(AmbientControllerTest, ShouldShowAmbientScreenWhenScreenIsDimmed) {
- GetSessionControllerClient()->SetShouldLockScreenAutomatically(false);
- SetPowerStateCharging();
- EXPECT_FALSE(ambient_controller()->IsShown());
- // Should not lock the device but enter ambient mode when the screen is
- // dimmed.
- SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
- EXPECT_FALSE(IsLocked());
- FastForwardTiny();
- EXPECT_TRUE(ambient_controller()->IsShown());
- FastForwardToBackgroundLockScreenTimeout();
- EXPECT_FALSE(IsLocked());
- // Closes ambient for clean-up.
- CloseAmbientScreen();
- }
- TEST_F(AmbientControllerTest, HandlesPreviousImageFailuresWithLockScreen) {
- // Simulate failures to download FIFE urls. Ambient mode should close and
- // remember the old failure.
- SetDownloadPhotoData("");
- LockScreen();
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- ASSERT_TRUE(ambient_controller()->IsShown());
- AmbientUiVisibilityBarrier ambient_closed_barrier(
- AmbientUiVisibility::kClosed);
- ambient_closed_barrier.WaitWithTimeout(base::Seconds(15));
- ASSERT_FALSE(ambient_controller()->IsShown());
- UnlockScreen();
- // Now simulate FIFE downloads starting to work again. The device should be
- // able to enter ambient mode.
- ClearDownloadPhotoData();
- LockScreen();
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- ASSERT_TRUE(ambient_controller()->IsShown());
- }
- TEST_F(AmbientControllerTest, HandlesPreviousImageFailuresWithDimmedScreen) {
- GetSessionControllerClient()->SetShouldLockScreenAutomatically(false);
- SetPowerStateCharging();
- // Simulate failures to download FIFE urls. Ambient mode should close and
- // remember the old failure.
- SetDownloadPhotoData("");
- SetScreenIdleStateAndWait(/*is_screen_dimmed=*/true, /*is_off=*/false);
- FastForwardTiny();
- ASSERT_TRUE(ambient_controller()->IsShown());
- AmbientUiVisibilityBarrier ambient_closed_barrier(
- AmbientUiVisibility::kClosed);
- ambient_closed_barrier.WaitWithTimeout(base::Seconds(15));
- ASSERT_FALSE(ambient_controller()->IsShown());
- SetScreenIdleStateAndWait(/*is_screen_dimmed=*/false, /*is_off=*/false);
- // Usually would enter ambient mode when the screen is dimmed, but this time
- // it shouldn't because of the previous image failures.
- SetScreenIdleStateAndWait(/*is_screen_dimmed=*/true, /*is_off=*/false);
- FastForwardTiny();
- ASSERT_FALSE(ambient_controller()->IsShown());
- SetScreenIdleStateAndWait(/*is_screen_dimmed=*/false, /*is_off=*/false);
- // Now simulate FIFE downloads starting to work again. The device should be
- // able to enter ambient mode.
- ClearDownloadPhotoData();
- SetScreenIdleStateAndWait(/*is_screen_dimmed=*/true, /*is_off=*/false);
- FastForwardTiny();
- ASSERT_TRUE(ambient_controller()->IsShown());
- // Closes ambient for clean-up.
- CloseAmbientScreen();
- }
- TEST_F(AmbientControllerTest, ShouldHideAmbientScreenWhenDisplayIsOff) {
- GetSessionControllerClient()->SetShouldLockScreenAutomatically(false);
- EXPECT_FALSE(ambient_controller()->IsShown());
- // Should not lock the device and enter ambient mode when the screen is
- // dimmed.
- SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
- EXPECT_FALSE(IsLocked());
- FastForwardTiny();
- EXPECT_TRUE(ambient_controller()->IsShown());
- // Should dismiss ambient mode screen.
- SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/true);
- FastForwardTiny();
- EXPECT_FALSE(ambient_controller()->IsShown());
- // Screen back on again, should not have ambient screen.
- SetScreenIdleStateAndWait(/*dimmed=*/false, /*off=*/false);
- FastForwardTiny();
- EXPECT_FALSE(ambient_controller()->IsShown());
- }
- TEST_F(AmbientControllerTest,
- ShouldHideAmbientScreenWhenDisplayIsOffThenComesBackWithLockScreen) {
- GetSessionControllerClient()->SetShouldLockScreenAutomatically(true);
- SetPowerStateCharging();
- EXPECT_FALSE(ambient_controller()->IsShown());
- // Should not lock the device and enter ambient mode when the screen is
- // dimmed.
- SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
- EXPECT_FALSE(IsLocked());
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- EXPECT_TRUE(ambient_controller()->IsShown());
- FastForwardToBackgroundLockScreenTimeout();
- EXPECT_TRUE(IsLocked());
- // Should dismiss ambient mode screen.
- SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/true);
- FastForwardTiny();
- EXPECT_FALSE(ambient_controller()->IsShown());
- // Screen back on again, should not have ambient screen, but still has lock
- // screen.
- SetScreenIdleStateAndWait(/*dimmed=*/false, /*off=*/false);
- EXPECT_TRUE(IsLocked());
- EXPECT_FALSE(ambient_controller()->IsShown());
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- EXPECT_TRUE(ambient_controller()->IsShown());
- }
- TEST_F(AmbientControllerTest,
- ShouldHideAmbientScreenWhenDisplayIsOffAndNotStartWhenLockScreen) {
- GetSessionControllerClient()->SetShouldLockScreenAutomatically(true);
- SetPowerStateDischarging();
- EXPECT_FALSE(ambient_controller()->IsShown());
- // Should not lock the device and enter ambient mode when the screen is
- // dimmed.
- SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
- EXPECT_FALSE(IsLocked());
- FastForwardTiny();
- EXPECT_TRUE(ambient_controller()->IsShown());
- // Should not lock the device because the device is not charging.
- FastForwardToBackgroundLockScreenTimeout();
- EXPECT_FALSE(IsLocked());
- // Should dismiss ambient mode screen.
- SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/true);
- FastForwardTiny();
- EXPECT_FALSE(ambient_controller()->IsShown());
- // Lock screen will not start ambient mode.
- LockScreen();
- EXPECT_TRUE(IsLocked());
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- EXPECT_FALSE(ambient_controller()->IsShown());
- // Screen back on again, should not have ambient screen, but still has lock
- // screen.
- SetScreenIdleStateAndWait(/*dimmed=*/false, /*off=*/false);
- EXPECT_TRUE(IsLocked());
- EXPECT_FALSE(ambient_controller()->IsShown());
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- EXPECT_TRUE(ambient_controller()->IsShown());
- }
- TEST_F(AmbientControllerTest, HandlesPhotoDownloadOutage) {
- SetDownloadPhotoData("");
- LockScreen();
- FastForwardToLockScreenTimeout();
- ASSERT_TRUE(ambient_controller()->IsShown());
- AmbientUiVisibilityBarrier ambient_closed_barrier(
- AmbientUiVisibility::kClosed);
- ambient_closed_barrier.WaitWithTimeout(base::Seconds(15));
- EXPECT_FALSE(ambient_controller()->IsShown());
- }
- TEST_P(AmbientControllerTestForAnyTheme, HideCursor) {
- auto* cursor_manager = Shell::Get()->cursor_manager();
- LockScreen();
- cursor_manager->ShowCursor();
- EXPECT_TRUE(cursor_manager->IsCursorVisible());
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- EXPECT_FALSE(GetContainerViews().empty());
- EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
- AmbientUiVisibility::kShown);
- EXPECT_TRUE(ambient_controller()->IsShown());
- EXPECT_FALSE(cursor_manager->IsCursorVisible());
- // Clean up.
- UnlockScreen();
- EXPECT_FALSE(ambient_controller()->IsShown());
- }
- TEST_P(AmbientControllerTestForAnyTheme, ShowsOnMultipleDisplays) {
- UpdateDisplay("800x600,800x600");
- FastForwardTiny();
- ShowAmbientScreen();
- auto* screen = display::Screen::GetScreen();
- EXPECT_EQ(screen->GetNumDisplays(), 2);
- EXPECT_EQ(GetContainerViews().size(), 2u);
- AmbientViewID expected_child_view_id =
- GetParam() == AmbientAnimationTheme::kSlideshow
- ? AmbientViewID::kAmbientPhotoView
- : AmbientViewID::kAmbientAnimationView;
- EXPECT_TRUE(GetContainerViews().front()->GetViewByID(expected_child_view_id));
- EXPECT_TRUE(GetContainerViews().back()->GetViewByID(expected_child_view_id));
- // Check that each root controller has an ambient widget.
- for (auto* ctrl : RootWindowController::root_window_controllers())
- EXPECT_TRUE(ctrl->ambient_widget_for_testing() &&
- ctrl->ambient_widget_for_testing()->IsVisible());
- }
- TEST_P(AmbientControllerTestForAnyTheme, RespondsToDisplayAdded) {
- // UpdateDisplay triggers a rogue MouseEvent that cancels Ambient mode when
- // testing with Xvfb. A corresponding MouseEvent is not fired on a real device
- // when an external display is added. Ignore this MouseEvent for testing.
- // Store the old |ShouldIgnoreNativePlatformEvents| value and reset it at the
- // end of the test.
- bool old_should_ignore_events =
- ui::PlatformEventSource::ShouldIgnoreNativePlatformEvents();
- ui::PlatformEventSource::SetIgnoreNativePlatformEvents(true);
- UpdateDisplay("800x600");
- ShowAmbientScreen();
- auto* screen = display::Screen::GetScreen();
- EXPECT_EQ(screen->GetNumDisplays(), 1);
- EXPECT_EQ(GetContainerViews().size(), 1u);
- UpdateDisplay("800x600,800x600");
- FastForwardTiny();
- EXPECT_TRUE(WidgetsVisible());
- EXPECT_EQ(screen->GetNumDisplays(), 2);
- EXPECT_EQ(GetContainerViews().size(), 2u);
- for (auto* ctrl : RootWindowController::root_window_controllers())
- EXPECT_TRUE(ctrl->ambient_widget_for_testing() &&
- ctrl->ambient_widget_for_testing()->IsVisible());
- ui::PlatformEventSource::SetIgnoreNativePlatformEvents(
- old_should_ignore_events);
- }
- TEST_P(AmbientControllerTestForAnyTheme, HandlesDisplayRemoved) {
- UpdateDisplay("800x600,800x600");
- FastForwardTiny();
- ShowAmbientScreen();
- auto* screen = display::Screen::GetScreen();
- EXPECT_EQ(screen->GetNumDisplays(), 2);
- EXPECT_EQ(GetContainerViews().size(), 2u);
- EXPECT_TRUE(WidgetsVisible());
- // Changing to one screen will destroy the widget on the non-primary screen.
- UpdateDisplay("800x600");
- FastForwardTiny();
- EXPECT_EQ(screen->GetNumDisplays(), 1);
- EXPECT_EQ(GetContainerViews().size(), 1u);
- EXPECT_TRUE(WidgetsVisible());
- }
- TEST_F(AmbientControllerTest, ClosesAmbientBeforeSuspend) {
- LockScreen();
- FastForwardToLockScreenTimeout();
- EXPECT_TRUE(ambient_controller()->IsShown());
- SimulateSystemSuspendAndWait(power_manager::SuspendImminent::Reason::
- SuspendImminent_Reason_LID_CLOSED);
- EXPECT_FALSE(ambient_controller()->IsShown());
- FastForwardToLockScreenTimeout();
- // Ambient mode should not resume until SuspendDone is received.
- EXPECT_FALSE(ambient_controller()->IsShown());
- }
- TEST_F(AmbientControllerTest, RestartsAmbientAfterSuspend) {
- LockScreen();
- FastForwardToLockScreenTimeout();
- EXPECT_TRUE(ambient_controller()->IsShown());
- SimulateSystemSuspendAndWait(
- power_manager::SuspendImminent::Reason::SuspendImminent_Reason_IDLE);
- EXPECT_FALSE(ambient_controller()->IsShown());
- // This call should be blocked by prior |SuspendImminent| until |SuspendDone|.
- ambient_controller()->ShowUi();
- EXPECT_FALSE(ambient_controller()->IsShown());
- SimulateSystemResumeAndWait();
- FastForwardToLockScreenTimeout();
- EXPECT_TRUE(ambient_controller()->IsShown());
- }
- TEST_F(AmbientControllerTest, ObservesPrefsWhenAmbientEnabled) {
- SetAmbientModeEnabled(false);
- // This pref is always observed.
- EXPECT_TRUE(IsPrefObserved(ambient::prefs::kAmbientModeEnabled));
- std::vector<std::string> other_prefs{
- ambient::prefs::kAmbientModeLockScreenInactivityTimeoutSeconds,
- ambient::prefs::kAmbientModeLockScreenBackgroundTimeoutSeconds,
- ambient::prefs::kAmbientModePhotoRefreshIntervalSeconds};
- for (auto& pref_name : other_prefs)
- EXPECT_FALSE(IsPrefObserved(pref_name));
- SetAmbientModeEnabled(true);
- EXPECT_TRUE(IsPrefObserved(ambient::prefs::kAmbientModeEnabled));
- for (auto& pref_name : other_prefs)
- EXPECT_TRUE(IsPrefObserved(pref_name));
- }
- TEST_F(AmbientControllerTest, BindsObserversWhenAmbientEnabled) {
- auto* ctrl = ambient_controller();
- SetAmbientModeEnabled(false);
- // SessionObserver must always be observing to detect when user pref service
- // is started.
- EXPECT_TRUE(ctrl->session_observer_.IsObserving());
- EXPECT_FALSE(AreSessionSpecificObserversBound());
- SetAmbientModeEnabled(true);
- // Session observer should still be observing.
- EXPECT_TRUE(ctrl->session_observer_.IsObserving());
- EXPECT_TRUE(AreSessionSpecificObserversBound());
- }
- TEST_F(AmbientControllerTest, SwitchActiveUsersDoesNotDoubleBindObservers) {
- ClearLogin();
- SimulateUserLogin(kUser1);
- SetAmbientModeEnabled(true);
- TestSessionControllerClient* session = GetSessionControllerClient();
- // Observers are bound for primary user with Ambient mode enabled.
- EXPECT_TRUE(AreSessionSpecificObserversBound());
- EXPECT_TRUE(IsPrefObserved(ambient::prefs::kAmbientModeEnabled));
- // Observers are still bound when secondary user logs in.
- SimulateUserLogin(kUser2);
- EXPECT_TRUE(AreSessionSpecificObserversBound());
- EXPECT_TRUE(IsPrefObserved(ambient::prefs::kAmbientModeEnabled));
- // Observers are not re-bound for primary user when session is active.
- session->SwitchActiveUser(AccountId::FromUserEmail(kUser1));
- EXPECT_TRUE(AreSessionSpecificObserversBound());
- EXPECT_TRUE(IsPrefObserved(ambient::prefs::kAmbientModeEnabled));
- // Switch back to secondary user.
- session->SwitchActiveUser(AccountId::FromUserEmail(kUser2));
- }
- TEST_F(AmbientControllerTest, BindsObserversWhenAmbientOn) {
- auto* ctrl = ambient_controller();
- LockScreen();
- // Start monitoring user activity on hidden ui.
- EXPECT_TRUE(ctrl->user_activity_observer_.IsObserving());
- // Do not monitor power status yet.
- EXPECT_FALSE(ctrl->power_status_observer_.IsObserving());
- FastForwardToLockScreenTimeout();
- EXPECT_TRUE(ctrl->user_activity_observer_.IsObserving());
- EXPECT_TRUE(ctrl->power_status_observer_.IsObserving());
- UnlockScreen();
- EXPECT_FALSE(ctrl->user_activity_observer_.IsObserving());
- EXPECT_FALSE(ctrl->power_status_observer_.IsObserving());
- }
- TEST_P(AmbientControllerTestForAnyTheme,
- ShowDismissAmbientScreenUponAssistantQuery) {
- // Without user interaction, should show ambient mode.
- ShowAmbientScreen();
- EXPECT_TRUE(ambient_controller()->IsShown());
- // Trigger Assistant interaction.
- static_cast<AssistantInteractionControllerImpl*>(
- AssistantInteractionController::Get())
- ->OnInteractionStarted(AssistantInteractionMetadata());
- base::RunLoop().RunUntilIdle();
- // Ambient screen should dismiss.
- EXPECT_TRUE(GetContainerViews().empty());
- EXPECT_FALSE(ambient_controller()->IsShown());
- }
- // For all test cases that depend on ash ambient resources (lottie files, image
- // assets, etc) being present to run.
- #if BUILDFLAG(HAS_ASH_AMBIENT_ANIMATION_RESOURCES)
- #define ANIMATION_TEST_WITH_RESOURCES(test_case_name) test_case_name
- #else
- #define ANIMATION_TEST_WITH_RESOURCES(test_case_name) DISABLED_##test_case_name
- #endif // BUILDFLAG(HAS_ASH_AMBIENT_ANIMATION_RESOURCES)
- TEST_F(AmbientControllerTest,
- ANIMATION_TEST_WITH_RESOURCES(RendersCorrectView)) {
- base::test::ScopedFeatureList scoped_feature_list;
- scoped_feature_list.InitAndEnableFeature(features::kPersonalizationHub);
- SetAmbientAnimationTheme(AmbientAnimationTheme::kFeelTheBreeze);
- LockScreen();
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- ASSERT_TRUE(GetContainerView());
- EXPECT_FALSE(
- GetContainerView()->GetViewByID(AmbientViewID::kAmbientPhotoView));
- EXPECT_TRUE(
- GetContainerView()->GetViewByID(AmbientViewID::kAmbientAnimationView));
- UnlockScreen();
- SetAmbientAnimationTheme(AmbientAnimationTheme::kSlideshow);
- LockScreen();
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- ASSERT_TRUE(GetContainerView());
- EXPECT_TRUE(
- GetContainerView()->GetViewByID(AmbientViewID::kAmbientPhotoView));
- EXPECT_FALSE(
- GetContainerView()->GetViewByID(AmbientViewID::kAmbientAnimationView));
- UnlockScreen();
- SetAmbientAnimationTheme(AmbientAnimationTheme::kFeelTheBreeze);
- LockScreen();
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- ASSERT_TRUE(GetContainerView());
- EXPECT_FALSE(
- GetContainerView()->GetViewByID(AmbientViewID::kAmbientPhotoView));
- EXPECT_TRUE(
- GetContainerView()->GetViewByID(AmbientViewID::kAmbientAnimationView));
- }
- TEST_F(AmbientControllerTest,
- ANIMATION_TEST_WITH_RESOURCES(ClearsCacheWhenSwitchingThemes)) {
- base::test::ScopedFeatureList scoped_feature_list;
- scoped_feature_list.InitAndEnableFeature(features::kPersonalizationHub);
- SetAmbientAnimationTheme(AmbientAnimationTheme::kSlideshow);
- LockScreen();
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- ASSERT_TRUE(GetContainerView());
- ASSERT_FALSE(GetCachedFiles().empty());
- UnlockScreen();
- SetAmbientAnimationTheme(AmbientAnimationTheme::kFeelTheBreeze);
- // Mimic a network outage where no photos can be downloaded. Since the cache
- // should have been cleared when we switched ambient animation themes, the
- // UI shouldn't start with a photo cached during slideshow mode.
- SetDownloadPhotoData(/*data=*/"");
- LockScreen();
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- EXPECT_FALSE(GetContainerView());
- EXPECT_TRUE(GetCachedFiles().empty());
- }
- TEST_P(AmbientControllerTestForAnyTheme, MetricsEngagementTime) {
- // TODO(esum): Find a better way of fast forwarding time for lottie animations
- // in unit tests. Currently, the whole compositor stack is being used in this
- // test harness and there is no good way to control the frame rate, so
- // FastForwardBy() blocks for long periods of time. Do not make this value
- // too high, or the test is at risk of timing out.
- constexpr base::TimeDelta kExpectedEngagementTime = base::Milliseconds(100);
- base::HistogramTester histogram_tester;
- Shell::Get()->tablet_mode_controller()->SetEnabledForTest(false);
- LockScreen();
- // Unlike other tests, the exact amount of time we spend in ambient mode
- // matters to write the correct test expectation. So fast forward by the
- // exact amount needed to trigger ambient mode.
- // (FastForwardToLockScreenTimeout() adds on a little buffer to the timeout)
- task_environment()->FastForwardBy(ambient_controller()
- ->ambient_ui_model()
- ->lock_screen_inactivity_timeout());
- ASSERT_TRUE(ambient_controller()->IsShown());
- task_environment()->FastForwardBy(kExpectedEngagementTime);
- UnlockScreen();
- ASSERT_FALSE(ambient_controller()->IsShown());
- histogram_tester.ExpectTimeBucketCount(
- "Ash.AmbientMode.EngagementTime.ClamshellMode", kExpectedEngagementTime,
- 1);
- histogram_tester.ExpectTimeBucketCount(
- base::StrCat({"Ash.AmbientMode.EngagementTime.", ToString(GetParam())}),
- kExpectedEngagementTime, 1);
- // Now do the same sequence in tablet mode.
- Shell::Get()->tablet_mode_controller()->SetEnabledForTest(true);
- LockScreen();
- task_environment()->FastForwardBy(ambient_controller()
- ->ambient_ui_model()
- ->lock_screen_inactivity_timeout());
- ASSERT_TRUE(ambient_controller()->IsShown());
- task_environment()->FastForwardBy(kExpectedEngagementTime);
- UnlockScreen();
- ASSERT_FALSE(ambient_controller()->IsShown());
- histogram_tester.ExpectTimeBucketCount(
- "Ash.AmbientMode.EngagementTime.TabletMode", kExpectedEngagementTime, 1);
- histogram_tester.ExpectTimeBucketCount(
- base::StrCat({"Ash.AmbientMode.EngagementTime.", ToString(GetParam())}),
- kExpectedEngagementTime, 2);
- }
- TEST_P(AmbientControllerTestForAnyTheme, MetricsStartupTime) {
- base::HistogramTester histogram_tester;
- LockScreen();
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- ASSERT_TRUE(ambient_controller()->IsShown());
- histogram_tester.ExpectTotalCount(
- base::StrCat({"Ash.AmbientMode.StartupTime.", ToString(GetParam())}), 1);
- UnlockScreen();
- ASSERT_FALSE(ambient_controller()->IsShown());
- LockScreen();
- FastForwardToLockScreenTimeout();
- FastForwardTiny();
- ASSERT_TRUE(ambient_controller()->IsShown());
- histogram_tester.ExpectTotalCount(
- base::StrCat({"Ash.AmbientMode.StartupTime.", ToString(GetParam())}), 2);
- }
- TEST_P(AmbientControllerTestForAnyTheme, MetricsStartupTimeFailedToStart) {
- // Simulate IMAX outage that doesn't return any photos.
- backend_controller()->SetFetchScreenUpdateInfoResponseSize(0);
- base::HistogramTester histogram_tester;
- LockScreen();
- FastForwardToLockScreenTimeout();
- task_environment()->FastForwardBy(base::Minutes(1));
- ASSERT_TRUE(GetContainerViews().empty());
- UnlockScreen();
- histogram_tester.ExpectUniqueTimeSample(
- base::StrCat({"Ash.AmbientMode.StartupTime.", ToString(GetParam())}),
- base::Minutes(1), 1);
- }
- } // namespace ash
|