1 // Copyright (c) 2011 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.
5 #include "printing/printing_context_win.h"
8 #include "base/message_loop/message_loop.h"
9 #include "printing/backend/printing_info_win.h"
10 #include "printing/backend/win_helper.h"
11 #include "printing/print_settings.h"
12 #include "printing/printing_context_system_dialog_win.h"
13 #include "printing/printing_test.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 // This test is automatically disabled if no printer is available.
19 class PrintingContextTest
: public PrintingTest
<testing::Test
>,
20 public PrintingContext::Delegate
{
22 void PrintSettingsCallback(PrintingContext::Result result
) {
26 // PrintingContext::Delegate methods.
27 virtual gfx::NativeView
GetParentView() override
{ return NULL
; }
28 virtual std::string
GetAppLocale() override
{ return std::string(); }
31 PrintingContext::Result
result() const { return result_
; }
34 PrintingContext::Result result_
;
37 class MockPrintingContextWin
: public PrintingContextSytemDialogWin
{
39 MockPrintingContextWin(Delegate
* delegate
)
40 : PrintingContextSytemDialogWin(delegate
) {}
43 // This is a fake PrintDlgEx implementation that sets the right fields in
44 // |lppd| to trigger a bug in older revisions of PrintingContext.
45 HRESULT
ShowPrintDialog(PRINTDLGEX
* lppd
) override
{
46 // The interesting bits:
47 // Pretend the user hit print
48 lppd
->dwResultAction
= PD_RESULT_PRINT
;
50 // Pretend the page range is 1-5, but since lppd->Flags does not have
51 // PD_SELECTION set, this really shouldn't matter.
52 lppd
->nPageRanges
= 1;
53 lppd
->lpPageRanges
[0].nFromPage
= 1;
54 lppd
->lpPageRanges
[0].nToPage
= 5;
56 base::string16 printer_name
= PrintingContextTest::GetDefaultPrinter();
57 ScopedPrinterHandle printer
;
58 if (!printer
.OpenPrinter(printer_name
.c_str()))
61 scoped_ptr
<uint8
[]> buffer
;
62 const DEVMODE
* dev_mode
= NULL
;
63 HRESULT result
= S_OK
;
65 lppd
->hDevMode
= NULL
;
66 lppd
->hDevNames
= NULL
;
69 if (info_2
.Init(printer
.Get())) {
70 dev_mode
= info_2
.get()->pDevMode
;
77 lppd
->hDC
= CreateDC(L
"WINSPOOL", printer_name
.c_str(), NULL
, dev_mode
);
83 size_t dev_mode_size
= dev_mode
->dmSize
+ dev_mode
->dmDriverExtra
;
84 lppd
->hDevMode
= GlobalAlloc(GMEM_MOVEABLE
, dev_mode_size
);
85 if (!lppd
->hDevMode
) {
89 void* dev_mode_ptr
= GlobalLock(lppd
->hDevMode
);
94 memcpy(dev_mode_ptr
, dev_mode
, dev_mode_size
);
95 GlobalUnlock(lppd
->hDevMode
);
99 2 + sizeof(wchar_t) * lstrlen(info_2
.get()->pDriverName
);
100 size_t printer_size
=
101 2 + sizeof(wchar_t) * lstrlen(info_2
.get()->pPrinterName
);
102 size_t port_size
= 2 + sizeof(wchar_t) * lstrlen(info_2
.get()->pPortName
);
103 size_t dev_names_size
=
104 sizeof(DEVNAMES
) + driver_size
+ printer_size
+ port_size
;
105 lppd
->hDevNames
= GlobalAlloc(GHND
, dev_names_size
);
106 if (!lppd
->hDevNames
) {
110 void* dev_names_ptr
= GlobalLock(lppd
->hDevNames
);
111 if (!dev_names_ptr
) {
115 DEVNAMES
* dev_names
= reinterpret_cast<DEVNAMES
*>(dev_names_ptr
);
116 dev_names
->wDefault
= 1;
117 dev_names
->wDriverOffset
= sizeof(DEVNAMES
) / sizeof(wchar_t);
118 memcpy(reinterpret_cast<uint8
*>(dev_names_ptr
) + dev_names
->wDriverOffset
,
119 info_2
.get()->pDriverName
,
121 dev_names
->wDeviceOffset
=
122 dev_names
->wDriverOffset
+ driver_size
/ sizeof(wchar_t);
123 memcpy(reinterpret_cast<uint8
*>(dev_names_ptr
) + dev_names
->wDeviceOffset
,
124 info_2
.get()->pPrinterName
,
126 dev_names
->wOutputOffset
=
127 dev_names
->wDeviceOffset
+ printer_size
/ sizeof(wchar_t);
128 memcpy(reinterpret_cast<uint8
*>(dev_names_ptr
) + dev_names
->wOutputOffset
,
129 info_2
.get()->pPortName
,
131 GlobalUnlock(lppd
->hDevNames
);
132 dev_names_ptr
= NULL
;
135 // Note: This section does proper deallocation/free of DC/global handles. We
136 // did not use ScopedHGlobal or ScopedHandle because they did not
137 // perform what we need. Goto's are used based on Windows programming
138 // idiom, to avoid deeply nested if's, and try-catch-finally is not
139 // allowed in Chromium.
140 if (FAILED(result
)) {
144 if (lppd
->hDevMode
) {
145 GlobalFree(lppd
->hDevMode
);
147 if (lppd
->hDevNames
) {
148 GlobalFree(lppd
->hDevNames
);
155 TEST_F(PrintingContextTest
, PrintAll
) {
156 base::MessageLoop message_loop
;
157 if (IsTestCaseDisabled())
160 MockPrintingContextWin
context(this);
161 context
.AskUserForSettings(
165 base::Bind(&PrintingContextTest::PrintSettingsCallback
,
166 base::Unretained(this)));
167 EXPECT_EQ(PrintingContext::OK
, result());
168 PrintSettings settings
= context
.settings();
169 EXPECT_EQ(settings
.ranges().size(), 0);
172 TEST_F(PrintingContextTest
, Base
) {
173 if (IsTestCaseDisabled())
176 PrintSettings settings
;
177 settings
.set_device_name(GetDefaultPrinter());
179 scoped_ptr
<PrintingContext
> context(PrintingContext::Create(this));
180 EXPECT_EQ(PrintingContext::OK
, context
->InitWithSettings(settings
));
182 // The print may lie to use and may not support world transformation.
184 XFORM random_matrix
= { 1, 0.1f
, 0, 1.5f
, 0, 1 };
185 EXPECT_TRUE(SetWorldTransform(context
->context(), &random_matrix
));
186 EXPECT_TRUE(ModifyWorldTransform(context
->context(), NULL
, MWT_IDENTITY
));
189 } // namespace printing