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 <Windows.h> | |
|
Lei Zhang
2017/04/19 21:14:07
lowercase
rbpotter
2017/04/19 23:30:12
Done.
| |
| 8 #include <commdlg.h> | |
| 9 | |
| 10 #include "base/auto_reset.h" | |
| 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 bool init_called = false; | |
|
Lei Zhang
2017/04/19 21:14:07
Can we avoid these global variables by doing the f
rbpotter
2017/04/19 23:30:13
Done.
| |
| 33 base::RunLoop* run_loop; | |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 37 // Defined here as Windows crashes if this is defined in a class or a | |
| 38 // namespace. | |
| 39 UINT_PTR CALLBACK PrintPreviewHandlerTestHookFunction(HWND hdlg, | |
| 40 UINT uiMsg, | |
|
Lei Zhang
2017/04/19 21:14:07
message, wparam, lparam
rbpotter
2017/04/19 23:30:13
Done.
| |
| 41 WPARAM wParam, | |
| 42 LPARAM lParam) { | |
| 43 if (uiMsg != WM_INITDIALOG) | |
| 44 return 0; | |
| 45 init_called = true; | |
| 46 PostMessage(hdlg, WM_COMMAND, IDABORT, 0); | |
| 47 if (run_loop) | |
| 48 run_loop->Quit(); | |
| 49 return 1; | |
| 50 } | |
| 51 | |
| 52 namespace { | |
| 53 bool GetOpenFileNameImpl(OPENFILENAME* ofn) { | |
| 54 return ::GetOpenFileName(ofn); | |
| 55 } | |
| 56 | |
| 57 bool GetSaveFileNameImpl(OPENFILENAME* ofn) { | |
| 58 // Modify ofn so that the hook function will be called. | |
| 59 ofn->Flags |= OFN_ENABLEHOOK; | |
| 60 ofn->lpfnHook = PrintPreviewHandlerTestHookFunction; | |
| 61 return ::GetSaveFileName(ofn); | |
| 62 } | |
| 63 | |
| 64 class FakePrintPreviewHandler : public PrintPreviewHandler { | |
| 65 public: | |
| 66 explicit FakePrintPreviewHandler(content::WebUI* web_ui) | |
| 67 : save_failed_(false) { | |
| 68 set_web_ui(web_ui); | |
| 69 } | |
| 70 | |
| 71 void FileSelected(const base::FilePath& path, | |
| 72 int index, | |
| 73 void* params) override { | |
| 74 save_failed_ = false; | |
| 75 if (run_loop) | |
| 76 run_loop->Quit(); | |
|
Lei Zhang
2017/04/19 21:14:07
As is, would this end up calling Quit() twice?
rbpotter
2017/04/19 23:30:13
Only one of FileSelected or FileSelectionCanceled
| |
| 77 } | |
| 78 | |
| 79 void FileSelectionCanceled(void* params) override { | |
| 80 save_failed_ = true; | |
| 81 if (run_loop) | |
| 82 run_loop->Quit(); | |
| 83 } | |
| 84 | |
| 85 void StartPrintToPdf() { | |
| 86 PrintToPdf(); | |
| 87 if (!run_loop && !init_called) { | |
| 88 base::RunLoop new_run_loop; | |
|
Lei Zhang
2017/04/19 21:14:08
If the RunLoop becomes a member variable, can we g
rbpotter
2017/04/19 23:30:12
Done.
| |
| 89 base::AutoReset<base::RunLoop*> auto_reset(&run_loop, &new_run_loop); | |
| 90 new_run_loop.Run(); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 bool save_failed() { return save_failed_; } | |
| 95 | |
| 96 protected: | |
|
Lei Zhang
2017/04/19 21:14:08
private?
rbpotter
2017/04/19 23:30:12
Done.
| |
| 97 // Simplified version of select file to avoid checking preferences and sticky | |
| 98 // settings in the test | |
| 99 void SelectFile(const base::FilePath& default_filename, | |
| 100 bool prompt_user) override { | |
| 101 ui::SelectFileDialog::FileTypeInfo file_type_info; | |
| 102 file_type_info.extensions.resize(1); | |
| 103 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf")); | |
| 104 select_file_dialog_ = ui::CreateWinSelectFileDialog( | |
| 105 this, nullptr /*policy already checked*/, | |
| 106 base::Bind(GetOpenFileNameImpl), base::Bind(GetSaveFileNameImpl)); | |
| 107 select_file_dialog_->SelectFile( | |
| 108 ui::SelectFileDialog::SELECT_SAVEAS_FILE, base::string16(), | |
| 109 default_filename, &file_type_info, 0, base::FilePath::StringType(), | |
| 110 platform_util::GetTopLevel(web_ui()->GetWebContents()->GetNativeView()), | |
| 111 NULL); | |
| 112 } | |
| 113 | |
| 114 private: | |
| 115 bool save_failed_; | |
| 116 }; | |
| 117 } // namespace | |
| 118 | |
| 119 class PrintPreviewHandlerTest : public PrintPreviewTest { | |
| 120 public: | |
| 121 PrintPreviewHandlerTest() {} | |
|
Lei Zhang
2017/04/19 21:14:08
Use braces or default consistently between the cto
rbpotter
2017/04/19 23:30:13
Done.
| |
| 122 ~PrintPreviewHandlerTest() override = default; | |
| 123 | |
| 124 void SetUp() override { | |
| 125 PrintPreviewTest::SetUp(); | |
| 126 | |
| 127 // Create a new tab | |
|
Lei Zhang
2017/04/19 21:14:08
Not sure if you need to do this. I think the brows
rbpotter
2017/04/19 23:30:13
Does not start with a tab - tried removing this an
Lei Zhang
2017/04/20 00:12:30
OK, I remembered wrong then.
| |
| 128 chrome::NewTab(browser()); | |
| 129 | |
| 130 // Initialize state | |
| 131 init_called = false; | |
| 132 run_loop = nullptr; | |
| 133 } | |
| 134 | |
| 135 protected: | |
| 136 void CreateUiAndHandler() { | |
| 137 WebContents* initiator = | |
| 138 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 139 ASSERT_TRUE(initiator); | |
| 140 | |
| 141 // Get print preview UI | |
| 142 printing::PrintPreviewDialogController* controller = | |
| 143 printing::PrintPreviewDialogController::GetInstance(); | |
| 144 ASSERT_TRUE(controller); | |
| 145 printing::PrintViewManager* print_view_manager = | |
| 146 printing::PrintViewManager::FromWebContents(initiator); | |
| 147 print_view_manager->PrintPreviewNow(initiator->GetMainFrame(), false); | |
| 148 WebContents* preview_dialog = | |
| 149 controller->GetOrCreatePreviewDialog(initiator); | |
| 150 ASSERT_TRUE(preview_dialog); | |
| 151 preview_ui_ = static_cast<PrintPreviewUI*>( | |
| 152 preview_dialog->GetWebUI()->GetController()); | |
| 153 ASSERT_TRUE(preview_ui_); | |
| 154 | |
| 155 // Initialize |preview_handler_| | |
|
Lei Zhang
2017/04/19 21:14:07
Probably not needed.
rbpotter
2017/04/19 23:30:13
Done.
| |
| 156 preview_handler_.reset( | |
|
Lei Zhang
2017/04/19 21:14:08
Can we use foo = base::MakeUnique<Foo>() instead o
rbpotter
2017/04/19 23:30:13
Done.
| |
| 157 new FakePrintPreviewHandler(preview_dialog->GetWebUI())); | |
| 158 } | |
| 159 | |
| 160 std::unique_ptr<FakePrintPreviewHandler> preview_handler_; | |
| 161 PrintPreviewUI* preview_ui_; | |
|
Lei Zhang
2017/04/19 21:14:07
Initialize this variable, or maybe just drop it an
rbpotter
2017/04/19 23:30:13
Done.
| |
| 162 | |
| 163 private: | |
| 164 DISALLOW_COPY_AND_ASSIGN(PrintPreviewHandlerTest); | |
| 165 }; | |
| 166 | |
| 167 TEST_F(PrintPreviewHandlerTest, TestSaveAsPdf) { | |
| 168 CreateUiAndHandler(); | |
| 169 preview_ui_->SetInitiatorTitle(L"111111111111111111111.html"); | |
| 170 preview_handler_->StartPrintToPdf(); | |
| 171 ASSERT_FALSE(!init_called && preview_handler_->save_failed()); | |
|
Lei Zhang
2017/04/19 21:14:07
So this is the same as the following, right?
ASSE
rbpotter
2017/04/19 23:30:13
Done.
| |
| 172 } | |
| 173 | |
| 174 TEST_F(PrintPreviewHandlerTest, TestSaveAsPdfLongFileName) { | |
| 175 CreateUiAndHandler(); | |
| 176 preview_ui_->SetInitiatorTitle( | |
| 177 L"11111111111111111111111111111111111111111111111111111111111111111111111" | |
| 178 L"11111111111111111111111111111111111111111111111111111111111111111111111" | |
| 179 L"11111111111111111111111111111111111111111111111111111111111111111111111" | |
| 180 L"11111111111111111111111111111111111111111111111111111111111111111111111" | |
| 181 L"1111111111111111111111111111111111111111111111111.html"); | |
| 182 preview_handler_->StartPrintToPdf(); | |
| 183 ASSERT_FALSE(!init_called && preview_handler_->save_failed()); | |
| 184 } | |
| OLD | NEW |