File: print_preview_dialog_controller_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; 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 (503 lines) | stat: -rw-r--r-- 20,891 bytes parent folder | download | duplicates (4)
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// 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/printing/print_preview_dialog_controller.h"

#include <memory>
#include <string>

#include "base/memory/raw_ref.h"
#include "build/build_config.h"
#include "chrome/browser/printing/print_preview_test.h"
#include "chrome/browser/printing/print_view_manager.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/webui/print_preview/print_preview_ui.h"
#include "chrome/test/base/dialog_test_browser_window.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/test_utils.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/size.h"
#include "ui/web_dialogs/web_dialog_delegate.h"
#include "url/gurl.h"

using content::WebContents;
using content::WebContentsObserver;

namespace {

// content::WebContentsDelegate destructor is protected: subclass for testing.
class TestWebContentsDelegate : public content::WebContentsDelegate {};

}  // namespace

namespace printing {

using PrintPreviewDialogControllerUnitTest = PrintPreviewTest;

TEST_F(PrintPreviewDialogControllerUnitTest, IsPrintPreviewURL) {
  EXPECT_TRUE(PrintPreviewDialogController::IsPrintPreviewURL(
      GURL("chrome://print/fake-path")));
  EXPECT_FALSE(PrintPreviewDialogController::IsPrintPreviewURL(
      GURL("chrome-untrusted://print/fake-path")));
}

TEST_F(PrintPreviewDialogControllerUnitTest, IsPrintPreviewContentURL) {
  EXPECT_TRUE(PrintPreviewDialogController::IsPrintPreviewContentURL(
      GURL("chrome-untrusted://print/fake-path")));
  EXPECT_FALSE(PrintPreviewDialogController::IsPrintPreviewContentURL(
      GURL("chrome://print/fake-path")));
}

// Create/Get a preview dialog for initiator.
TEST_F(PrintPreviewDialogControllerUnitTest, GetOrCreatePreviewDialog) {
  // Lets start with one window with one tab.
  EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
  EXPECT_EQ(0, browser()->tab_strip_model()->count());
  chrome::NewTab(browser());
  EXPECT_EQ(1, browser()->tab_strip_model()->count());

  // Create a reference to initiator contents.
  WebContents* initiator = browser()->tab_strip_model()->GetActiveWebContents();

  PrintPreviewDialogController* dialog_controller =
      PrintPreviewDialogController::GetInstance();
  ASSERT_TRUE(dialog_controller);

  // Get the preview dialog for initiator.
  PrintViewManager::FromWebContents(initiator)->PrintPreviewNow(
      initiator->GetPrimaryMainFrame(), false);
  WebContents* preview_dialog =
      dialog_controller->GetOrCreatePreviewDialogForTesting(initiator);

  // New print preview dialog is a constrained window, so the number of tabs is
  // still 1.
  EXPECT_EQ(1, browser()->tab_strip_model()->count());
  EXPECT_NE(initiator, preview_dialog);

  // Get the print preview dialog for the same initiator.
  WebContents* new_preview_dialog =
      dialog_controller->GetOrCreatePreviewDialogForTesting(initiator);

  // Preview dialog already exists. Tab count remains the same.
  EXPECT_EQ(1, browser()->tab_strip_model()->count());

  // 1:1 relationship between initiator and preview dialog.
  EXPECT_EQ(new_preview_dialog, preview_dialog);
}

// Tests multiple print preview dialogs exist in the same browser for different
// initiators. If a preview dialog already exists for an initiator, that
// initiator gets focused.
//
// Flaky on Mac. https://crbug.com/845844
#if BUILDFLAG(IS_MAC)
#define MAYBE_MultiplePreviewDialogs DISABLED_MultiplePreviewDialogs
#else
#define MAYBE_MultiplePreviewDialogs MultiplePreviewDialogs
#endif
TEST_F(PrintPreviewDialogControllerUnitTest, MAYBE_MultiplePreviewDialogs) {
  // Lets start with one window and two tabs.
  EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
  TabStripModel* tab_strip_model = browser()->tab_strip_model();
  ASSERT_TRUE(tab_strip_model);

  EXPECT_EQ(0, tab_strip_model->count());

  // Create some new initiators.
  chrome::NewTab(browser());
  WebContents* web_contents_1 = tab_strip_model->GetActiveWebContents();
  ASSERT_TRUE(web_contents_1);

  chrome::NewTab(browser());
  WebContents* web_contents_2 = tab_strip_model->GetActiveWebContents();
  ASSERT_TRUE(web_contents_2);
  EXPECT_EQ(2, tab_strip_model->count());

  PrintPreviewDialogController* dialog_controller =
      PrintPreviewDialogController::GetInstance();
  ASSERT_TRUE(dialog_controller);

  // Create preview dialog for `web_contents_1`
  PrintViewManager::FromWebContents(web_contents_1)
      ->PrintPreviewNow(web_contents_1->GetPrimaryMainFrame(), false);
  WebContents* preview_dialog_1 =
      dialog_controller->GetOrCreatePreviewDialogForTesting(web_contents_1);

  EXPECT_NE(web_contents_1, preview_dialog_1);
  EXPECT_EQ(2, tab_strip_model->count());

  // Create preview dialog for `web_contents_2`
  PrintViewManager::FromWebContents(web_contents_2)
      ->PrintPreviewNow(web_contents_2->GetPrimaryMainFrame(), false);
  WebContents* preview_dialog_2 =
      dialog_controller->GetOrCreatePreviewDialogForTesting(web_contents_2);

  EXPECT_NE(web_contents_2, preview_dialog_2);
  EXPECT_NE(preview_dialog_1, preview_dialog_2);
  // 2 initiators and 2 preview dialogs exist in the same browser.  The preview
  // dialogs are constrained in their respective initiators.
  EXPECT_EQ(2, tab_strip_model->count());

  int tab_1_index = tab_strip_model->GetIndexOfWebContents(web_contents_1);
  int tab_2_index = tab_strip_model->GetIndexOfWebContents(web_contents_2);
  EXPECT_NE(tab_1_index, tab_2_index);

  int preview_dialog_1_index =
      tab_strip_model->GetIndexOfWebContents(preview_dialog_1);
  int preview_dialog_2_index =
      tab_strip_model->GetIndexOfWebContents(preview_dialog_2);

  // Constrained dialogs are not in the TabStripModel.
  EXPECT_EQ(-1, preview_dialog_1_index);
  EXPECT_EQ(-1, preview_dialog_2_index);

  // Since `preview_dialog_2_index` was the most recently created dialog, its
  // initiator should have focus.
  EXPECT_EQ(tab_2_index, tab_strip_model->active_index());

  // When we get the preview dialog for `web_contents_1`, `preview_dialog_2`
  // remains activated and focused. The previous behavior was to activate
  // `preview_dialog_1`, but that allowed tabs to steal focus.
  dialog_controller->GetOrCreatePreviewDialogForTesting(web_contents_1);
  EXPECT_EQ(tab_2_index, tab_strip_model->active_index());
}

// Check clearing the initiator details associated with a print preview dialog
// allows the initiator to create another print preview dialog.
TEST_F(PrintPreviewDialogControllerUnitTest, ClearInitiatorDetails) {
  // Lets start with one window with one tab.
  EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
  EXPECT_EQ(0, browser()->tab_strip_model()->count());
  chrome::NewTab(browser());
  EXPECT_EQ(1, browser()->tab_strip_model()->count());

  // Create a reference to initiator contents.
  WebContents* initiator = browser()->tab_strip_model()->GetActiveWebContents();

  PrintPreviewDialogController* dialog_controller =
      PrintPreviewDialogController::GetInstance();
  ASSERT_TRUE(dialog_controller);

  // Get the preview dialog for the initiator.
  PrintViewManager::FromWebContents(initiator)->PrintPreviewNow(
      initiator->GetPrimaryMainFrame(), false);
  WebContents* preview_dialog =
      dialog_controller->GetOrCreatePreviewDialogForTesting(initiator);

  // New print preview dialog is a constrained window, so the number of tabs is
  // still 1.
  EXPECT_EQ(1, browser()->tab_strip_model()->count());
  EXPECT_NE(initiator, preview_dialog);

  // Clear the initiator details associated with the preview dialog.
  dialog_controller->EraseInitiatorInfo(preview_dialog);

  // Get a new print preview dialog for the initiator.
  WebContents* new_preview_dialog =
      dialog_controller->GetOrCreatePreviewDialogForTesting(initiator);

  // New print preview dialog is a constrained window, so the number of tabs is
  // still 1.
  EXPECT_EQ(1, browser()->tab_strip_model()->count());
  // Verify a new print preview dialog has been created.
  EXPECT_NE(new_preview_dialog, preview_dialog);
}

// Test that print preview dialogs close on navigation to new pages
// and when navigating to old pages via fwd/back, but that auto navigation
// (typed + address bar) to an existing page as occurs in gmail does not cause
// the dialogs to close.
TEST_F(PrintPreviewDialogControllerUnitTest, CloseDialogOnNavigation) {
  // Two similar URLs (same webpage, different URL fragment/query)
  // Gmail navigates from fragment to query when opening an email to print.
  GURL tiger("https://www.google.com/#q=tiger");
  GURL tiger_barb("https://www.google.com/#?q=tiger+barb");

  // Set up by opening a new tab and getting web contents
  EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
  EXPECT_EQ(0, browser()->tab_strip_model()->count());
  chrome::NewTab(browser());
  EXPECT_EQ(1, browser()->tab_strip_model()->count());
  WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  ASSERT_TRUE(web_contents);

  // Navigate to first page
  content::NavigationSimulator::NavigateAndCommitFromBrowser(web_contents,
                                                             tiger);
  EXPECT_EQ(tiger, web_contents->GetLastCommittedURL());

  // Get the preview dialog
  PrintPreviewDialogController* dialog_controller =
      PrintPreviewDialogController::GetInstance();
  ASSERT_TRUE(dialog_controller);
  WebContents* tiger_preview_dialog =
      dialog_controller->GetOrCreatePreviewDialogForTesting(web_contents);
  PrintViewManager* manager = PrintViewManager::FromWebContents(web_contents);
  manager->PrintPreviewNow(web_contents->GetPrimaryMainFrame(), false);

  // New print preview dialog is a constrained window, so the number of tabs is
  // still 1.
  EXPECT_EQ(1, browser()->tab_strip_model()->count());
  EXPECT_NE(web_contents, tiger_preview_dialog);
  content::WebContentsDestroyedWatcher tiger_destroyed(tiger_preview_dialog);

  // Navigate via link to a similar page.
  content::NavigationSimulator::NavigateAndCommitFromBrowser(web_contents,
                                                             tiger_barb);

  // Check navigation was successful
  EXPECT_EQ(tiger_barb, web_contents->GetLastCommittedURL());

  // Print preview now should return true as the navigation should have closed
  // `tiger_preview_dialog` and the previous dialog should have closed.
  EXPECT_TRUE(
      manager->PrintPreviewNow(web_contents->GetPrimaryMainFrame(), false));
  WebContents* tiger_barb_preview_dialog =
      dialog_controller->GetOrCreatePreviewDialogForTesting(web_contents);
  ASSERT_TRUE(tiger_barb_preview_dialog);

  // Check a new dialog was created - either the pointers should be different or
  // the previous web contents must have been destroyed.
  EXPECT_TRUE(tiger_destroyed.IsDestroyed() ||
              tiger_barb_preview_dialog != tiger_preview_dialog);
  EXPECT_NE(tiger_barb_preview_dialog, web_contents);
  content::WebContentsDestroyedWatcher tiger_barb_destroyed(
      tiger_barb_preview_dialog);

  // Now this returns false as `tiger_barb_preview_dialog` is open.
  EXPECT_FALSE(
      manager->PrintPreviewNow(web_contents->GetPrimaryMainFrame(), false));

  // Navigate with back button or ALT+LEFT ARROW to a similar page.
  content::NavigationSimulator::GoBack(web_contents);
  EXPECT_EQ(tiger, web_contents->GetLastCommittedURL());
  EXPECT_TRUE(
      manager->PrintPreviewNow(web_contents->GetPrimaryMainFrame(), false));

  // Get new dialog
  WebContents* tiger_preview_dialog_2 =
      dialog_controller->GetOrCreatePreviewDialogForTesting(web_contents);
  ASSERT_TRUE(tiger_preview_dialog_2);

  // Verify this is a new dialog.
  EXPECT_TRUE(tiger_barb_destroyed.IsDestroyed() ||
              tiger_barb_preview_dialog != tiger_preview_dialog_2);
  EXPECT_NE(tiger_preview_dialog_2, web_contents);
  content::WebContentsDestroyedWatcher tiger_2_destroyed(
      tiger_preview_dialog_2);

  // Try to simulate Gmail navigation: Navigate to an existing page (via
  // Forward) but modify the navigation type while pending to look like an
  // address bar + typed transition (like Gmail auto navigation)
  std::unique_ptr<content::NavigationSimulator> forward_nav =
      content::NavigationSimulator::CreateHistoryNavigation(
          1, web_contents, /*is_renderer_initiated=*/false);
  forward_nav->Start();
  web_contents->GetController().GetPendingEntry()->SetTransitionType(
      ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED |
                                ui::PAGE_TRANSITION_FROM_ADDRESS_BAR));
  forward_nav->Commit();
  // Navigation successful
  EXPECT_EQ(tiger_barb, web_contents->GetLastCommittedURL());

