thestig | 746da1cc6 | 2017-03-16 06:18:21 | [diff] [blame] | 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 | |
rbpotter | fdff067 | 2018-04-12 20:46:58 | [diff] [blame] | 5 | #include <memory> |
| 6 | #include <utility> |
| 7 | |
| 8 | #include "base/auto_reset.h" |
Sebastien Marchand | f1349f5 | 2019-01-25 03:16:41 | [diff] [blame] | 9 | #include "base/bind.h" |
rbpotter | fdff067 | 2018-04-12 20:46:58 | [diff] [blame] | 10 | #include "base/run_loop.h" |
| 11 | #include "build/build_config.h" |
| 12 | #include "chrome/browser/browser_process.h" |
rbpotter | fdff067 | 2018-04-12 20:46:58 | [diff] [blame] | 13 | #include "chrome/browser/printing/print_test_utils.h" |
| 14 | #include "chrome/browser/printing/print_view_manager.h" |
| 15 | #include "chrome/browser/printing/print_view_manager_base.h" |
| 16 | #include "chrome/browser/printing/test_print_job.h" |
| 17 | #include "chrome/browser/printing/test_printer_query.h" |
thestig | 746da1cc6 | 2017-03-16 06:18:21 | [diff] [blame] | 18 | #include "chrome/browser/ui/browser_commands.h" |
| 19 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
Evan Stade | e8499d4 | 2018-11-16 21:17:48 | [diff] [blame] | 20 | #include "chrome/test/base/browser_with_test_window_test.h" |
rbpotter | fdff067 | 2018-04-12 20:46:58 | [diff] [blame] | 21 | #include "components/printing/common/print_messages.h" |
thestig | 746da1cc6 | 2017-03-16 06:18:21 | [diff] [blame] | 22 | #include "content/public/test/test_renderer_host.h" |
| 23 | |
| 24 | namespace printing { |
| 25 | |
Evan Stade | e8499d4 | 2018-11-16 21:17:48 | [diff] [blame] | 26 | using PrintViewManagerTest = BrowserWithTestWindowTest; |
thestig | 746da1cc6 | 2017-03-16 06:18:21 | [diff] [blame] | 27 | |
rbpotter | fdff067 | 2018-04-12 20:46:58 | [diff] [blame] | 28 | class TestPrintViewManager : public PrintViewManagerBase { |
| 29 | public: |
| 30 | explicit TestPrintViewManager(content::WebContents* web_contents) |
| 31 | : PrintViewManagerBase(web_contents) {} |
| 32 | |
| 33 | ~TestPrintViewManager() override { |
| 34 | // Set this null here. Otherwise, the PrintViewManagerBase destructor will |
| 35 | // try to de-register for notifications that were not registered for in |
| 36 | // CreateNewPrintJob(). |
| 37 | print_job_ = nullptr; |
| 38 | } |
| 39 | |
| 40 | // Mostly copied from PrintViewManager::PrintPreviewNow(). We can't override |
| 41 | // PrintViewManager since it is a user data class. |
| 42 | bool PrintPreviewNow(content::RenderFrameHost* rfh, bool has_selection) { |
| 43 | auto msg = std::make_unique<PrintMsg_InitiatePrintPreview>( |
| 44 | rfh->GetRoutingID(), has_selection); |
| 45 | return PrintNowInternal(rfh, std::move(msg)); |
| 46 | } |
| 47 | |
| 48 | // Getters for validating arguments to StartPdf...Conversion functions |
| 49 | const gfx::Size& page_size() { return test_job()->page_size(); } |
| 50 | |
| 51 | const gfx::Rect& content_area() { return test_job()->content_area(); } |
| 52 | |
Lei Zhang | 56e6771 | 2018-04-14 00:55:09 | [diff] [blame] | 53 | const gfx::Point& physical_offsets() { |
| 54 | return test_job()->physical_offsets(); |
| 55 | } |
rbpotter | fdff067 | 2018-04-12 20:46:58 | [diff] [blame] | 56 | |
| 57 | #if defined(OS_WIN) |
| 58 | PrintSettings::PrinterType type() { return test_job()->type(); } |
| 59 | #endif |
| 60 | |
| 61 | // Ends the run loop. |
| 62 | void FakePrintCallback(const base::Value& error) { |
| 63 | DCHECK(run_loop_); |
| 64 | run_loop_->Quit(); |
| 65 | } |
| 66 | |
| 67 | // Starts a run loop that quits when the print callback is called to indicate |
| 68 | // printing is complete. |
| 69 | void WaitForCallback() { |
| 70 | base::RunLoop run_loop; |
| 71 | base::AutoReset<base::RunLoop*> auto_reset(&run_loop_, &run_loop); |
| 72 | run_loop.Run(); |
| 73 | } |
| 74 | |
| 75 | protected: |
| 76 | // Override to create a TestPrintJob instead of a real one. |
Vladislav Kuzkokov | 5742c64 | 2019-06-04 14:52:11 | [diff] [blame] | 77 | bool CreateNewPrintJob(std::unique_ptr<PrinterQuery> query) override { |
rbpotter | fdff067 | 2018-04-12 20:46:58 | [diff] [blame] | 78 | print_job_ = base::MakeRefCounted<TestPrintJob>(); |
Vladislav Kuzkokov | 5742c64 | 2019-06-04 14:52:11 | [diff] [blame] | 79 | print_job_->Initialize(std::move(query), RenderSourceName(), number_pages_); |
rbpotter | fdff067 | 2018-04-12 20:46:58 | [diff] [blame] | 80 | return true; |
| 81 | } |
| 82 | |
| 83 | private: |
| 84 | TestPrintJob* test_job() { |
| 85 | return reinterpret_cast<TestPrintJob*>(print_job_.get()); |
| 86 | } |
| 87 | |
| 88 | base::RunLoop* run_loop_ = nullptr; |
| 89 | |
| 90 | DISALLOW_COPY_AND_ASSIGN(TestPrintViewManager); |
| 91 | }; |
| 92 | |
thestig | 746da1cc6 | 2017-03-16 06:18:21 | [diff] [blame] | 93 | TEST_F(PrintViewManagerTest, PrintSubFrameAndDestroy) { |
| 94 | chrome::NewTab(browser()); |
| 95 | content::WebContents* web_contents = |
| 96 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 97 | ASSERT_TRUE(web_contents); |
| 98 | |
| 99 | content::RenderFrameHost* sub_frame = |
| 100 | content::RenderFrameHostTester::For(web_contents->GetMainFrame()) |
| 101 | ->AppendChild("child"); |
| 102 | |
| 103 | PrintViewManager* print_view_manager = |
| 104 | PrintViewManager::FromWebContents(web_contents); |
| 105 | ASSERT_TRUE(print_view_manager); |
| 106 | EXPECT_FALSE(print_view_manager->print_preview_rfh()); |
| 107 | |
| 108 | print_view_manager->PrintPreviewNow(sub_frame, false); |
| 109 | EXPECT_TRUE(print_view_manager->print_preview_rfh()); |
| 110 | |
| 111 | content::RenderFrameHostTester::For(sub_frame)->Detach(); |
| 112 | EXPECT_FALSE(print_view_manager->print_preview_rfh()); |
| 113 | } |
| 114 | |
rbpotter | fdff067 | 2018-04-12 20:46:58 | [diff] [blame] | 115 | #if defined(OS_WIN) |
| 116 | // Verifies that StartPdfToPostScriptConversion is called with the correct |
| 117 | // printable area offsets. See crbug.com/821485. |
| 118 | TEST_F(PrintViewManagerTest, PostScriptHasCorrectOffsets) { |
| 119 | scoped_refptr<TestPrintQueriesQueue> queue = |
| 120 | base::MakeRefCounted<TestPrintQueriesQueue>(); |
| 121 | |
| 122 | // Setup PostScript printer with printable area offsets of 0.1in. |
| 123 | queue->SetupPrinterType(PrintSettings::PrinterType::TYPE_POSTSCRIPT_LEVEL2); |
Lei Zhang | 56e6771 | 2018-04-14 00:55:09 | [diff] [blame] | 124 | int offset_in_pixels = static_cast<int>(kTestPrinterDpi * 0.1f); |
| 125 | queue->SetupPrinterOffsets(offset_in_pixels, offset_in_pixels); |
rbpotter | fdff067 | 2018-04-12 20:46:58 | [diff] [blame] | 126 | g_browser_process->print_job_manager()->SetQueueForTest(queue); |
| 127 | |
| 128 | chrome::NewTab(browser()); |
| 129 | content::WebContents* web_contents = |
| 130 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 131 | ASSERT_TRUE(web_contents); |
| 132 | |
| 133 | std::unique_ptr<TestPrintViewManager> print_view_manager = |
| 134 | std::make_unique<TestPrintViewManager>(web_contents); |
| 135 | |
| 136 | print_view_manager->PrintPreviewNow(web_contents->GetMainFrame(), false); |
| 137 | |
| 138 | base::Value print_ticket = GetPrintTicket(printing::kLocalPrinter, false); |
rbpotter | fdff067 | 2018-04-12 20:46:58 | [diff] [blame] | 139 | const char kTestData[] = "abc"; |
| 140 | auto print_data = base::MakeRefCounted<base::RefCountedStaticMemory>( |
| 141 | kTestData, sizeof(kTestData)); |
| 142 | PrinterHandler::PrintCallback callback = |
| 143 | base::BindOnce(&TestPrintViewManager::FakePrintCallback, |
| 144 | base::Unretained(print_view_manager.get())); |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 145 | print_view_manager->PrintForPrintPreview(std::move(print_ticket), print_data, |
rbpotter | fdff067 | 2018-04-12 20:46:58 | [diff] [blame] | 146 | web_contents->GetMainFrame(), |
| 147 | std::move(callback)); |
| 148 | print_view_manager->WaitForCallback(); |
| 149 | |
Lei Zhang | 56e6771 | 2018-04-14 00:55:09 | [diff] [blame] | 150 | EXPECT_EQ(gfx::Point(60, 60), print_view_manager->physical_offsets()); |
rbpotter | fdff067 | 2018-04-12 20:46:58 | [diff] [blame] | 151 | EXPECT_EQ(gfx::Rect(0, 0, 5100, 6600), print_view_manager->content_area()); |
| 152 | EXPECT_EQ(PrintSettings::PrinterType::TYPE_POSTSCRIPT_LEVEL2, |
| 153 | print_view_manager->type()); |
| 154 | } |
| 155 | #endif |
| 156 | |
thestig | 746da1cc6 | 2017-03-16 06:18:21 | [diff] [blame] | 157 | } // namespace printing |