• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/browser/ui/tab_modal_confirm_dialog_browsertest.h"
6 
7 #include "base/bind.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"
18 
MockTabModalConfirmDialogDelegate(content::WebContents * web_contents,Delegate * delegate)19 MockTabModalConfirmDialogDelegate::MockTabModalConfirmDialogDelegate(
20     content::WebContents* web_contents,
21     Delegate* delegate)
22     : TabModalConfirmDialogDelegate(web_contents),
23       delegate_(delegate) {
24 }
25 
~MockTabModalConfirmDialogDelegate()26 MockTabModalConfirmDialogDelegate::~MockTabModalConfirmDialogDelegate() {
27 }
28 
GetTitle()29 base::string16 MockTabModalConfirmDialogDelegate::GetTitle() {
30   return base::string16();
31 }
32 
GetDialogMessage()33 base::string16 MockTabModalConfirmDialogDelegate::GetDialogMessage() {
34   return base::string16();
35 }
36 
OnAccepted()37 void MockTabModalConfirmDialogDelegate::OnAccepted() {
38   if (delegate_)
39     delegate_->OnAccepted();
40 }
41 
OnCanceled()42 void MockTabModalConfirmDialogDelegate::OnCanceled() {
43   if (delegate_)
44     delegate_->OnCanceled();
45 }
46 
OnClosed()47 void MockTabModalConfirmDialogDelegate::OnClosed() {
48   if (delegate_)
49     delegate_->OnClosed();
50 }
51 
TabModalConfirmDialogTest()52 TabModalConfirmDialogTest::TabModalConfirmDialogTest()
53     : delegate_(NULL),
54       dialog_(NULL),
55       accepted_count_(0),
56       canceled_count_(0),
57       closed_count_(0) {
58 }
59 
SetUpOnMainThread()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();
66 }
67 
TearDownOnMainThread()68 void TabModalConfirmDialogTest::TearDownOnMainThread() {
69   content::RunAllPendingInMessageLoop();
70 }
71 
OnAccepted()72 void TabModalConfirmDialogTest::OnAccepted() {
73   ++accepted_count_;
74 }
75 
OnCanceled()76 void TabModalConfirmDialogTest::OnCanceled() {
77   ++canceled_count_;
78 }
79 
OnClosed()80 void TabModalConfirmDialogTest::OnClosed() {
81   ++closed_count_;
82 }
83 
IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest,Accept)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_);
89 }
90 
IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest,Cancel)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_);
96 }
97 
IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest,CancelSelf)98 IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, CancelSelf) {
99   delegate_->Cancel();
100   EXPECT_EQ(0, accepted_count_);
101   EXPECT_EQ(1, canceled_count_);
102   EXPECT_EQ(0, closed_count_);
103 }
104 
IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest,Close)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_);
110 }
111 
IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest,CloseSelf)112 IN_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 
IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest,Navigate)119 IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, Navigate) {
120   content::OpenURLParams params(GURL("about:blank"),
121                                 content::Referrer(),
122                                 CURRENT_TAB,
123                                 ui::PAGE_TRANSITION_LINK,
124                                 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_);
130 }
131 
IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest,Quit)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_);
139 }
140