  // Print preview should not have changed due to this navigation type so print
  // preview now should return false, dialog is still alive, and the dialog
  // returned by GetOrCreatePreviewDialogForTesting() should be the same as the
  // earlier dialog.
  EXPECT_FALSE(
      manager->PrintPreviewNow(web_contents->GetPrimaryMainFrame(), false));
  EXPECT_FALSE(tiger_2_destroyed.IsDestroyed());
  WebContents* tiger_preview_dialog_2b =
      dialog_controller->GetOrCreatePreviewDialogForTesting(web_contents);
  ASSERT_TRUE(tiger_preview_dialog_2b);
  EXPECT_EQ(tiger_preview_dialog_2b, tiger_preview_dialog_2);
  EXPECT_NE(tiger_preview_dialog_2b, web_contents);

  // Navigate with back button or ALT+LEFT ARROW to a similar page.
  content::NavigationSimulator::GoBack(web_contents);
  EXPECT_EQ(tiger, web_contents->GetLastCommittedURL());
  EXPECT_TRUE(
      manager->PrintPreviewNow(web_contents->GetPrimaryMainFrame(), false));

  // Get new dialog
  WebContents* tiger_preview_dialog_3 =
      dialog_controller->GetOrCreatePreviewDialogForTesting(web_contents);
  ASSERT_TRUE(tiger_preview_dialog_3);

