1 // 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.
5 #include "chrome/browser/ui/tab_modal_confirm_dialog_browsertest.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/lifetime/application_lifetime.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_dialogs.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "content/public/browser/page_navigator.h"
15 #include "content/public/browser/web_contents.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "ui/base/window_open_disposition.h"
19 MockTabModalConfirmDialogDelegate::MockTabModalConfirmDialogDelegate(
20 content::WebContents
* web_contents
,
22 : TabModalConfirmDialogDelegate(web_contents
),
26 MockTabModalConfirmDialogDelegate::~MockTabModalConfirmDialogDelegate() {
29 base::string16
MockTabModalConfirmDialogDelegate::GetTitle() {
30 return base::string16();
33 base::string16
MockTabModalConfirmDialogDelegate::GetMessage() {
34 return base::string16();
37 void MockTabModalConfirmDialogDelegate::OnAccepted() {
39 delegate_
->OnAccepted();
42 void MockTabModalConfirmDialogDelegate::OnCanceled() {
44 delegate_
->OnCanceled();
47 void MockTabModalConfirmDialogDelegate::OnClosed() {
49 delegate_
->OnClosed();
52 TabModalConfirmDialogTest::TabModalConfirmDialogTest()
60 void TabModalConfirmDialogTest::SetUpOnMainThread() {
61 delegate_
= new MockTabModalConfirmDialogDelegate(
62 browser()->tab_strip_model()->GetActiveWebContents(), this);
63 dialog_
= TabModalConfirmDialog::Create(
64 delegate_
, browser()->tab_strip_model()->GetActiveWebContents());
65 content::RunAllPendingInMessageLoop();
68 void TabModalConfirmDialogTest::CleanUpOnMainThread() {
69 content::RunAllPendingInMessageLoop();
72 void TabModalConfirmDialogTest::OnAccepted() {
76 void TabModalConfirmDialogTest::OnCanceled() {
80 void TabModalConfirmDialogTest::OnClosed() {
84 IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest
, Accept
) {
85 dialog_
->AcceptTabModalDialog();
86 EXPECT_EQ(1, accepted_count_
);
87 EXPECT_EQ(0, canceled_count_
);
88 EXPECT_EQ(0, closed_count_
);
91 IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest
, Cancel
) {
92 dialog_
->CancelTabModalDialog();
93 EXPECT_EQ(0, accepted_count_
);
94 EXPECT_EQ(1, canceled_count_
);
95 EXPECT_EQ(0, closed_count_
);
98 IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest
, CancelSelf
) {
100 EXPECT_EQ(0, accepted_count_
);
101 EXPECT_EQ(1, canceled_count_
);
102 EXPECT_EQ(0, closed_count_
);
105 IN_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_
);
112 IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest
, CloseSelf
) {
114 EXPECT_EQ(0, accepted_count_
);
115 EXPECT_EQ(0, canceled_count_
);
116 EXPECT_EQ(1, closed_count_
);
119 IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest
, Navigate
) {
120 content::OpenURLParams
params(GURL("about:blank"),
123 content::PAGE_TRANSITION_LINK
,
125 browser()->tab_strip_model()->GetActiveWebContents()->OpenURL(params
);
127 EXPECT_EQ(0, accepted_count_
);
128 EXPECT_EQ(0, canceled_count_
);
129 EXPECT_EQ(1, closed_count_
);
132 IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest
, Quit
) {
133 base::MessageLoopForUI::current()->PostTask(FROM_HERE
,
134 base::Bind(&chrome::AttemptExit
));
135 content::RunMessageLoop();
136 EXPECT_EQ(0, accepted_count_
);
137 EXPECT_EQ(0, canceled_count_
);
138 EXPECT_EQ(1, closed_count_
);