blob: 0eb025b0f9fd85abfd7265f205b1010b5ff9598e [file] [log] [blame]
[email protected]f6e5d8c2012-01-04 21:25:361// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// 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
7#include "base/bind.h"
[email protected]774cc3c2013-06-07 20:26:458#include "base/strings/utf_string_conversions.h"
[email protected]2e6389f2012-05-18 19:41:259#include "chrome/browser/lifetime/application_lifetime.h"
[email protected]f6e5d8c2012-01-04 21:25:3610#include "chrome/browser/ui/browser.h"
[email protected]52877dbc62012-06-29 22:22:0311#include "chrome/browser/ui/browser_dialogs.h"
[email protected]47ae23372013-01-29 01:50:4812#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]ec35112f2013-08-01 05:46:1813#include "content/public/browser/page_navigator.h"
14#include "content/public/browser/web_contents.h"
thestigaf7f4152014-10-31 23:19:1515#include "content/public/test/test_utils.h"
[email protected]f6e5d8c2012-01-04 21:25:3616#include "testing/gtest/include/gtest/gtest.h"
[email protected]ec35112f2013-08-01 05:46:1817#include "ui/base/window_open_disposition.h"
[email protected]f6e5d8c2012-01-04 21:25:3618
[email protected]3cac87232012-11-20 01:48:2719MockTabModalConfirmDialogDelegate::MockTabModalConfirmDialogDelegate(
[email protected]fa6b2462013-07-30 22:37:0020 content::WebContents* web_contents,
[email protected]3cac87232012-11-20 01:48:2721 Delegate* delegate)
[email protected]fa6b2462013-07-30 22:37:0022 : TabModalConfirmDialogDelegate(web_contents),
23 delegate_(delegate) {
[email protected]3cac87232012-11-20 01:48:2724}
[email protected]f6e5d8c2012-01-04 21:25:3625
[email protected]3cac87232012-11-20 01:48:2726MockTabModalConfirmDialogDelegate::~MockTabModalConfirmDialogDelegate() {
27}
[email protected]f6e5d8c2012-01-04 21:25:3628
[email protected]6a72a632013-12-12 22:22:0029base::string16 MockTabModalConfirmDialogDelegate::GetTitle() {
[email protected]dcd0249872013-12-06 23:58:4530 return base::string16();
[email protected]3cac87232012-11-20 01:48:2731}
[email protected]f6e5d8c2012-01-04 21:25:3632
[email protected]53fcb0562014-02-05 21:27:1033base::string16 MockTabModalConfirmDialogDelegate::GetDialogMessage() {
[email protected]dcd0249872013-12-06 23:58:4534 return base::string16();
[email protected]3cac87232012-11-20 01:48:2735}
36
37void MockTabModalConfirmDialogDelegate::OnAccepted() {
38 if (delegate_)
39 delegate_->OnAccepted();
40}
41
42void MockTabModalConfirmDialogDelegate::OnCanceled() {
43 if (delegate_)
44 delegate_->OnCanceled();
45}
[email protected]f6e5d8c2012-01-04 21:25:3646
[email protected]ec35112f2013-08-01 05:46:1847void MockTabModalConfirmDialogDelegate::OnClosed() {
48 if (delegate_)
49 delegate_->OnClosed();
50}
51
[email protected]f6e5d8c2012-01-04 21:25:3652TabModalConfirmDialogTest::TabModalConfirmDialogTest()
53 : delegate_(NULL),
[email protected]3cac87232012-11-20 01:48:2754 dialog_(NULL),
55 accepted_count_(0),
[email protected]ec35112f2013-08-01 05:46:1856 canceled_count_(0),
57 closed_count_(0) {
[email protected]3cac87232012-11-20 01:48:2758}
[email protected]f6e5d8c2012-01-04 21:25:3659
60void TabModalConfirmDialogTest::SetUpOnMainThread() {
[email protected]fa6b2462013-07-30 22:37:0061 delegate_ = new MockTabModalConfirmDialogDelegate(
62 browser()->tab_strip_model()->GetActiveWebContents(), this);
[email protected]51296c62012-09-26 13:02:4463 dialog_ = TabModalConfirmDialog::Create(
[email protected]47ae23372013-01-29 01:50:4864 delegate_, browser()->tab_strip_model()->GetActiveWebContents());
[email protected]b8deecd2012-07-30 21:09:4465 content::RunAllPendingInMessageLoop();
[email protected]f6e5d8c2012-01-04 21:25:3666}
67
[email protected]ad354972014-07-25 18:58:2068void TabModalConfirmDialogTest::TearDownOnMainThread() {
[email protected]b8deecd2012-07-30 21:09:4469 content::RunAllPendingInMessageLoop();
[email protected]3cac87232012-11-20 01:48:2770}
71
72void TabModalConfirmDialogTest::OnAccepted() {
73 ++accepted_count_;
74}
75
76void TabModalConfirmDialogTest::OnCanceled() {
77 ++canceled_count_;
[email protected]f6e5d8c2012-01-04 21:25:3678}
79
[email protected]ec35112f2013-08-01 05:46:1880void TabModalConfirmDialogTest::OnClosed() {
81 ++closed_count_;
82}
83
[email protected]f6e5d8c2012-01-04 21:25:3684IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, Accept) {
[email protected]51296c62012-09-26 13:02:4485 dialog_->AcceptTabModalDialog();
[email protected]3cac87232012-11-20 01:48:2786 EXPECT_EQ(1, accepted_count_);
87 EXPECT_EQ(0, canceled_count_);
[email protected]ec35112f2013-08-01 05:46:1888 EXPECT_EQ(0, closed_count_);
[email protected]f6e5d8c2012-01-04 21:25:3689}
90
91IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, Cancel) {
[email protected]51296c62012-09-26 13:02:4492 dialog_->CancelTabModalDialog();
[email protected]3cac87232012-11-20 01:48:2793 EXPECT_EQ(0, accepted_count_);
94 EXPECT_EQ(1, canceled_count_);
[email protected]ec35112f2013-08-01 05:46:1895 EXPECT_EQ(0, closed_count_);
[email protected]f6e5d8c2012-01-04 21:25:3696}
97
98IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, CancelSelf) {
[email protected]f6e5d8c2012-01-04 21:25:3699 delegate_->Cancel();
[email protected]3cac87232012-11-20 01:48:27100 EXPECT_EQ(0, accepted_count_);
101 EXPECT_EQ(1, canceled_count_);
[email protected]ec35112f2013-08-01 05:46:18102 EXPECT_EQ(0, closed_count_);
103}
104
105IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, Close) {
106 dialog_->CloseDialog();
107 EXPECT_EQ(0, accepted_count_);
108 EXPECT_EQ(0, canceled_count_);
109 EXPECT_EQ(1, closed_count_);
110}
111
112IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, CloseSelf) {
113 delegate_->Close();
114 EXPECT_EQ(0, accepted_count_);
115 EXPECT_EQ(0, canceled_count_);
116 EXPECT_EQ(1, closed_count_);
117}
118
119IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, Navigate) {
120 content::OpenURLParams params(GURL("about:blank"),
121 content::Referrer(),
122 CURRENT_TAB,
Sylvain Defresnec6ccc77d2014-09-19 10:19:35123 ui::PAGE_TRANSITION_LINK,
[email protected]ec35112f2013-08-01 05:46:18124 false);
125 browser()->tab_strip_model()->GetActiveWebContents()->OpenURL(params);
126
127 EXPECT_EQ(0, accepted_count_);
128 EXPECT_EQ(0, canceled_count_);
129 EXPECT_EQ(1, closed_count_);
[email protected]f6e5d8c2012-01-04 21:25:36130}
131
132IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, Quit) {
[email protected]3264d162013-05-08 22:02:11133 base::MessageLoopForUI::current()->PostTask(FROM_HERE,
134 base::Bind(&chrome::AttemptExit));
[email protected]729eb632012-07-26 04:45:26135 content::RunMessageLoop();
[email protected]3cac87232012-11-20 01:48:27136 EXPECT_EQ(0, accepted_count_);
[email protected]ec35112f2013-08-01 05:46:18137 EXPECT_EQ(0, canceled_count_);
138 EXPECT_EQ(1, closed_count_);
[email protected]f6e5d8c2012-01-04 21:25:36139}