| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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/webui/print_preview/print_preview_handler.h" |
| 6 |
| 7 #include <commdlg.h> |
| 8 #include <windows.h> |
| 9 |
| 10 #include "base/run_loop.h" |
| 11 #include "chrome/browser/platform_util.h" |
| 12 #include "chrome/browser/printing/print_preview_dialog_controller.h" |
| 13 #include "chrome/browser/printing/print_preview_test.h" |
| 14 #include "chrome/browser/printing/print_view_manager.h" |
| 15 #include "chrome/browser/ui/browser_commands.h" |
| 16 #include "chrome/browser/ui/browser_tabstrip.h" |
| 17 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 18 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" |
| 19 #include "chrome/test/base/browser_with_test_window_test.h" |
| 20 #include "components/web_modal/web_contents_modal_dialog_manager.h" |
| 21 #include "content/public/browser/web_contents.h" |
| 22 #include "ui/shell_dialogs/select_file_dialog_win.h" |
| 23 |
| 24 using content::WebContents; |
| 25 |
| 26 namespace { |
| 27 |
| 28 class FakePrintPreviewHandler; |
| 29 bool GetOpenFileNameImpl(OPENFILENAME* ofn); |
| 30 bool GetSaveFileNameImpl(FakePrintPreviewHandler* handler, OPENFILENAME* ofn); |
| 31 |
| 32 class FakePrintPreviewHandler : public PrintPreviewHandler { |
| 33 public: |
| 34 explicit FakePrintPreviewHandler(content::WebUI* web_ui) |
| 35 : init_called_(false), save_failed_(false) { |
| 36 set_web_ui(web_ui); |
| 37 } |
| 38 |
| 39 void FileSelected(const base::FilePath& path, |
| 40 int index, |
| 41 void* params) override { |
| 42 // Since we always cancel the dialog as soon as it is initialized, this |
| 43 // should never be called. |
| 44 NOTREACHED(); |
| 45 } |
| 46 |
| 47 void FileSelectionCanceled(void* params) override { |
| 48 save_failed_ = true; |
| 49 run_loop_.Quit(); |
| 50 } |
| 51 |
| 52 void StartPrintToPdf() { |
| 53 PrintToPdf(); |
| 54 run_loop_.Run(); |
| 55 } |
| 56 |
| 57 bool save_failed() const { return save_failed_; } |
| 58 |
| 59 bool init_called() const { return init_called_; } |
| 60 |
| 61 void set_init_called() { init_called_ = true; } |
| 62 |
| 63 private: |
| 64 // Simplified version of select file to avoid checking preferences and sticky |
| 65 // settings in the test |
| 66 void SelectFile(const base::FilePath& default_filename, |
| 67 bool prompt_user) override { |
| 68 ui::SelectFileDialog::FileTypeInfo file_type_info; |
| 69 file_type_info.extensions.resize(1); |
| 70 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf")); |
| 71 select_file_dialog_ = ui::CreateWinSelectFileDialog( |
| 72 this, nullptr /*policy already checked*/, |
| 73 base::Bind(GetOpenFileNameImpl), base::Bind(GetSaveFileNameImpl, this)); |
| 74 select_file_dialog_->SelectFile( |
| 75 ui::SelectFileDialog::SELECT_SAVEAS_FILE, base::string16(), |
| 76 default_filename, &file_type_info, 0, base::FilePath::StringType(), |
| 77 platform_util::GetTopLevel(web_ui()->GetWebContents()->GetNativeView()), |
| 78 nullptr); |
| 79 } |
| 80 |
| 81 bool init_called_; |
| 82 bool save_failed_; |
| 83 base::RunLoop run_loop_; |
| 84 }; |
| 85 |
| 86 // Hook function to cancel the dialog when it is successfully initialized. |
| 87 UINT_PTR CALLBACK PrintPreviewHandlerTestHookFunction(HWND hdlg, |
| 88 UINT message, |
| 89 WPARAM wparam, |
| 90 LPARAM lparam) { |
| 91 if (message != WM_INITDIALOG) |
| 92 return 0; |
| 93 OPENFILENAME* ofn = reinterpret_cast<OPENFILENAME*>(lparam); |
| 94 FakePrintPreviewHandler* handler = |
| 95 reinterpret_cast<FakePrintPreviewHandler*>(ofn->lCustData); |
| 96 handler->set_init_called(); |
| 97 PostMessage(GetParent(hdlg), WM_COMMAND, MAKEWPARAM(IDCANCEL, 0), 0); |
| 98 return 1; |
| 99 } |
| 100 |
| 101 bool GetOpenFileNameImpl(OPENFILENAME* ofn) { |
| 102 return ::GetOpenFileName(ofn); |
| 103 } |
| 104 |
| 105 bool GetSaveFileNameImpl(FakePrintPreviewHandler* handler, OPENFILENAME* ofn) { |
| 106 // Modify ofn so that the hook function will be called. |
| 107 ofn->Flags |= OFN_ENABLEHOOK; |
| 108 ofn->lpfnHook = PrintPreviewHandlerTestHookFunction; |
| 109 ofn->lCustData = reinterpret_cast<LPARAM>(handler); |
| 110 return ::GetSaveFileName(ofn); |
| 111 } |
| 112 |
| 113 } // namespace |
| 114 |
| 115 class PrintPreviewHandlerTest : public PrintPreviewTest { |
| 116 public: |
| 117 PrintPreviewHandlerTest() : preview_ui_(nullptr) {} |
| 118 ~PrintPreviewHandlerTest() override {} |
| 119 |
| 120 void SetUp() override { |
| 121 PrintPreviewTest::SetUp(); |
| 122 |
| 123 // Create a new tab |
| 124 chrome::NewTab(browser()); |
| 125 } |
| 126 |
| 127 protected: |
| 128 void CreateUIAndHandler() { |
| 129 WebContents* initiator = |
| 130 browser()->tab_strip_model()->GetActiveWebContents(); |
| 131 ASSERT_TRUE(initiator); |
| 132 |
| 133 // Get print preview UI |
| 134 printing::PrintPreviewDialogController* controller = |
| 135 printing::PrintPreviewDialogController::GetInstance(); |
| 136 ASSERT_TRUE(controller); |
| 137 printing::PrintViewManager* print_view_manager = |
| 138 printing::PrintViewManager::FromWebContents(initiator); |
| 139 print_view_manager->PrintPreviewNow(initiator->GetMainFrame(), false); |
| 140 WebContents* preview_dialog = |
| 141 controller->GetOrCreatePreviewDialog(initiator); |
| 142 ASSERT_TRUE(preview_dialog); |
| 143 preview_ui_ = static_cast<PrintPreviewUI*>( |
| 144 preview_dialog->GetWebUI()->GetController()); |
| 145 ASSERT_TRUE(preview_ui_); |
| 146 |
| 147 preview_handler_ = |
| 148 base::MakeUnique<FakePrintPreviewHandler>(preview_dialog->GetWebUI()); |
| 149 } |
| 150 |
| 151 std::unique_ptr<FakePrintPreviewHandler> preview_handler_; |
| 152 PrintPreviewUI* preview_ui_; |
| 153 |
| 154 private: |
| 155 DISALLOW_COPY_AND_ASSIGN(PrintPreviewHandlerTest); |
| 156 }; |
| 157 |
| 158 TEST_F(PrintPreviewHandlerTest, TestSaveAsPdf) { |
| 159 CreateUIAndHandler(); |
| 160 preview_ui_->SetInitiatorTitle(L"111111111111111111111.html"); |
| 161 preview_handler_->StartPrintToPdf(); |
| 162 EXPECT_TRUE(preview_handler_->init_called()); |
| 163 EXPECT_TRUE(preview_handler_->save_failed()); |
| 164 } |
| 165 |
| 166 TEST_F(PrintPreviewHandlerTest, TestSaveAsPdfLongFileName) { |
| 167 CreateUIAndHandler(); |
| 168 preview_ui_->SetInitiatorTitle( |
| 169 L"11111111111111111111111111111111111111111111111111111111111111111111111" |
| 170 L"11111111111111111111111111111111111111111111111111111111111111111111111" |
| 171 L"11111111111111111111111111111111111111111111111111111111111111111111111" |
| 172 L"11111111111111111111111111111111111111111111111111111111111111111111111" |
| 173 L"1111111111111111111111111111111111111111111111111.html"); |
| 174 preview_handler_->StartPrintToPdf(); |
| 175 EXPECT_TRUE(preview_handler_->init_called()); |
| 176 EXPECT_TRUE(preview_handler_->save_failed()); |
| 177 } |
| OLD | NEW |