blob: e0d86e702c8974526a1e4fa0d2c5de131a9e510f [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2012 The Chromium Authors
[email protected]f6e5d8c2012-01-04 21:25:362// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/ui/tab_modal_confirm_dialog_browsertest.h"
6
Jeremy Chinsene54596432019-05-29 00:18:467#include <memory>
8#include <utility>
9
[email protected]f6e5d8c2012-01-04 21:25:3610#include "base/bind.h"
fdoraybd6c0982016-06-11 00:23:0511#include "base/location.h"
[email protected]774cc3c2013-06-07 20:26:4512#include "base/strings/utf_string_conversions.h"
Patrick Monette643cdf62021-10-15 19:13:4213#include "base/task/single_thread_task_runner.h"
[email protected]2e6389f2012-05-18 19:41:2514#include "chrome/browser/lifetime/application_lifetime.h"
[email protected]f6e5d8c2012-01-04 21:25:3615#include "chrome/browser/ui/browser.h"
[email protected]52877dbc62012-06-29 22:22:0316#include "chrome/browser/ui/browser_dialogs.h"
[email protected]47ae23372013-01-29 01:50:4817#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]ec35112f2013-08-01 05:46:1818#include "content/public/browser/page_navigator.h"
19#include "content/public/browser/web_contents.h"
Peter Kasting919ce652020-05-07 10:22:3620#include "content/public/test/browser_test.h"
thestigaf7f4152014-10-31 23:19:1521#include "content/public/test/test_utils.h"
[email protected]f6e5d8c2012-01-04 21:25:3622#include "testing/gtest/include/gtest/gtest.h"
[email protected]ec35112f2013-08-01 05:46:1823#include "ui/base/window_open_disposition.h"
[email protected]f6e5d8c2012-01-04 21:25:3624
[email protected]3cac87232012-11-20 01:48:2725MockTabModalConfirmDialogDelegate::MockTabModalConfirmDialogDelegate(
[email protected]fa6b2462013-07-30 22:37:0026 content::WebContents* web_contents,
[email protected]3cac87232012-11-20 01:48:2727 Delegate* delegate)
Aran Gilmand217b5b2019-04-11 17:45:4328 : TabModalConfirmDialogDelegate(web_contents), delegate_(delegate) {}
[email protected]f6e5d8c2012-01-04 21:25:3629
Aran Gilmand217b5b2019-04-11 17:45:4330MockTabModalConfirmDialogDelegate::~MockTabModalConfirmDialogDelegate() {}
[email protected]f6e5d8c2012-01-04 21:25:3631
Jan Wilken Dörrie3f97e292021-03-11 18:07:1432std::u16string MockTabModalConfirmDialogDelegate::GetTitle() {
33 return std::u16string();
[email protected]3cac87232012-11-20 01:48:2734}
[email protected]f6e5d8c2012-01-04 21:25:3635
Jan Wilken Dörrie3f97e292021-03-11 18:07:1436std::u16string MockTabModalConfirmDialogDelegate::GetDialogMessage() {
37 return std::u16string();
[email protected]3cac87232012-11-20 01:48:2738}
39
40void MockTabModalConfirmDialogDelegate::OnAccepted() {
41 if (delegate_)
42 delegate_->OnAccepted();
43}
44
45void MockTabModalConfirmDialogDelegate::OnCanceled() {
46 if (delegate_)
47 delegate_->OnCanceled();
48}
[email protected]f6e5d8c2012-01-04 21:25:3649
[email protected]ec35112f2013-08-01 05:46:1850void MockTabModalConfirmDialogDelegate::OnClosed() {
51 if (delegate_)
52 delegate_->OnClosed();
53}
54
[email protected]f6e5d8c2012-01-04 21:25:3655TabModalConfirmDialogTest::TabModalConfirmDialogTest()
Lukasz Anforowiczc695e532020-06-09 02:09:4556 : delegate_(nullptr),
57 dialog_(nullptr),
[email protected]3cac87232012-11-20 01:48:2758 accepted_count_(0),
[email protected]ec35112f2013-08-01 05:46:1859 canceled_count_(0),
Aran Gilmand217b5b2019-04-11 17:45:4360 closed_count_(0) {}
[email protected]f6e5d8c2012-01-04 21:25:3661
62void TabModalConfirmDialogTest::SetUpOnMainThread() {
Jeremy Chinsene54596432019-05-29 00:18:4663 auto delegate = std::make_unique<MockTabModalConfirmDialogDelegate>(
[email protected]fa6b2462013-07-30 22:37:0064 browser()->tab_strip_model()->GetActiveWebContents(), this);
Jeremy Chinsene54596432019-05-29 00:18:4665 delegate_ = delegate.get();
[email protected]51296c62012-09-26 13:02:4466 dialog_ = TabModalConfirmDialog::Create(
Jeremy Chinsene54596432019-05-29 00:18:4667 std::move(delegate),
68 browser()->tab_strip_model()->GetActiveWebContents());
[email protected]b8deecd2012-07-30 21:09:4469 content::RunAllPendingInMessageLoop();
[email protected]f6e5d8c2012-01-04 21:25:3670}
71
[email protected]ad354972014-07-25 18:58:2072void TabModalConfirmDialogTest::TearDownOnMainThread() {
[email protected]b8deecd2012-07-30 21:09:4473 content::RunAllPendingInMessageLoop();
[email protected]3cac87232012-11-20 01:48:2774}
75
76void TabModalConfirmDialogTest::OnAccepted() {
77 ++accepted_count_;
78}
79
80void TabModalConfirmDialogTest::OnCanceled() {
81 ++canceled_count_;
[email protected]f6e5d8c2012-01-04 21:25:3682}
83
[email protected]ec35112f2013-08-01 05:46:1884void TabModalConfirmDialogTest::OnClosed() {
85 ++closed_count_;
86}
87
[email protected]f6e5d8c2012-01-04 21:25:3688IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, Accept) {
[email protected]51296c62012-09-26 13:02:4489 dialog_->AcceptTabModalDialog();
[email protected]3cac87232012-11-20 01:48:2790 EXPECT_EQ(1, accepted_count_);
91 EXPECT_EQ(0, canceled_count_);
[email protected]ec35112f2013-08-01 05:46:1892 EXPECT_EQ(0, closed_count_);
[email protected]f6e5d8c2012-01-04 21:25:3693}
94
95IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, Cancel) {
[email protected]51296c62012-09-26 13:02:4496 dialog_->CancelTabModalDialog();
[email protected]3cac87232012-11-20 01:48:2797 EXPECT_EQ(0, accepted_count_);
98 EXPECT_EQ(1, canceled_count_);
[email protected]ec35112f2013-08-01 05:46:1899 EXPECT_EQ(0, closed_count_);
[email protected]f6e5d8c2012-01-04 21:25:36100}
101
102IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, CancelSelf) {
[email protected]f6e5d8c2012-01-04 21:25:36103 delegate_->Cancel();
[email protected]3cac87232012-11-20 01:48:27104 EXPECT_EQ(0, accepted_count_);
105 EXPECT_EQ(1, canceled_count_);
[email protected]ec35112f2013-08-01 05:46:18106 EXPECT_EQ(0, closed_count_);
107}
108
109IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, Close) {
110 dialog_->CloseDialog();
111 EXPECT_EQ(0, accepted_count_);
112 EXPECT_EQ(0, canceled_count_);
113 EXPECT_EQ(1, closed_count_);
114}
115
116IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, CloseSelf) {
117 delegate_->Close();
118 EXPECT_EQ(0, accepted_count_);
119 EXPECT_EQ(0, canceled_count_);
120 EXPECT_EQ(1, closed_count_);
121}
122
123IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, Navigate) {
nick3b04f322016-08-31 19:29:19124 content::OpenURLParams params(GURL("about:blank"), content::Referrer(),
125 WindowOpenDisposition::CURRENT_TAB,
126 ui::PAGE_TRANSITION_LINK, false);
[email protected]ec35112f2013-08-01 05:46:18127 browser()->tab_strip_model()->GetActiveWebContents()->OpenURL(params);
128
129 EXPECT_EQ(0, accepted_count_);
130 EXPECT_EQ(0, canceled_count_);
131 EXPECT_EQ(1, closed_count_);
[email protected]f6e5d8c2012-01-04 21:25:36132}
133
134IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, Quit) {
Sean Maher5b9af51f2022-11-21 15:32:47135 base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
tzik22036cc2017-04-21 04:08:18136 FROM_HERE, base::BindOnce(&chrome::AttemptExit));
Wez25fd35f2018-05-25 22:57:41137 RunUntilBrowserProcessQuits();
138
[email protected]3cac87232012-11-20 01:48:27139 EXPECT_EQ(0, accepted_count_);
[email protected]ec35112f2013-08-01 05:46:18140 EXPECT_EQ(0, canceled_count_);
141 EXPECT_EQ(1, closed_count_);
[email protected]f6e5d8c2012-01-04 21:25:36142}