shelf_tooltip_manager_unittest.cc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 "ash/shelf/shelf_tooltip_manager.h"
  5. #include <memory>
  6. #include "ash/public/cpp/shelf_model.h"
  7. #include "ash/public/cpp/test/test_shelf_item_delegate.h"
  8. #include "ash/shelf/home_button.h"
  9. #include "ash/shelf/shelf.h"
  10. #include "ash/shelf/shelf_bubble.h"
  11. #include "ash/shelf/shelf_view.h"
  12. #include "ash/shelf/shelf_view_test_api.h"
  13. #include "ash/shell.h"
  14. #include "ash/test/ash_test_base.h"
  15. #include "ash/wm/collision_detection/collision_detection_utils.h"
  16. #include "base/run_loop.h"
  17. #include "ui/events/event_constants.h"
  18. #include "ui/events/test/event_generator.h"
  19. #include "ui/views/bubble/bubble_dialog_delegate_view.h"
  20. #include "ui/views/widget/widget.h"
  21. namespace ash {
  22. class ShelfTooltipManagerTest : public AshTestBase {
  23. public:
  24. ShelfTooltipManagerTest() = default;
  25. ShelfTooltipManagerTest(const ShelfTooltipManagerTest&) = delete;
  26. ShelfTooltipManagerTest& operator=(const ShelfTooltipManagerTest&) = delete;
  27. ~ShelfTooltipManagerTest() override = default;
  28. void SetUp() override {
  29. AshTestBase::SetUp();
  30. shelf_view_ = GetPrimaryShelf()->GetShelfViewForTesting();
  31. test_api_ = std::make_unique<ShelfViewTestAPI>(shelf_view_);
  32. test_api_->AddItem(TYPE_PINNED_APP);
  33. test_api_->RunMessageLoopUntilAnimationsDone();
  34. tooltip_manager_ = test_api_->tooltip_manager();
  35. tooltip_manager_->set_timer_delay_for_test(0);
  36. }
  37. bool IsTimerRunning() { return tooltip_manager_->timer_.IsRunning(); }
  38. views::Widget* GetTooltip() { return tooltip_manager_->bubble_->GetWidget(); }
  39. void ShowTooltipForFirstAppIcon() {
  40. EXPECT_GE(shelf_view_->number_of_visible_apps(), 1u);
  41. tooltip_manager_->ShowTooltip(
  42. shelf_view_->first_visible_button_for_testing());
  43. }
  44. protected:
  45. ShelfView* shelf_view_;
  46. ShelfTooltipManager* tooltip_manager_;
  47. std::unique_ptr<ShelfViewTestAPI> test_api_;
  48. };
  49. TEST_F(ShelfTooltipManagerTest, ShowTooltip) {
  50. ShowTooltipForFirstAppIcon();
  51. EXPECT_TRUE(tooltip_manager_->IsVisible());
  52. EXPECT_FALSE(IsTimerRunning());
  53. }
  54. TEST_F(ShelfTooltipManagerTest, ShowTooltipWithDelay) {
  55. // ShowTooltipWithDelay should start the timer instead of showing immediately.
  56. tooltip_manager_->ShowTooltipWithDelay(
  57. shelf_view_->first_visible_button_for_testing());
  58. EXPECT_FALSE(tooltip_manager_->IsVisible());
  59. EXPECT_TRUE(IsTimerRunning());
  60. // TODO: Test that the delayed tooltip is shown, without flaky failures.
  61. }
  62. TEST_F(ShelfTooltipManagerTest, DoNotShowForInvalidView) {
  63. // The manager should not show or start the timer for a null view.
  64. tooltip_manager_->ShowTooltip(nullptr);
  65. EXPECT_FALSE(tooltip_manager_->IsVisible());
  66. tooltip_manager_->ShowTooltipWithDelay(nullptr);
  67. EXPECT_FALSE(IsTimerRunning());
  68. // The manager should not show or start the timer for a non-shelf view.
  69. views::View view;
  70. tooltip_manager_->ShowTooltip(&view);
  71. EXPECT_FALSE(tooltip_manager_->IsVisible());
  72. tooltip_manager_->ShowTooltipWithDelay(&view);
  73. EXPECT_FALSE(IsTimerRunning());
  74. // The manager should start the timer for a view on the shelf.
  75. ShelfModel* model = shelf_view_->model();
  76. ShelfItem item;
  77. item.id = ShelfID("foo");
  78. item.type = TYPE_PINNED_APP;
  79. const int index =
  80. model->Add(item, std::make_unique<TestShelfItemDelegate>(item.id));
  81. ShelfViewTestAPI(GetPrimaryShelf()->GetShelfViewForTesting())
  82. .RunMessageLoopUntilAnimationsDone();
  83. // The index of a ShelfItem in the model should be the same as its index
  84. // within the |shelf_view_|'s list of children.
  85. tooltip_manager_->ShowTooltipWithDelay(shelf_view_->children().at(index));
  86. EXPECT_TRUE(IsTimerRunning());
  87. // Removing the view won't stop the timer, but the tooltip shouldn't be shown.
  88. model->RemoveItemAt(index);
  89. EXPECT_TRUE(IsTimerRunning());
  90. base::RunLoop().RunUntilIdle();
  91. EXPECT_FALSE(IsTimerRunning());
  92. EXPECT_FALSE(tooltip_manager_->IsVisible());
  93. }
  94. TEST_F(ShelfTooltipManagerTest, HideWhenShelfIsHidden) {
  95. ShowTooltipForFirstAppIcon();
  96. ASSERT_TRUE(tooltip_manager_->IsVisible());
  97. // Create a full-screen window to hide the shelf.
  98. std::unique_ptr<views::Widget> widget = CreateTestWidget();
  99. widget->SetFullscreen(true);
  100. // Once the shelf is hidden, the tooltip should be invisible.
  101. ASSERT_EQ(SHELF_HIDDEN, GetPrimaryShelf()->GetVisibilityState());
  102. EXPECT_FALSE(tooltip_manager_->IsVisible());
  103. // Do not show the view if the shelf is hidden.
  104. ShowTooltipForFirstAppIcon();
  105. EXPECT_FALSE(tooltip_manager_->IsVisible());
  106. // ShowTooltipWithDelay doesn't even start the timer for the hidden shelf.
  107. tooltip_manager_->ShowTooltipWithDelay(
  108. shelf_view_->first_visible_button_for_testing());
  109. EXPECT_FALSE(IsTimerRunning());
  110. }
  111. TEST_F(ShelfTooltipManagerTest, HideWhenShelfIsAutoHideHidden) {
  112. // Create a visible window so auto-hide behavior can actually hide the shelf.
  113. std::unique_ptr<views::Widget> widget = CreateTestWidget();
  114. ShowTooltipForFirstAppIcon();
  115. ASSERT_TRUE(tooltip_manager_->IsVisible());
  116. GetPrimaryShelf()->SetAutoHideBehavior(ShelfAutoHideBehavior::kAlways);
  117. GetPrimaryShelf()->UpdateAutoHideState();
  118. ASSERT_EQ(ShelfAutoHideBehavior::kAlways,
  119. GetPrimaryShelf()->auto_hide_behavior());
  120. ASSERT_EQ(SHELF_AUTO_HIDE_HIDDEN, GetPrimaryShelf()->GetAutoHideState());
  121. EXPECT_FALSE(tooltip_manager_->IsVisible());
  122. // Do not show the view if the shelf is hidden.
  123. ShowTooltipForFirstAppIcon();
  124. EXPECT_FALSE(tooltip_manager_->IsVisible());
  125. // ShowTooltipWithDelay doesn't even run the timer for the hidden shelf.
  126. tooltip_manager_->ShowTooltipWithDelay(
  127. shelf_view_->first_visible_button_for_testing());
  128. EXPECT_FALSE(IsTimerRunning());
  129. // Close the window to show the auto-hide shelf; tooltips should now show.
  130. widget.reset();
  131. GetPrimaryShelf()->UpdateAutoHideState();
  132. ASSERT_EQ(ShelfAutoHideBehavior::kAlways,
  133. GetPrimaryShelf()->auto_hide_behavior());
  134. ASSERT_EQ(SHELF_AUTO_HIDE_SHOWN, GetPrimaryShelf()->GetAutoHideState());
  135. // The tooltip should show for an auto-hide-shown shelf.
  136. ShowTooltipForFirstAppIcon();
  137. EXPECT_TRUE(tooltip_manager_->IsVisible());
  138. // ShowTooltipWithDelay should run the timer for an auto-hide-shown shelf.
  139. tooltip_manager_->ShowTooltipWithDelay(
  140. shelf_view_->first_visible_button_for_testing());
  141. EXPECT_TRUE(IsTimerRunning());
  142. }
  143. TEST_F(ShelfTooltipManagerTest, HideForEvents) {
  144. ui::test::EventGenerator* generator = GetEventGenerator();
  145. gfx::Rect shelf_bounds = shelf_view_->GetBoundsInScreen();
  146. // Should hide if the mouse exits the shelf area.
  147. ShowTooltipForFirstAppIcon();
  148. ASSERT_TRUE(tooltip_manager_->IsVisible());
  149. generator->MoveMouseTo(shelf_bounds.CenterPoint());
  150. generator->SendMouseExit();
  151. EXPECT_FALSE(tooltip_manager_->IsVisible());
  152. // Should hide if the mouse is pressed in the shelf area.
  153. ShowTooltipForFirstAppIcon();
  154. ASSERT_TRUE(tooltip_manager_->IsVisible());
  155. generator->MoveMouseTo(shelf_bounds.CenterPoint());
  156. generator->PressLeftButton();
  157. EXPECT_FALSE(tooltip_manager_->IsVisible());
  158. generator->ReleaseLeftButton();
  159. // Should hide for touch events in the shelf.
  160. ShowTooltipForFirstAppIcon();
  161. ASSERT_TRUE(tooltip_manager_->IsVisible());
  162. generator->set_current_screen_location(shelf_bounds.CenterPoint());
  163. generator->PressTouch();
  164. EXPECT_FALSE(tooltip_manager_->IsVisible());
  165. // Should hide for gesture events in the shelf.
  166. ShowTooltipForFirstAppIcon();
  167. ASSERT_TRUE(tooltip_manager_->IsVisible());
  168. generator->GestureTapDownAndUp(shelf_bounds.CenterPoint());
  169. EXPECT_FALSE(tooltip_manager_->IsVisible());
  170. }
  171. TEST_F(ShelfTooltipManagerTest, HideForExternalEvents) {
  172. ui::test::EventGenerator* generator = GetEventGenerator();
  173. // Should hide for touches outside the shelf.
  174. ShowTooltipForFirstAppIcon();
  175. ASSERT_TRUE(tooltip_manager_->IsVisible());
  176. generator->set_current_screen_location(gfx::Point());
  177. generator->PressTouch();
  178. EXPECT_FALSE(tooltip_manager_->IsVisible());
  179. generator->ReleaseTouch();
  180. // Should hide for touch events on the tooltip.
  181. ShowTooltipForFirstAppIcon();
  182. ASSERT_TRUE(tooltip_manager_->IsVisible());
  183. generator->set_current_screen_location(
  184. GetTooltip()->GetWindowBoundsInScreen().CenterPoint());
  185. generator->PressTouch();
  186. EXPECT_FALSE(tooltip_manager_->IsVisible());
  187. generator->ReleaseTouch();
  188. // Should hide for gestures outside the shelf.
  189. ShowTooltipForFirstAppIcon();
  190. ASSERT_TRUE(tooltip_manager_->IsVisible());
  191. generator->GestureTapDownAndUp(gfx::Point());
  192. EXPECT_FALSE(tooltip_manager_->IsVisible());
  193. }
  194. TEST_F(ShelfTooltipManagerTest, KeyEvents) {
  195. ui::test::EventGenerator* generator = GetEventGenerator();
  196. // Should hide when 'Esc' is pressed.
  197. ShowTooltipForFirstAppIcon();
  198. ASSERT_TRUE(tooltip_manager_->IsVisible());
  199. generator->PressKey(ui::VKEY_ESCAPE, ui::EF_NONE);
  200. EXPECT_FALSE(tooltip_manager_->IsVisible());
  201. }
  202. TEST_F(ShelfTooltipManagerTest, ShelfTooltipDoesNotAffectPipWindow) {
  203. ShowTooltipForFirstAppIcon();
  204. EXPECT_TRUE(tooltip_manager_->IsVisible());
  205. auto display = display::Screen::GetScreen()->GetPrimaryDisplay();
  206. auto tooltip_bounds = GetTooltip()->GetWindowBoundsInScreen();
  207. tooltip_bounds.Intersect(CollisionDetectionUtils::GetMovementArea(display));
  208. EXPECT_FALSE(tooltip_bounds.IsEmpty());
  209. EXPECT_EQ(tooltip_bounds,
  210. CollisionDetectionUtils::GetRestingPosition(
  211. display, tooltip_bounds,
  212. CollisionDetectionUtils::RelativePriority::kPictureInPicture));
  213. }
  214. TEST_F(ShelfTooltipManagerTest, ShelfTooltipClosesIfScroll) {
  215. ui::test::EventGenerator* generator = GetEventGenerator();
  216. ShowTooltipForFirstAppIcon();
  217. ASSERT_TRUE(tooltip_manager_->IsVisible());
  218. gfx::Point cursor_position_in_screen =
  219. display::Screen::GetScreen()->GetCursorScreenPoint();
  220. generator->ScrollSequence(cursor_position_in_screen, base::TimeDelta(), 0, 3,
  221. 10, 1);
  222. EXPECT_FALSE(tooltip_manager_->IsVisible());
  223. }
  224. } // namespace ash