File: constrained_web_dialog_ui_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (315 lines) | stat: -rw-r--r-- 12,284 bytes parent folder | download | duplicates (5)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright 2012 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/webui/constrained_web_dialog_ui.h"

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/location.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "build/build_config.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/web_modal/web_contents_modal_dialog_manager.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_utils.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/web_dialogs/test/test_web_dialog_delegate.h"

using content::WebContents;
using ui::WebDialogDelegate;
using web_modal::WebContentsModalDialogManager;

namespace {

static const char kTestDataURL[] =
    "data:text/html,<!doctype html>"
    "<body></body>"
    "<style>"
    "body { height: 150px; width: 150px; }"
    "</style>";

bool IsEqualSizes(gfx::Size expected,
                  ConstrainedWebDialogDelegate* dialog_delegate) {
  return expected == dialog_delegate->GetConstrainedWebDialogPreferredSize();
}

std::string GetChangeDimensionsScript(int dimension) {
  return base::StringPrintf(
      "window.document.body.style.width = %d + 'px';"
      "window.document.body.style.height = %d + 'px';",
      dimension, dimension);
}

class AutoResizingTestWebDialogDelegate
    : public ui::test::TestWebDialogDelegate {
 public:
  explicit AutoResizingTestWebDialogDelegate(const GURL& url)
      : TestWebDialogDelegate(url) {}
  ~AutoResizingTestWebDialogDelegate() override = default;

  // Dialog delegates for auto-resizing dialogs are expected not to set |size|.
  void GetDialogSize(gfx::Size* size) const override {}
};

}  // namespace

class ConstrainedWebDialogBrowserTest : public InProcessBrowserTest {
 public:
  ConstrainedWebDialogBrowserTest() = default;

  // Runs the current MessageLoop until |condition| is true or timeout.
  bool RunLoopUntil(base::RepeatingCallback<bool()> condition) {
    const base::TimeTicks start_time = base::TimeTicks::Now();
    while (!condition.Run()) {
      const base::TimeTicks current_time = base::TimeTicks::Now();
      if (current_time - start_time > base::Seconds(5)) {
        ADD_FAILURE() << "Condition not met within five seconds.";
        return false;
      }

      base::RunLoop run_loop;
      base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
          FROM_HERE, run_loop.QuitClosure(), base::Milliseconds(20));
      run_loop.Run();
    }
    return true;
  }

 protected:
  bool IsShowingWebContentsModalDialog(WebContents* web_contents) const {
    WebContentsModalDialogManager* web_contents_modal_dialog_manager =
        WebContentsModalDialogManager::FromWebContents(web_contents);
    return web_contents_modal_dialog_manager->IsDialogActive();
  }
};

// Tests that opening/closing the constrained window won't crash it.
// Flaky on trusty builder: http://crbug.com/1020490.
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#define MAYBE_BasicTest DISABLED_BasicTest
#else
#define MAYBE_BasicTest BasicTest
#endif
IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest, MAYBE_BasicTest) {
  auto delegate = std::make_unique<ui::test::TestWebDialogDelegate>(
      GURL(chrome::kChromeUIConstrainedHTMLTestURL));
  WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  ASSERT_TRUE(web_contents);

  ConstrainedWebDialogDelegate* dialog_delegate = ShowConstrainedWebDialog(
      browser()->profile(), std::move(delegate), web_contents);

  ASSERT_TRUE(dialog_delegate);
  EXPECT_TRUE(dialog_delegate->GetNativeDialog());
  EXPECT_TRUE(IsShowingWebContentsModalDialog(web_contents));
}