  // Verify this is a new dialog.
  EXPECT_TRUE(tiger_barb_destroyed.IsDestroyed() ||
              tiger_preview_dialog_2 != tiger_preview_dialog_3);
  EXPECT_NE(tiger_preview_dialog_3, web_contents);
  content::WebContentsDestroyedWatcher tiger_3_destroyed(
      tiger_preview_dialog_3);

  // Try to simulate renderer reloading a PWA page: Navigate to an existing page
  // (via Forward) but modify the navigation type while pending to look like a
  // PAGE_TRANSITION_AUTO_BOOKMARK.
  forward_nav = content::NavigationSimulator::CreateHistoryNavigation(
      1, web_contents, /*is_renderer_initiated=*/false);
  forward_nav->Start();
  web_contents->GetController().GetPendingEntry()->SetTransitionType(
      ui::PageTransitionFromInt(ui::PAGE_TRANSITION_AUTO_BOOKMARK));
  forward_nav->Commit();
  // Navigation successful
  EXPECT_EQ(tiger_barb, web_contents->GetLastCommittedURL());

  // Print preview should not have changed due to this navigation type so print
  // preview now should return false, dialog is still alive, and the dialog
  // returned by GetOrCreatePreviewDialogForTesting() should be the same as the
  // earlier dialog.
  EXPECT_FALSE(
      manager->PrintPreviewNow(web_contents->GetPrimaryMainFrame(), false));
  EXPECT_FALSE(tiger_3_destroyed.IsDestroyed());
  WebContents* tiger_preview_dialog_3b =
      dialog_controller->GetOrCreatePreviewDialogForTesting(web_contents);
  ASSERT_TRUE(tiger_preview_dialog_3);
  EXPECT_EQ(tiger_preview_dialog_3b, tiger_preview_dialog_3);
  EXPECT_NE(tiger_preview_dialog_3, web_contents);
}

