blob: e252fa51e84cc99837f9392c1013e27ba235bc60 [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
Avi Drissman9269d4ed2023-01-07 01:38:0610#include "base/functional/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
Sorin Jianu76486952024-11-28 04:21:5030MockTabModalConfirmDialogDelegate::~MockTabModalConfirmDialogDelegate() =
31 default;
[email protected]f6e5d8c2012-01-04 21:25:3632
Jan Wilken Dörrie3f97e292021-03-11 18:07:1433std::u16string MockTabModalConfirmDialogDelegate::GetTitle() {
34 return std::u16string();
[email protected]3cac87232012-11-20 01:48:2735}
[email protected]f6e5d8c2012-01-04 21:25:3636
Jan Wilken Dörrie3f97e292021-03-11 18:07:1437std::u16string MockTabModalConfirmDialogDelegate::GetDialogMessage() {
38 return std::u16string();
[email protected]3cac87232012-11-20 01:48:2739}
40
41void MockTabModalConfirmDialogDelegate::OnAccepted() {
Peter Kastinga4863242024-12-23 00:19:4342 if (delegate_) {
[email protected]3cac87232012-11-20 01:48:2743 delegate_->OnAccepted();
Peter Kastinga4863242024-12-23 00:19:4344 }
[email protected]3cac87232012-11-20 01:48:2745}
46
47void MockTabModalConfirmDialogDelegate::OnCanceled() {
Peter Kastinga4863242024-12-23 00:19:4348 if (delegate_) {
[email protected]3cac87232012-11-20 01:48:2749 delegate_->OnCanceled();
Peter Kastinga4863242024-12-23 00:19:4350 }
[email protected]3cac87232012-11-20 01:48:2751}
[email protected]f6e5d8c2012-01-04 21:25:3652
[email protected]ec35112f2013-08-01 05:46:1853void MockTabModalConfirmDialogDelegate::OnClosed() {
Peter Kastinga4863242024-12-23 00:19:4354 if (delegate_) {
[email protected]ec35112f2013-08-01 05:46:1855 delegate_->OnClosed();
Peter Kastinga4863242024-12-23 00:19:4356 }
[email protected]ec35112f2013-08-01 05:46:1857}
58
[email protected]f6e5d8c2012-01-04 21:25:3659TabModalConfirmDialogTest::TabModalConfirmDialogTest()
Lukasz Anforowiczc695e532020-06-09 02:09:4560 : delegate_(nullptr),
61 dialog_(nullptr),
[email protected]3cac87232012-11-20 01:48:2762 accepted_count_(0),
[email protected]ec35112f2013-08-01 05:46:1863 canceled_count_(0),
Aran Gilmand217b5b2019-04-11 17:45:4364 closed_count_(0) {}
[email protected]f6e5d8c2012-01-04 21:25:3665
66void TabModalConfirmDialogTest::SetUpOnMainThread() {
Jeremy Chinsene54596432019-05-29 00:18:4667 auto delegate = std::make_unique<MockTabModalConfirmDialogDelegate>(
[email protected]fa6b2462013-07-30 22:37:0068 browser()->tab_strip_model()->GetActiveWebContents(), this);
Jeremy Chinsene54596432019-05-29 00:18:4669 delegate_ = delegate.get();
[email protected]51296c62012-09-26 13:02:4470 dialog_ = TabModalConfirmDialog::Create(
Jeremy Chinsene54596432019-05-29 00:18:4671 std::move(delegate),
72 browser()->tab_strip_model()->GetActiveWebContents());
[email protected]b8deecd2012-07-30 21:09:4473 content::RunAllPendingInMessageLoop();
[email protected]f6e5d8c2012-01-04 21:25:3674}
75
[email protected]ad354972014-07-25 18:58:2076void TabModalConfirmDialogTest::TearDownOnMainThread() {
[email protected]b8deecd2012-07-30 21:09:4477 content::RunAllPendingInMessageLoop();
[email protected]3cac87232012-11-20 01:48:2778}
79
80void TabModalConfirmDialogTest::OnAccepted() {
81 ++accepted_count_;
82}
83
84void TabModalConfirmDialogTest::OnCanceled() {
85 ++canceled_count_;
[email protected]f6e5d8c2012-01-04 21:25:3686}
87
[email protected]ec35112f2013-08-01 05:46:1888void TabModalConfirmDialogTest::OnClosed() {
89 ++closed_count_;
90}
91
[email protected]f6e5d8c2012-01-04 21:25:3692IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, Accept) {
[email protected]51296c62012-09-26 13:02:4493 dialog_->AcceptTabModalDialog();
[email protected]3cac87232012-11-20 01:48:2794 EXPECT_EQ(1, accepted_count_);
95 EXPECT_EQ(0, canceled_count_);
[email protected]ec35112f2013-08-01 05:46:1896 EXPECT_EQ(0, closed_count_);
[email protected]f6e5d8c2012-01-04 21:25:3697}
98
99IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, Cancel) {
[email protected]51296c62012-09-26 13:02:44100 dialog_->CancelTabModalDialog();
[email protected]3cac87232012-11-20 01:48:27101 EXPECT_EQ(0, accepted_count_);
102 EXPECT_EQ(1, canceled_count_);
[email protected]ec35112f2013-08-01 05:46:18103 EXPECT_EQ(0, closed_count_);
[email protected]f6e5d8c2012-01-04 21:25:36104}
105
106IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, CancelSelf) {
[email protected]f6e5d8c2012-01-04 21:25:36107 delegate_->Cancel();
[email protected]3cac87232012-11-20 01:48:27108 EXPECT_EQ(0, accepted_count_);
109 EXPECT_EQ(1, canceled_count_);
[email protected]ec35112f2013-08-01 05:46:18110 EXPECT_EQ(0, closed_count_);
111}
112
113IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, Close) {
114 dialog_->CloseDialog();
115 EXPECT_EQ(0, accepted_count_);
116 EXPECT_EQ(0, canceled_count_);
117 EXPECT_EQ(1, closed_count_);
118}
119
120IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, CloseSelf) {
121 delegate_->Close();
122 EXPECT_EQ(0, accepted_count_);
123 EXPECT_EQ(0, canceled_count_);
124 EXPECT_EQ(1, closed_count_);
125}
126
127IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, Navigate) {
nick3b04f322016-08-31 19:29:19128 content::OpenURLParams params(GURL("about:blank"), content::Referrer(),
129 WindowOpenDisposition::CURRENT_TAB,
130 ui::PAGE_TRANSITION_LINK, false);
HuanPo Lin0d795c62024-03-28 03:54:05131 browser()->tab_strip_model()->GetActiveWebContents()->OpenURL(
132 params, /*navigation_handle_callback=*/{});
[email protected]ec35112f2013-08-01 05:46:18133
134 EXPECT_EQ(0, accepted_count_);
135 EXPECT_EQ(0, canceled_count_);
136 EXPECT_EQ(1, closed_count_);
[email protected]f6e5d8c2012-01-04 21:25:36137}
138
139IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, Quit) {
Sean Maher5b9af51f2022-11-21 15:32:47140 base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
tzik22036cc2017-04-21 04:08:18141 FROM_HERE, base::BindOnce(&chrome::AttemptExit));
Wez25fd35f2018-05-25 22:57:41142 RunUntilBrowserProcessQuits();
143
[email protected]3cac87232012-11-20 01:48:27144 EXPECT_EQ(0, accepted_count_);
[email protected]ec35112f2013-08-01 05:46:18145 EXPECT_EQ(0, canceled_count_);
146 EXPECT_EQ(1, closed_count_);
[email protected]f6e5d8c2012-01-04 21:25:36147}