// TODO(crbug.com/40656271): Crashy on Linux
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#define MAYBE_ReleaseWebContents DISABLED_ReleaseWebContents
#else
#define MAYBE_ReleaseWebContents ReleaseWebContents
#endif
// Tests that ReleaseWebContents() works.
IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest,
                       MAYBE_ReleaseWebContents) {
  auto delegate = std::make_unique<ui::test::TestWebDialogDelegate>(
      GURL(chrome::kChromeUIConstrainedHTMLTestURL));
  WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  ASSERT_TRUE(web_contents);

  ConstrainedWebDialogDelegate* dialog_delegate = ShowConstrainedWebDialog(
      browser()->profile(), std::move(delegate), web_contents);
  ASSERT_TRUE(dialog_delegate);
  WebContents* dialog_contents = dialog_delegate->GetWebContents();
  ASSERT_TRUE(dialog_contents);
  ASSERT_TRUE(IsShowingWebContentsModalDialog(web_contents));

  content::WebContentsDestroyedWatcher watcher(dialog_contents);
  std::unique_ptr<WebContents> dialog_contents_holder =
      dialog_delegate->ReleaseWebContents();
  dialog_delegate->OnDialogCloseFromWebUI();

  ASSERT_FALSE(watcher.IsDestroyed());
  EXPECT_FALSE(IsShowingWebContentsModalDialog(web_contents));
  dialog_contents_holder.reset();
  EXPECT_TRUE(watcher.IsDestroyed());
}

// Tests that dialog autoresizes based on web contents when autoresizing
// is enabled.
// Flaky on CrOS: http://crbug.com/928924
// Flaky on Mac: http://crbug.com/1498848
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_CHROMEOS)
#define MAYBE_ContentResizeInAutoResizingDialog \
  DISABLED_ContentResizeInAutoResizingDialog
#else
#define MAYBE_ContentResizeInAutoResizingDialog \
  ContentResizeInAutoResizingDialog
#endif
IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest,
                       MAYBE_ContentResizeInAutoResizingDialog) {
  // During auto-resizing, dialogs size to (WebContents size) + 16.
  const int dialog_border_space = 16;

  // Expected dialog sizes after auto-resizing.
  const int initial_size = 150 + dialog_border_space;
  const int new_size = 175 + dialog_border_space;

  auto delegate =
      std::make_unique<AutoResizingTestWebDialogDelegate>(GURL(kTestDataURL));
  WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  ASSERT_TRUE(web_contents);

  // Observes the next created WebContents.
  content::TestNavigationObserver observer(nullptr);
  observer.StartWatchingNewWebContents();

  gfx::Size min_size = gfx::Size(100, 100);
  gfx::Size max_size = gfx::Size(200, 200);
  gfx::Size initial_dialog_size;

  delegate->GetDialogSize(&initial_dialog_size);

  ConstrainedWebDialogDelegate* dialog_delegate =
      ShowConstrainedWebDialogWithAutoResize(browser()->profile(),
                                             std::move(delegate), web_contents,
                                             min_size, max_size);
  ASSERT_TRUE(dialog_delegate);
  EXPECT_TRUE(dialog_delegate->GetNativeDialog());
  ASSERT_FALSE(IsShowingWebContentsModalDialog(web_contents));
  EXPECT_EQ(min_size, dialog_delegate->GetConstrainedWebDialogMinimumSize());
  EXPECT_EQ(max_size, dialog_delegate->GetConstrainedWebDialogMaximumSize());

  // Check for initial sizing. Dialog was created as a 400x400 dialog.
  ASSERT_EQ(initial_dialog_size,
            dialog_delegate->GetConstrainedWebDialogPreferredSize());

  observer.Wait();

  // Wait until the entire WebContents has loaded.
  EXPECT_TRUE(WaitForLoadStop(dialog_delegate->GetWebContents()));

  ASSERT_TRUE(IsShowingWebContentsModalDialog(web_contents));

  // Resize to content's originally set dimensions.
  ASSERT_TRUE(RunLoopUntil(base::BindRepeating(
      &IsEqualSizes, gfx::Size(initial_size, initial_size), dialog_delegate)));

  // Resize to dimensions within expected bounds.
  EXPECT_TRUE(ExecJs(dialog_delegate->GetWebContents(),
                     GetChangeDimensionsScript(175)));
  ASSERT_TRUE(RunLoopUntil(base::BindRepeating(
      &IsEqualSizes, gfx::Size(new_size, new_size), dialog_delegate)));

  // Resize to dimensions smaller than the minimum bounds.
  EXPECT_TRUE(
      ExecJs(dialog_delegate->GetWebContents(), GetChangeDimensionsScript(50)));
  ASSERT_TRUE(RunLoopUntil(
      base::BindRepeating(&IsEqualSizes, min_size, dialog_delegate)));

  // Resize to dimensions greater than the maximum bounds.
  EXPECT_TRUE(ExecJs(dialog_delegate->GetWebContents(),
                     GetChangeDimensionsScript(250)));
  ASSERT_TRUE(RunLoopUntil(
      base::BindRepeating(&IsEqualSizes, max_size, dialog_delegate)));
}