// Tests preview dialog controller cleans up correctly and does not throw errors
// on a renderer process crash. Checks that the renderer process closed
// notification is still received even if one of two preview dialogs with the
// same renderer process host is closed before the process "crashes".
TEST_F(PrintPreviewDialogControllerUnitTest, MultiplePreviewDialogsClose) {
  // Set up the browser.
  EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
  TabStripModel* tab_strip_model = browser()->tab_strip_model();
  ASSERT_TRUE(tab_strip_model);
  EXPECT_EQ(0, tab_strip_model->count());

  // Create a new tab with contents `web_contents_1`
  chrome::NewTab(browser());
  WebContents* web_contents_1 = tab_strip_model->GetActiveWebContents();
  ASSERT_TRUE(web_contents_1);
  EXPECT_EQ(1, tab_strip_model->count());
  PrintPreviewDialogController* dialog_controller =
      PrintPreviewDialogController::GetInstance();
  ASSERT_TRUE(dialog_controller);

  // Create preview dialog for `web_contents_1`. Should not create a new tab.
  PrintViewManager::FromWebContents(web_contents_1)
      ->PrintPreviewNow(web_contents_1->GetPrimaryMainFrame(), false);
  WebContents* preview_dialog_1 =
      dialog_controller->GetOrCreatePreviewDialogForTesting(web_contents_1);
  EXPECT_NE(web_contents_1, preview_dialog_1);
  EXPECT_EQ(1, tab_strip_model->count());

  // Create a new tab with contents `web_contents_2`
  chrome::NewTab(browser());
  WebContents* web_contents_2 = tab_strip_model->GetActiveWebContents();
  ASSERT_TRUE(web_contents_2);
  EXPECT_EQ(2, tab_strip_model->count());

  // Create preview dialog for `web_contents_2`
  PrintViewManager::FromWebContents(web_contents_2)
      ->PrintPreviewNow(web_contents_2->GetPrimaryMainFrame(), false);
  WebContents* preview_dialog_2 =
      dialog_controller->GetOrCreatePreviewDialogForTesting(web_contents_2);
  EXPECT_NE(web_contents_2, preview_dialog_2);
  EXPECT_NE(preview_dialog_1, preview_dialog_2);

  // 2 initiators and 2 preview dialogs exist in the same browser.  The preview
  // dialogs are constrained in their respective initiators.
  EXPECT_EQ(2, tab_strip_model->count());

  // Close `web_contents_1`'s tab
  int tab_1_index = tab_strip_model->GetIndexOfWebContents(web_contents_1);
  tab_strip_model->CloseWebContentsAt(tab_1_index, 0);
  EXPECT_EQ(1, tab_strip_model->count());

  // Simulate a crash of the render process host for `web_contents_2`. Print
  // preview controller should exit cleanly and not crash.
  content::MockRenderProcessHost* rph =
      static_cast<content::MockRenderProcessHost*>(
          web_contents_2->GetPrimaryMainFrame()->GetProcess());
  rph->SimulateCrash();
}

