scrollbar_animation_controller_unittest.cc 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  1. // Copyright 2013 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 "cc/input/scrollbar_animation_controller.h"
  5. #include <utility>
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/time/time.h"
  8. #include "cc/layers/solid_color_scrollbar_layer_impl.h"
  9. #include "cc/test/layer_tree_impl_test_base.h"
  10. #include "cc/trees/layer_tree_impl.h"
  11. #include "testing/gmock/include/gmock/gmock.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. using testing::AtLeast;
  14. using testing::Mock;
  15. using testing::NiceMock;
  16. using testing::_;
  17. namespace cc {
  18. namespace {
  19. const float kIdleThicknessScale =
  20. SingleScrollbarAnimationControllerThinning::kIdleThicknessScale;
  21. const int kThumbThickness = 10;
  22. class MockScrollbarAnimationControllerClient
  23. : public ScrollbarAnimationControllerClient {
  24. public:
  25. MockScrollbarAnimationControllerClient(LayerTreeHostImpl* host_impl,
  26. bool is_fluent)
  27. : host_impl_(host_impl), is_fluent_(is_fluent) {}
  28. ~MockScrollbarAnimationControllerClient() override = default;
  29. void PostDelayedScrollbarAnimationTask(base::OnceClosure start_fade,
  30. base::TimeDelta delay) override {
  31. start_fade_ = std::move(start_fade);
  32. delay_ = delay;
  33. }
  34. void SetNeedsRedrawForScrollbarAnimation() override {}
  35. void SetNeedsAnimateForScrollbarAnimation() override {}
  36. ScrollbarSet ScrollbarsFor(ElementId scroll_element_id) const override {
  37. return host_impl_->ScrollbarsFor(scroll_element_id);
  38. }
  39. MOCK_METHOD0(DidChangeScrollbarVisibility, void());
  40. bool IsFluentScrollbar() const override { return is_fluent_; }
  41. base::OnceClosure& start_fade() { return start_fade_; }
  42. base::TimeDelta& delay() { return delay_; }
  43. private:
  44. base::OnceClosure start_fade_;
  45. base::TimeDelta delay_;
  46. raw_ptr<LayerTreeHostImpl> host_impl_;
  47. bool is_fluent_;
  48. };
  49. class ScrollbarAnimationControllerAuraOverlayTest
  50. : public LayerTreeImplTestBase,
  51. public testing::Test {
  52. public:
  53. explicit ScrollbarAnimationControllerAuraOverlayTest(bool is_fluent = false)
  54. : client_(host_impl(), is_fluent) {}
  55. void ExpectScrollbarsOpacity(float opacity) {
  56. EXPECT_FLOAT_EQ(opacity, v_scrollbar_layer_->Opacity());
  57. EXPECT_FLOAT_EQ(opacity, h_scrollbar_layer_->Opacity());
  58. }
  59. protected:
  60. const base::TimeDelta kFadeDelay = base::Seconds(4);
  61. const base::TimeDelta kFadeDuration = base::Seconds(3);
  62. const base::TimeDelta kThinningDuration = base::Seconds(2);
  63. void SetUp() override {
  64. const int kTrackStart = 0;
  65. const int kTrackLength = 100;
  66. const bool kIsLeftSideVerticalScrollbar = false;
  67. scroll_layer_ = AddLayer<LayerImpl>();
  68. h_scrollbar_layer_ = AddLayer<SolidColorScrollbarLayerImpl>(
  69. ScrollbarOrientation::HORIZONTAL, kThumbThickness, kTrackStart,
  70. kIsLeftSideVerticalScrollbar);
  71. v_scrollbar_layer_ = AddLayer<SolidColorScrollbarLayerImpl>(
  72. ScrollbarOrientation::VERTICAL, kThumbThickness, kTrackStart,
  73. kIsLeftSideVerticalScrollbar);
  74. SetElementIdsForTesting();
  75. clip_layer_ = root_layer();
  76. clip_layer_->SetBounds(gfx::Size(100, 100));
  77. scroll_layer_->SetBounds(gfx::Size(200, 200));
  78. CopyProperties(clip_layer_, scroll_layer_);
  79. CreateTransformNode(scroll_layer_);
  80. CreateScrollNode(scroll_layer_, gfx::Size(100, 100));
  81. v_scrollbar_layer_->SetBounds(gfx::Size(kThumbThickness, kTrackLength));
  82. v_scrollbar_layer_->SetScrollElementId(scroll_layer_->element_id());
  83. CopyProperties(scroll_layer_, v_scrollbar_layer_);
  84. v_scrollbar_layer_->SetOffsetToTransformParent(gfx::Vector2dF(90, 0));
  85. auto& v_scrollbar_effect = CreateEffectNode(v_scrollbar_layer_);
  86. v_scrollbar_effect.opacity = 0.f;
  87. v_scrollbar_effect.has_potential_opacity_animation = true;
  88. h_scrollbar_layer_->SetBounds(gfx::Size(kTrackLength, kThumbThickness));
  89. h_scrollbar_layer_->SetScrollElementId(scroll_layer_->element_id());
  90. CopyProperties(scroll_layer_, h_scrollbar_layer_);
  91. h_scrollbar_layer_->SetOffsetToTransformParent(gfx::Vector2dF(0, 90));
  92. auto& h_scrollbar_effect = CreateEffectNode(h_scrollbar_layer_);
  93. h_scrollbar_effect.opacity = 0.f;
  94. h_scrollbar_effect.has_potential_opacity_animation = true;
  95. UpdateActiveTreeDrawProperties();
  96. scrollbar_controller_ = ScrollbarAnimationController::
  97. CreateScrollbarAnimationControllerAuraOverlay(
  98. scroll_layer_->element_id(), &client_, kFadeDelay, kFadeDuration,
  99. kThinningDuration, 0.0f);
  100. v_scrollbar_layer_->SetCurrentPos(0);
  101. h_scrollbar_layer_->SetCurrentPos(0);
  102. mouse_move_distance_to_trigger_fade_in_ =
  103. scrollbar_controller_
  104. ->GetScrollbarAnimationController(ScrollbarOrientation::VERTICAL)
  105. .MouseMoveDistanceToTriggerFadeIn();
  106. mouse_move_distance_to_trigger_expand_ =
  107. scrollbar_controller_
  108. ->GetScrollbarAnimationController(ScrollbarOrientation::VERTICAL)
  109. .MouseMoveDistanceToTriggerExpand();
  110. }
  111. // Return a point with given offset from the top-left of vertical scrollbar.
  112. gfx::PointF NearVerticalScrollbarBegin(float offset_x, float offset_y) {
  113. gfx::PointF p(90, 0);
  114. p.Offset(offset_x, offset_y);
  115. return p;
  116. }
  117. // Return a point with given offset from the bottom-left of vertical
  118. // scrollbar.
  119. gfx::PointF NearVerticalScrollbarEnd(float offset_x, float offset_y) {
  120. gfx::PointF p(90, 90);
  121. p.Offset(offset_x, offset_y);
  122. return p;
  123. }
  124. // Return a point with given offset from the top-left of horizontal scrollbar.
  125. gfx::PointF NearHorizontalScrollbarBegin(float offset_x, float offset_y) {
  126. gfx::PointF p(0, 90);
  127. p.Offset(offset_x, offset_y);
  128. return p;
  129. }
  130. float mouse_move_distance_to_trigger_fade_in_;
  131. float mouse_move_distance_to_trigger_expand_;
  132. std::unique_ptr<ScrollbarAnimationController> scrollbar_controller_;
  133. raw_ptr<LayerImpl> clip_layer_;
  134. raw_ptr<LayerImpl> scroll_layer_;
  135. raw_ptr<SolidColorScrollbarLayerImpl> v_scrollbar_layer_;
  136. raw_ptr<SolidColorScrollbarLayerImpl> h_scrollbar_layer_;
  137. NiceMock<MockScrollbarAnimationControllerClient> client_;
  138. };
  139. class ScrollbarAnimationControllerFluentOverlayTest
  140. : public ScrollbarAnimationControllerAuraOverlayTest {
  141. public:
  142. ScrollbarAnimationControllerFluentOverlayTest()
  143. : ScrollbarAnimationControllerAuraOverlayTest(/* is_fluent */ true) {}
  144. void SetUp() override {
  145. ScrollbarAnimationControllerAuraOverlayTest::SetUp();
  146. // Mock initial call for did request show on page load.
  147. scrollbar_controller_->DidRequestShow();
  148. }
  149. };
  150. // Check initialization of scrollbar. Should start off invisible and thin.
  151. TEST_F(ScrollbarAnimationControllerAuraOverlayTest, Idle) {
  152. ExpectScrollbarsOpacity(0);
  153. EXPECT_TRUE(scrollbar_controller_->ScrollbarsHidden());
  154. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  155. v_scrollbar_layer_->thumb_thickness_scale_factor());
  156. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  157. v_scrollbar_layer_->thumb_thickness_scale_factor());
  158. }
  159. // Check that scrollbar appears again when the layer becomes scrollable.
  160. TEST_F(ScrollbarAnimationControllerAuraOverlayTest, AppearOnResize) {
  161. base::TimeTicks time;
  162. time += base::Seconds(1);
  163. scrollbar_controller_->DidScrollUpdate();
  164. ExpectScrollbarsOpacity(1);
  165. // Make the Layer non-scrollable, scrollbar disappears.
  166. clip_layer_->SetBounds(gfx::Size(200, 200));
  167. GetScrollNode(scroll_layer_.get())->container_bounds = gfx::Size(200, 200);
  168. scroll_layer_->UpdateScrollable();
  169. UpdateActiveTreeDrawProperties();
  170. scrollbar_controller_->DidScrollUpdate();
  171. ExpectScrollbarsOpacity(0);
  172. // Make the layer scrollable, scrollbar appears again.
  173. clip_layer_->SetBounds(gfx::Size(100, 100));
  174. GetScrollNode(scroll_layer_.get())->container_bounds = gfx::Size(100, 100);
  175. scroll_layer_->UpdateScrollable();
  176. UpdateActiveTreeDrawProperties();
  177. scrollbar_controller_->DidScrollUpdate();
  178. ExpectScrollbarsOpacity(1);
  179. }
  180. // Check that scrollbar disappears when the layer becomes non-scrollable.
  181. TEST_F(ScrollbarAnimationControllerAuraOverlayTest, HideOnResize) {
  182. base::TimeTicks time;
  183. time += base::Seconds(1);
  184. EXPECT_EQ(gfx::Size(200, 200), scroll_layer_->bounds());
  185. // Shrink along X axis, horizontal scrollbar should appear.
  186. clip_layer_->SetBounds(gfx::Size(100, 200));
  187. EXPECT_EQ(gfx::Size(100, 200), clip_layer_->bounds());
  188. GetScrollNode(scroll_layer_.get())->container_bounds = gfx::Size(100, 200);
  189. scroll_layer_->UpdateScrollable();
  190. UpdateActiveTreeDrawProperties();
  191. scrollbar_controller_->DidScrollUpdate();
  192. EXPECT_FLOAT_EQ(1, h_scrollbar_layer_->Opacity());
  193. // Shrink along Y axis and expand along X, horizontal scrollbar
  194. // should disappear.
  195. clip_layer_->SetBounds(gfx::Size(200, 100));
  196. EXPECT_EQ(gfx::Size(200, 100), clip_layer_->bounds());
  197. GetScrollNode(scroll_layer_.get())->container_bounds = gfx::Size(200, 100);
  198. scroll_layer_->UpdateScrollable();
  199. UpdateActiveTreeDrawProperties();
  200. scrollbar_controller_->DidScrollUpdate();
  201. EXPECT_FLOAT_EQ(0.0f, h_scrollbar_layer_->Opacity());
  202. }
  203. // Scroll content. Confirm the scrollbar appears and fades out.
  204. TEST_F(ScrollbarAnimationControllerAuraOverlayTest, BasicAppearAndFadeOut) {
  205. base::TimeTicks time;
  206. time += base::Seconds(1);
  207. // Scrollbar should be invisible.
  208. ExpectScrollbarsOpacity(0);
  209. EXPECT_TRUE(scrollbar_controller_->ScrollbarsHidden());
  210. // Scrollbar should appear only on scroll update.
  211. ExpectScrollbarsOpacity(0);
  212. EXPECT_TRUE(scrollbar_controller_->ScrollbarsHidden());
  213. scrollbar_controller_->DidScrollUpdate();
  214. ExpectScrollbarsOpacity(1);
  215. EXPECT_FALSE(scrollbar_controller_->ScrollbarsHidden());
  216. ExpectScrollbarsOpacity(1);
  217. EXPECT_FALSE(scrollbar_controller_->ScrollbarsHidden());
  218. // An fade out animation should have been enqueued.
  219. EXPECT_EQ(kFadeDelay, client_.delay());
  220. EXPECT_FALSE(client_.start_fade().is_null());
  221. std::move(client_.start_fade()).Run();
  222. // Scrollbar should fade out over kFadeDuration.
  223. scrollbar_controller_->Animate(time);
  224. time += kFadeDuration;
  225. scrollbar_controller_->Animate(time);
  226. ExpectScrollbarsOpacity(0);
  227. EXPECT_TRUE(scrollbar_controller_->ScrollbarsHidden());
  228. }
  229. // Confirm the scrollbar appears by WillUpdateScroll and fade out.
  230. TEST_F(ScrollbarAnimationControllerAuraOverlayTest,
  231. BasicAppearByWillUpdateScrollThenFadeOut) {
  232. base::TimeTicks time;
  233. time += base::Seconds(1);
  234. // Scrollbar should be invisible.
  235. ExpectScrollbarsOpacity(0);
  236. EXPECT_TRUE(scrollbar_controller_->ScrollbarsHidden());
  237. // Scrollbar should appear when scroll will update.
  238. scrollbar_controller_->WillUpdateScroll();
  239. ExpectScrollbarsOpacity(1);
  240. EXPECT_FALSE(scrollbar_controller_->ScrollbarsHidden());
  241. // An fade out animation should have been enqueued.
  242. EXPECT_EQ(kFadeDelay, client_.delay());
  243. EXPECT_FALSE(client_.start_fade().is_null());
  244. std::move(client_.start_fade()).Run();
  245. // Scrollbar should fade out over kFadeDuration.
  246. scrollbar_controller_->Animate(time);
  247. time += kFadeDuration;
  248. scrollbar_controller_->Animate(time);
  249. ExpectScrollbarsOpacity(0);
  250. EXPECT_TRUE(scrollbar_controller_->ScrollbarsHidden());
  251. }
  252. // Scroll content. Move the mouse near the scrollbar track but not near thumb
  253. // and confirm it stay thin. Move the mouse near the scrollbar thumb and
  254. // confirm it becomes thick.
  255. TEST_F(ScrollbarAnimationControllerAuraOverlayTest,
  256. MoveNearTrackThenNearThumb) {
  257. base::TimeTicks time;
  258. time += base::Seconds(1);
  259. scrollbar_controller_->DidScrollUpdate();
  260. // An fade out animation should have been enqueued.
  261. EXPECT_EQ(kFadeDelay, client_.delay());
  262. EXPECT_FALSE(client_.start_fade().is_null());
  263. EXPECT_FALSE(client_.start_fade().IsCancelled());
  264. // Now move the mouse near the vertical scrollbar track. This should cancel
  265. // the currently queued fading animation and stay scrollbar thin.
  266. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarEnd(-1, 0));
  267. ExpectScrollbarsOpacity(1);
  268. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  269. v_scrollbar_layer_->thumb_thickness_scale_factor());
  270. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  271. h_scrollbar_layer_->thumb_thickness_scale_factor());
  272. EXPECT_TRUE(client_.start_fade().IsCancelled());
  273. scrollbar_controller_->Animate(time);
  274. time += kThinningDuration;
  275. scrollbar_controller_->Animate(time);
  276. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  277. v_scrollbar_layer_->thumb_thickness_scale_factor());
  278. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  279. h_scrollbar_layer_->thumb_thickness_scale_factor());
  280. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarBegin(-1, 0));
  281. scrollbar_controller_->Animate(time);
  282. time += kThinningDuration;
  283. scrollbar_controller_->Animate(time);
  284. EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
  285. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  286. h_scrollbar_layer_->thumb_thickness_scale_factor());
  287. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarEnd(-1, 0));
  288. EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
  289. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  290. h_scrollbar_layer_->thumb_thickness_scale_factor());
  291. EXPECT_TRUE(client_.start_fade().IsCancelled());
  292. scrollbar_controller_->Animate(time);
  293. time += kThinningDuration;
  294. scrollbar_controller_->Animate(time);
  295. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  296. v_scrollbar_layer_->thumb_thickness_scale_factor());
  297. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  298. h_scrollbar_layer_->thumb_thickness_scale_factor());
  299. }
  300. // Scroll content. Move the mouse near the scrollbar thumb and confirm it
  301. // becomes thick. Ensure it remains visible as long as the mouse is near the
  302. // scrollbar.
  303. TEST_F(ScrollbarAnimationControllerAuraOverlayTest, MoveNearAndDontFadeOut) {
  304. base::TimeTicks time;
  305. time += base::Seconds(1);
  306. scrollbar_controller_->DidScrollUpdate();
  307. // An fade out animation should have been enqueued.
  308. EXPECT_EQ(kFadeDelay, client_.delay());
  309. EXPECT_FALSE(client_.start_fade().is_null());
  310. EXPECT_FALSE(client_.start_fade().IsCancelled());
  311. // Now move the mouse near the vertical scrollbar thumb. This should cancel
  312. // the currently queued fading animation and start animating thickness.
  313. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarBegin(-1, 0));
  314. ExpectScrollbarsOpacity(1);
  315. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  316. v_scrollbar_layer_->thumb_thickness_scale_factor());
  317. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  318. h_scrollbar_layer_->thumb_thickness_scale_factor());
  319. EXPECT_TRUE(client_.start_fade().IsCancelled());
  320. // Vertical scrollbar should become thick.
  321. scrollbar_controller_->Animate(time);
  322. time += kThinningDuration;
  323. scrollbar_controller_->Animate(time);
  324. ExpectScrollbarsOpacity(1);
  325. EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
  326. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  327. h_scrollbar_layer_->thumb_thickness_scale_factor());
  328. // Mouse is still near the Scrollbar. Once the thickness animation is
  329. // complete, the queued delayed fade out animation should be either cancelled
  330. // or null.
  331. EXPECT_TRUE(client_.start_fade().is_null() ||
  332. client_.start_fade().IsCancelled());
  333. }
  334. // Scroll content. Move the mouse over the scrollbar and confirm it becomes
  335. // thick. Ensure it remains visible as long as the mouse is over the scrollbar.
  336. TEST_F(ScrollbarAnimationControllerAuraOverlayTest, MoveOverAndDontFadeOut) {
  337. base::TimeTicks time;
  338. time += base::Seconds(1);
  339. scrollbar_controller_->DidScrollUpdate();
  340. // An fade out animation should have been enqueued.
  341. EXPECT_EQ(kFadeDelay, client_.delay());
  342. EXPECT_FALSE(client_.start_fade().is_null());
  343. EXPECT_FALSE(client_.start_fade().IsCancelled());
  344. // Now move the mouse over the vertical scrollbar thumb. This should cancel
  345. // the currently queued fading animation and start animating thickness.
  346. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarBegin(0, 0));
  347. ExpectScrollbarsOpacity(1);
  348. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  349. v_scrollbar_layer_->thumb_thickness_scale_factor());
  350. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  351. h_scrollbar_layer_->thumb_thickness_scale_factor());
  352. EXPECT_TRUE(client_.start_fade().IsCancelled());
  353. // Vertical scrollbar should become thick.
  354. scrollbar_controller_->Animate(time);
  355. time += kThinningDuration;
  356. scrollbar_controller_->Animate(time);
  357. ExpectScrollbarsOpacity(1);
  358. EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
  359. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  360. h_scrollbar_layer_->thumb_thickness_scale_factor());
  361. // Mouse is still over the Scrollbar. Once the thickness animation is
  362. // complete, the queued delayed fade out animation should be either cancelled
  363. // or null.
  364. EXPECT_TRUE(client_.start_fade().is_null() ||
  365. client_.start_fade().IsCancelled());
  366. }
  367. // Make sure a scrollbar captured before the thickening animation doesn't try
  368. // to fade out.
  369. TEST_F(ScrollbarAnimationControllerAuraOverlayTest,
  370. DontFadeWhileCapturedBeforeThick) {
  371. base::TimeTicks time;
  372. time += base::Seconds(1);
  373. scrollbar_controller_->DidScrollUpdate();
  374. // An fade out animation should have been enqueued.
  375. EXPECT_EQ(kFadeDelay, client_.delay());
  376. EXPECT_FALSE(client_.start_fade().is_null());
  377. // Now move the mouse over the vertical scrollbar thumb and capture it. It
  378. // should become thick without need for an animation.
  379. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarBegin(0, 0));
  380. scrollbar_controller_->DidMouseDown();
  381. ExpectScrollbarsOpacity(1);
  382. EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
  383. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  384. h_scrollbar_layer_->thumb_thickness_scale_factor());
  385. // The fade out animation should have been cleared or cancelled.
  386. EXPECT_TRUE(client_.start_fade().is_null() ||
  387. client_.start_fade().IsCancelled());
  388. }
  389. // Make sure a scrollbar captured then move mouse away doesn't try to fade out.
  390. TEST_F(ScrollbarAnimationControllerAuraOverlayTest,
  391. DontFadeWhileCapturedThenAway) {
  392. base::TimeTicks time;
  393. time += base::Seconds(1);
  394. scrollbar_controller_->DidScrollUpdate();
  395. // An fade out animation should have been enqueued.
  396. EXPECT_EQ(kFadeDelay, client_.delay());
  397. EXPECT_FALSE(client_.start_fade().is_null());
  398. // Now move the mouse over the vertical scrollbar and capture it. It should
  399. // become thick without need for an animation.
  400. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarBegin(0, 0));
  401. scrollbar_controller_->DidMouseDown();
  402. ExpectScrollbarsOpacity(1);
  403. EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
  404. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  405. h_scrollbar_layer_->thumb_thickness_scale_factor());
  406. // The fade out animation should have been cleared or cancelled.
  407. EXPECT_TRUE(client_.start_fade().is_null() ||
  408. client_.start_fade().IsCancelled());
  409. // Then move mouse away, The fade out animation should have been cleared or
  410. // cancelled.
  411. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarBegin(
  412. -mouse_move_distance_to_trigger_expand_ - 1, 0));
  413. EXPECT_TRUE(client_.start_fade().is_null() ||
  414. client_.start_fade().IsCancelled());
  415. }
  416. // Make sure a scrollbar captured after a thickening animation doesn't try to
  417. // fade out.
  418. TEST_F(ScrollbarAnimationControllerAuraOverlayTest, DontFadeWhileCaptured) {
  419. base::TimeTicks time;
  420. time += base::Seconds(1);
  421. scrollbar_controller_->DidScrollUpdate();
  422. // An fade out animation should have been enqueued.
  423. EXPECT_EQ(kFadeDelay, client_.delay());
  424. EXPECT_FALSE(client_.start_fade().is_null());
  425. EXPECT_FALSE(client_.start_fade().IsCancelled());
  426. // Now move the mouse over the vertical scrollbar thumb and animate it until
  427. // it's thick.
  428. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarBegin(0, 0));
  429. scrollbar_controller_->Animate(time);
  430. time += kThinningDuration;
  431. scrollbar_controller_->Animate(time);
  432. ExpectScrollbarsOpacity(1);
  433. EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
  434. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  435. h_scrollbar_layer_->thumb_thickness_scale_factor());
  436. // Since the mouse is over the scrollbar, it should either clear or cancel the
  437. // queued fade.
  438. EXPECT_TRUE(client_.start_fade().is_null() ||
  439. client_.start_fade().IsCancelled());
  440. // Make sure the queued fade out animation is still null or cancelled after
  441. // capturing the scrollbar.
  442. scrollbar_controller_->DidMouseDown();
  443. EXPECT_TRUE(client_.start_fade().is_null() ||
  444. client_.start_fade().IsCancelled());
  445. }
  446. // Make sure releasing a captured scrollbar when the mouse isn't near it, causes
  447. // the scrollbar to fade out.
  448. TEST_F(ScrollbarAnimationControllerAuraOverlayTest, FadeAfterReleasedFar) {
  449. base::TimeTicks time;
  450. time += base::Seconds(1);
  451. scrollbar_controller_->DidScrollUpdate();
  452. // An fade out animation should have been enqueued.
  453. EXPECT_EQ(kFadeDelay, client_.delay());
  454. EXPECT_FALSE(client_.start_fade().is_null());
  455. EXPECT_FALSE(client_.start_fade().IsCancelled());
  456. // Now move the mouse over the vertical scrollbar thumb and capture it.
  457. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarBegin(0, 0));
  458. scrollbar_controller_->DidMouseDown();
  459. ExpectScrollbarsOpacity(1);
  460. EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
  461. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  462. h_scrollbar_layer_->thumb_thickness_scale_factor());
  463. // Since the mouse is still near the scrollbar, the queued fade should be
  464. // either null or cancelled.
  465. EXPECT_TRUE(client_.start_fade().is_null() ||
  466. client_.start_fade().IsCancelled());
  467. // Now move the mouse away from the scrollbar and release it.
  468. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarBegin(
  469. -mouse_move_distance_to_trigger_fade_in_ - 1, 0));
  470. scrollbar_controller_->DidMouseUp();
  471. scrollbar_controller_->Animate(time);
  472. ExpectScrollbarsOpacity(1);
  473. EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
  474. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  475. h_scrollbar_layer_->thumb_thickness_scale_factor());
  476. time += kThinningDuration;
  477. scrollbar_controller_->Animate(time);
  478. ExpectScrollbarsOpacity(1);
  479. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  480. v_scrollbar_layer_->thumb_thickness_scale_factor());
  481. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  482. h_scrollbar_layer_->thumb_thickness_scale_factor());
  483. // The thickness animation is complete, a fade out must be queued.
  484. EXPECT_FALSE(client_.start_fade().is_null());
  485. EXPECT_FALSE(client_.start_fade().IsCancelled());
  486. }
  487. // Make sure releasing a captured scrollbar when the mouse is near/over it,
  488. // doesn't cause the scrollbar to fade out.
  489. TEST_F(ScrollbarAnimationControllerAuraOverlayTest, DontFadeAfterReleasedNear) {
  490. base::TimeTicks time;
  491. time += base::Seconds(1);
  492. scrollbar_controller_->DidScrollUpdate();
  493. // An fade out animation should have been enqueued.
  494. EXPECT_EQ(kFadeDelay, client_.delay());
  495. EXPECT_FALSE(client_.start_fade().is_null());
  496. EXPECT_FALSE(client_.start_fade().IsCancelled());
  497. // Now move the mouse over the vertical scrollbar thumb and capture it.
  498. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarBegin(0, 0));
  499. scrollbar_controller_->DidMouseDown();
  500. ExpectScrollbarsOpacity(1);
  501. EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
  502. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  503. h_scrollbar_layer_->thumb_thickness_scale_factor());
  504. // Since the mouse is over the scrollbar, the queued fade must be either
  505. // null or cancelled.
  506. EXPECT_TRUE(client_.start_fade().is_null() ||
  507. client_.start_fade().IsCancelled());
  508. // Mouse is still near the scrollbar, releasing it shouldn't do anything.
  509. scrollbar_controller_->DidMouseUp();
  510. EXPECT_TRUE(client_.start_fade().is_null() ||
  511. client_.start_fade().IsCancelled());
  512. ExpectScrollbarsOpacity(1);
  513. EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
  514. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  515. h_scrollbar_layer_->thumb_thickness_scale_factor());
  516. }
  517. // Make sure moving near a scrollbar while it's fading out causes it to reset
  518. // the opacity and thicken.
  519. TEST_F(ScrollbarAnimationControllerAuraOverlayTest,
  520. MoveNearScrollbarWhileFading) {
  521. base::TimeTicks time;
  522. time += base::Seconds(1);
  523. scrollbar_controller_->DidScrollUpdate();
  524. // A fade out animation should have been enqueued. Start it.
  525. EXPECT_EQ(kFadeDelay, client_.delay());
  526. EXPECT_FALSE(client_.start_fade().is_null());
  527. std::move(client_.start_fade()).Run();
  528. scrollbar_controller_->Animate(time);
  529. ExpectScrollbarsOpacity(1);
  530. // Proceed half way through the fade out animation.
  531. time += kFadeDuration / 2;
  532. scrollbar_controller_->Animate(time);
  533. ExpectScrollbarsOpacity(.5f);
  534. // Now move the mouse near the vertical scrollbar thumb. It should reset
  535. // opacity to 1 instantly and start animating to thick.
  536. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarBegin(0, 0));
  537. ExpectScrollbarsOpacity(1);
  538. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  539. v_scrollbar_layer_->thumb_thickness_scale_factor());
  540. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  541. h_scrollbar_layer_->thumb_thickness_scale_factor());
  542. scrollbar_controller_->Animate(time);
  543. time += kThinningDuration;
  544. scrollbar_controller_->Animate(time);
  545. ExpectScrollbarsOpacity(1);
  546. EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
  547. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  548. h_scrollbar_layer_->thumb_thickness_scale_factor());
  549. }
  550. // Make sure we can't capture scrollbar that's completely faded out.
  551. TEST_F(ScrollbarAnimationControllerAuraOverlayTest, TestCantCaptureWhenFaded) {
  552. base::TimeTicks time;
  553. time += base::Seconds(1);
  554. scrollbar_controller_->DidScrollUpdate();
  555. EXPECT_EQ(kFadeDelay, client_.delay());
  556. EXPECT_FALSE(client_.start_fade().is_null());
  557. EXPECT_FALSE(client_.start_fade().IsCancelled());
  558. std::move(client_.start_fade()).Run();
  559. scrollbar_controller_->Animate(time);
  560. ExpectScrollbarsOpacity(1);
  561. // Fade the scrollbar out completely.
  562. time += kFadeDuration;
  563. scrollbar_controller_->Animate(time);
  564. ExpectScrollbarsOpacity(0);
  565. // Move mouse over the vertical scrollbar thumb. It shouldn't thicken the
  566. // scrollbar since it's completely faded out.
  567. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarBegin(0, 0));
  568. scrollbar_controller_->Animate(time);
  569. time += kThinningDuration;
  570. scrollbar_controller_->Animate(time);
  571. ExpectScrollbarsOpacity(0);
  572. EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
  573. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  574. h_scrollbar_layer_->thumb_thickness_scale_factor());
  575. client_.start_fade().Reset();
  576. // Now try to capture the scrollbar. It shouldn't do anything since it's
  577. // completely faded out.
  578. scrollbar_controller_->DidMouseDown();
  579. ExpectScrollbarsOpacity(0);
  580. EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
  581. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  582. h_scrollbar_layer_->thumb_thickness_scale_factor());
  583. EXPECT_TRUE(client_.start_fade().is_null());
  584. // Similarly, releasing the scrollbar should have no effect but trigger a fade
  585. // in.
  586. scrollbar_controller_->DidMouseUp();
  587. ExpectScrollbarsOpacity(0);
  588. EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
  589. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  590. h_scrollbar_layer_->thumb_thickness_scale_factor());
  591. // An fade in animation should have been enqueued.
  592. EXPECT_FALSE(client_.start_fade().is_null());
  593. EXPECT_FALSE(client_.start_fade().IsCancelled());
  594. EXPECT_EQ(kFadeDelay, client_.delay());
  595. // Play the delay animation.
  596. std::move(client_.start_fade()).Run();
  597. scrollbar_controller_->Animate(time);
  598. time += kFadeDuration;
  599. scrollbar_controller_->Animate(time);
  600. EXPECT_FALSE(scrollbar_controller_->ScrollbarsHidden());
  601. }
  602. // Initiate a scroll when the pointer is already near the scrollbar. It should
  603. // appear thick and remain thick.
  604. TEST_F(ScrollbarAnimationControllerAuraOverlayTest, ScrollWithMouseNear) {
  605. base::TimeTicks time;
  606. time += base::Seconds(1);
  607. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarBegin(-1, 0));
  608. scrollbar_controller_->Animate(time);
  609. time += kThinningDuration;
  610. // Since the scrollbar isn't visible yet (because we haven't scrolled), we
  611. // shouldn't have applied the thickening.
  612. scrollbar_controller_->Animate(time);
  613. EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
  614. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  615. h_scrollbar_layer_->thumb_thickness_scale_factor());
  616. scrollbar_controller_->DidScrollUpdate();
  617. // Now that we've received a scroll, we should be thick without an animation.
  618. ExpectScrollbarsOpacity(1);
  619. // An animation for the fade should be either null or cancelled, since
  620. // mouse is still near the scrollbar.
  621. EXPECT_TRUE(client_.start_fade().is_null() ||
  622. client_.start_fade().IsCancelled());
  623. scrollbar_controller_->Animate(time);
  624. ExpectScrollbarsOpacity(1);
  625. EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
  626. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  627. h_scrollbar_layer_->thumb_thickness_scale_factor());
  628. // Scrollbar should still be thick and visible.
  629. time += kFadeDuration;
  630. scrollbar_controller_->Animate(time);
  631. ExpectScrollbarsOpacity(1);
  632. EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
  633. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  634. h_scrollbar_layer_->thumb_thickness_scale_factor());
  635. }
  636. // Tests that main thread scroll updates immediately queue a fade out animation
  637. TEST_F(ScrollbarAnimationControllerAuraOverlayTest,
  638. MainThreadScrollQueuesFade) {
  639. ASSERT_TRUE(client_.start_fade().is_null());
  640. // A ScrollUpdate indicates a main thread scroll update so we should schedule
  641. // a fade out animation since there is no scroll end notification.
  642. scrollbar_controller_->DidScrollUpdate();
  643. EXPECT_FALSE(client_.start_fade().is_null());
  644. EXPECT_EQ(kFadeDelay, client_.delay());
  645. }
  646. // Tests that the fade effect is animated.
  647. TEST_F(ScrollbarAnimationControllerAuraOverlayTest, FadeAnimated) {
  648. base::TimeTicks time;
  649. time += base::Seconds(1);
  650. // Scroll to make the scrollbars visible.
  651. scrollbar_controller_->DidScrollUpdate();
  652. // Appearance is instant.
  653. ExpectScrollbarsOpacity(1);
  654. // An fade out animation should have been enqueued.
  655. EXPECT_EQ(kFadeDelay, client_.delay());
  656. EXPECT_FALSE(client_.start_fade().is_null());
  657. std::move(client_.start_fade()).Run();
  658. // Test that at half the fade duration time, the opacity is at half.
  659. scrollbar_controller_->Animate(time);
  660. ExpectScrollbarsOpacity(1);
  661. time += kFadeDuration / 2;
  662. scrollbar_controller_->Animate(time);
  663. ExpectScrollbarsOpacity(.5f);
  664. time += kFadeDuration / 2;
  665. scrollbar_controller_->Animate(time);
  666. ExpectScrollbarsOpacity(0);
  667. }
  668. // Tests that the controller tells the client when the scrollbars hide/show.
  669. TEST_F(ScrollbarAnimationControllerAuraOverlayTest, NotifyChangedVisibility) {
  670. base::TimeTicks time;
  671. time += base::Seconds(1);
  672. EXPECT_CALL(client_, DidChangeScrollbarVisibility()).Times(1);
  673. // Scroll to make the scrollbars visible.
  674. scrollbar_controller_->DidScrollUpdate();
  675. EXPECT_FALSE(scrollbar_controller_->ScrollbarsHidden());
  676. Mock::VerifyAndClearExpectations(&client_);
  677. // Play out the fade out animation. We shouldn't notify that the scrollbars
  678. // are hidden until the animation is completly over. We can (but don't have
  679. // to) notify during the animation that the scrollbars are still visible.
  680. EXPECT_CALL(client_, DidChangeScrollbarVisibility()).Times(0);
  681. ASSERT_FALSE(client_.start_fade().is_null());
  682. std::move(client_.start_fade()).Run();
  683. scrollbar_controller_->Animate(time);
  684. time += kFadeDuration / 4;
  685. EXPECT_FALSE(scrollbar_controller_->ScrollbarsHidden());
  686. scrollbar_controller_->Animate(time);
  687. time += kFadeDuration / 4;
  688. EXPECT_FALSE(scrollbar_controller_->ScrollbarsHidden());
  689. scrollbar_controller_->Animate(time);
  690. time += kFadeDuration / 4;
  691. EXPECT_FALSE(scrollbar_controller_->ScrollbarsHidden());
  692. scrollbar_controller_->Animate(time);
  693. ExpectScrollbarsOpacity(.25f);
  694. Mock::VerifyAndClearExpectations(&client_);
  695. EXPECT_CALL(client_, DidChangeScrollbarVisibility()).Times(1);
  696. time += kFadeDuration / 4;
  697. scrollbar_controller_->Animate(time);
  698. EXPECT_TRUE(scrollbar_controller_->ScrollbarsHidden());
  699. ExpectScrollbarsOpacity(0);
  700. Mock::VerifyAndClearExpectations(&client_);
  701. // Calling DidScrollUpdate without a begin (i.e. update from commit) should
  702. // also notify.
  703. EXPECT_CALL(client_, DidChangeScrollbarVisibility()).Times(1);
  704. scrollbar_controller_->DidScrollUpdate();
  705. EXPECT_FALSE(scrollbar_controller_->ScrollbarsHidden());
  706. Mock::VerifyAndClearExpectations(&client_);
  707. }
  708. // Move the pointer near each scrollbar. Confirm it gets thick and narrow when
  709. // moved away.
  710. TEST_F(ScrollbarAnimationControllerAuraOverlayTest, MouseNearEach) {
  711. base::TimeTicks time;
  712. time += base::Seconds(1);
  713. // Scroll to make the scrollbars visible.
  714. scrollbar_controller_->DidScrollUpdate();
  715. // Near vertical scrollbar.
  716. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarBegin(-1, 0));
  717. scrollbar_controller_->Animate(time);
  718. ExpectScrollbarsOpacity(1);
  719. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  720. v_scrollbar_layer_->thumb_thickness_scale_factor());
  721. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  722. h_scrollbar_layer_->thumb_thickness_scale_factor());
  723. // Should animate to thickened.
  724. time += kThinningDuration;
  725. scrollbar_controller_->Animate(time);
  726. ExpectScrollbarsOpacity(1);
  727. EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
  728. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  729. h_scrollbar_layer_->thumb_thickness_scale_factor());
  730. // Subsequent moves within the nearness threshold should not change anything.
  731. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarBegin(-2, 0));
  732. scrollbar_controller_->Animate(time);
  733. time += base::Seconds(10);
  734. scrollbar_controller_->Animate(time);
  735. ExpectScrollbarsOpacity(1);
  736. EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
  737. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  738. h_scrollbar_layer_->thumb_thickness_scale_factor());
  739. // Now move away from bar.
  740. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarBegin(
  741. -mouse_move_distance_to_trigger_expand_ - 1, 0));
  742. scrollbar_controller_->Animate(time);
  743. time += kThinningDuration;
  744. scrollbar_controller_->Animate(time);
  745. ExpectScrollbarsOpacity(1);
  746. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  747. v_scrollbar_layer_->thumb_thickness_scale_factor());
  748. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  749. h_scrollbar_layer_->thumb_thickness_scale_factor());
  750. // Near horizontal scrollbar
  751. scrollbar_controller_->DidMouseMove(NearHorizontalScrollbarBegin(0, -1));
  752. scrollbar_controller_->Animate(time);
  753. ExpectScrollbarsOpacity(1);
  754. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  755. v_scrollbar_layer_->thumb_thickness_scale_factor());
  756. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  757. h_scrollbar_layer_->thumb_thickness_scale_factor());
  758. // Should animate to thickened.
  759. time += kThinningDuration;
  760. scrollbar_controller_->Animate(time);
  761. ExpectScrollbarsOpacity(1);
  762. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  763. v_scrollbar_layer_->thumb_thickness_scale_factor());
  764. EXPECT_FLOAT_EQ(1, h_scrollbar_layer_->thumb_thickness_scale_factor());
  765. // Subsequent moves within the nearness threshold should not change anything.
  766. scrollbar_controller_->DidMouseMove(NearHorizontalScrollbarBegin(0, -2));
  767. scrollbar_controller_->Animate(time);
  768. time += base::Seconds(10);
  769. scrollbar_controller_->Animate(time);
  770. ExpectScrollbarsOpacity(1);
  771. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  772. v_scrollbar_layer_->thumb_thickness_scale_factor());
  773. EXPECT_FLOAT_EQ(1, h_scrollbar_layer_->thumb_thickness_scale_factor());
  774. // Now move away from bar.
  775. scrollbar_controller_->DidMouseMove(NearHorizontalScrollbarBegin(
  776. 0, -mouse_move_distance_to_trigger_expand_ - 1));
  777. scrollbar_controller_->Animate(time);
  778. time += kThinningDuration;
  779. scrollbar_controller_->Animate(time);
  780. ExpectScrollbarsOpacity(1);
  781. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  782. v_scrollbar_layer_->thumb_thickness_scale_factor());
  783. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  784. h_scrollbar_layer_->thumb_thickness_scale_factor());
  785. // An fade out animation should have been enqueued.
  786. EXPECT_FALSE(client_.start_fade().is_null());
  787. EXPECT_EQ(kFadeDelay, client_.delay());
  788. }
  789. // Move mouse near both scrollbars at the same time.
  790. TEST_F(ScrollbarAnimationControllerAuraOverlayTest, MouseNearBoth) {
  791. base::TimeTicks time;
  792. time += base::Seconds(1);
  793. // Scroll to make the scrollbars visible.
  794. scrollbar_controller_->DidScrollUpdate();
  795. // Move scrollbar thumb to the end of track.
  796. v_scrollbar_layer_->SetCurrentPos(100);
  797. h_scrollbar_layer_->SetCurrentPos(100);
  798. // Near both Scrollbar
  799. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarEnd(-1, -1));
  800. scrollbar_controller_->Animate(time);
  801. ExpectScrollbarsOpacity(1);
  802. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  803. v_scrollbar_layer_->thumb_thickness_scale_factor());
  804. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  805. h_scrollbar_layer_->thumb_thickness_scale_factor());
  806. // Should animate to thickened.
  807. time += kThinningDuration;
  808. scrollbar_controller_->Animate(time);
  809. ExpectScrollbarsOpacity(1);
  810. EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
  811. EXPECT_FLOAT_EQ(1, h_scrollbar_layer_->thumb_thickness_scale_factor());
  812. }
  813. // Move mouse from one to the other scrollbar before animation is finished, then
  814. // away before animation finished.
  815. TEST_F(ScrollbarAnimationControllerAuraOverlayTest,
  816. MouseNearOtherBeforeAnimationFinished) {
  817. base::TimeTicks time;
  818. time += base::Seconds(1);
  819. // Scroll to make the scrollbars visible.
  820. scrollbar_controller_->DidScrollUpdate();
  821. // Near vertical scrollbar.
  822. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarBegin(-1, 0));
  823. scrollbar_controller_->Animate(time);
  824. ExpectScrollbarsOpacity(1);
  825. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  826. v_scrollbar_layer_->thumb_thickness_scale_factor());
  827. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  828. h_scrollbar_layer_->thumb_thickness_scale_factor());
  829. // Vertical scrollbar animate to half thickened.
  830. time += kThinningDuration / 2;
  831. scrollbar_controller_->Animate(time);
  832. ExpectScrollbarsOpacity(1);
  833. EXPECT_FLOAT_EQ(kIdleThicknessScale + (1.0f - kIdleThicknessScale) / 2,
  834. v_scrollbar_layer_->thumb_thickness_scale_factor());
  835. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  836. h_scrollbar_layer_->thumb_thickness_scale_factor());
  837. // Away vertical scrollbar and near horizontal scrollbar.
  838. scrollbar_controller_->DidMouseMove(gfx::PointF(0, 0));
  839. scrollbar_controller_->DidMouseMove(NearHorizontalScrollbarBegin(0, -1));
  840. scrollbar_controller_->Animate(time);
  841. // Vertical scrollbar animate to thin. horizontal scrollbar animate to
  842. // thickened.
  843. time += kThinningDuration;
  844. scrollbar_controller_->Animate(time);
  845. ExpectScrollbarsOpacity(1);
  846. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  847. v_scrollbar_layer_->thumb_thickness_scale_factor());
  848. EXPECT_FLOAT_EQ(1, h_scrollbar_layer_->thumb_thickness_scale_factor());
  849. // Away horizontal scrollbar.
  850. scrollbar_controller_->DidMouseMove(gfx::PointF(0, 0));
  851. scrollbar_controller_->Animate(time);
  852. // Horizontal scrollbar animate to thin.
  853. time += kThinningDuration;
  854. scrollbar_controller_->Animate(time);
  855. ExpectScrollbarsOpacity(1);
  856. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  857. v_scrollbar_layer_->thumb_thickness_scale_factor());
  858. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  859. h_scrollbar_layer_->thumb_thickness_scale_factor());
  860. // An fade out animation should have been enqueued.
  861. EXPECT_FALSE(client_.start_fade().is_null());
  862. EXPECT_EQ(kFadeDelay, client_.delay());
  863. }
  864. // Ensure we have a delay fadeout animation after mouse leave without a mouse
  865. // move.
  866. TEST_F(ScrollbarAnimationControllerAuraOverlayTest, MouseLeaveFadeOut) {
  867. base::TimeTicks time;
  868. time += base::Seconds(1);
  869. // Move mouse near scrollbar.
  870. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarBegin(-1, 0));
  871. // Scroll to make the scrollbars visible.
  872. scrollbar_controller_->DidScrollUpdate();
  873. // Should not have delay fadeout animation.
  874. EXPECT_TRUE(client_.start_fade().is_null() ||
  875. client_.start_fade().IsCancelled());
  876. // Mouse leave.
  877. scrollbar_controller_->DidMouseLeave();
  878. // An fade out animation should have been enqueued.
  879. EXPECT_FALSE(client_.start_fade().is_null());
  880. EXPECT_EQ(kFadeDelay, client_.delay());
  881. }
  882. // Scrollbars should schedule a delay fade in when mouse hover the show
  883. // scrollbar region of a hidden scrollbar.
  884. TEST_F(ScrollbarAnimationControllerAuraOverlayTest, BasicMouseHoverFadeIn) {
  885. base::TimeTicks time;
  886. time += base::Seconds(1);
  887. // Move mouse over the fade in region of scrollbar.
  888. scrollbar_controller_->DidMouseMove(
  889. NearVerticalScrollbarBegin(-mouse_move_distance_to_trigger_fade_in_, 0));
  890. // An fade in animation should have been enqueued.
  891. EXPECT_FALSE(client_.start_fade().is_null());
  892. EXPECT_FALSE(client_.start_fade().IsCancelled());
  893. EXPECT_EQ(kFadeDelay, client_.delay());
  894. // Play the delay animation.
  895. std::move(client_.start_fade()).Run();
  896. scrollbar_controller_->Animate(time);
  897. time += kFadeDuration / 2;
  898. scrollbar_controller_->Animate(time);
  899. ExpectScrollbarsOpacity(0.5);
  900. EXPECT_FALSE(scrollbar_controller_->ScrollbarsHidden());
  901. time += kFadeDuration / 2;
  902. scrollbar_controller_->Animate(time);
  903. ExpectScrollbarsOpacity(1);
  904. EXPECT_FALSE(scrollbar_controller_->ScrollbarsHidden());
  905. }
  906. // Scrollbars should not schedule a new delay fade in when the mouse hovers
  907. // inside a scrollbar already scheduled a delay fade in.
  908. TEST_F(ScrollbarAnimationControllerAuraOverlayTest,
  909. MouseHoverScrollbarAndMoveInside) {
  910. base::TimeTicks time;
  911. time += base::Seconds(1);
  912. // Move mouse over the fade in region of scrollbar.
  913. scrollbar_controller_->DidMouseMove(
  914. NearVerticalScrollbarBegin(-mouse_move_distance_to_trigger_fade_in_, 0));
  915. // An fade in animation should have been enqueued.
  916. EXPECT_FALSE(client_.start_fade().is_null());
  917. EXPECT_FALSE(client_.start_fade().IsCancelled());
  918. EXPECT_EQ(kFadeDelay, client_.delay());
  919. client_.start_fade().Reset();
  920. // Move mouse still hover the fade in region of scrollbar should not
  921. // post a new fade in.
  922. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarBegin(
  923. -mouse_move_distance_to_trigger_fade_in_ + 2, 0));
  924. EXPECT_TRUE(client_.start_fade().is_null());
  925. }
  926. // Scrollbars should cancel delay fade in when mouse hover hidden scrollbar then
  927. // move far away.
  928. TEST_F(ScrollbarAnimationControllerAuraOverlayTest,
  929. MouseHoverThenOutShouldCancelFadeIn) {
  930. base::TimeTicks time;
  931. time += base::Seconds(1);
  932. // Move mouse over the fade in region of scrollbar.
  933. scrollbar_controller_->DidMouseMove(
  934. NearVerticalScrollbarBegin(-mouse_move_distance_to_trigger_fade_in_, 0));
  935. // An fade in animation should have been enqueued.
  936. EXPECT_FALSE(client_.start_fade().is_null());
  937. EXPECT_FALSE(client_.start_fade().IsCancelled());
  938. EXPECT_EQ(kFadeDelay, client_.delay());
  939. // Move mouse far away,delay fade in should be canceled.
  940. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarBegin(
  941. -mouse_move_distance_to_trigger_fade_in_ - 1, 0));
  942. EXPECT_TRUE(client_.start_fade().is_null() ||
  943. client_.start_fade().IsCancelled());
  944. }
  945. // Scrollbars should cancel delay fade in when mouse hover hidden scrollbar then
  946. // move out of window.
  947. TEST_F(ScrollbarAnimationControllerAuraOverlayTest,
  948. MouseHoverThenLeaveShouldCancelShowThenEnterShouldFadeIn) {
  949. base::TimeTicks time;
  950. time += base::Seconds(1);
  951. // Move mouse over the fade in region of scrollbar.
  952. scrollbar_controller_->DidMouseMove(
  953. NearVerticalScrollbarBegin(-mouse_move_distance_to_trigger_fade_in_, 0));
  954. // An fade in animation should have been enqueued.
  955. EXPECT_FALSE(client_.start_fade().is_null());
  956. EXPECT_FALSE(client_.start_fade().IsCancelled());
  957. EXPECT_EQ(kFadeDelay, client_.delay());
  958. // Move mouse out of window,delay fade in should be canceled.
  959. scrollbar_controller_->DidMouseLeave();
  960. EXPECT_TRUE(client_.start_fade().is_null() ||
  961. client_.start_fade().IsCancelled());
  962. // Move mouse over the fade in region of scrollbar.
  963. scrollbar_controller_->DidMouseMove(
  964. NearVerticalScrollbarBegin(-mouse_move_distance_to_trigger_fade_in_, 0));
  965. // An fade in animation should have been enqueued.
  966. EXPECT_FALSE(client_.start_fade().is_null());
  967. EXPECT_FALSE(client_.start_fade().IsCancelled());
  968. EXPECT_EQ(kFadeDelay, client_.delay());
  969. // Play the delay animation.
  970. std::move(client_.start_fade()).Run();
  971. scrollbar_controller_->Animate(time);
  972. time += kFadeDuration;
  973. scrollbar_controller_->Animate(time);
  974. EXPECT_FALSE(scrollbar_controller_->ScrollbarsHidden());
  975. }
  976. // Make sure mouse down will cancel hover fade in timer, then mouse move with
  977. // press will not trigger hover fade in, mouse release near will trigger new
  978. // hover fade in.
  979. TEST_F(ScrollbarAnimationControllerAuraOverlayTest,
  980. MouseHoverThenMouseDownShouldCancelFadeInThenReleaseNearShouldFadeIn) {
  981. base::TimeTicks time;
  982. time += base::Seconds(1);
  983. // Move mouse over the fade in region of scrollbar.
  984. scrollbar_controller_->DidMouseMove(
  985. NearVerticalScrollbarBegin(-mouse_move_distance_to_trigger_fade_in_, 0));
  986. // An fade in animation should have been enqueued.
  987. EXPECT_FALSE(client_.start_fade().is_null());
  988. EXPECT_FALSE(client_.start_fade().IsCancelled());
  989. EXPECT_EQ(kFadeDelay, client_.delay());
  990. // Mouse down,delay fade in should be canceled.
  991. scrollbar_controller_->DidMouseDown();
  992. EXPECT_TRUE(client_.start_fade().is_null() ||
  993. client_.start_fade().IsCancelled());
  994. // Move mouse hover the fade in region of scrollbar with press.
  995. scrollbar_controller_->DidMouseMove(
  996. NearVerticalScrollbarBegin(-mouse_move_distance_to_trigger_fade_in_, 0));
  997. // Should not have delay fade animation.
  998. EXPECT_TRUE(client_.start_fade().is_null() ||
  999. client_.start_fade().IsCancelled());
  1000. // Mouse up.
  1001. scrollbar_controller_->DidMouseUp();
  1002. // An fade in animation should have been enqueued.
  1003. EXPECT_FALSE(client_.start_fade().is_null());
  1004. EXPECT_FALSE(client_.start_fade().IsCancelled());
  1005. EXPECT_EQ(kFadeDelay, client_.delay());
  1006. // Play the delay animation.
  1007. std::move(client_.start_fade()).Run();
  1008. scrollbar_controller_->Animate(time);
  1009. time += kFadeDuration;
  1010. scrollbar_controller_->Animate(time);
  1011. EXPECT_FALSE(scrollbar_controller_->ScrollbarsHidden());
  1012. }
  1013. // Make sure mouse down will cancel hover fade in timer, then mouse move with
  1014. // press will not trigger hover fade in, mouse release far will not trigger new
  1015. // hover fade in.
  1016. TEST_F(ScrollbarAnimationControllerAuraOverlayTest,
  1017. MouseReleaseFarShouldNotFadeIn) {
  1018. base::TimeTicks time;
  1019. time += base::Seconds(1);
  1020. // Move mouse over the fade in region of scrollbar.
  1021. scrollbar_controller_->DidMouseMove(
  1022. NearVerticalScrollbarBegin(-mouse_move_distance_to_trigger_fade_in_, 0));
  1023. // An fade in animation should have been enqueued.
  1024. EXPECT_FALSE(client_.start_fade().is_null());
  1025. EXPECT_FALSE(client_.start_fade().IsCancelled());
  1026. EXPECT_EQ(kFadeDelay, client_.delay());
  1027. // Mouse down,delay fade in should be canceled.
  1028. scrollbar_controller_->DidMouseDown();
  1029. EXPECT_TRUE(client_.start_fade().is_null() ||
  1030. client_.start_fade().IsCancelled());
  1031. // Move mouse far from hover the fade in region of scrollbar with
  1032. // press.
  1033. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarBegin(
  1034. -mouse_move_distance_to_trigger_fade_in_ - 1, 0));
  1035. // Should not have delay fade animation.
  1036. EXPECT_TRUE(client_.start_fade().is_null() ||
  1037. client_.start_fade().IsCancelled());
  1038. // Mouse up.
  1039. scrollbar_controller_->DidMouseUp();
  1040. // Should not have delay fade animation.
  1041. EXPECT_TRUE(client_.start_fade().is_null() ||
  1042. client_.start_fade().IsCancelled());
  1043. }
  1044. // Ensure Aura Overlay Scrollbars shows and did not fade out when tickmarks show
  1045. // and fade out when tickmarks hide.
  1046. TEST_F(ScrollbarAnimationControllerAuraOverlayTest, TickmakrsShowHide) {
  1047. base::TimeTicks time;
  1048. time += base::Seconds(1);
  1049. // Overlay Scrollbar hidden at beginnging.
  1050. EXPECT_TRUE(scrollbar_controller_->ScrollbarsHidden());
  1051. EXPECT_TRUE(client_.start_fade().is_null() ||
  1052. client_.start_fade().IsCancelled());
  1053. // Scrollbars show when tickmarks show.
  1054. scrollbar_controller_->UpdateTickmarksVisibility(true);
  1055. EXPECT_FALSE(scrollbar_controller_->ScrollbarsHidden());
  1056. EXPECT_TRUE(client_.start_fade().is_null() ||
  1057. client_.start_fade().IsCancelled());
  1058. // Scroll update, no delay fade animation.
  1059. scrollbar_controller_->DidScrollUpdate();
  1060. EXPECT_TRUE(client_.start_fade().is_null() ||
  1061. client_.start_fade().IsCancelled());
  1062. // Scroll update with phase, no delay fade animation.
  1063. scrollbar_controller_->DidScrollUpdate();
  1064. EXPECT_TRUE(client_.start_fade().is_null() ||
  1065. client_.start_fade().IsCancelled());
  1066. EXPECT_TRUE(client_.start_fade().is_null() ||
  1067. client_.start_fade().IsCancelled());
  1068. // Move mouse, no delay fade animation.
  1069. scrollbar_controller_->DidMouseMove(NearVerticalScrollbarBegin(0, 0));
  1070. EXPECT_TRUE(client_.start_fade().is_null() ||
  1071. client_.start_fade().IsCancelled());
  1072. // Mouse leave, no delay fade animation.
  1073. scrollbar_controller_->DidMouseLeave();
  1074. EXPECT_TRUE(client_.start_fade().is_null() ||
  1075. client_.start_fade().IsCancelled());
  1076. // Scrollbars fade out animation has enqueued when tickmarks hide.
  1077. scrollbar_controller_->UpdateTickmarksVisibility(false);
  1078. EXPECT_FALSE(client_.start_fade().is_null());
  1079. EXPECT_FALSE(client_.start_fade().IsCancelled());
  1080. EXPECT_EQ(kFadeDelay, client_.delay());
  1081. }
  1082. TEST_F(ScrollbarAnimationControllerFluentOverlayTest,
  1083. FluentScrollbarMinimalModeByDefault) {
  1084. // An fade out animation should have not been enqueued. Scrollbar stays in
  1085. // the minimal (thin) mode.
  1086. EXPECT_TRUE(client_.start_fade().is_null());
  1087. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  1088. v_scrollbar_layer_->thumb_thickness_scale_factor());
  1089. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  1090. h_scrollbar_layer_->thumb_thickness_scale_factor());
  1091. EXPECT_FALSE(scrollbar_controller_->ScrollbarsHidden());
  1092. ExpectScrollbarsOpacity(1.f);
  1093. }
  1094. TEST_F(ScrollbarAnimationControllerFluentOverlayTest,
  1095. FluentScrollbarMinimalModeOnMouseLeave) {
  1096. // Move mouse inside scroller.
  1097. scrollbar_controller_->DidMouseMove(gfx::PointF(50, 50));
  1098. EXPECT_TRUE(client_.start_fade().is_null());
  1099. ExpectScrollbarsOpacity(1.f);
  1100. // Trigger mouse leave to check that Fluent overlay scrollbars don't fade out.
  1101. scrollbar_controller_->DidMouseLeave();
  1102. // An fade out animation should have not been enqueued. Scrollbar stays in
  1103. // the minimal (thin) mode.
  1104. EXPECT_TRUE(client_.start_fade().is_null());
  1105. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  1106. v_scrollbar_layer_->thumb_thickness_scale_factor());
  1107. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  1108. h_scrollbar_layer_->thumb_thickness_scale_factor());
  1109. ExpectScrollbarsOpacity(1.f);
  1110. }
  1111. TEST_F(ScrollbarAnimationControllerFluentOverlayTest,
  1112. FluentScrollbarMinimalModeOnWillUpdateScroll) {
  1113. // Scrollbar should be visible on scroll will update.
  1114. scrollbar_controller_->WillUpdateScroll();
  1115. // An fade out animation should have not been enqueued. Scrollbar stays in
  1116. // the minimal (thin) mode.
  1117. EXPECT_TRUE(client_.start_fade().is_null());
  1118. EXPECT_FLOAT_EQ(kIdleThicknessScale,
  1119. v_scrollbar_layer_->thumb_thickness_scale_factor());
  1120. ExpectScrollbarsOpacity(1.f);
  1121. }
  1122. class ScrollbarAnimationControllerAndroidTest
  1123. : public LayerTreeImplTestBase,
  1124. public testing::Test,
  1125. public ScrollbarAnimationControllerClient {
  1126. public:
  1127. ScrollbarAnimationControllerAndroidTest()
  1128. : did_request_redraw_(false), did_request_animate_(false) {}
  1129. void PostDelayedScrollbarAnimationTask(base::OnceClosure start_fade,
  1130. base::TimeDelta delay) override {
  1131. start_fade_ = std::move(start_fade);
  1132. delay_ = delay;
  1133. }
  1134. void SetNeedsRedrawForScrollbarAnimation() override {
  1135. did_request_redraw_ = true;
  1136. }
  1137. void SetNeedsAnimateForScrollbarAnimation() override {
  1138. did_request_animate_ = true;
  1139. }
  1140. ScrollbarSet ScrollbarsFor(ElementId scroll_element_id) const override {
  1141. return host_impl()->ScrollbarsFor(scroll_element_id);
  1142. }
  1143. void DidChangeScrollbarVisibility() override {}
  1144. bool IsFluentScrollbar() const override { return false; }
  1145. protected:
  1146. void SetUp() override {
  1147. const int kTrackStart = 0;
  1148. const bool kIsLeftSideVerticalScrollbar = false;
  1149. LayerImpl* root = root_layer();
  1150. scroll_layer_ = AddLayer<LayerImpl>();
  1151. scrollbar_layer_ = AddLayer<SolidColorScrollbarLayerImpl>(
  1152. orientation(), kThumbThickness, kTrackStart,
  1153. kIsLeftSideVerticalScrollbar);
  1154. SetElementIdsForTesting();
  1155. scroll_layer_->SetBounds(gfx::Size(200, 200));
  1156. CopyProperties(root, scroll_layer_);
  1157. CreateTransformNode(scroll_layer_);
  1158. CreateScrollNode(scroll_layer_, gfx::Size(100, 100));
  1159. scrollbar_layer_->SetScrollElementId(scroll_layer_->element_id());
  1160. CopyProperties(scroll_layer_, scrollbar_layer_);
  1161. auto& scrollbar_effect = CreateEffectNode(scrollbar_layer_);
  1162. scrollbar_effect.opacity = 0.f;
  1163. scrollbar_effect.has_potential_opacity_animation = true;
  1164. UpdateActiveTreeDrawProperties();
  1165. scrollbar_controller_ =
  1166. ScrollbarAnimationController::CreateScrollbarAnimationControllerAndroid(
  1167. scroll_layer_->element_id(), this, base::Seconds(2),
  1168. base::Seconds(3), 0.0f);
  1169. }
  1170. virtual ScrollbarOrientation orientation() const {
  1171. return ScrollbarOrientation::HORIZONTAL;
  1172. }
  1173. std::unique_ptr<ScrollbarAnimationController> scrollbar_controller_;
  1174. raw_ptr<LayerImpl> scroll_layer_;
  1175. raw_ptr<SolidColorScrollbarLayerImpl> scrollbar_layer_;
  1176. base::OnceClosure start_fade_;
  1177. base::TimeDelta delay_;
  1178. bool did_request_redraw_;
  1179. bool did_request_animate_;
  1180. };
  1181. class VerticalScrollbarAnimationControllerAndroidTest
  1182. : public ScrollbarAnimationControllerAndroidTest {
  1183. protected:
  1184. ScrollbarOrientation orientation() const override {
  1185. return ScrollbarOrientation::VERTICAL;
  1186. }
  1187. };
  1188. TEST_F(ScrollbarAnimationControllerAndroidTest, HiddenInBegin) {
  1189. scrollbar_layer_->SetOverlayScrollbarLayerOpacityAnimated(0.f);
  1190. scrollbar_controller_->Animate(base::TimeTicks());
  1191. EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->Opacity());
  1192. }
  1193. TEST_F(ScrollbarAnimationControllerAndroidTest,
  1194. HiddenAfterNonScrollingGesture) {
  1195. scrollbar_layer_->SetOverlayScrollbarLayerOpacityAnimated(0.f);
  1196. base::TimeTicks time;
  1197. time += base::Seconds(100);
  1198. scrollbar_controller_->Animate(time);
  1199. EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->Opacity());
  1200. EXPECT_TRUE(start_fade_.is_null());
  1201. time += base::Seconds(100);
  1202. scrollbar_controller_->Animate(time);
  1203. EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->Opacity());
  1204. }
  1205. // Confirm the scrollbar does not appear on WillUpdateScroll on Android.
  1206. TEST_F(ScrollbarAnimationControllerAndroidTest,
  1207. WillUpdateScrollNotAppearScrollbar) {
  1208. base::TimeTicks time;
  1209. time += base::Seconds(1);
  1210. // Scrollbar should be invisible.
  1211. EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->Opacity());
  1212. EXPECT_TRUE(scrollbar_controller_->ScrollbarsHidden());
  1213. // Scrollbar should appear when scroll will update.
  1214. scrollbar_controller_->WillUpdateScroll();
  1215. EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->Opacity());
  1216. EXPECT_TRUE(scrollbar_controller_->ScrollbarsHidden());
  1217. // No fade out animation should have been enqueued.
  1218. EXPECT_TRUE(start_fade_.is_null());
  1219. }
  1220. TEST_F(ScrollbarAnimationControllerAndroidTest, HideOnResize) {
  1221. EXPECT_EQ(gfx::Size(200, 200), scroll_layer_->bounds());
  1222. EXPECT_EQ(ScrollbarOrientation::HORIZONTAL, scrollbar_layer_->orientation());
  1223. // Shrink along X axis, horizontal scrollbar should appear.
  1224. GetScrollNode(scroll_layer_.get())->container_bounds = gfx::Size(100, 200);
  1225. scroll_layer_->UpdateScrollable();
  1226. UpdateActiveTreeDrawProperties();
  1227. scrollbar_controller_->DidScrollUpdate();
  1228. EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
  1229. // Shrink along Y axis and expand along X, horizontal scrollbar
  1230. // should disappear.
  1231. GetScrollNode(scroll_layer_.get())->container_bounds = gfx::Size(200, 100);
  1232. scroll_layer_->UpdateScrollable();
  1233. UpdateActiveTreeDrawProperties();
  1234. scrollbar_controller_->DidScrollUpdate();
  1235. EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->Opacity());
  1236. }
  1237. TEST_F(VerticalScrollbarAnimationControllerAndroidTest, HideOnResize) {
  1238. EXPECT_EQ(gfx::Size(200, 200), scroll_layer_->bounds());
  1239. EXPECT_EQ(ScrollbarOrientation::VERTICAL, scrollbar_layer_->orientation());
  1240. // Shrink along X axis, vertical scrollbar should remain invisible.
  1241. GetScrollNode(scroll_layer_.get())->container_bounds = gfx::Size(100, 200);
  1242. scroll_layer_->UpdateScrollable();
  1243. UpdateActiveTreeDrawProperties();
  1244. scrollbar_controller_->DidScrollUpdate();
  1245. EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->Opacity());
  1246. // Shrink along Y axis and expand along X, vertical scrollbar should appear.
  1247. GetScrollNode(scroll_layer_.get())->container_bounds = gfx::Size(200, 100);
  1248. scroll_layer_->UpdateScrollable();
  1249. UpdateActiveTreeDrawProperties();
  1250. scrollbar_controller_->DidScrollUpdate();
  1251. EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
  1252. }
  1253. TEST_F(ScrollbarAnimationControllerAndroidTest, HideOnUserNonScrollableHorz) {
  1254. EXPECT_EQ(ScrollbarOrientation::HORIZONTAL, scrollbar_layer_->orientation());
  1255. GetScrollNode(scroll_layer_.get())->user_scrollable_horizontal = false;
  1256. UpdateActiveTreeDrawProperties();
  1257. scrollbar_controller_->DidScrollUpdate();
  1258. EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->Opacity());
  1259. }
  1260. TEST_F(ScrollbarAnimationControllerAndroidTest, ShowOnUserNonScrollableVert) {
  1261. EXPECT_EQ(ScrollbarOrientation::HORIZONTAL, scrollbar_layer_->orientation());
  1262. GetScrollNode(scroll_layer_.get())->user_scrollable_vertical = false;
  1263. UpdateActiveTreeDrawProperties();
  1264. scrollbar_controller_->DidScrollUpdate();
  1265. EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
  1266. }
  1267. TEST_F(VerticalScrollbarAnimationControllerAndroidTest,
  1268. HideOnUserNonScrollableVert) {
  1269. EXPECT_EQ(ScrollbarOrientation::VERTICAL, scrollbar_layer_->orientation());
  1270. GetScrollNode(scroll_layer_.get())->user_scrollable_vertical = false;
  1271. UpdateActiveTreeDrawProperties();
  1272. scrollbar_controller_->DidScrollUpdate();
  1273. EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->Opacity());
  1274. }
  1275. TEST_F(VerticalScrollbarAnimationControllerAndroidTest,
  1276. ShowOnUserNonScrollableHorz) {
  1277. EXPECT_EQ(ScrollbarOrientation::VERTICAL, scrollbar_layer_->orientation());
  1278. GetScrollNode(scroll_layer_.get())->user_scrollable_horizontal = false;
  1279. UpdateActiveTreeDrawProperties();
  1280. scrollbar_controller_->DidScrollUpdate();
  1281. EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
  1282. }
  1283. TEST_F(ScrollbarAnimationControllerAndroidTest, AwakenByScrollingGesture) {
  1284. base::TimeTicks time;
  1285. time += base::Seconds(1);
  1286. EXPECT_FALSE(did_request_animate_);
  1287. scrollbar_controller_->DidScrollUpdate();
  1288. EXPECT_FALSE(did_request_animate_);
  1289. EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
  1290. EXPECT_FALSE(start_fade_.is_null());
  1291. time += base::Seconds(100);
  1292. scrollbar_controller_->Animate(time);
  1293. EXPECT_FALSE(did_request_animate_);
  1294. EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
  1295. EXPECT_FALSE(did_request_animate_);
  1296. std::move(start_fade_).Run();
  1297. EXPECT_TRUE(did_request_animate_);
  1298. did_request_animate_ = false;
  1299. time += base::Seconds(2);
  1300. scrollbar_controller_->Animate(time);
  1301. EXPECT_TRUE(did_request_animate_);
  1302. did_request_animate_ = false;
  1303. EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
  1304. time += base::Seconds(1);
  1305. scrollbar_controller_->Animate(time);
  1306. EXPECT_TRUE(did_request_animate_);
  1307. did_request_animate_ = false;
  1308. EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->Opacity());
  1309. time += base::Seconds(1);
  1310. scrollbar_controller_->Animate(time);
  1311. EXPECT_TRUE(did_request_animate_);
  1312. did_request_animate_ = false;
  1313. EXPECT_FLOAT_EQ(1.0f / 3.0f, scrollbar_layer_->Opacity());
  1314. time += base::Seconds(1);
  1315. scrollbar_controller_->DidScrollUpdate();
  1316. std::move(start_fade_).Run();
  1317. EXPECT_TRUE(did_request_animate_);
  1318. did_request_animate_ = false;
  1319. time += base::Seconds(2);
  1320. scrollbar_controller_->Animate(time);
  1321. EXPECT_TRUE(did_request_animate_);
  1322. did_request_animate_ = false;
  1323. EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
  1324. time += base::Seconds(1);
  1325. scrollbar_controller_->Animate(time);
  1326. EXPECT_TRUE(did_request_animate_);
  1327. did_request_animate_ = false;
  1328. EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->Opacity());
  1329. time += base::Seconds(1);
  1330. scrollbar_controller_->Animate(time);
  1331. EXPECT_TRUE(did_request_animate_);
  1332. did_request_animate_ = false;
  1333. EXPECT_FLOAT_EQ(1.0f / 3.0f, scrollbar_layer_->Opacity());
  1334. time += base::Seconds(1);
  1335. scrollbar_controller_->Animate(time);
  1336. EXPECT_FALSE(did_request_animate_);
  1337. EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->Opacity());
  1338. }
  1339. TEST_F(ScrollbarAnimationControllerAndroidTest, AwakenByProgrammaticScroll) {
  1340. base::TimeTicks time;
  1341. time += base::Seconds(1);
  1342. scrollbar_controller_->DidScrollUpdate();
  1343. EXPECT_FALSE(did_request_animate_);
  1344. std::move(start_fade_).Run();
  1345. EXPECT_TRUE(did_request_animate_);
  1346. did_request_animate_ = false;
  1347. scrollbar_controller_->Animate(time);
  1348. EXPECT_TRUE(did_request_animate_);
  1349. did_request_animate_ = false;
  1350. EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
  1351. time += base::Seconds(1);
  1352. scrollbar_controller_->Animate(time);
  1353. EXPECT_TRUE(did_request_animate_);
  1354. did_request_animate_ = false;
  1355. EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->Opacity());
  1356. scrollbar_controller_->DidScrollUpdate();
  1357. EXPECT_FALSE(did_request_animate_);
  1358. std::move(start_fade_).Run();
  1359. EXPECT_TRUE(did_request_animate_);
  1360. did_request_animate_ = false;
  1361. time += base::Seconds(2);
  1362. scrollbar_controller_->Animate(time);
  1363. EXPECT_TRUE(did_request_animate_);
  1364. did_request_animate_ = false;
  1365. EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
  1366. time += base::Seconds(1);
  1367. scrollbar_controller_->Animate(time);
  1368. EXPECT_TRUE(did_request_animate_);
  1369. did_request_animate_ = false;
  1370. EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->Opacity());
  1371. time += base::Seconds(1);
  1372. scrollbar_controller_->Animate(time);
  1373. EXPECT_TRUE(did_request_animate_);
  1374. did_request_animate_ = false;
  1375. EXPECT_FLOAT_EQ(1.0f / 3.0f, scrollbar_layer_->Opacity());
  1376. time += base::Seconds(1);
  1377. scrollbar_controller_->DidScrollUpdate();
  1378. std::move(start_fade_).Run();
  1379. time += base::Seconds(1);
  1380. scrollbar_controller_->Animate(time);
  1381. EXPECT_TRUE(did_request_animate_);
  1382. did_request_animate_ = false;
  1383. EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
  1384. time += base::Seconds(1);
  1385. scrollbar_controller_->Animate(time);
  1386. EXPECT_TRUE(did_request_animate_);
  1387. did_request_animate_ = false;
  1388. EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->Opacity());
  1389. time += base::Seconds(1);
  1390. scrollbar_controller_->Animate(time);
  1391. EXPECT_TRUE(did_request_animate_);
  1392. did_request_animate_ = false;
  1393. EXPECT_FLOAT_EQ(1.0f / 3.0f, scrollbar_layer_->Opacity());
  1394. time += base::Seconds(1);
  1395. scrollbar_controller_->Animate(time);
  1396. EXPECT_FALSE(did_request_animate_);
  1397. EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->Opacity());
  1398. }
  1399. TEST_F(ScrollbarAnimationControllerAndroidTest,
  1400. AnimationPreservedByNonScrollingGesture) {
  1401. base::TimeTicks time;
  1402. time += base::Seconds(1);
  1403. scrollbar_controller_->DidScrollUpdate();
  1404. std::move(start_fade_).Run();
  1405. EXPECT_TRUE(did_request_animate_);
  1406. did_request_animate_ = false;
  1407. scrollbar_controller_->Animate(time);
  1408. EXPECT_TRUE(did_request_animate_);
  1409. did_request_animate_ = false;
  1410. EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
  1411. time += base::Seconds(1);
  1412. scrollbar_controller_->Animate(time);
  1413. EXPECT_TRUE(did_request_animate_);
  1414. did_request_animate_ = false;
  1415. EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->Opacity());
  1416. EXPECT_FALSE(did_request_animate_);
  1417. EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->Opacity());
  1418. time += base::Seconds(1);
  1419. scrollbar_controller_->Animate(time);
  1420. EXPECT_TRUE(did_request_animate_);
  1421. did_request_animate_ = false;
  1422. EXPECT_FLOAT_EQ(1.0f / 3.0f, scrollbar_layer_->Opacity());
  1423. EXPECT_FALSE(did_request_animate_);
  1424. EXPECT_FLOAT_EQ(1.0f / 3.0f, scrollbar_layer_->Opacity());
  1425. time += base::Seconds(1);
  1426. scrollbar_controller_->Animate(time);
  1427. EXPECT_FALSE(did_request_animate_);
  1428. EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->Opacity());
  1429. }
  1430. TEST_F(ScrollbarAnimationControllerAndroidTest,
  1431. AnimationOverriddenByScrollingGesture) {
  1432. base::TimeTicks time;
  1433. time += base::Seconds(1);
  1434. scrollbar_controller_->DidScrollUpdate();
  1435. EXPECT_FALSE(did_request_animate_);
  1436. std::move(start_fade_).Run();
  1437. EXPECT_TRUE(did_request_animate_);
  1438. did_request_animate_ = false;
  1439. scrollbar_controller_->Animate(time);
  1440. EXPECT_TRUE(did_request_animate_);
  1441. did_request_animate_ = false;
  1442. EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
  1443. time += base::Seconds(1);
  1444. scrollbar_controller_->Animate(time);
  1445. EXPECT_TRUE(did_request_animate_);
  1446. did_request_animate_ = false;
  1447. EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->Opacity());
  1448. EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->Opacity());
  1449. time += base::Seconds(1);
  1450. scrollbar_controller_->Animate(time);
  1451. EXPECT_TRUE(did_request_animate_);
  1452. did_request_animate_ = false;
  1453. EXPECT_FLOAT_EQ(1.0f / 3.0f, scrollbar_layer_->Opacity());
  1454. time += base::Seconds(1);
  1455. scrollbar_controller_->DidScrollUpdate();
  1456. EXPECT_FALSE(did_request_animate_);
  1457. EXPECT_FLOAT_EQ(1, scrollbar_layer_->Opacity());
  1458. time += base::Seconds(1);
  1459. EXPECT_FALSE(did_request_animate_);
  1460. EXPECT_FLOAT_EQ(1, scrollbar_layer_->Opacity());
  1461. }
  1462. } // namespace
  1463. } // namespace cc