Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/test/ui/ui_test.h" | 5 #include "chrome/test/ui/ui_test.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | |
| 8 #include "base/message_loop.h" | |
| 9 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/ui/browser.h" | 8 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/constrained_window_tab_helper.h" | 9 #include "chrome/browser/ui/constrained_window_tab_helper.h" |
| 12 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 10 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 13 #include "chrome/browser/ui/webui/constrained_html_ui.h" | 11 #include "chrome/browser/ui/webui/constrained_html_ui.h" |
| 14 #include "chrome/browser/ui/webui/html_dialog_ui.h" | 12 #include "chrome/browser/ui/webui/html_dialog_ui.h" |
| 15 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
| 16 #include "chrome/test/base/in_process_browser_test.h" | 14 #include "chrome/test/base/in_process_browser_test.h" |
| 17 #include "chrome/test/base/ui_test_utils.h" | 15 #include "chrome/test/base/ui_test_utils.h" |
| 18 #include "content/browser/renderer_host/render_widget_host_view.h" | |
| 19 #include "content/browser/tab_contents/tab_contents.h" | 16 #include "content/browser/tab_contents/tab_contents.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "content/browser/tab_contents/tab_contents_observer.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | |
| 22 | |
| 23 using testing::Eq; | |
| 24 | 18 |
| 25 namespace { | 19 namespace { |
| 26 | 20 |
| 21 class ConstrainedHtmlDialogBrowserTestHandler : public WebUIMessageHandler { | |
| 22 public: | |
| 23 ConstrainedHtmlDialogBrowserTestHandler() {} | |
| 24 virtual ~ConstrainedHtmlDialogBrowserTestHandler() {} | |
| 25 | |
| 26 ConstrainedHtmlUI* constrained_ui() { | |
| 27 return static_cast<ConstrainedHtmlUI*>(web_ui()); | |
| 28 } | |
| 29 private: | |
| 30 virtual void RegisterMessages() {} | |
| 31 }; | |
| 32 | |
| 27 class TestHtmlDialogUIDelegate : public HtmlDialogUIDelegate { | 33 class TestHtmlDialogUIDelegate : public HtmlDialogUIDelegate { |
| 28 public: | 34 public: |
| 29 TestHtmlDialogUIDelegate() {} | 35 TestHtmlDialogUIDelegate() { |
| 36 test_handler_.reset(new ConstrainedHtmlDialogBrowserTestHandler); | |
| 37 } | |
| 30 virtual ~TestHtmlDialogUIDelegate() {} | 38 virtual ~TestHtmlDialogUIDelegate() {} |
| 31 | 39 |
| 32 // HTMLDialogUIDelegate implementation: | 40 // HTMLDialogUIDelegate implementation: |
| 33 virtual bool IsDialogModal() const OVERRIDE { | 41 virtual bool IsDialogModal() const OVERRIDE { |
| 34 return true; | 42 return true; |
| 35 } | 43 } |
| 36 virtual string16 GetDialogTitle() const OVERRIDE { | 44 virtual string16 GetDialogTitle() const OVERRIDE { |
| 37 return UTF8ToUTF16("Test"); | 45 return UTF8ToUTF16("Test"); |
| 38 } | 46 } |
| 39 virtual GURL GetDialogContentURL() const OVERRIDE { | 47 virtual GURL GetDialogContentURL() const OVERRIDE { |
| 40 return GURL(chrome::kChromeUIConstrainedHTMLTestURL); | 48 return GURL(chrome::kChromeUIConstrainedHTMLTestURL); |
| 41 } | 49 } |
| 42 virtual void GetWebUIMessageHandlers( | 50 virtual void GetWebUIMessageHandlers( |
| 43 std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE {} | 51 std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE { |
| 52 handlers->push_back(test_handler_.get()); | |
| 53 } | |
| 44 virtual void GetDialogSize(gfx::Size* size) const OVERRIDE { | 54 virtual void GetDialogSize(gfx::Size* size) const OVERRIDE { |
| 45 size->set_width(400); | 55 size->set_width(400); |
| 46 size->set_height(400); | 56 size->set_height(400); |
| 47 } | 57 } |
| 48 virtual std::string GetDialogArgs() const OVERRIDE { | 58 virtual std::string GetDialogArgs() const OVERRIDE { |
| 49 return std::string(); | 59 return std::string(); |
| 50 } | 60 } |
| 51 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE { } | 61 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE { } |
| 52 virtual void OnCloseContents(TabContents* source, bool* out_close_dialog) | 62 virtual void OnCloseContents(TabContents* source, bool* out_close_dialog) |
| 53 OVERRIDE { | 63 OVERRIDE { |
| 54 if (out_close_dialog) | 64 if (out_close_dialog) |
| 55 *out_close_dialog = true; | 65 *out_close_dialog = true; |
| 56 } | 66 } |
| 57 virtual bool ShouldShowDialogTitle() const OVERRIDE { return true; } | 67 virtual bool ShouldShowDialogTitle() const OVERRIDE { return true; } |
| 68 | |
| 69 ConstrainedHtmlDialogBrowserTestHandler* test_handler() { | |
| 70 return test_handler_.get(); | |
| 71 } | |
| 72 | |
| 73 private: | |
| 74 scoped_ptr<ConstrainedHtmlDialogBrowserTestHandler> test_handler_; | |
| 75 }; | |
| 76 | |
| 77 class ConstrainedHtmlDialogBrowserTestObserver : public TabContentsObserver { | |
| 78 public: | |
| 79 explicit ConstrainedHtmlDialogBrowserTestObserver(TabContents* contents) | |
| 80 : TabContentsObserver(contents), | |
| 81 tab_destroyed_(false) { | |
| 82 } | |
| 83 virtual ~ConstrainedHtmlDialogBrowserTestObserver() {} | |
| 84 | |
| 85 bool tab_destroyed() { return tab_destroyed_; } | |
| 86 | |
| 87 private: | |
| 88 virtual void TabContentsDestroyed(TabContents* tab) { | |
| 89 tab_destroyed_ = true; | |
| 90 } | |
| 91 | |
| 92 bool tab_destroyed_; | |
| 58 }; | 93 }; |
| 59 | 94 |
| 60 } // namespace | 95 } // namespace |
| 61 | 96 |
| 62 class ConstrainedHtmlDialogBrowserTest : public InProcessBrowserTest { | 97 class ConstrainedHtmlDialogBrowserTest : public InProcessBrowserTest { |
| 63 public: | 98 public: |
| 64 ConstrainedHtmlDialogBrowserTest() {} | 99 ConstrainedHtmlDialogBrowserTest() {} |
| 100 | |
| 101 protected: | |
| 102 size_t GetConstrainedWindowCount(TabContentsWrapper* wrapper) const { | |
| 103 return wrapper->constrained_window_tab_helper()->constrained_window_count(); | |
| 104 } | |
| 65 }; | 105 }; |
| 66 | 106 |
| 67 // Tests that opening/closing the constrained window won't crash it. | 107 // Tests that opening/closing the constrained window won't crash it. |
| 68 IN_PROC_BROWSER_TEST_F(ConstrainedHtmlDialogBrowserTest, BasicTest) { | 108 IN_PROC_BROWSER_TEST_F(ConstrainedHtmlDialogBrowserTest, BasicTest) { |
| 69 // The delegate deletes itself. | 109 // The delegate deletes itself. |
| 70 HtmlDialogUIDelegate* delegate = new TestHtmlDialogUIDelegate(); | 110 HtmlDialogUIDelegate* delegate = new TestHtmlDialogUIDelegate(); |
| 71 TabContentsWrapper* wrapper = browser()->GetSelectedTabContentsWrapper(); | 111 TabContentsWrapper* wrapper = browser()->GetSelectedTabContentsWrapper(); |
| 72 ASSERT_TRUE(wrapper != NULL); | 112 ASSERT_TRUE(wrapper); |
| 73 | 113 |
| 74 ConstrainedHtmlUI::CreateConstrainedHtmlDialog(browser()->profile(), | 114 ConstrainedWindow* window = |
| 75 delegate, | 115 ConstrainedHtmlUI::CreateConstrainedHtmlDialog(browser()->profile(), |
| 76 wrapper); | 116 delegate, |
| 117 wrapper); | |
| 118 EXPECT_TRUE(window); | |
| 119 EXPECT_EQ(1U, GetConstrainedWindowCount(wrapper)); | |
| 120 } | |
| 77 | 121 |
| 78 ASSERT_EQ(1U, wrapper->constrained_window_tab_helper()-> | 122 // Tests that ReleaseTabContentsOnDialogClose() works. |
| 79 constrained_window_count()); | 123 IN_PROC_BROWSER_TEST_F(ConstrainedHtmlDialogBrowserTest, |
| 124 ReleaseTabContentsOnDialogClose) { | |
| 125 // The delegate deletes itself. | |
| 126 TestHtmlDialogUIDelegate* delegate = new TestHtmlDialogUIDelegate(); | |
| 127 TabContentsWrapper* wrapper = browser()->GetSelectedTabContentsWrapper(); | |
| 128 ASSERT_TRUE(wrapper); | |
| 129 | |
| 130 TabContentsWrapper* new_tab = | |
| 131 ConstrainedHtmlUI::CreateConstrainedPrintPreviewHtmlUI( | |
| 132 browser()->profile(), | |
| 133 delegate, | |
| 134 wrapper); | |
| 135 ASSERT_TRUE(new_tab); | |
| 136 ASSERT_EQ(1U, GetConstrainedWindowCount(wrapper)); | |
| 137 | |
| 138 ConstrainedHtmlDialogBrowserTestObserver observer(new_tab->tab_contents()); | |
| 139 ConstrainedHtmlDialogBrowserTestHandler* handler = delegate->test_handler(); | |
| 140 ConstrainedHtmlUI* constrained_ui = handler->constrained_ui(); | |
| 141 ASSERT_TRUE(constrained_ui); | |
| 142 ConstrainedHtmlUIDelegate* constrained_delegate = | |
| 143 constrained_ui->GetConstrainedDelegate(); | |
| 144 ASSERT_TRUE(constrained_delegate); | |
| 145 constrained_delegate->ReleaseTabContentsOnDialogClose(); | |
| 146 constrained_delegate->OnDialogCloseFromWebUI(); | |
| 147 | |
| 148 ASSERT_FALSE(observer.tab_destroyed()); | |
| 149 EXPECT_EQ(0U, GetConstrainedWindowCount(wrapper)); | |
| 150 delete new_tab; | |
|
Paweł Hajdan Jr.
2011/10/11 22:16:48
Why not scoped_ptr? Otherwise if any of ASSERTs fa
Lei Zhang
2011/10/11 23:41:25
Done.
| |
| 151 EXPECT_TRUE(observer.tab_destroyed()); | |
| 80 } | 152 } |
| OLD | NEW |