class PrintPreviewDialogControllerDialogDelegateTest
    : public PrintPreviewDialogControllerUnitTest {
 public:
  class DialogTestBrowserWindowWithMaxDialogSize
      : public DialogTestBrowserWindow {
   public:
    explicit DialogTestBrowserWindowWithMaxDialogSize(
        PrintPreviewDialogControllerDialogDelegateTest& owner)
        : owner_(owner) {}
    ~DialogTestBrowserWindowWithMaxDialogSize() override = default;

    // DialogTestBrowserWindow:
    gfx::Size GetMaximumDialogSize() override { return owner_->size(); }

   private:
    const raw_ref<PrintPreviewDialogControllerDialogDelegateTest> owner_;
  };

  PrintPreviewDialogControllerDialogDelegateTest() = default;
  ~PrintPreviewDialogControllerDialogDelegateTest() override = default;

  // PrintPreviewDialogControllerUnitTest:
  std::unique_ptr<BrowserWindow> CreateBrowserWindow() override {
    return std::make_unique<DialogTestBrowserWindowWithMaxDialogSize>(*this);
  }

  std::unique_ptr<ui::WebDialogDelegate> CreateDelegateWithSize(
      const gfx::Size& size) {
    size_ = size;
    chrome::NewTab(browser());
    WebContents* initiator =
        browser()->tab_strip_model()->GetActiveWebContents();
    return PrintPreviewDialogController::
        CreatePrintPreviewDialogDelegateForTesting(initiator);
  }

  const gfx::Size& size() const { return size_; }

 private:
  gfx::Size size_;
};

TEST_F(PrintPreviewDialogControllerDialogDelegateTest, GetDialogSizeMinSize) {
  auto delegate = CreateDelegateWithSize({0, 0});
  ASSERT_TRUE(delegate);

  gfx::Size size;
  delegate->GetDialogSize(&size);
  EXPECT_EQ(750, size.width());
  EXPECT_EQ(455, size.height());
}

TEST_F(PrintPreviewDialogControllerDialogDelegateTest, GetDialogSizeHD) {
  auto delegate = CreateDelegateWithSize({1920, 1080});
  ASSERT_TRUE(delegate);

  gfx::Size size;
  delegate->GetDialogSize(&size);
  EXPECT_EQ(1309, size.width());
  EXPECT_EQ(863, size.height());
}

TEST_F(PrintPreviewDialogControllerDialogDelegateTest, GetDialogSizeUWFHD) {
  auto delegate = CreateDelegateWithSize({2560, 1080});
  ASSERT_TRUE(delegate);

  gfx::Size size;
  delegate->GetDialogSize(&size);
  EXPECT_EQ(1757, size.width());
  EXPECT_EQ(1055, size.height());
}

}  // namespace printing