1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175

ash / quick_insert / views / quick_insert_widget_unittest.cc [blame]

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/quick_insert/views/quick_insert_widget.h"

#include <memory>
#include <string_view>

#include "ash/quick_insert/metrics/quick_insert_session_metrics.h"
#include "ash/quick_insert/model/quick_insert_action_type.h"
#include "ash/quick_insert/model/quick_insert_caps_lock_position.h"
#include "ash/quick_insert/views/quick_insert_preview_bubble.h"
#include "ash/quick_insert/views/quick_insert_view.h"
#include "ash/quick_insert/views/quick_insert_view_delegate.h"
#include "ash/test/ash_test_base.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/window.h"
#include "ui/display/screen.h"
#include "ui/events/test/event_generator.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/view_utils.h"
#include "ui/views/widget/widget_utils.h"

namespace ash {
namespace {

using ::testing::ElementsAre;
using ::testing::Truly;

constexpr gfx::Rect kDefaultAnchorBounds(200, 100, 0, 10);

class FakeQuickInsertViewDelegate : public QuickInsertViewDelegate {
 public:
  // QuickInsertViewDelegate:
  std::vector<QuickInsertCategory> GetAvailableCategories() override {
    return {};
  }
  void GetZeroStateSuggestedResults(
      SuggestedResultsCallback callback) override {}
  void GetResultsForCategory(QuickInsertCategory category,
                             SearchResultsCallback callback) override {}
  void StartSearch(std::u16string_view query,
                   std::optional<QuickInsertCategory> category,
                   SearchResultsCallback callback) override {}
  void StopSearch() override {}
  void StartEmojiSearch(std::u16string_view query,
                        EmojiSearchResultsCallback callback) override {}
  void CloseWidgetThenInsertResultOnNextFocus(
      const QuickInsertSearchResult& result) override {}
  void OpenResult(const QuickInsertSearchResult& result) override {}
  void ShowEmojiPicker(ui::EmojiPickerCategory category,
                       std::u16string_view query) override {}
  void ShowEditor(std::optional<std::string> preset_query_id,
                  std::optional<std::string> freeform_text) override {}
  void ShowLobster(std::optional<std::string> freeform_text) override {}
  QuickInsertAssetFetcher* GetAssetFetcher() override { return nullptr; }
  QuickInsertSessionMetrics& GetSessionMetrics() override {
    return session_metrics_;
  }
  QuickInsertActionType GetActionForResult(
      const QuickInsertSearchResult& result) override {
    return QuickInsertActionType::kInsert;
  }
  std::vector<QuickInsertEmojiResult> GetSuggestedEmoji() override {
    return {};
  }
  bool IsGifsEnabled() override { return true; }
  QuickInsertModeType GetMode() override {
    return QuickInsertModeType::kNoSelection;
  }
  QuickInsertCapsLockPosition GetCapsLockPosition() override {
    return QuickInsertCapsLockPosition::kTop;
  }

 private:
  QuickInsertSessionMetrics session_metrics_;
};

using QuickInsertWidgetTest = AshTestBase;

TEST_F(QuickInsertWidgetTest, CreateWidgetHasCorrectHierarchy) {
  FakeQuickInsertViewDelegate delegate;
  auto widget = QuickInsertWidget::Create(&delegate, kDefaultAnchorBounds);

  // Widget should contain a NonClientView, which has a NonClientFrameView for
  // borders and shadows, and a ClientView with a sole child of the
  // QuickInsertView.
  ASSERT_TRUE(widget);
  ASSERT_TRUE(widget->non_client_view());
  ASSERT_TRUE(widget->non_client_view()->frame_view());
  ASSERT_TRUE(widget->non_client_view()->client_view());
  EXPECT_THAT(widget->non_client_view()->client_view()->children(),
              ElementsAre(Truly(views::IsViewClass<QuickInsertView>)));
}

TEST_F(QuickInsertWidgetTest, CreateWidgetHasCorrectBorder) {
  FakeQuickInsertViewDelegate delegate;
  auto widget = QuickInsertWidget::Create(&delegate, kDefaultAnchorBounds);

  EXPECT_TRUE(widget->non_client_view()->frame_view()->GetBorder());
}

TEST_F(QuickInsertWidgetTest, ClickingOutsideClosesQuickInsertWidget) {
  FakeQuickInsertViewDelegate delegate;
  auto widget = QuickInsertWidget::Create(&delegate, kDefaultAnchorBounds);
  widget->Show();

  gfx::Point point_outside_widget = widget->GetWindowBoundsInScreen().origin();
  point_outside_widget.Offset(-10, -10);
  GetEventGenerator()->MoveMouseTo(point_outside_widget);
  GetEventGenerator()->ClickLeftButton();

  EXPECT_TRUE(widget->IsClosed());
}

TEST_F(QuickInsertWidgetTest, LosingFocusClosesQuickInsertWidget) {
  // Create something other than Quick Insert to focus.
  auto window = CreateTestWindow();
  window->Show();

  // Create the fake Quick Insert and make sure it has focus.
  FakeQuickInsertViewDelegate delegate;
  auto quick_insert_widget =
      QuickInsertWidget::Create(&delegate, kDefaultAnchorBounds);
  quick_insert_widget->Show();
  EXPECT_THAT(quick_insert_widget->GetFocusManager()->GetFocusedView(),
              testing::NotNull());

  // Focus the other Widget and expect Quick Insert to have closed.
  window->Focus();
  EXPECT_TRUE(window->HasFocus());

  EXPECT_TRUE(quick_insert_widget->IsClosed());
  EXPECT_EQ(delegate.GetSessionMetrics().GetOutcomeForTesting(),
            QuickInsertSessionMetrics::SessionOutcome::kAbandoned);
}

TEST_F(QuickInsertWidgetTest, PreviewBubbleDoesNotStealFocusQuickInsertWidget) {
  std::unique_ptr<views::Widget> anchor_widget = CreateFramelessTestWidget();
  anchor_widget->SetContentsView(std::make_unique<views::View>());

  // Create the QuickInsertWidget and make sure it has focus.
  FakeQuickInsertViewDelegate delegate;
  auto quick_insert_widget =
      QuickInsertWidget::Create(&delegate, kDefaultAnchorBounds);
  quick_insert_widget->Show();

  // Show bubble widget and expect the QuickInsertWidget to not close.
  views::View* bubble_view =
      new QuickInsertPreviewBubbleView(anchor_widget->GetContentsView());
  bubble_view->GetWidget()->Show();

  EXPECT_FALSE(quick_insert_widget->IsClosed());

  bubble_view->GetWidget()->CloseNow();
}

TEST_F(QuickInsertWidgetTest, CreatesCenteredWidget) {
  FakeQuickInsertViewDelegate delegate;
  auto widget =
      QuickInsertWidget::CreateCentered(&delegate, gfx::Rect(10, 10, 10, 10));
  widget->Show();

  EXPECT_EQ(widget->GetWindowBoundsInScreen().CenterPoint(),
            display::Screen::GetScreen()
                ->GetPrimaryDisplay()
                .work_area()
                .CenterPoint());
}

}  // namespace
}  // namespace ash