ambient_controller_unittest.cc 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/ambient/ambient_controller.h"
  5. #include <memory>
  6. #include <string>
  7. #include <utility>
  8. #include "ash/ambient/test/ambient_ash_test_base.h"
  9. #include "ash/ambient/ui/ambient_container_view.h"
  10. #include "ash/ambient/ui/ambient_view_ids.h"
  11. #include "ash/assistant/assistant_interaction_controller_impl.h"
  12. #include "ash/assistant/model/assistant_interaction_model.h"
  13. #include "ash/constants/ambient_animation_theme.h"
  14. #include "ash/constants/ash_features.h"
  15. #include "ash/public/cpp/ambient/ambient_prefs.h"
  16. #include "ash/public/cpp/ambient/ambient_ui_model.h"
  17. #include "ash/public/cpp/ambient/fake_ambient_backend_controller_impl.h"
  18. #include "ash/public/cpp/assistant/controller/assistant_interaction_controller.h"
  19. #include "ash/root_window_controller.h"
  20. #include "ash/shell.h"
  21. #include "ash/system/power/power_status.h"
  22. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  23. #include "base/callback.h"
  24. #include "base/location.h"
  25. #include "base/run_loop.h"
  26. #include "base/scoped_observation.h"
  27. #include "base/strings/strcat.h"
  28. #include "base/test/bind.h"
  29. #include "base/test/metrics/histogram_tester.h"
  30. #include "base/test/scoped_feature_list.h"
  31. #include "base/test/scoped_run_loop_timeout.h"
  32. #include "base/threading/sequenced_task_runner_handle.h"
  33. #include "base/time/time.h"
  34. #include "build/buildflag.h"
  35. #include "chromeos/ash/components/assistant/buildflags.h"
  36. #include "chromeos/dbus/power/fake_power_manager_client.h"
  37. #include "chromeos/dbus/power/power_manager_client.h"
  38. #include "chromeos/dbus/power_manager/power_supply_properties.pb.h"
  39. #include "chromeos/dbus/power_manager/suspend.pb.h"
  40. #include "chromeos/services/libassistant/public/cpp/assistant_interaction_metadata.h"
  41. #include "ui/events/event.h"
  42. #include "ui/events/keycodes/keyboard_codes_posix.h"
  43. #include "ui/events/platform/platform_event_source.h"
  44. #include "ui/events/pointer_details.h"
  45. #include "ui/events/types/event_type.h"
  46. namespace ash {
  47. namespace {
  48. using chromeos::assistant::AssistantInteractionMetadata;
  49. constexpr char kUser1[] = "user1@gmail.com";
  50. constexpr char kUser2[] = "user2@gmail.com";
  51. class AmbientUiVisibilityBarrier : public AmbientUiModelObserver {
  52. public:
  53. explicit AmbientUiVisibilityBarrier(AmbientUiVisibility target_visibility)
  54. : target_visibility_(target_visibility) {
  55. observation_.Observe(AmbientUiModel::Get());
  56. }
  57. AmbientUiVisibilityBarrier(const AmbientUiVisibilityBarrier&) = delete;
  58. AmbientUiVisibilityBarrier& operator=(const AmbientUiVisibilityBarrier&) =
  59. delete;
  60. ~AmbientUiVisibilityBarrier() override = default;
  61. void WaitWithTimeout(base::TimeDelta timeout) {
  62. if (AmbientUiModel::Get()->ui_visibility() == target_visibility_)
  63. return;
  64. base::test::ScopedRunLoopTimeout run_loop_timeout(FROM_HERE, timeout);
  65. base::RunLoop run_loop;
  66. run_loop_quit_closure_ = run_loop.QuitClosure();
  67. run_loop.Run();
  68. }
  69. private:
  70. void OnAmbientUiVisibilityChanged(AmbientUiVisibility visibility) override {
  71. if (visibility == target_visibility_ && run_loop_quit_closure_) {
  72. // Post task so that any existing tasks get run before WaitWithTimeout()
  73. // completes.
  74. base::SequencedTaskRunnerHandle::Get()->PostTask(
  75. FROM_HERE, std::move(run_loop_quit_closure_));
  76. }
  77. }
  78. const AmbientUiVisibility target_visibility_;
  79. base::ScopedObservation<AmbientUiModel, AmbientUiModelObserver> observation_{
  80. this};
  81. base::RepeatingClosure run_loop_quit_closure_;
  82. };
  83. } // namespace
  84. class AmbientControllerTest : public AmbientAshTestBase {
  85. public:
  86. AmbientControllerTest() = default;
  87. ~AmbientControllerTest() override = default;
  88. // AmbientAshTestBase:
  89. void SetUp() override {
  90. AmbientAshTestBase::SetUp();
  91. GetSessionControllerClient()->set_show_lock_screen_views(true);
  92. }
  93. bool IsPrefObserved(const std::string& pref_name) {
  94. auto* pref_change_registrar =
  95. ambient_controller()->pref_change_registrar_.get();
  96. DCHECK(pref_change_registrar);
  97. return pref_change_registrar->IsObserved(pref_name);
  98. }
  99. bool WidgetsVisible() {
  100. const auto& views = GetContainerViews();
  101. return views.size() > 0 &&
  102. std::all_of(views.cbegin(), views.cend(), [](const auto* view) {
  103. return view->GetWidget()->IsVisible();
  104. });
  105. }
  106. bool AreSessionSpecificObserversBound() {
  107. auto* ctrl = ambient_controller();
  108. bool ui_model_bound = ctrl->ambient_ui_model_observer_.IsObserving();
  109. bool backend_model_bound =
  110. ctrl->ambient_backend_model_observer_.IsObserving();
  111. bool power_manager_bound =
  112. ctrl->power_manager_client_observer_.IsObserving();
  113. bool fingerprint_bound = ctrl->fingerprint_observer_receiver_.is_bound();
  114. EXPECT_EQ(ui_model_bound, backend_model_bound)
  115. << "observers should all have the same state";
  116. EXPECT_EQ(ui_model_bound, power_manager_bound)
  117. << "observers should all have the same state";
  118. EXPECT_EQ(ui_model_bound, fingerprint_bound)
  119. << "observers should all have the same state";
  120. return ui_model_bound;
  121. }
  122. };
  123. // Tests for behavior that are agnostic to the AmbientAnimationTheme selected by
  124. // the user should use this test harness.
  125. //
  126. // Currently there are test cases that actually fall under this category but
  127. // do not use this test fixture. This is done purely for time constraint reasons
  128. // (it takes a lot of compute time to repeat every single one of these test
  129. // cases).
  130. class AmbientControllerTestForAnyTheme
  131. : public AmbientControllerTest,
  132. public ::testing::WithParamInterface<AmbientAnimationTheme> {
  133. protected:
  134. void SetUp() override {
  135. AmbientControllerTest::SetUp();
  136. SetAmbientAnimationTheme(GetParam());
  137. }
  138. };
  139. INSTANTIATE_TEST_SUITE_P(
  140. SlideshowAndAnimationTheme,
  141. AmbientControllerTestForAnyTheme,
  142. // Only one lottie-animated theme is sufficient here. The main goal here is
  143. // to make sure that fundamental behavior holds for both the slideshow and
  144. // lottie-animated codepaths.
  145. testing::Values(AmbientAnimationTheme::kSlideshow
  146. #if BUILDFLAG(HAS_ASH_AMBIENT_ANIMATION_RESOURCES)
  147. ,
  148. AmbientAnimationTheme::kFeelTheBreeze
  149. #endif // BUILDFLAG(HAS_ASH_AMBIENT_ANIMATION_RESOURCES)
  150. ));
  151. TEST_P(AmbientControllerTestForAnyTheme, ShowAmbientScreenUponLock) {
  152. LockScreen();
  153. // Lockscreen will not immediately show Ambient mode.
  154. EXPECT_FALSE(ambient_controller()->IsShown());
  155. // Ambient mode will show after inacivity and successfully loading first
  156. // image.
  157. FastForwardToLockScreenTimeout();
  158. FastForwardTiny();
  159. EXPECT_FALSE(GetContainerViews().empty());
  160. EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
  161. AmbientUiVisibility::kShown);
  162. EXPECT_TRUE(ambient_controller()->IsShown());
  163. // Clean up.
  164. UnlockScreen();
  165. EXPECT_FALSE(ambient_controller()->IsShown());
  166. }
  167. TEST_P(AmbientControllerTestForAnyTheme, NotShowAmbientWhenPrefNotEnabled) {
  168. SetAmbientModeEnabled(false);
  169. LockScreen();
  170. // Lockscreen will not immediately show Ambient mode.
  171. EXPECT_FALSE(ambient_controller()->IsShown());
  172. // Ambient mode will not show after inacivity and successfully loading first
  173. // image.
  174. FastForwardToLockScreenTimeout();
  175. FastForwardTiny();
  176. EXPECT_TRUE(GetContainerViews().empty());
  177. EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
  178. AmbientUiVisibility::kClosed);
  179. EXPECT_FALSE(ambient_controller()->IsShown());
  180. // Clean up.
  181. UnlockScreen();
  182. EXPECT_FALSE(ambient_controller()->IsShown());
  183. }
  184. TEST_P(AmbientControllerTestForAnyTheme, HideAmbientScreen) {
  185. LockScreen();
  186. FastForwardToLockScreenTimeout();
  187. FastForwardTiny();
  188. EXPECT_FALSE(GetContainerViews().empty());
  189. EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
  190. AmbientUiVisibility::kShown);
  191. EXPECT_TRUE(ambient_controller()->IsShown());
  192. HideAmbientScreen();
  193. FastForwardTiny();
  194. EXPECT_TRUE(GetContainerViews().empty());
  195. EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
  196. AmbientUiVisibility::kHidden);
  197. // Clean up.
  198. UnlockScreen();
  199. EXPECT_FALSE(ambient_controller()->IsShown());
  200. }
  201. TEST_P(AmbientControllerTestForAnyTheme, CloseAmbientScreenUponUnlock) {
  202. LockScreen();
  203. FastForwardToLockScreenTimeout();
  204. FastForwardTiny();
  205. EXPECT_FALSE(GetContainerViews().empty());
  206. EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
  207. AmbientUiVisibility::kShown);
  208. EXPECT_TRUE(ambient_controller()->IsShown());
  209. UnlockScreen();
  210. EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
  211. AmbientUiVisibility::kClosed);
  212. EXPECT_FALSE(ambient_controller()->IsShown());
  213. // The view should be destroyed along the widget.
  214. FastForwardTiny();
  215. EXPECT_TRUE(GetContainerViews().empty());
  216. }
  217. TEST_P(AmbientControllerTestForAnyTheme,
  218. CloseAmbientScreenUponUnlockSecondaryUser) {
  219. // Simulate the login screen.
  220. ClearLogin();
  221. SimulateUserLogin(kUser1);
  222. SetAmbientModeEnabled(true);
  223. LockScreen();
  224. FastForwardToLockScreenTimeout();
  225. FastForwardTiny();
  226. EXPECT_FALSE(GetContainerViews().empty());
  227. EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
  228. AmbientUiVisibility::kShown);
  229. EXPECT_TRUE(ambient_controller()->IsShown());
  230. SimulateUserLogin(kUser2);
  231. EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
  232. AmbientUiVisibility::kClosed);
  233. EXPECT_FALSE(ambient_controller()->IsShown());
  234. // The view should be destroyed along the widget.
  235. FastForwardTiny();
  236. EXPECT_TRUE(GetContainerViews().empty());
  237. FastForwardToLockScreenTimeout();
  238. FastForwardTiny();
  239. EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
  240. AmbientUiVisibility::kClosed);
  241. EXPECT_FALSE(ambient_controller()->IsShown());
  242. // The view should be destroyed along the widget.
  243. FastForwardTiny();
  244. EXPECT_TRUE(GetContainerViews().empty());
  245. }
  246. TEST_F(AmbientControllerTest, NotShowAmbientWhenLockSecondaryUser) {
  247. // Simulate the login screen.
  248. ClearLogin();
  249. SimulateUserLogin(kUser1);
  250. SetAmbientModeEnabled(true);
  251. LockScreen();
  252. FastForwardToLockScreenTimeout();
  253. FastForwardTiny();
  254. EXPECT_FALSE(GetContainerViews().empty());
  255. EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
  256. AmbientUiVisibility::kShown);
  257. EXPECT_TRUE(ambient_controller()->IsShown());
  258. SimulateUserLogin(kUser2);
  259. SetAmbientModeEnabled(true);
  260. // Ambient mode should not show for second user even if that user has the pref
  261. // turned on.
  262. EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
  263. AmbientUiVisibility::kClosed);
  264. EXPECT_FALSE(ambient_controller()->IsShown());
  265. // The view should be destroyed along the widget.
  266. FastForwardTiny();
  267. EXPECT_TRUE(GetContainerViews().empty());
  268. LockScreen();
  269. FastForwardToLockScreenTimeout();
  270. FastForwardTiny();
  271. EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
  272. AmbientUiVisibility::kClosed);
  273. EXPECT_FALSE(ambient_controller()->IsShown());
  274. // The view should be destroyed along the widget.
  275. EXPECT_TRUE(GetContainerViews().empty());
  276. }
  277. TEST_P(AmbientControllerTestForAnyTheme,
  278. ShouldRequestAccessTokenWhenLockingScreen) {
  279. EXPECT_FALSE(IsAccessTokenRequestPending());
  280. // Lock the screen will request a token.
  281. LockScreen();
  282. EXPECT_TRUE(IsAccessTokenRequestPending());
  283. IssueAccessToken(/*is_empty=*/false);
  284. EXPECT_FALSE(IsAccessTokenRequestPending());
  285. // Should close ambient widget already when unlocking screen.
  286. UnlockScreen();
  287. EXPECT_FALSE(IsAccessTokenRequestPending());
  288. }
  289. TEST_F(AmbientControllerTest, ShouldNotRequestAccessTokenWhenPrefNotEnabled) {
  290. SetAmbientModeEnabled(false);
  291. EXPECT_FALSE(IsAccessTokenRequestPending());
  292. // Lock the screen will not request a token.
  293. LockScreen();
  294. EXPECT_FALSE(IsAccessTokenRequestPending());
  295. UnlockScreen();
  296. EXPECT_FALSE(IsAccessTokenRequestPending());
  297. }
  298. TEST_P(AmbientControllerTestForAnyTheme, ShouldReturnCachedAccessToken) {
  299. EXPECT_FALSE(IsAccessTokenRequestPending());
  300. // Lock the screen will request a token.
  301. LockScreen();
  302. EXPECT_TRUE(IsAccessTokenRequestPending());
  303. IssueAccessToken(/*is_empty=*/false);
  304. EXPECT_FALSE(IsAccessTokenRequestPending());
  305. // Another token request will return cached token.
  306. base::OnceClosure closure = base::MakeExpectedRunClosure(FROM_HERE);
  307. base::RunLoop run_loop;
  308. ambient_controller()->RequestAccessToken(base::BindLambdaForTesting(
  309. [&](const std::string& gaia_id, const std::string& access_token_fetched) {
  310. EXPECT_EQ(access_token_fetched, TestAmbientClient::kTestAccessToken);
  311. std::move(closure).Run();
  312. run_loop.Quit();
  313. }));
  314. EXPECT_FALSE(IsAccessTokenRequestPending());
  315. run_loop.Run();
  316. // Clean up.
  317. CloseAmbientScreen();
  318. }
  319. TEST_F(AmbientControllerTest, ShouldReturnEmptyAccessToken) {
  320. EXPECT_FALSE(IsAccessTokenRequestPending());
  321. // Lock the screen will request a token.
  322. LockScreen();
  323. EXPECT_TRUE(IsAccessTokenRequestPending());
  324. IssueAccessToken(/*is_empty=*/false);
  325. EXPECT_FALSE(IsAccessTokenRequestPending());
  326. // Another token request will return cached token.
  327. base::OnceClosure closure = base::MakeExpectedRunClosure(FROM_HERE);
  328. base::RunLoop run_loop_1;
  329. ambient_controller()->RequestAccessToken(base::BindLambdaForTesting(
  330. [&](const std::string& gaia_id, const std::string& access_token_fetched) {
  331. EXPECT_EQ(access_token_fetched, TestAmbientClient::kTestAccessToken);
  332. std::move(closure).Run();
  333. run_loop_1.Quit();
  334. }));
  335. EXPECT_FALSE(IsAccessTokenRequestPending());
  336. run_loop_1.Run();
  337. base::RunLoop run_loop_2;
  338. // When token expired, another token request will get empty token.
  339. constexpr base::TimeDelta kTokenRefreshDelay = base::Seconds(60);
  340. task_environment()->FastForwardBy(kTokenRefreshDelay);
  341. closure = base::MakeExpectedRunClosure(FROM_HERE);
  342. ambient_controller()->RequestAccessToken(base::BindLambdaForTesting(
  343. [&](const std::string& gaia_id, const std::string& access_token_fetched) {
  344. EXPECT_TRUE(access_token_fetched.empty());
  345. std::move(closure).Run();
  346. run_loop_2.Quit();
  347. }));
  348. EXPECT_FALSE(IsAccessTokenRequestPending());
  349. run_loop_2.Run();
  350. // Clean up.
  351. CloseAmbientScreen();
  352. }
  353. TEST_F(AmbientControllerTest, ShouldRetryRefreshAccessTokenAfterFailure) {
  354. EXPECT_FALSE(IsAccessTokenRequestPending());
  355. // Lock the screen will request a token.
  356. LockScreen();
  357. EXPECT_TRUE(IsAccessTokenRequestPending());
  358. IssueAccessToken(/*is_empty=*/true);
  359. EXPECT_FALSE(IsAccessTokenRequestPending());
  360. // Token request automatically retry.
  361. task_environment()->FastForwardBy(GetRefreshTokenDelay() * 1.1);
  362. EXPECT_TRUE(IsAccessTokenRequestPending());
  363. // Clean up.
  364. CloseAmbientScreen();
  365. }
  366. TEST_F(AmbientControllerTest, ShouldRetryRefreshAccessTokenWithBackoffPolicy) {
  367. EXPECT_FALSE(IsAccessTokenRequestPending());
  368. // Lock the screen will request a token.
  369. LockScreen();
  370. EXPECT_TRUE(IsAccessTokenRequestPending());
  371. IssueAccessToken(/*is_empty=*/true);
  372. EXPECT_FALSE(IsAccessTokenRequestPending());
  373. base::TimeDelta delay1 = GetRefreshTokenDelay();
  374. task_environment()->FastForwardBy(delay1 * 1.1);
  375. EXPECT_TRUE(IsAccessTokenRequestPending());
  376. IssueAccessToken(/*is_empty=*/true);
  377. EXPECT_FALSE(IsAccessTokenRequestPending());
  378. base::TimeDelta delay2 = GetRefreshTokenDelay();
  379. EXPECT_GT(delay2, delay1);
  380. task_environment()->FastForwardBy(delay2 * 1.1);
  381. EXPECT_TRUE(IsAccessTokenRequestPending());
  382. // Clean up.
  383. CloseAmbientScreen();
  384. }
  385. TEST_F(AmbientControllerTest, ShouldRetryRefreshAccessTokenOnlyThreeTimes) {
  386. EXPECT_FALSE(IsAccessTokenRequestPending());
  387. // Lock the screen will request a token.
  388. LockScreen();
  389. EXPECT_TRUE(IsAccessTokenRequestPending());
  390. IssueAccessToken(/*is_empty=*/true);
  391. EXPECT_FALSE(IsAccessTokenRequestPending());
  392. // 1st retry.
  393. task_environment()->FastForwardBy(GetRefreshTokenDelay() * 1.1);
  394. EXPECT_TRUE(IsAccessTokenRequestPending());
  395. IssueAccessToken(/*is_empty=*/true);
  396. EXPECT_FALSE(IsAccessTokenRequestPending());
  397. // 2nd retry.
  398. task_environment()->FastForwardBy(GetRefreshTokenDelay() * 1.1);
  399. EXPECT_TRUE(IsAccessTokenRequestPending());
  400. IssueAccessToken(/*is_empty=*/true);
  401. EXPECT_FALSE(IsAccessTokenRequestPending());
  402. // 3rd retry.
  403. task_environment()->FastForwardBy(GetRefreshTokenDelay() * 1.1);
  404. EXPECT_TRUE(IsAccessTokenRequestPending());
  405. IssueAccessToken(/*is_empty=*/true);
  406. EXPECT_FALSE(IsAccessTokenRequestPending());
  407. // Will not retry.
  408. task_environment()->FastForwardBy(GetRefreshTokenDelay() * 1.1);
  409. EXPECT_FALSE(IsAccessTokenRequestPending());
  410. CloseAmbientScreen();
  411. }
  412. TEST_F(AmbientControllerTest,
  413. CheckAcquireAndReleaseWakeLockWhenBatteryIsCharging) {
  414. // Simulate a device being connected to a charger initially.
  415. SetPowerStateCharging();
  416. // Lock screen to start ambient mode, and flush the loop to ensure
  417. // the acquire wake lock request has reached the wake lock provider.
  418. LockScreen();
  419. FastForwardToLockScreenTimeout();
  420. FastForwardTiny();
  421. EXPECT_EQ(1, GetNumOfActiveWakeLocks(
  422. device::mojom::WakeLockType::kPreventDisplaySleep));
  423. HideAmbientScreen();
  424. base::RunLoop().RunUntilIdle();
  425. EXPECT_EQ(0, GetNumOfActiveWakeLocks(
  426. device::mojom::WakeLockType::kPreventDisplaySleep));
  427. // Ambient screen showup again after inactivity.
  428. FastForwardToLockScreenTimeout();
  429. EXPECT_EQ(1, GetNumOfActiveWakeLocks(
  430. device::mojom::WakeLockType::kPreventDisplaySleep));
  431. // Unlock screen to exit ambient mode.
  432. UnlockScreen();
  433. base::RunLoop().RunUntilIdle();
  434. EXPECT_EQ(0, GetNumOfActiveWakeLocks(
  435. device::mojom::WakeLockType::kPreventDisplaySleep));
  436. }
  437. TEST_F(AmbientControllerTest,
  438. CheckAcquireAndReleaseWakeLockWhenBatteryBatteryIsFullAndDischarging) {
  439. SetPowerStateDischarging();
  440. SetBatteryPercent(100.f);
  441. SetExternalPowerConnected();
  442. // Lock screen to start ambient mode, and flush the loop to ensure
  443. // the acquire wake lock request has reached the wake lock provider.
  444. LockScreen();
  445. FastForwardToLockScreenTimeout();
  446. FastForwardTiny();
  447. EXPECT_EQ(1, GetNumOfActiveWakeLocks(
  448. device::mojom::WakeLockType::kPreventDisplaySleep));
  449. HideAmbientScreen();
  450. base::RunLoop().RunUntilIdle();
  451. EXPECT_EQ(0, GetNumOfActiveWakeLocks(
  452. device::mojom::WakeLockType::kPreventDisplaySleep));
  453. // Ambient screen showup again after inactivity.
  454. FastForwardToLockScreenTimeout();
  455. EXPECT_EQ(1, GetNumOfActiveWakeLocks(
  456. device::mojom::WakeLockType::kPreventDisplaySleep));
  457. // Unlock screen to exit ambient mode.
  458. UnlockScreen();
  459. base::RunLoop().RunUntilIdle();
  460. EXPECT_EQ(0, GetNumOfActiveWakeLocks(
  461. device::mojom::WakeLockType::kPreventDisplaySleep));
  462. }
  463. TEST_F(AmbientControllerTest,
  464. CheckAcquireAndReleaseWakeLockWhenBatteryStateChanged) {
  465. SetPowerStateDischarging();
  466. SetExternalPowerConnected();
  467. SetBatteryPercent(50.f);
  468. // Lock screen to start ambient mode.
  469. LockScreen();
  470. FastForwardToLockScreenTimeout();
  471. FastForwardTiny();
  472. EXPECT_TRUE(ambient_controller()->IsShown());
  473. // Should not acquire wake lock when device is not charging and with low
  474. // battery.
  475. EXPECT_EQ(0, GetNumOfActiveWakeLocks(
  476. device::mojom::WakeLockType::kPreventDisplaySleep));
  477. // Connect the device with a charger.
  478. SetPowerStateCharging();
  479. base::RunLoop().RunUntilIdle();
  480. // Should acquire the wake lock when battery is charging.
  481. EXPECT_EQ(1, GetNumOfActiveWakeLocks(
  482. device::mojom::WakeLockType::kPreventDisplaySleep));
  483. // Simulates a full battery.
  484. SetBatteryPercent(100.f);
  485. // Should keep the wake lock as the charger is still connected.
  486. EXPECT_EQ(1, GetNumOfActiveWakeLocks(
  487. device::mojom::WakeLockType::kPreventDisplaySleep));
  488. // Disconnects the charger again.
  489. SetPowerStateDischarging();
  490. base::RunLoop().RunUntilIdle();
  491. // Should keep the wake lock when battery is high.
  492. EXPECT_EQ(1, GetNumOfActiveWakeLocks(
  493. device::mojom::WakeLockType::kPreventDisplaySleep));
  494. SetBatteryPercent(50.f);
  495. base::RunLoop().RunUntilIdle();
  496. // Should release the wake lock when battery is not charging and low.
  497. EXPECT_EQ(0, GetNumOfActiveWakeLocks(
  498. device::mojom::WakeLockType::kPreventDisplaySleep));
  499. SetBatteryPercent(100.f);
  500. base::RunLoop().RunUntilIdle();
  501. // Should take the wake lock when battery is not charging and high.
  502. EXPECT_EQ(1, GetNumOfActiveWakeLocks(
  503. device::mojom::WakeLockType::kPreventDisplaySleep));
  504. SetExternalPowerDisconnected();
  505. base::RunLoop().RunUntilIdle();
  506. // Should release the wake lock when power is not connected.
  507. EXPECT_EQ(0, GetNumOfActiveWakeLocks(
  508. device::mojom::WakeLockType::kPreventDisplaySleep));
  509. // An unbalanced release should do nothing.
  510. UnlockScreen();
  511. EXPECT_EQ(0, GetNumOfActiveWakeLocks(
  512. device::mojom::WakeLockType::kPreventDisplaySleep));
  513. }
  514. // TODO(cowmoo): find a way to simulate events to trigger |UserActivityDetector|
  515. TEST_P(AmbientControllerTestForAnyTheme, ShouldDismissContainerViewOnEvents) {
  516. std::vector<std::unique_ptr<ui::Event>> events;
  517. for (auto mouse_event_type : {ui::ET_MOUSE_PRESSED, ui::ET_MOUSE_MOVED}) {
  518. events.emplace_back(std::make_unique<ui::MouseEvent>(
  519. mouse_event_type, gfx::Point(), gfx::Point(), base::TimeTicks(),
  520. ui::EF_NONE, ui::EF_NONE));
  521. }
  522. events.emplace_back(std::make_unique<ui::MouseWheelEvent>(
  523. gfx::Vector2d(), gfx::PointF(), gfx::PointF(), base::TimeTicks(),
  524. ui::EF_NONE, ui::EF_NONE));
  525. events.emplace_back(std::make_unique<ui::ScrollEvent>(
  526. ui::ET_SCROLL, gfx::PointF(), gfx::PointF(), base::TimeTicks(),
  527. ui::EF_NONE, /*x_offset=*/0.0f,
  528. /*y_offset=*/0.0f,
  529. /*x_offset_ordinal=*/0.0f,
  530. /*x_offset_ordinal=*/0.0f, /*finger_count=*/2));
  531. events.emplace_back(std::make_unique<ui::TouchEvent>(
  532. ui::ET_TOUCH_PRESSED, gfx::PointF(), gfx::PointF(), base::TimeTicks(),
  533. ui::PointerDetails()));
  534. for (const auto& event : events) {
  535. ShowAmbientScreen();
  536. FastForwardTiny();
  537. EXPECT_TRUE(WidgetsVisible());
  538. ambient_controller()->OnUserActivity(event.get());
  539. FastForwardTiny();
  540. EXPECT_TRUE(GetContainerViews().empty());
  541. // Clean up.
  542. CloseAmbientScreen();
  543. }
  544. }
  545. TEST_P(AmbientControllerTestForAnyTheme, ShouldDismissAndThenComesBack) {
  546. LockScreen();
  547. FastForwardToLockScreenTimeout();
  548. FastForwardTiny();
  549. EXPECT_TRUE(WidgetsVisible());
  550. ui::MouseEvent mouse_event(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(),
  551. base::TimeTicks(), ui::EF_NONE, ui::EF_NONE);
  552. ambient_controller()->OnUserActivity(&mouse_event);
  553. FastForwardTiny();
  554. EXPECT_TRUE(GetContainerViews().empty());
  555. FastForwardToLockScreenTimeout();
  556. FastForwardTiny();
  557. EXPECT_TRUE(WidgetsVisible());
  558. }
  559. TEST_P(AmbientControllerTestForAnyTheme, ShouldDismissContainerViewOnKeyEvent) {
  560. // Without user interaction, should show ambient mode.
  561. ShowAmbientScreen();
  562. EXPECT_TRUE(WidgetsVisible());
  563. CloseAmbientScreen();
  564. // When ambient is shown, OnUserActivity() should ignore key event.
  565. ambient_controller()->ShowUi();
  566. EXPECT_TRUE(ambient_controller()->IsShown());
  567. // General key press will exit ambient mode.
  568. // Simulate key press to close the widget.
  569. PressAndReleaseKey(ui::VKEY_A);
  570. EXPECT_FALSE(ambient_controller()->IsShown());
  571. }
  572. TEST_F(AmbientControllerTest,
  573. ShouldDismissContainerViewOnKeyEventWhenLockScreenInBackground) {
  574. GetSessionControllerClient()->SetShouldLockScreenAutomatically(true);
  575. SetPowerStateCharging();
  576. EXPECT_FALSE(ambient_controller()->IsShown());
  577. // Should not lock the device and enter ambient mode when the screen is
  578. // dimmed.
  579. SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
  580. EXPECT_FALSE(IsLocked());
  581. EXPECT_TRUE(ambient_controller()->IsShown());
  582. FastForwardToBackgroundLockScreenTimeout();
  583. EXPECT_TRUE(IsLocked());
  584. // Should not disrupt ongoing ambient mode.
  585. EXPECT_TRUE(ambient_controller()->IsShown());
  586. // General key press will exit ambient mode.
  587. // Simulate key press to close the widget.
  588. PressAndReleaseKey(ui::VKEY_A);
  589. EXPECT_FALSE(ambient_controller()->IsShown());
  590. }
  591. TEST_F(AmbientControllerTest,
  592. ShouldShowAmbientScreenWithLockscreenWhenScreenIsDimmed) {
  593. GetSessionControllerClient()->SetShouldLockScreenAutomatically(true);
  594. SetPowerStateCharging();
  595. EXPECT_FALSE(ambient_controller()->IsShown());
  596. // Should enter ambient mode when the screen is dimmed.
  597. SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
  598. EXPECT_FALSE(IsLocked());
  599. EXPECT_TRUE(ambient_controller()->IsShown());
  600. FastForwardToBackgroundLockScreenTimeout();
  601. EXPECT_TRUE(IsLocked());
  602. // Should not disrupt ongoing ambient mode.
  603. EXPECT_TRUE(ambient_controller()->IsShown());
  604. // Closes ambient for clean-up.
  605. UnlockScreen();
  606. EXPECT_FALSE(ambient_controller()->IsShown());
  607. }
  608. TEST_F(AmbientControllerTest,
  609. ShouldShowAmbientScreenWithLockscreenWithNoisyPowerEvents) {
  610. GetSessionControllerClient()->SetShouldLockScreenAutomatically(true);
  611. SetPowerStateCharging();
  612. EXPECT_FALSE(ambient_controller()->IsShown());
  613. // Should enter ambient mode when the screen is dimmed.
  614. SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
  615. EXPECT_FALSE(IsLocked());
  616. FastForwardTiny();
  617. EXPECT_TRUE(ambient_controller()->IsShown());
  618. FastForwardHalfLockScreenDelay();
  619. SetPowerStateCharging();
  620. FastForwardHalfLockScreenDelay();
  621. SetPowerStateCharging();
  622. EXPECT_TRUE(IsLocked());
  623. // Should not disrupt ongoing ambient mode.
  624. EXPECT_TRUE(ambient_controller()->IsShown());
  625. // Closes ambient for clean-up.
  626. UnlockScreen();
  627. EXPECT_FALSE(ambient_controller()->IsShown());
  628. }
  629. TEST_F(AmbientControllerTest,
  630. ShouldShowAmbientScreenWithoutLockscreenWhenScreenIsDimmed) {
  631. GetSessionControllerClient()->SetShouldLockScreenAutomatically(true);
  632. // When power is discharging, we do not lock the screen with ambient
  633. // mode since we do not prevent the device go to sleep which will natually
  634. // lock the device.
  635. SetPowerStateDischarging();
  636. EXPECT_FALSE(ambient_controller()->IsShown());
  637. // Should not lock the device but still enter ambient mode when the screen is
  638. // dimmed.
  639. SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
  640. EXPECT_FALSE(IsLocked());
  641. EXPECT_TRUE(ambient_controller()->IsShown());
  642. FastForwardToLockScreenTimeout();
  643. FastForwardTiny();
  644. EXPECT_TRUE(ambient_controller()->IsShown());
  645. FastForwardToBackgroundLockScreenTimeout();
  646. EXPECT_FALSE(IsLocked());
  647. // Closes ambient for clean-up.
  648. CloseAmbientScreen();
  649. }
  650. TEST_F(AmbientControllerTest, ShouldShowAmbientScreenWhenScreenIsDimmed) {
  651. GetSessionControllerClient()->SetShouldLockScreenAutomatically(false);
  652. SetPowerStateCharging();
  653. EXPECT_FALSE(ambient_controller()->IsShown());
  654. // Should not lock the device but enter ambient mode when the screen is
  655. // dimmed.
  656. SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
  657. EXPECT_FALSE(IsLocked());
  658. FastForwardTiny();
  659. EXPECT_TRUE(ambient_controller()->IsShown());
  660. FastForwardToBackgroundLockScreenTimeout();
  661. EXPECT_FALSE(IsLocked());
  662. // Closes ambient for clean-up.
  663. CloseAmbientScreen();
  664. }
  665. TEST_F(AmbientControllerTest, HandlesPreviousImageFailuresWithLockScreen) {
  666. // Simulate failures to download FIFE urls. Ambient mode should close and
  667. // remember the old failure.
  668. SetDownloadPhotoData("");
  669. LockScreen();
  670. FastForwardToLockScreenTimeout();
  671. FastForwardTiny();
  672. ASSERT_TRUE(ambient_controller()->IsShown());
  673. AmbientUiVisibilityBarrier ambient_closed_barrier(
  674. AmbientUiVisibility::kClosed);
  675. ambient_closed_barrier.WaitWithTimeout(base::Seconds(15));
  676. ASSERT_FALSE(ambient_controller()->IsShown());
  677. UnlockScreen();
  678. // Now simulate FIFE downloads starting to work again. The device should be
  679. // able to enter ambient mode.
  680. ClearDownloadPhotoData();
  681. LockScreen();
  682. FastForwardToLockScreenTimeout();
  683. FastForwardTiny();
  684. ASSERT_TRUE(ambient_controller()->IsShown());
  685. }
  686. TEST_F(AmbientControllerTest, HandlesPreviousImageFailuresWithDimmedScreen) {
  687. GetSessionControllerClient()->SetShouldLockScreenAutomatically(false);
  688. SetPowerStateCharging();
  689. // Simulate failures to download FIFE urls. Ambient mode should close and
  690. // remember the old failure.
  691. SetDownloadPhotoData("");
  692. SetScreenIdleStateAndWait(/*is_screen_dimmed=*/true, /*is_off=*/false);
  693. FastForwardTiny();
  694. ASSERT_TRUE(ambient_controller()->IsShown());
  695. AmbientUiVisibilityBarrier ambient_closed_barrier(
  696. AmbientUiVisibility::kClosed);
  697. ambient_closed_barrier.WaitWithTimeout(base::Seconds(15));
  698. ASSERT_FALSE(ambient_controller()->IsShown());
  699. SetScreenIdleStateAndWait(/*is_screen_dimmed=*/false, /*is_off=*/false);
  700. // Usually would enter ambient mode when the screen is dimmed, but this time
  701. // it shouldn't because of the previous image failures.
  702. SetScreenIdleStateAndWait(/*is_screen_dimmed=*/true, /*is_off=*/false);
  703. FastForwardTiny();
  704. ASSERT_FALSE(ambient_controller()->IsShown());
  705. SetScreenIdleStateAndWait(/*is_screen_dimmed=*/false, /*is_off=*/false);
  706. // Now simulate FIFE downloads starting to work again. The device should be
  707. // able to enter ambient mode.
  708. ClearDownloadPhotoData();
  709. SetScreenIdleStateAndWait(/*is_screen_dimmed=*/true, /*is_off=*/false);
  710. FastForwardTiny();
  711. ASSERT_TRUE(ambient_controller()->IsShown());
  712. // Closes ambient for clean-up.
  713. CloseAmbientScreen();
  714. }
  715. TEST_F(AmbientControllerTest, ShouldHideAmbientScreenWhenDisplayIsOff) {
  716. GetSessionControllerClient()->SetShouldLockScreenAutomatically(false);
  717. EXPECT_FALSE(ambient_controller()->IsShown());
  718. // Should not lock the device and enter ambient mode when the screen is
  719. // dimmed.
  720. SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
  721. EXPECT_FALSE(IsLocked());
  722. FastForwardTiny();
  723. EXPECT_TRUE(ambient_controller()->IsShown());
  724. // Should dismiss ambient mode screen.
  725. SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/true);
  726. FastForwardTiny();
  727. EXPECT_FALSE(ambient_controller()->IsShown());
  728. // Screen back on again, should not have ambient screen.
  729. SetScreenIdleStateAndWait(/*dimmed=*/false, /*off=*/false);
  730. FastForwardTiny();
  731. EXPECT_FALSE(ambient_controller()->IsShown());
  732. }
  733. TEST_F(AmbientControllerTest,
  734. ShouldHideAmbientScreenWhenDisplayIsOffThenComesBackWithLockScreen) {
  735. GetSessionControllerClient()->SetShouldLockScreenAutomatically(true);
  736. SetPowerStateCharging();
  737. EXPECT_FALSE(ambient_controller()->IsShown());
  738. // Should not lock the device and enter ambient mode when the screen is
  739. // dimmed.
  740. SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
  741. EXPECT_FALSE(IsLocked());
  742. FastForwardToLockScreenTimeout();
  743. FastForwardTiny();
  744. EXPECT_TRUE(ambient_controller()->IsShown());
  745. FastForwardToBackgroundLockScreenTimeout();
  746. EXPECT_TRUE(IsLocked());
  747. // Should dismiss ambient mode screen.
  748. SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/true);
  749. FastForwardTiny();
  750. EXPECT_FALSE(ambient_controller()->IsShown());
  751. // Screen back on again, should not have ambient screen, but still has lock
  752. // screen.
  753. SetScreenIdleStateAndWait(/*dimmed=*/false, /*off=*/false);
  754. EXPECT_TRUE(IsLocked());
  755. EXPECT_FALSE(ambient_controller()->IsShown());
  756. FastForwardToLockScreenTimeout();
  757. FastForwardTiny();
  758. EXPECT_TRUE(ambient_controller()->IsShown());
  759. }
  760. TEST_F(AmbientControllerTest,
  761. ShouldHideAmbientScreenWhenDisplayIsOffAndNotStartWhenLockScreen) {
  762. GetSessionControllerClient()->SetShouldLockScreenAutomatically(true);
  763. SetPowerStateDischarging();
  764. EXPECT_FALSE(ambient_controller()->IsShown());
  765. // Should not lock the device and enter ambient mode when the screen is
  766. // dimmed.
  767. SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
  768. EXPECT_FALSE(IsLocked());
  769. FastForwardTiny();
  770. EXPECT_TRUE(ambient_controller()->IsShown());
  771. // Should not lock the device because the device is not charging.
  772. FastForwardToBackgroundLockScreenTimeout();
  773. EXPECT_FALSE(IsLocked());
  774. // Should dismiss ambient mode screen.
  775. SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/true);
  776. FastForwardTiny();
  777. EXPECT_FALSE(ambient_controller()->IsShown());
  778. // Lock screen will not start ambient mode.
  779. LockScreen();
  780. EXPECT_TRUE(IsLocked());
  781. FastForwardToLockScreenTimeout();
  782. FastForwardTiny();
  783. EXPECT_FALSE(ambient_controller()->IsShown());
  784. // Screen back on again, should not have ambient screen, but still has lock
  785. // screen.
  786. SetScreenIdleStateAndWait(/*dimmed=*/false, /*off=*/false);
  787. EXPECT_TRUE(IsLocked());
  788. EXPECT_FALSE(ambient_controller()->IsShown());
  789. FastForwardToLockScreenTimeout();
  790. FastForwardTiny();
  791. EXPECT_TRUE(ambient_controller()->IsShown());
  792. }
  793. TEST_F(AmbientControllerTest, HandlesPhotoDownloadOutage) {
  794. SetDownloadPhotoData("");
  795. LockScreen();
  796. FastForwardToLockScreenTimeout();
  797. ASSERT_TRUE(ambient_controller()->IsShown());
  798. AmbientUiVisibilityBarrier ambient_closed_barrier(
  799. AmbientUiVisibility::kClosed);
  800. ambient_closed_barrier.WaitWithTimeout(base::Seconds(15));
  801. EXPECT_FALSE(ambient_controller()->IsShown());
  802. }
  803. TEST_P(AmbientControllerTestForAnyTheme, HideCursor) {
  804. auto* cursor_manager = Shell::Get()->cursor_manager();
  805. LockScreen();
  806. cursor_manager->ShowCursor();
  807. EXPECT_TRUE(cursor_manager->IsCursorVisible());
  808. FastForwardToLockScreenTimeout();
  809. FastForwardTiny();
  810. EXPECT_FALSE(GetContainerViews().empty());
  811. EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
  812. AmbientUiVisibility::kShown);
  813. EXPECT_TRUE(ambient_controller()->IsShown());
  814. EXPECT_FALSE(cursor_manager->IsCursorVisible());
  815. // Clean up.
  816. UnlockScreen();
  817. EXPECT_FALSE(ambient_controller()->IsShown());
  818. }
  819. TEST_P(AmbientControllerTestForAnyTheme, ShowsOnMultipleDisplays) {
  820. UpdateDisplay("800x600,800x600");
  821. FastForwardTiny();
  822. ShowAmbientScreen();
  823. auto* screen = display::Screen::GetScreen();
  824. EXPECT_EQ(screen->GetNumDisplays(), 2);
  825. EXPECT_EQ(GetContainerViews().size(), 2u);
  826. AmbientViewID expected_child_view_id =
  827. GetParam() == AmbientAnimationTheme::kSlideshow
  828. ? AmbientViewID::kAmbientPhotoView
  829. : AmbientViewID::kAmbientAnimationView;
  830. EXPECT_TRUE(GetContainerViews().front()->GetViewByID(expected_child_view_id));
  831. EXPECT_TRUE(GetContainerViews().back()->GetViewByID(expected_child_view_id));
  832. // Check that each root controller has an ambient widget.
  833. for (auto* ctrl : RootWindowController::root_window_controllers())
  834. EXPECT_TRUE(ctrl->ambient_widget_for_testing() &&
  835. ctrl->ambient_widget_for_testing()->IsVisible());
  836. }
  837. TEST_P(AmbientControllerTestForAnyTheme, RespondsToDisplayAdded) {
  838. // UpdateDisplay triggers a rogue MouseEvent that cancels Ambient mode when
  839. // testing with Xvfb. A corresponding MouseEvent is not fired on a real device
  840. // when an external display is added. Ignore this MouseEvent for testing.
  841. // Store the old |ShouldIgnoreNativePlatformEvents| value and reset it at the
  842. // end of the test.
  843. bool old_should_ignore_events =
  844. ui::PlatformEventSource::ShouldIgnoreNativePlatformEvents();
  845. ui::PlatformEventSource::SetIgnoreNativePlatformEvents(true);
  846. UpdateDisplay("800x600");
  847. ShowAmbientScreen();
  848. auto* screen = display::Screen::GetScreen();
  849. EXPECT_EQ(screen->GetNumDisplays(), 1);
  850. EXPECT_EQ(GetContainerViews().size(), 1u);
  851. UpdateDisplay("800x600,800x600");
  852. FastForwardTiny();
  853. EXPECT_TRUE(WidgetsVisible());
  854. EXPECT_EQ(screen->GetNumDisplays(), 2);
  855. EXPECT_EQ(GetContainerViews().size(), 2u);
  856. for (auto* ctrl : RootWindowController::root_window_controllers())
  857. EXPECT_TRUE(ctrl->ambient_widget_for_testing() &&
  858. ctrl->ambient_widget_for_testing()->IsVisible());
  859. ui::PlatformEventSource::SetIgnoreNativePlatformEvents(
  860. old_should_ignore_events);
  861. }
  862. TEST_P(AmbientControllerTestForAnyTheme, HandlesDisplayRemoved) {
  863. UpdateDisplay("800x600,800x600");
  864. FastForwardTiny();
  865. ShowAmbientScreen();
  866. auto* screen = display::Screen::GetScreen();
  867. EXPECT_EQ(screen->GetNumDisplays(), 2);
  868. EXPECT_EQ(GetContainerViews().size(), 2u);
  869. EXPECT_TRUE(WidgetsVisible());
  870. // Changing to one screen will destroy the widget on the non-primary screen.
  871. UpdateDisplay("800x600");
  872. FastForwardTiny();
  873. EXPECT_EQ(screen->GetNumDisplays(), 1);
  874. EXPECT_EQ(GetContainerViews().size(), 1u);
  875. EXPECT_TRUE(WidgetsVisible());
  876. }
  877. TEST_F(AmbientControllerTest, ClosesAmbientBeforeSuspend) {
  878. LockScreen();
  879. FastForwardToLockScreenTimeout();
  880. EXPECT_TRUE(ambient_controller()->IsShown());
  881. SimulateSystemSuspendAndWait(power_manager::SuspendImminent::Reason::
  882. SuspendImminent_Reason_LID_CLOSED);
  883. EXPECT_FALSE(ambient_controller()->IsShown());
  884. FastForwardToLockScreenTimeout();
  885. // Ambient mode should not resume until SuspendDone is received.
  886. EXPECT_FALSE(ambient_controller()->IsShown());
  887. }
  888. TEST_F(AmbientControllerTest, RestartsAmbientAfterSuspend) {
  889. LockScreen();
  890. FastForwardToLockScreenTimeout();
  891. EXPECT_TRUE(ambient_controller()->IsShown());
  892. SimulateSystemSuspendAndWait(
  893. power_manager::SuspendImminent::Reason::SuspendImminent_Reason_IDLE);
  894. EXPECT_FALSE(ambient_controller()->IsShown());
  895. // This call should be blocked by prior |SuspendImminent| until |SuspendDone|.
  896. ambient_controller()->ShowUi();
  897. EXPECT_FALSE(ambient_controller()->IsShown());
  898. SimulateSystemResumeAndWait();
  899. FastForwardToLockScreenTimeout();
  900. EXPECT_TRUE(ambient_controller()->IsShown());
  901. }
  902. TEST_F(AmbientControllerTest, ObservesPrefsWhenAmbientEnabled) {
  903. SetAmbientModeEnabled(false);
  904. // This pref is always observed.
  905. EXPECT_TRUE(IsPrefObserved(ambient::prefs::kAmbientModeEnabled));
  906. std::vector<std::string> other_prefs{
  907. ambient::prefs::kAmbientModeLockScreenInactivityTimeoutSeconds,
  908. ambient::prefs::kAmbientModeLockScreenBackgroundTimeoutSeconds,
  909. ambient::prefs::kAmbientModePhotoRefreshIntervalSeconds};
  910. for (auto& pref_name : other_prefs)
  911. EXPECT_FALSE(IsPrefObserved(pref_name));
  912. SetAmbientModeEnabled(true);
  913. EXPECT_TRUE(IsPrefObserved(ambient::prefs::kAmbientModeEnabled));
  914. for (auto& pref_name : other_prefs)
  915. EXPECT_TRUE(IsPrefObserved(pref_name));
  916. }
  917. TEST_F(AmbientControllerTest, BindsObserversWhenAmbientEnabled) {
  918. auto* ctrl = ambient_controller();
  919. SetAmbientModeEnabled(false);
  920. // SessionObserver must always be observing to detect when user pref service
  921. // is started.
  922. EXPECT_TRUE(ctrl->session_observer_.IsObserving());
  923. EXPECT_FALSE(AreSessionSpecificObserversBound());
  924. SetAmbientModeEnabled(true);
  925. // Session observer should still be observing.
  926. EXPECT_TRUE(ctrl->session_observer_.IsObserving());
  927. EXPECT_TRUE(AreSessionSpecificObserversBound());
  928. }
  929. TEST_F(AmbientControllerTest, SwitchActiveUsersDoesNotDoubleBindObservers) {
  930. ClearLogin();
  931. SimulateUserLogin(kUser1);
  932. SetAmbientModeEnabled(true);
  933. TestSessionControllerClient* session = GetSessionControllerClient();
  934. // Observers are bound for primary user with Ambient mode enabled.
  935. EXPECT_TRUE(AreSessionSpecificObserversBound());
  936. EXPECT_TRUE(IsPrefObserved(ambient::prefs::kAmbientModeEnabled));
  937. // Observers are still bound when secondary user logs in.
  938. SimulateUserLogin(kUser2);
  939. EXPECT_TRUE(AreSessionSpecificObserversBound());
  940. EXPECT_TRUE(IsPrefObserved(ambient::prefs::kAmbientModeEnabled));
  941. // Observers are not re-bound for primary user when session is active.
  942. session->SwitchActiveUser(AccountId::FromUserEmail(kUser1));
  943. EXPECT_TRUE(AreSessionSpecificObserversBound());
  944. EXPECT_TRUE(IsPrefObserved(ambient::prefs::kAmbientModeEnabled));
  945. // Switch back to secondary user.
  946. session->SwitchActiveUser(AccountId::FromUserEmail(kUser2));
  947. }
  948. TEST_F(AmbientControllerTest, BindsObserversWhenAmbientOn) {
  949. auto* ctrl = ambient_controller();
  950. LockScreen();
  951. // Start monitoring user activity on hidden ui.
  952. EXPECT_TRUE(ctrl->user_activity_observer_.IsObserving());
  953. // Do not monitor power status yet.
  954. EXPECT_FALSE(ctrl->power_status_observer_.IsObserving());
  955. FastForwardToLockScreenTimeout();
  956. EXPECT_TRUE(ctrl->user_activity_observer_.IsObserving());
  957. EXPECT_TRUE(ctrl->power_status_observer_.IsObserving());
  958. UnlockScreen();
  959. EXPECT_FALSE(ctrl->user_activity_observer_.IsObserving());
  960. EXPECT_FALSE(ctrl->power_status_observer_.IsObserving());
  961. }
  962. TEST_P(AmbientControllerTestForAnyTheme,
  963. ShowDismissAmbientScreenUponAssistantQuery) {
  964. // Without user interaction, should show ambient mode.
  965. ShowAmbientScreen();
  966. EXPECT_TRUE(ambient_controller()->IsShown());
  967. // Trigger Assistant interaction.
  968. static_cast<AssistantInteractionControllerImpl*>(
  969. AssistantInteractionController::Get())
  970. ->OnInteractionStarted(AssistantInteractionMetadata());
  971. base::RunLoop().RunUntilIdle();
  972. // Ambient screen should dismiss.
  973. EXPECT_TRUE(GetContainerViews().empty());
  974. EXPECT_FALSE(ambient_controller()->IsShown());
  975. }
  976. // For all test cases that depend on ash ambient resources (lottie files, image
  977. // assets, etc) being present to run.
  978. #if BUILDFLAG(HAS_ASH_AMBIENT_ANIMATION_RESOURCES)
  979. #define ANIMATION_TEST_WITH_RESOURCES(test_case_name) test_case_name
  980. #else
  981. #define ANIMATION_TEST_WITH_RESOURCES(test_case_name) DISABLED_##test_case_name
  982. #endif // BUILDFLAG(HAS_ASH_AMBIENT_ANIMATION_RESOURCES)
  983. TEST_F(AmbientControllerTest,
  984. ANIMATION_TEST_WITH_RESOURCES(RendersCorrectView)) {
  985. base::test::ScopedFeatureList scoped_feature_list;
  986. scoped_feature_list.InitAndEnableFeature(features::kPersonalizationHub);
  987. SetAmbientAnimationTheme(AmbientAnimationTheme::kFeelTheBreeze);
  988. LockScreen();
  989. FastForwardToLockScreenTimeout();
  990. FastForwardTiny();
  991. ASSERT_TRUE(GetContainerView());
  992. EXPECT_FALSE(
  993. GetContainerView()->GetViewByID(AmbientViewID::kAmbientPhotoView));
  994. EXPECT_TRUE(
  995. GetContainerView()->GetViewByID(AmbientViewID::kAmbientAnimationView));
  996. UnlockScreen();
  997. SetAmbientAnimationTheme(AmbientAnimationTheme::kSlideshow);
  998. LockScreen();
  999. FastForwardToLockScreenTimeout();
  1000. FastForwardTiny();
  1001. ASSERT_TRUE(GetContainerView());
  1002. EXPECT_TRUE(
  1003. GetContainerView()->GetViewByID(AmbientViewID::kAmbientPhotoView));
  1004. EXPECT_FALSE(
  1005. GetContainerView()->GetViewByID(AmbientViewID::kAmbientAnimationView));
  1006. UnlockScreen();
  1007. SetAmbientAnimationTheme(AmbientAnimationTheme::kFeelTheBreeze);
  1008. LockScreen();
  1009. FastForwardToLockScreenTimeout();
  1010. FastForwardTiny();
  1011. ASSERT_TRUE(GetContainerView());
  1012. EXPECT_FALSE(
  1013. GetContainerView()->GetViewByID(AmbientViewID::kAmbientPhotoView));
  1014. EXPECT_TRUE(
  1015. GetContainerView()->GetViewByID(AmbientViewID::kAmbientAnimationView));
  1016. }
  1017. TEST_F(AmbientControllerTest,
  1018. ANIMATION_TEST_WITH_RESOURCES(ClearsCacheWhenSwitchingThemes)) {
  1019. base::test::ScopedFeatureList scoped_feature_list;
  1020. scoped_feature_list.InitAndEnableFeature(features::kPersonalizationHub);
  1021. SetAmbientAnimationTheme(AmbientAnimationTheme::kSlideshow);
  1022. LockScreen();
  1023. FastForwardToLockScreenTimeout();
  1024. FastForwardTiny();
  1025. ASSERT_TRUE(GetContainerView());
  1026. ASSERT_FALSE(GetCachedFiles().empty());
  1027. UnlockScreen();
  1028. SetAmbientAnimationTheme(AmbientAnimationTheme::kFeelTheBreeze);
  1029. // Mimic a network outage where no photos can be downloaded. Since the cache
  1030. // should have been cleared when we switched ambient animation themes, the
  1031. // UI shouldn't start with a photo cached during slideshow mode.
  1032. SetDownloadPhotoData(/*data=*/"");
  1033. LockScreen();
  1034. FastForwardToLockScreenTimeout();
  1035. FastForwardTiny();
  1036. EXPECT_FALSE(GetContainerView());
  1037. EXPECT_TRUE(GetCachedFiles().empty());
  1038. }
  1039. TEST_P(AmbientControllerTestForAnyTheme, MetricsEngagementTime) {
  1040. // TODO(esum): Find a better way of fast forwarding time for lottie animations
  1041. // in unit tests. Currently, the whole compositor stack is being used in this
  1042. // test harness and there is no good way to control the frame rate, so
  1043. // FastForwardBy() blocks for long periods of time. Do not make this value
  1044. // too high, or the test is at risk of timing out.
  1045. constexpr base::TimeDelta kExpectedEngagementTime = base::Milliseconds(100);
  1046. base::HistogramTester histogram_tester;
  1047. Shell::Get()->tablet_mode_controller()->SetEnabledForTest(false);
  1048. LockScreen();
  1049. // Unlike other tests, the exact amount of time we spend in ambient mode
  1050. // matters to write the correct test expectation. So fast forward by the
  1051. // exact amount needed to trigger ambient mode.
  1052. // (FastForwardToLockScreenTimeout() adds on a little buffer to the timeout)
  1053. task_environment()->FastForwardBy(ambient_controller()
  1054. ->ambient_ui_model()
  1055. ->lock_screen_inactivity_timeout());
  1056. ASSERT_TRUE(ambient_controller()->IsShown());
  1057. task_environment()->FastForwardBy(kExpectedEngagementTime);
  1058. UnlockScreen();
  1059. ASSERT_FALSE(ambient_controller()->IsShown());
  1060. histogram_tester.ExpectTimeBucketCount(
  1061. "Ash.AmbientMode.EngagementTime.ClamshellMode", kExpectedEngagementTime,
  1062. 1);
  1063. histogram_tester.ExpectTimeBucketCount(
  1064. base::StrCat({"Ash.AmbientMode.EngagementTime.", ToString(GetParam())}),
  1065. kExpectedEngagementTime, 1);
  1066. // Now do the same sequence in tablet mode.
  1067. Shell::Get()->tablet_mode_controller()->SetEnabledForTest(true);
  1068. LockScreen();
  1069. task_environment()->FastForwardBy(ambient_controller()
  1070. ->ambient_ui_model()
  1071. ->lock_screen_inactivity_timeout());
  1072. ASSERT_TRUE(ambient_controller()->IsShown());
  1073. task_environment()->FastForwardBy(kExpectedEngagementTime);
  1074. UnlockScreen();
  1075. ASSERT_FALSE(ambient_controller()->IsShown());
  1076. histogram_tester.ExpectTimeBucketCount(
  1077. "Ash.AmbientMode.EngagementTime.TabletMode", kExpectedEngagementTime, 1);
  1078. histogram_tester.ExpectTimeBucketCount(
  1079. base::StrCat({"Ash.AmbientMode.EngagementTime.", ToString(GetParam())}),
  1080. kExpectedEngagementTime, 2);
  1081. }
  1082. TEST_P(AmbientControllerTestForAnyTheme, MetricsStartupTime) {
  1083. base::HistogramTester histogram_tester;
  1084. LockScreen();
  1085. FastForwardToLockScreenTimeout();
  1086. FastForwardTiny();
  1087. ASSERT_TRUE(ambient_controller()->IsShown());
  1088. histogram_tester.ExpectTotalCount(
  1089. base::StrCat({"Ash.AmbientMode.StartupTime.", ToString(GetParam())}), 1);
  1090. UnlockScreen();
  1091. ASSERT_FALSE(ambient_controller()->IsShown());
  1092. LockScreen();
  1093. FastForwardToLockScreenTimeout();
  1094. FastForwardTiny();
  1095. ASSERT_TRUE(ambient_controller()->IsShown());
  1096. histogram_tester.ExpectTotalCount(
  1097. base::StrCat({"Ash.AmbientMode.StartupTime.", ToString(GetParam())}), 2);
  1098. }
  1099. TEST_P(AmbientControllerTestForAnyTheme, MetricsStartupTimeFailedToStart) {
  1100. // Simulate IMAX outage that doesn't return any photos.
  1101. backend_controller()->SetFetchScreenUpdateInfoResponseSize(0);
  1102. base::HistogramTester histogram_tester;
  1103. LockScreen();
  1104. FastForwardToLockScreenTimeout();
  1105. task_environment()->FastForwardBy(base::Minutes(1));
  1106. ASSERT_TRUE(GetContainerViews().empty());
  1107. UnlockScreen();
  1108. histogram_tester.ExpectUniqueTimeSample(
  1109. base::StrCat({"Ash.AmbientMode.StartupTime.", ToString(GetParam())}),
  1110. base::Minutes(1), 1);
  1111. }
  1112. } // namespace ash