app_list_bubble_event_filter_unittest.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2021 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/app_list/app_list_bubble_event_filter.h"
  5. #include <memory>
  6. #include "ash/test/ash_test_base.h"
  7. #include "ash/test/test_widget_builder.h"
  8. #include "base/run_loop.h"
  9. #include "base/test/bind.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. namespace ash {
  12. namespace {
  13. // Parameterized by mouse events vs. touch events.
  14. class AppListBubbleEventFilterTest : public AshTestBase,
  15. public testing::WithParamInterface<bool> {
  16. public:
  17. AppListBubbleEventFilterTest() = default;
  18. AppListBubbleEventFilterTest(const AppListBubbleEventFilterTest&) = delete;
  19. AppListBubbleEventFilterTest& operator=(const AppListBubbleEventFilterTest&) =
  20. delete;
  21. ~AppListBubbleEventFilterTest() override = default;
  22. // testing::Test:
  23. void SetUp() override {
  24. AshTestBase::SetUp();
  25. widget_ = TestWidgetBuilder()
  26. .SetBounds({10, 10, 100, 100})
  27. .SetShow(true)
  28. .BuildOwnsNativeWidget();
  29. // Create a separate Widget to host the View. A View must live in a Widget
  30. // to have valid screen coordinates.
  31. view_holder_widget_ = TestWidgetBuilder()
  32. .SetBounds({500, 500, 100, 100})
  33. .SetShow(true)
  34. .BuildOwnsNativeWidget();
  35. view_ = view_holder_widget_->client_view()->AddChildView(
  36. std::make_unique<views::View>());
  37. view_->SetBoundsRect({0, 0, 32, 32});
  38. }
  39. // Generates a click or a tap based on test parameterization.
  40. void ClickOrTapAt(gfx::Point point_in_screen) {
  41. auto* generator = GetEventGenerator();
  42. if (GetParam()) {
  43. generator->MoveMouseTo(point_in_screen);
  44. generator->ClickLeftButton();
  45. } else {
  46. generator->GestureTapAt(point_in_screen);
  47. }
  48. }
  49. std::unique_ptr<views::Widget> widget_;
  50. std::unique_ptr<views::Widget> view_holder_widget_;
  51. views::View* view_ = nullptr;
  52. };
  53. INSTANTIATE_TEST_SUITE_P(MouseOrTouch,
  54. AppListBubbleEventFilterTest,
  55. testing::Bool());
  56. TEST_P(AppListBubbleEventFilterTest, ClickOutsideWidgetRunsCallback) {
  57. int callback_count = 0;
  58. auto callback = base::BindLambdaForTesting([&]() { ++callback_count; });
  59. AppListBubbleEventFilter filter(widget_.get(), view_, callback);
  60. // Click outside the widget.
  61. gfx::Point point_outside_widget = widget_->GetWindowBoundsInScreen().origin();
  62. point_outside_widget.Offset(-1, -1);
  63. ClickOrTapAt(point_outside_widget);
  64. EXPECT_EQ(callback_count, 1);
  65. }
  66. TEST_P(AppListBubbleEventFilterTest, ClickInsideWidgetDoesNotRunCallback) {
  67. bool callback_ran = false;
  68. auto callback = base::BindLambdaForTesting([&]() { callback_ran = true; });
  69. AppListBubbleEventFilter filter(widget_.get(), view_, callback);
  70. // Click inside the widget.
  71. ClickOrTapAt(widget_->GetWindowBoundsInScreen().CenterPoint());
  72. EXPECT_FALSE(callback_ran);
  73. }
  74. TEST_P(AppListBubbleEventFilterTest, ClickInsideViewDoesNotRunCallback) {
  75. bool callback_ran = false;
  76. auto callback = base::BindLambdaForTesting([&]() { callback_ran = true; });
  77. AppListBubbleEventFilter filter(widget_.get(), view_, callback);
  78. // Click inside the view.
  79. ClickOrTapAt(view_->GetBoundsInScreen().CenterPoint());
  80. EXPECT_FALSE(callback_ran);
  81. }
  82. } // namespace
  83. } // namespace ash