File: toast_view_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (177 lines) | stat: -rw-r--r-- 5,875 bytes parent folder | download | duplicates (6)
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
176
177
// 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 "chrome/browser/ui/toasts/toast_view.h"

#include <memory>

#include "base/functional/callback_helpers.h"
#include "chrome/browser/ui/test/test_browser_dialog.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/top_container_view.h"
#include "components/vector_icons/vector_icons.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/image/image_unittest_util.h"
#include "ui/menus/simple_menu_model.h"
#include "ui/strings/grit/ui_strings.h"
#include "ui/views/window/dialog_client_view.h"

namespace {

class TestMenuModel : public ui::SimpleMenuModel,
                      ui::SimpleMenuModel::Delegate {
 public:
  TestMenuModel() : ui::SimpleMenuModel(/*delegate=*/this) {}

  // ui::SimpleMenuModel::Delegate:
  void ExecuteCommand(int command_id, int event_flags) override {}
};

class ToastViewTest : public DialogBrowserTest {
 public:
  ToastViewTest() = default;

  struct ToastOptions {
    std::u16string text;
    bool add_close_button = false;
    bool add_action_button = false;
    bool add_menu = false;
    bool add_image_override = false;
  };

  void ConfigureToast(const ToastOptions& options) { options_ = options; }

  void ShowUi(const std::string& name) override {
    anchor_view_ =
        BrowserView::GetBrowserViewForBrowser(browser())->top_container();
    std::u16string toast_text = l10n_util::GetStringUTF16(IDS_LINK_COPIED);
    if (!options_.text.empty()) {
      toast_text = options_.text;
    }
    if (options_.add_image_override) {
      int size = toasts::ToastView::GetIconSize();
      image_override_ =
          std::make_unique<ui::ImageModel>(ui::ImageModel::FromImage(
              gfx::test::CreateImage(size, size, 0xff0000)));
    }
    std::unique_ptr<toasts::ToastView> toast =
        std::make_unique<toasts::ToastView>(
            anchor_view_, toast_text, vector_icons::kLinkIcon,
            image_override_.get(), false, base::DoNothing());
    if (options_.add_close_button) {
      toast->AddCloseButton(base::DoNothing());
    }
    if (options_.add_action_button) {
      toast->AddActionButton(l10n_util::GetStringUTF16(IDS_APP_OK));
    }
    if (options_.add_menu) {
      toast->AddMenu(std::make_unique<TestMenuModel>());
    }
    toast_ = toast.get();
    widget_ = views::BubbleDialogDelegateView::CreateBubble(std::move(toast));
    widget_->ShowInactive();
  }

  bool VerifyUi() override {
    if (!widget_ || !widget_->IsVisible()) {
      return false;
    }
    ChromeLayoutProvider* lp = ChromeLayoutProvider::Get();
    EXPECT_EQ(u"Link copied",
              toast_->label_for_testing()->GetDisplayTextForTesting());
    EXPECT_EQ(lp->GetDistanceMetric(DISTANCE_TOAST_BUBBLE_HEIGHT),
              toast_->GetDialogClientView()->height());
    return true;
  }

  void DismissUi() override {
    anchor_view_ = nullptr;
    toast_ = nullptr;
    widget_ = nullptr;
  }

  views::View* anchor_view() { return anchor_view_; }
  toasts::ToastView* toast() { return toast_; }

 private:
  raw_ptr<views::View> anchor_view_;
  std::unique_ptr<ui::ImageModel> image_override_;
  raw_ptr<toasts::ToastView> toast_;
  raw_ptr<views::Widget> widget_;
  ToastOptions options_;
};

IN_PROC_BROWSER_TEST_F(ToastViewTest, InvokeUi_Basic) {
  ShowAndVerifyUi();
}

IN_PROC_BROWSER_TEST_F(ToastViewTest, InvokeUi_ActionButton) {
  ConfigureToast({
      .add_action_button = true,
  });
  ShowUi("");
  ASSERT_TRUE(VerifyUi());
  ChromeLayoutProvider* lp = ChromeLayoutProvider::Get();
  EXPECT_TRUE(toast()->action_button_for_testing());
  EXPECT_EQ(lp->GetDistanceMetric(
                DISTANCE_TOAST_BUBBLE_BETWEEN_LABEL_ACTION_BUTTON_SPACING),
            toast()->action_button_for_testing()->x() -
                toast()->label_for_testing()->bounds().right());
  EXPECT_EQ(
      lp->GetDistanceMetric(DISTANCE_TOAST_BUBBLE_MARGIN_RIGHT_ACTION_BUTTON),
      toast()->GetDialogClientView()->width() - toast()->bounds().right());
  DismissUi();
}

IN_PROC_BROWSER_TEST_F(ToastViewTest, InvokeUi_CloseButton) {
  ConfigureToast({
      .add_close_button = true,
  });
  ShowUi("");
  ASSERT_TRUE(VerifyUi());
  ChromeLayoutProvider* lp = ChromeLayoutProvider::Get();
  EXPECT_TRUE(toast()->close_button_for_testing());
  EXPECT_EQ(
      lp->GetDistanceMetric(DISTANCE_TOAST_BUBBLE_MARGIN_RIGHT_CLOSE_BUTTON),
      toast()->GetDialogClientView()->width() - toast()->bounds().right());
  DismissUi();
}

IN_PROC_BROWSER_TEST_F(ToastViewTest, InvokeUi_Image) {
  ConfigureToast({
      .add_image_override = true,
  });
  ShowAndVerifyUi();
}

IN_PROC_BROWSER_TEST_F(ToastViewTest, InvokeUi_Menu) {
  ConfigureToast({
      .add_menu = true,
  });
  ShowAndVerifyUi();
}

// http://crbug.com/371579791
IN_PROC_BROWSER_TEST_F(ToastViewTest, InvokeUi_ShrinkToFitWindow) {
  ConfigureToast({
      // Use an arbitrarily-long label string to force a wide toast.
      .text = std::u16string(1000, 'a'),
  });
  ShowUi("");
  const views::View* bubble = toast()->GetBubbleFrameView();
  const int expected_margin = ChromeLayoutProvider::Get()->GetDistanceMetric(
                                  DISTANCE_TOAST_BUBBLE_BROWSER_WINDOW_MARGIN) -
                              views::BubbleBorder::kShadowBlur;
  EXPECT_GT(bubble->GetPreferredSize().width(), bubble->width());
  EXPECT_EQ(bubble->GetBoundsInScreen().x(),
            anchor_view()->GetBoundsInScreen().x() + expected_margin);
  EXPECT_EQ(bubble->GetBoundsInScreen().right(),
            anchor_view()->GetBoundsInScreen().right() - expected_margin);
  DismissUi();
}

}  // namespace