[email protected] | f6e5d8c | 2012-01-04 21:25:36 | [diff] [blame^] | 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/utf_string_conversions.h" |
| 9 | #include "chrome/browser/ui/browser_dialogs.h" |
| 10 | #include "chrome/browser/ui/browser.h" |
| 11 | #include "chrome/browser/ui/browser_list.h" |
| 12 | #include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h" |
| 13 | #include "chrome/test/base/ui_test_utils.h" |
| 14 | #include "content/browser/tab_contents/test_tab_contents.h" |
| 15 | #include "testing/gmock/include/gmock/gmock.h" |
| 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
| 18 | class MockTabModalConfirmDialogDelegate : public TabModalConfirmDialogDelegate { |
| 19 | public: |
| 20 | explicit MockTabModalConfirmDialogDelegate(content::WebContents* web_contents) |
| 21 | : TabModalConfirmDialogDelegate(web_contents) {} |
| 22 | |
| 23 | virtual string16 GetTitle() OVERRIDE { |
| 24 | return ASCIIToUTF16(""); |
| 25 | } |
| 26 | virtual string16 GetMessage() OVERRIDE { |
| 27 | return ASCIIToUTF16(""); |
| 28 | } |
| 29 | |
| 30 | MOCK_METHOD0(OnAccepted, void()); |
| 31 | MOCK_METHOD0(OnCanceled, void()); |
| 32 | |
| 33 | private: |
| 34 | DISALLOW_COPY_AND_ASSIGN(MockTabModalConfirmDialogDelegate); |
| 35 | }; |
| 36 | |
| 37 | TabModalConfirmDialogTest::TabModalConfirmDialogTest() |
| 38 | : delegate_(NULL), |
| 39 | dialog_(NULL) {} |
| 40 | |
| 41 | void TabModalConfirmDialogTest::SetUpOnMainThread() { |
| 42 | delegate_ = new MockTabModalConfirmDialogDelegate( |
| 43 | browser()->GetSelectedWebContents()); |
| 44 | dialog_ = CreateTestDialog(delegate_, |
| 45 | browser()->GetSelectedTabContentsWrapper()); |
| 46 | ui_test_utils::RunAllPendingInMessageLoop(); |
| 47 | } |
| 48 | |
| 49 | void TabModalConfirmDialogTest::CleanUpOnMainThread() { |
| 50 | ui_test_utils::RunAllPendingInMessageLoop(); |
| 51 | ::testing::Mock::VerifyAndClearExpectations(delegate_); |
| 52 | } |
| 53 | |
| 54 | // On Mac OS, these methods need to be compiled as Objective-C++, so they're in |
| 55 | // a separate file. |
| 56 | #if !defined(OS_MACOSX) |
| 57 | TabModalConfirmDialog* TabModalConfirmDialogTest::CreateTestDialog( |
| 58 | TabModalConfirmDialogDelegate* delegate, TabContentsWrapper* wrapper) { |
| 59 | return new TabModalConfirmDialog(delegate, wrapper); |
| 60 | } |
| 61 | |
| 62 | void TabModalConfirmDialogTest::CloseDialog(bool accept) { |
| 63 | #if defined(TOOLKIT_GTK) |
| 64 | if (accept) |
| 65 | dialog_->OnAccept(NULL); |
| 66 | else |
| 67 | dialog_->OnCancel(NULL); |
| 68 | #elif defined(OS_CHROMEOS) || defined(USE_AURA) |
| 69 | // |dialog_| deletes itself in |OnDialogClosed()|, so we need to save its |
| 70 | // ConstrainedHTMLUIDelegate before that. |
| 71 | ConstrainedHtmlUIDelegate* constrained_html_ui_delegate = |
| 72 | dialog_->constrained_html_ui_delegate(); |
| 73 | dialog_->OnDialogClosed(accept ? "true" : "false"); |
| 74 | constrained_html_ui_delegate->OnDialogCloseFromWebUI(); |
| 75 | #elif defined(OS_WIN) |
| 76 | if (accept) |
| 77 | dialog_->GetDialogClientView()->AcceptWindow(); |
| 78 | else |
| 79 | dialog_->GetDialogClientView()->CancelWindow(); |
| 80 | #endif |
| 81 | } |
| 82 | #endif // !defined(OS_MACOSX) |
| 83 | |
| 84 | IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, Accept) { |
| 85 | EXPECT_CALL(*delegate_, OnAccepted()); |
| 86 | CloseDialog(true); |
| 87 | } |
| 88 | |
| 89 | IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, Cancel) { |
| 90 | EXPECT_CALL(*delegate_, OnCanceled()); |
| 91 | CloseDialog(false); |
| 92 | } |
| 93 | |
| 94 | IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, CancelSelf) { |
| 95 | EXPECT_CALL(*delegate_, OnCanceled()); |
| 96 | delegate_->Cancel(); |
| 97 | } |
| 98 | |
| 99 | IN_PROC_BROWSER_TEST_F(TabModalConfirmDialogTest, Quit) { |
| 100 | EXPECT_CALL(*delegate_, OnCanceled()); |
| 101 | MessageLoopForUI::current()->PostTask(FROM_HERE, |
| 102 | base::Bind(&BrowserList::AttemptExit)); |
| 103 | ui_test_utils::RunMessageLoop(); |
| 104 | } |