blob: 89e5c28f2c8ae8faddede0a77416fbe5f418a860 [file] [log] [blame]
thestig746da1cc62017-03-16 06:18:211// 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
rbpotterfdff0672018-04-12 20:46:585#include <memory>
6#include <utility>
7
8#include "base/auto_reset.h"
Sebastien Marchandf1349f52019-01-25 03:16:419#include "base/bind.h"
rbpotterfdff0672018-04-12 20:46:5810#include "base/run_loop.h"
11#include "build/build_config.h"
12#include "chrome/browser/browser_process.h"
rbpotterfdff0672018-04-12 20:46:5813#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"
thestig746da1cc62017-03-16 06:18:2118#include "chrome/browser/ui/browser_commands.h"
19#include "chrome/browser/ui/tabs/tab_strip_model.h"
Evan Stadee8499d42018-11-16 21:17:4820#include "chrome/test/base/browser_with_test_window_test.h"
rbpotterfdff0672018-04-12 20:46:5821#include "components/printing/common/print_messages.h"
thestig746da1cc62017-03-16 06:18:2122#include "content/public/test/test_renderer_host.h"
23
24namespace printing {
25
Evan Stadee8499d42018-11-16 21:17:4826using PrintViewManagerTest = BrowserWithTestWindowTest;
thestig746da1cc62017-03-16 06:18:2127
rbpotterfdff0672018-04-12 20:46:5828class 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 Zhang56e67712018-04-14 00:55:0953 const gfx::Point& physical_offsets() {
54 return test_job()->physical_offsets();
55 }
rbpotterfdff0672018-04-12 20:46:5856
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 Kuzkokov5742c642019-06-04 14:52:1177 bool CreateNewPrintJob(std::unique_ptr<PrinterQuery> query) override {
rbpotterfdff0672018-04-12 20:46:5878 print_job_ = base::MakeRefCounted<TestPrintJob>();
Vladislav Kuzkokov5742c642019-06-04 14:52:1179 print_job_->Initialize(std::move(query), RenderSourceName(), number_pages_);
rbpotterfdff0672018-04-12 20:46:5880 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
thestig746da1cc62017-03-16 06:18:2193TEST_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
rbpotterfdff0672018-04-12 20:46:58115#if defined(OS_WIN)
116// Verifies that StartPdfToPostScriptConversion is called with the correct
117// printable area offsets. See crbug.com/821485.
118TEST_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 Zhang56e67712018-04-14 00:55:09124 int offset_in_pixels = static_cast<int>(kTestPrinterDpi * 0.1f);
125 queue->SetupPrinterOffsets(offset_in_pixels, offset_in_pixels);
rbpotterfdff0672018-04-12 20:46:58126 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);
rbpotterfdff0672018-04-12 20:46:58139 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 Kuzkokov490ab0c2019-01-16 11:21:57145 print_view_manager->PrintForPrintPreview(std::move(print_ticket), print_data,
rbpotterfdff0672018-04-12 20:46:58146 web_contents->GetMainFrame(),
147 std::move(callback));
148 print_view_manager->WaitForCallback();
149
Lei Zhang56e67712018-04-14 00:55:09150 EXPECT_EQ(gfx::Point(60, 60), print_view_manager->physical_offsets());
rbpotterfdff0672018-04-12 20:46:58151 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
thestig746da1cc62017-03-16 06:18:21157} // namespace printing