Chromium Code Reviews| Index: chrome/browser/ui/webui/constrained_html_ui_browsertest.cc |
| =================================================================== |
| --- chrome/browser/ui/webui/constrained_html_ui_browsertest.cc (revision 104824) |
| +++ chrome/browser/ui/webui/constrained_html_ui_browsertest.cc (working copy) |
| @@ -4,8 +4,6 @@ |
| #include "chrome/test/ui/ui_test.h" |
| -#include "base/file_path.h" |
| -#include "base/message_loop.h" |
| #include "base/utf_string_conversions.h" |
| #include "chrome/browser/ui/browser.h" |
| #include "chrome/browser/ui/constrained_window_tab_helper.h" |
| @@ -15,18 +13,28 @@ |
| #include "chrome/common/url_constants.h" |
| #include "chrome/test/base/in_process_browser_test.h" |
| #include "chrome/test/base/ui_test_utils.h" |
| -#include "content/browser/renderer_host/render_widget_host_view.h" |
| #include "content/browser/tab_contents/tab_contents.h" |
| -#include "testing/gmock/include/gmock/gmock.h" |
| -#include "testing/gtest/include/gtest/gtest.h" |
| +#include "content/browser/tab_contents/tab_contents_observer.h" |
| -using testing::Eq; |
| - |
| namespace { |
| +class ConstrainedHtmlDialogBrowserTestHandler : public WebUIMessageHandler { |
| + public: |
| + ConstrainedHtmlDialogBrowserTestHandler() {} |
| + virtual ~ConstrainedHtmlDialogBrowserTestHandler() {} |
| + |
| + ConstrainedHtmlUI* constrained_ui() { |
| + return static_cast<ConstrainedHtmlUI*>(web_ui()); |
| + } |
| + private: |
| + virtual void RegisterMessages() {} |
| +}; |
| + |
| class TestHtmlDialogUIDelegate : public HtmlDialogUIDelegate { |
| public: |
| - TestHtmlDialogUIDelegate() {} |
| + TestHtmlDialogUIDelegate() { |
| + test_handler_.reset(new ConstrainedHtmlDialogBrowserTestHandler); |
| + } |
| virtual ~TestHtmlDialogUIDelegate() {} |
| // HTMLDialogUIDelegate implementation: |
| @@ -40,7 +48,9 @@ |
| return GURL(chrome::kChromeUIConstrainedHTMLTestURL); |
| } |
| virtual void GetWebUIMessageHandlers( |
| - std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE {} |
| + std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE { |
| + handlers->push_back(test_handler_.get()); |
| + } |
| virtual void GetDialogSize(gfx::Size* size) const OVERRIDE { |
| size->set_width(400); |
| size->set_height(400); |
| @@ -55,8 +65,33 @@ |
| *out_close_dialog = true; |
| } |
| virtual bool ShouldShowDialogTitle() const OVERRIDE { return true; } |
| + |
| + ConstrainedHtmlDialogBrowserTestHandler* test_handler() { |
| + return test_handler_.get(); |
| + } |
| + |
| + private: |
| + scoped_ptr<ConstrainedHtmlDialogBrowserTestHandler> test_handler_; |
| }; |
| +class ConstrainedHtmlDialogBrowserTestObserver : public TabContentsObserver { |
| + public: |
| + explicit ConstrainedHtmlDialogBrowserTestObserver(TabContents* contents) |
| + : TabContentsObserver(contents), |
| + tab_destroyed_(false) { |
| + } |
| + virtual ~ConstrainedHtmlDialogBrowserTestObserver() {} |
| + |
| + bool tab_destroyed() { return tab_destroyed_; } |
| + |
| + private: |
| + virtual void TabContentsDestroyed(TabContents* tab) { |
| + tab_destroyed_ = true; |
| + } |
| + |
| + bool tab_destroyed_; |
| +}; |
| + |
| } // namespace |
| class ConstrainedHtmlDialogBrowserTest : public InProcessBrowserTest { |
| @@ -69,12 +104,45 @@ |
| // The delegate deletes itself. |
| HtmlDialogUIDelegate* delegate = new TestHtmlDialogUIDelegate(); |
| TabContentsWrapper* wrapper = browser()->GetSelectedTabContentsWrapper(); |
| - ASSERT_TRUE(wrapper != NULL); |
| + ASSERT_TRUE(wrapper); |
| - ConstrainedHtmlUI::CreateConstrainedHtmlDialog(browser()->profile(), |
| - delegate, |
| - wrapper); |
| + ConstrainedWindow* window = |
| + ConstrainedHtmlUI::CreateConstrainedHtmlDialog(browser()->profile(), |
| + delegate, |
| + wrapper); |
| + EXPECT_TRUE(window); |
| + EXPECT_EQ(1U, wrapper->constrained_window_tab_helper()-> |
| + constrained_window_count()); |
| +} |
| +// Tests that ReleaseTabContentsOnDialogClose() works. |
| +IN_PROC_BROWSER_TEST_F(ConstrainedHtmlDialogBrowserTest, |
| + ReleaseTabContentsOnDialogClose) { |
| + // The delegate deletes itself. |
| + TestHtmlDialogUIDelegate* delegate = new TestHtmlDialogUIDelegate(); |
| + TabContentsWrapper* wrapper = browser()->GetSelectedTabContentsWrapper(); |
| + ASSERT_TRUE(wrapper); |
| + |
| + TabContentsWrapper* new_tab = |
| + ConstrainedHtmlUI::CreateConstrainedPrintPreviewHtmlUI( |
| + browser()->profile(), |
| + delegate, |
| + wrapper); |
| + ASSERT_TRUE(new_tab); |
| ASSERT_EQ(1U, wrapper->constrained_window_tab_helper()-> |
| constrained_window_count()); |
| + |
| + ConstrainedHtmlDialogBrowserTestObserver observer(new_tab->tab_contents()); |
| + ConstrainedHtmlDialogBrowserTestHandler* handler = delegate->test_handler(); |
| + ConstrainedHtmlUI* constrained_ui = handler->constrained_ui(); |
| + ASSERT_TRUE(constrained_ui); |
| + ConstrainedHtmlUIDelegate* constrained_delegate = |
| + constrained_ui->GetConstrainedDelegate(); |
| + ASSERT_TRUE(constrained_delegate); |
| + constrained_delegate->ReleaseTabContentsOnDialogClose(); |
|
Evan Stade
2011/10/11 02:49:17
so doesn't this leak?
Lei Zhang
2011/10/11 03:15:58
In this test, yes we leak |new_tab|. In its intend
Evan Stade
2011/10/11 22:54:59
i see that you have plugged this leak
|
| + constrained_delegate->OnDialogCloseFromWebUI(); |
| + |
| + EXPECT_FALSE(observer.tab_destroyed()); |
| + EXPECT_EQ(0U, wrapper->constrained_window_tab_helper()-> |
| + constrained_window_count()); |
|
Evan Stade
2011/10/11 02:49:17
4 more indent
Lei Zhang
2011/10/11 03:15:58
This is just copy + pasted. I'll change the other
|
| } |