// Tests that dialog does not autoresize when autoresizing is not enabled.
IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest,
                       ContentResizeInNonAutoResizingDialog) {
  auto delegate =
      std::make_unique<ui::test::TestWebDialogDelegate>(GURL(kTestDataURL));
  auto* delegate_ptr = delegate.get();
  WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  ASSERT_TRUE(web_contents);

  ConstrainedWebDialogDelegate* dialog_delegate = ShowConstrainedWebDialog(
      browser()->profile(), std::move(delegate), web_contents);
  ASSERT_TRUE(dialog_delegate);
  EXPECT_TRUE(dialog_delegate->GetNativeDialog());
  EXPECT_TRUE(IsShowingWebContentsModalDialog(web_contents));

  // Wait until the entire WebContents has loaded.
  EXPECT_TRUE(WaitForLoadStop(dialog_delegate->GetWebContents()));

  gfx::Size initial_dialog_size;
  delegate_ptr->GetDialogSize(&initial_dialog_size);

  // Check for initial sizing. Dialog was created as a 400x400 dialog.
  ASSERT_EQ(initial_dialog_size,
            dialog_delegate->GetConstrainedWebDialogPreferredSize());

  // Resize <body> to dimension smaller than dialog.
  EXPECT_TRUE(ExecJs(dialog_delegate->GetWebContents(),
                     GetChangeDimensionsScript(100)));
  ASSERT_TRUE(RunLoopUntil(base::BindRepeating(
      &IsEqualSizes, initial_dialog_size, dialog_delegate)));

  // Resize <body> to dimension larger than dialog.
  EXPECT_TRUE(ExecJs(dialog_delegate->GetWebContents(),
                     GetChangeDimensionsScript(500)));
  ASSERT_TRUE(RunLoopUntil(base::BindRepeating(
      &IsEqualSizes, initial_dialog_size, dialog_delegate)));
}

IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest, RootViewAcessibleName) {
  auto delegate =
      std::make_unique<ui::test::TestWebDialogDelegate>(GURL(kTestDataURL));
  auto* delegate_ptr = delegate.get();
  WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  ASSERT_TRUE(web_contents);

  views::WidgetDelegate* widget_delegate =
      GetConstrainedWebDialogForAccessibilityTesting(
          browser()->profile(), std::move(delegate), web_contents);

  ui::AXNodeData root_view_data;
  widget_delegate->GetWidget()
      ->GetRootView()
      ->GetViewAccessibility()
      .GetAccessibleNodeData(&root_view_data);
  EXPECT_EQ(
      root_view_data.GetString16Attribute(ax::mojom::StringAttribute::kName),
      widget_delegate->GetAccessibleWindowTitle());

  root_view_data = ui::AXNodeData();
  widget_delegate->GetWidget()
      ->GetRootView()
      ->GetViewAccessibility()
      .GetAccessibleNodeData(&root_view_data);
  EXPECT_EQ(u"Test", widget_delegate->GetAccessibleWindowTitle());
  EXPECT_EQ(
      root_view_data.GetString16Attribute(ax::mojom::StringAttribute::kName),
      widget_delegate->GetAccessibleWindowTitle());

  delegate_ptr->set_accessible_dialog_title(u"Acessible Dialog Title");
  root_view_data = ui::AXNodeData();
  widget_delegate->GetWidget()
      ->GetRootView()
      ->GetViewAccessibility()
      .GetAccessibleNodeData(&root_view_data);
  EXPECT_EQ(u"Acessible Dialog Title",
            widget_delegate->GetAccessibleWindowTitle());
  EXPECT_EQ(
      root_view_data.GetString16Attribute(ax::mojom::StringAttribute::kName),
      widget_delegate->GetAccessibleWindowTitle());
}