Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "base/bind_helpers.h" | |
| 5 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 6 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "base/strings/utf_string_conversions.h" | |
| 9 #include "chrome/browser/plugins/chrome_plugin_service_filter.h" | |
| 10 #include "chrome/browser/plugins/plugin_prefs.h" | |
| 7 #include "chrome/browser/printing/print_preview_dialog_controller.h" | 11 #include "chrome/browser/printing/print_preview_dialog_controller.h" |
| 8 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 9 #include "chrome/browser/ui/browser_commands.h" | 13 #include "chrome/browser/ui/browser_commands.h" |
| 10 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 15 #include "chrome/common/chrome_content_client.h" | |
| 11 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
| 12 #include "chrome/test/base/in_process_browser_test.h" | 17 #include "chrome/test/base/in_process_browser_test.h" |
| 13 #include "chrome/test/base/ui_test_utils.h" | 18 #include "chrome/test/base/ui_test_utils.h" |
| 14 #include "components/printing/common/print_messages.h" | 19 #include "components/printing/common/print_messages.h" |
| 20 #include "content/public/browser/plugin_service.h" | |
| 21 #include "content/public/browser/render_frame_host.h" | |
| 22 #include "content/public/browser/render_process_host.h" | |
| 15 #include "content/public/browser/web_contents_observer.h" | 23 #include "content/public/browser/web_contents_observer.h" |
| 16 #include "content/public/test/browser_test_utils.h" | 24 #include "content/public/test/browser_test_utils.h" |
| 17 #include "ipc/ipc_message_macros.h" | 25 #include "ipc/ipc_message_macros.h" |
| 18 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 19 | 27 |
| 20 using content::WebContents; | 28 using content::WebContents; |
| 21 using content::WebContentsObserver; | 29 using content::WebContentsObserver; |
| 22 | 30 |
| 31 namespace { | |
| 32 | |
| 23 class RequestPrintPreviewObserver : public WebContentsObserver { | 33 class RequestPrintPreviewObserver : public WebContentsObserver { |
| 24 public: | 34 public: |
| 25 explicit RequestPrintPreviewObserver(WebContents* dialog) | 35 explicit RequestPrintPreviewObserver(WebContents* dialog) |
| 26 : WebContentsObserver(dialog) { | 36 : WebContentsObserver(dialog) { |
| 27 } | 37 } |
| 28 ~RequestPrintPreviewObserver() override {} | 38 ~RequestPrintPreviewObserver() override {} |
| 29 | 39 |
| 30 void set_quit_closure(const base::Closure& quit_closure) { | 40 void set_quit_closure(const base::Closure& quit_closure) { |
| 31 quit_closure_ = quit_closure; | 41 quit_closure_ = quit_closure; |
| 32 } | 42 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 88 | 98 |
| 89 private: | 99 private: |
| 90 // content::WebContentsObserver implementation. | 100 // content::WebContentsObserver implementation. |
| 91 void WebContentsDestroyed() override { dialog_destroyed_ = true; } | 101 void WebContentsDestroyed() override { dialog_destroyed_ = true; } |
| 92 | 102 |
| 93 bool dialog_destroyed_; | 103 bool dialog_destroyed_; |
| 94 | 104 |
| 95 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogDestroyedObserver); | 105 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogDestroyedObserver); |
| 96 }; | 106 }; |
| 97 | 107 |
| 108 bool GetPdfPluginInfo(content::WebPluginInfo* info) { | |
| 109 base::FilePath pdf_plugin_path = base::FilePath::FromUTF8Unsafe( | |
| 110 ChromeContentClient::kPDFPluginPath); | |
| 111 return content::PluginService::GetInstance()->GetPluginInfoByPath( | |
| 112 pdf_plugin_path, info); | |
| 113 } | |
| 114 | |
| 115 const char kDummyPrintUrl[] = "chrome://print/dummy.pdf"; | |
| 116 | |
| 117 void CountFrames(int* frame_count, | |
| 118 content::RenderFrameHost* frame) { | |
| 119 ++(*frame_count); | |
| 120 } | |
| 121 | |
| 122 void CheckPdfPluginForRenderFrame(content::RenderFrameHost* frame) { | |
| 123 content::WebPluginInfo pdf_plugin_info; | |
| 124 ASSERT_TRUE(GetPdfPluginInfo(&pdf_plugin_info)); | |
| 125 | |
| 126 ChromePluginServiceFilter* filter = ChromePluginServiceFilter::GetInstance(); | |
| 127 EXPECT_TRUE(filter->IsPluginAvailable( | |
| 128 frame->GetProcess()->GetID(), | |
| 129 frame->GetRoutingID(), | |
| 130 nullptr, | |
| 131 GURL(kDummyPrintUrl), | |
| 132 GURL(), | |
| 133 &pdf_plugin_info)); | |
| 134 } | |
| 135 | |
| 136 } // namespace | |
| 137 | |
| 98 class PrintPreviewDialogControllerBrowserTest : public InProcessBrowserTest { | 138 class PrintPreviewDialogControllerBrowserTest : public InProcessBrowserTest { |
| 99 public: | 139 public: |
| 100 PrintPreviewDialogControllerBrowserTest() : initiator_(NULL) {} | 140 PrintPreviewDialogControllerBrowserTest() : initiator_(NULL) {} |
| 101 ~PrintPreviewDialogControllerBrowserTest() override {} | 141 ~PrintPreviewDialogControllerBrowserTest() override {} |
| 102 | 142 |
| 103 WebContents* initiator() { | 143 WebContents* initiator() { |
| 104 return initiator_; | 144 return initiator_; |
| 105 } | 145 } |
| 106 | 146 |
| 107 void PrintPreview() { | 147 void PrintPreview() { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 154 // Test to verify that when a initiator navigates, we can create a new preview | 194 // Test to verify that when a initiator navigates, we can create a new preview |
| 155 // dialog for the new tab contents. | 195 // dialog for the new tab contents. |
| 156 // http://crbug.com/377337 | 196 // http://crbug.com/377337 |
| 157 #if defined(OS_WIN) | 197 #if defined(OS_WIN) |
| 158 #define MAYBE_NavigateFromInitiatorTab DISABLED_NavigateFromInitiatorTab | 198 #define MAYBE_NavigateFromInitiatorTab DISABLED_NavigateFromInitiatorTab |
| 159 #else | 199 #else |
| 160 #define MAYBE_NavigateFromInitiatorTab NavigateFromInitiatorTab | 200 #define MAYBE_NavigateFromInitiatorTab NavigateFromInitiatorTab |
| 161 #endif | 201 #endif |
| 162 IN_PROC_BROWSER_TEST_F(PrintPreviewDialogControllerBrowserTest, | 202 IN_PROC_BROWSER_TEST_F(PrintPreviewDialogControllerBrowserTest, |
| 163 MAYBE_NavigateFromInitiatorTab) { | 203 MAYBE_NavigateFromInitiatorTab) { |
| 164 // print for the first time. | 204 // Print for the first time. |
| 165 PrintPreview(); | 205 PrintPreview(); |
| 166 | 206 |
| 167 // Get the preview dialog for the initiator tab. | 207 // Get the preview dialog for the initiator tab. |
| 168 WebContents* preview_dialog = GetPrintPreviewDialog(); | 208 WebContents* preview_dialog = GetPrintPreviewDialog(); |
| 169 | 209 |
| 170 // Check a new print preview dialog got created. | 210 // Check a new print preview dialog got created. |
| 171 ASSERT_TRUE(preview_dialog); | 211 ASSERT_TRUE(preview_dialog); |
| 172 ASSERT_NE(initiator(), preview_dialog); | 212 ASSERT_NE(initiator(), preview_dialog); |
| 173 | 213 |
| 174 // Navigate in the initiator tab. Make sure navigating destroys the print | 214 // Navigate in the initiator tab. Make sure navigating destroys the print |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 190 // Test to verify that after reloading the initiator, it creates a new print | 230 // Test to verify that after reloading the initiator, it creates a new print |
| 191 // preview dialog. | 231 // preview dialog. |
| 192 // http://crbug.com/377337 | 232 // http://crbug.com/377337 |
| 193 #if defined(OS_WIN) | 233 #if defined(OS_WIN) |
| 194 #define MAYBE_ReloadInitiatorTab DISABLED_ReloadInitiatorTab | 234 #define MAYBE_ReloadInitiatorTab DISABLED_ReloadInitiatorTab |
| 195 #else | 235 #else |
| 196 #define MAYBE_ReloadInitiatorTab ReloadInitiatorTab | 236 #define MAYBE_ReloadInitiatorTab ReloadInitiatorTab |
| 197 #endif | 237 #endif |
| 198 IN_PROC_BROWSER_TEST_F(PrintPreviewDialogControllerBrowserTest, | 238 IN_PROC_BROWSER_TEST_F(PrintPreviewDialogControllerBrowserTest, |
| 199 MAYBE_ReloadInitiatorTab) { | 239 MAYBE_ReloadInitiatorTab) { |
| 200 // print for the first time. | 240 // Print for the first time. |
| 201 PrintPreview(); | 241 PrintPreview(); |
| 202 | 242 |
| 203 WebContents* preview_dialog = GetPrintPreviewDialog(); | 243 WebContents* preview_dialog = GetPrintPreviewDialog(); |
| 204 | 244 |
| 205 // Check a new print preview dialog got created. | 245 // Check a new print preview dialog got created. |
| 206 ASSERT_TRUE(preview_dialog); | 246 ASSERT_TRUE(preview_dialog); |
| 207 ASSERT_NE(initiator(), preview_dialog); | 247 ASSERT_NE(initiator(), preview_dialog); |
| 208 | 248 |
| 209 // Reload the initiator. Make sure reloading destroys the print preview | 249 // Reload the initiator. Make sure reloading destroys the print preview |
| 210 // dialog. | 250 // dialog. |
| 211 PrintPreviewDialogDestroyedObserver dialog_destroyed_observer(preview_dialog); | 251 PrintPreviewDialogDestroyedObserver dialog_destroyed_observer(preview_dialog); |
| 212 chrome::Reload(browser(), CURRENT_TAB); | 252 chrome::Reload(browser(), CURRENT_TAB); |
| 213 content::WaitForLoadStop( | 253 content::WaitForLoadStop( |
| 214 browser()->tab_strip_model()->GetActiveWebContents()); | 254 browser()->tab_strip_model()->GetActiveWebContents()); |
| 215 ASSERT_TRUE(dialog_destroyed_observer.dialog_destroyed()); | 255 ASSERT_TRUE(dialog_destroyed_observer.dialog_destroyed()); |
| 216 | 256 |
| 217 // Try printing again. | 257 // Try printing again. |
| 218 PrintPreview(); | 258 PrintPreview(); |
| 219 | 259 |
| 220 // Create a preview dialog for the initiator tab. | 260 // Create a preview dialog for the initiator tab. |
| 221 WebContents* new_preview_dialog = GetPrintPreviewDialog(); | 261 WebContents* new_preview_dialog = GetPrintPreviewDialog(); |
| 222 EXPECT_TRUE(new_preview_dialog); | 262 EXPECT_TRUE(new_preview_dialog); |
| 223 } | 263 } |
| 264 | |
| 265 // Test to verify that after print preview works even when the PDF plugin is | |
| 266 // disabled for webpages. | |
| 267 IN_PROC_BROWSER_TEST_F(PrintPreviewDialogControllerBrowserTest, | |
| 268 PdfPluginDisabled) { | |
| 269 // Print for the first time. | |
| 270 PrintPreview(); | |
| 271 | |
| 272 // Navigate in the initiator tab. | |
| 273 WebContents* preview_dialog = GetPrintPreviewDialog(); | |
| 274 PrintPreviewDialogDestroyedObserver dialog_destroyed_observer(preview_dialog); | |
| 275 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL)); | |
| 276 ASSERT_TRUE(dialog_destroyed_observer.dialog_destroyed()); | |
| 277 | |
| 278 // Get the PDF plugin info. | |
| 279 content::WebPluginInfo pdf_plugin_info; | |
| 280 ASSERT_TRUE(GetPdfPluginInfo(&pdf_plugin_info)); | |
|
Lei Zhang
2015/03/19 02:55:34
This failed on the CrOS bot. I'm pretty sure CrOS
| |
| 281 | |
| 282 // Disable the PDF plugin. | |
| 283 PluginPrefs::GetForProfile(browser()->profile())->EnablePluginGroup( | |
| 284 false, base::ASCIIToUTF16(ChromeContentClient::kPDFPluginName)); | |
| 285 | |
| 286 // Make sure it is actually disabled for webpages. | |
| 287 ChromePluginServiceFilter* filter = ChromePluginServiceFilter::GetInstance(); | |
| 288 content::WebPluginInfo dummy_pdf_plugin_info = pdf_plugin_info; | |
| 289 EXPECT_FALSE(filter->IsPluginAvailable( | |
| 290 initiator()->GetRenderProcessHost()->GetID(), | |
| 291 initiator()->GetMainFrame()->GetRoutingID(), | |
| 292 nullptr, | |
| 293 GURL("http://google.com"), | |
| 294 GURL(), | |
| 295 &dummy_pdf_plugin_info)); | |
| 296 | |
| 297 // Print preview again. | |
| 298 PrintPreview(); | |
| 299 | |
| 300 // Check a new print preview dialog got created. | |
| 301 WebContents* new_preview_dialog = GetPrintPreviewDialog(); | |
| 302 ASSERT_TRUE(new_preview_dialog); | |
| 303 ASSERT_NE(initiator(), new_preview_dialog); | |
| 304 | |
| 305 // Wait until the <iframe> in the print preview renderer has loaded. | |
| 306 int frame_count; | |
| 307 do { | |
| 308 base::RunLoop run_loop; | |
| 309 base::MessageLoop::current()->PostDelayedTask( | |
| 310 FROM_HERE, run_loop.QuitClosure(), base::TimeDelta::FromSeconds(1)); | |
| 311 run_loop.Run(); | |
| 312 | |
| 313 frame_count = 0; | |
| 314 new_preview_dialog->ForEachFrame( | |
| 315 base::Bind(&CountFrames, base::Unretained(&frame_count))); | |
| 316 } while (frame_count < 2); | |
| 317 ASSERT_EQ(2, frame_count); | |
| 318 | |
| 319 // Make sure all the frames in the dialog has access to the PDF plugin. | |
| 320 new_preview_dialog->ForEachFrame(base::Bind(&CheckPdfPluginForRenderFrame)); | |
| 321 } | |
| OLD | NEW |