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