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