Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(202)

Side by Side Diff: printing/printing_context_win_unittest.cc

Issue 168003002: Use DocumentProperties to get default DEVMODE instead of PRINTER_INFO_*. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « printing/printing_context_win.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <ocidl.h> 5 #include <ocidl.h>
6 #include <commdlg.h> 6 #include <commdlg.h>
7 7
8 #include <string> 8 #include <string>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "printing/backend/printing_info_win.h" 14 #include "printing/backend/printing_info_win.h"
15 #include "printing/backend/win_helper.h"
15 #include "printing/printing_test.h" 16 #include "printing/printing_test.h"
16 #include "printing/printing_context.h" 17 #include "printing/printing_context.h"
17 #include "printing/printing_context_win.h" 18 #include "printing/printing_context_win.h"
18 #include "printing/print_settings.h" 19 #include "printing/print_settings.h"
19 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
20 21
22 namespace printing {
23
21 // This test is automatically disabled if no printer is available. 24 // This test is automatically disabled if no printer is available.
22 class PrintingContextTest : public PrintingTest<testing::Test> { 25 class PrintingContextTest : public PrintingTest<testing::Test> {
23 public: 26 public:
24 void PrintSettingsCallback(printing::PrintingContext::Result result) { 27 void PrintSettingsCallback(PrintingContext::Result result) {
25 result_ = result; 28 result_ = result;
26 } 29 }
27 30
28 protected: 31 protected:
29 printing::PrintingContext::Result result() const { return result_; } 32 PrintingContext::Result result() const { return result_; }
30 33
31 private: 34 private:
32 printing::PrintingContext::Result result_; 35 PrintingContext::Result result_;
33 }; 36 };
34 37
35 // This is a fake PrintDlgEx implementation that sets the right fields in 38 // This is a fake PrintDlgEx implementation that sets the right fields in
36 // |lppd| to trigger a bug in older revisions of PrintingContext. 39 // |lppd| to trigger a bug in older revisions of PrintingContext.
37 HRESULT WINAPI PrintDlgExMock(LPPRINTDLGEX lppd) { 40 HRESULT WINAPI PrintDlgExMock(LPPRINTDLGEX lppd) {
38 // The interesting bits: 41 // The interesting bits:
39 // Pretend the user hit print 42 // Pretend the user hit print
40 lppd->dwResultAction = PD_RESULT_PRINT; 43 lppd->dwResultAction = PD_RESULT_PRINT;
41 44
42 // Pretend the page range is 1-5, but since lppd->Flags does not have 45 // Pretend the page range is 1-5, but since lppd->Flags does not have
43 // PD_SELECTION set, this really shouldn't matter. 46 // PD_SELECTION set, this really shouldn't matter.
44 lppd->nPageRanges = 1; 47 lppd->nPageRanges = 1;
45 lppd->lpPageRanges[0].nFromPage = 1; 48 lppd->lpPageRanges[0].nFromPage = 1;
46 lppd->lpPageRanges[0].nToPage = 5; 49 lppd->lpPageRanges[0].nToPage = 5;
47 50
48 // Painful paperwork. 51 base::string16 printer_name = PrintingContextTest::GetDefaultPrinter();
49 std::wstring printer_name = PrintingContextTest::GetDefaultPrinter(); 52 ScopedPrinterHandle printer;
50 HANDLE printer; 53 if (!printer.OpenPrinter(printer_name.c_str()))
51 if (!OpenPrinter(const_cast<wchar_t*>(printer_name.c_str()), &printer, NULL))
52 return E_FAIL; 54 return E_FAIL;
53 55
54 scoped_ptr<uint8[]> buffer; 56 scoped_ptr<uint8[]> buffer;
55 const DEVMODE* dev_mode = NULL; 57 const DEVMODE* dev_mode = NULL;
56 HRESULT result = S_OK; 58 HRESULT result = S_OK;
57 lppd->hDC = NULL; 59 lppd->hDC = NULL;
58 lppd->hDevMode = NULL; 60 lppd->hDevMode = NULL;
59 lppd->hDevNames = NULL; 61 lppd->hDevNames = NULL;
60 62
61 printing::PrinterInfo2 info_2; 63 PrinterInfo2 info_2;
62 if (info_2.Init(printer)) { 64 if (info_2.Init(printer)) {
63 dev_mode = info_2.get()->pDevMode; 65 dev_mode = info_2.get()->pDevMode;
64 } 66 }
65 if (!dev_mode) { 67 if (!dev_mode) {
66 result = E_FAIL; 68 result = E_FAIL;
67 goto Cleanup; 69 goto Cleanup;
68 } 70 }
69 71
70 if (!printing::PrintingContextWin::AllocateContext(printer_name, dev_mode, 72 if (!PrintingContextWin::AllocateContext(printer_name, dev_mode,
71 &lppd->hDC)) { 73 &lppd->hDC)) {
72 result = E_FAIL; 74 result = E_FAIL;
73 goto Cleanup; 75 goto Cleanup;
74 } 76 }
75 77
76 size_t dev_mode_size = dev_mode->dmSize + dev_mode->dmDriverExtra; 78 size_t dev_mode_size = dev_mode->dmSize + dev_mode->dmDriverExtra;
77 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, dev_mode_size); 79 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, dev_mode_size);
78 if (!lppd->hDevMode) { 80 if (!lppd->hDevMode) {
79 result = E_FAIL; 81 result = E_FAIL;
80 goto Cleanup; 82 goto Cleanup;
81 } 83 }
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 if (lppd->hDC) { 132 if (lppd->hDC) {
131 DeleteDC(lppd->hDC); 133 DeleteDC(lppd->hDC);
132 } 134 }
133 if (lppd->hDevMode) { 135 if (lppd->hDevMode) {
134 GlobalFree(lppd->hDevMode); 136 GlobalFree(lppd->hDevMode);
135 } 137 }
136 if (lppd->hDevNames) { 138 if (lppd->hDevNames) {
137 GlobalFree(lppd->hDevNames); 139 GlobalFree(lppd->hDevNames);
138 } 140 }
139 } 141 }
140 ClosePrinter(printer);
141 return result; 142 return result;
142 } 143 }
143 144
144 TEST_F(PrintingContextTest, Base) { 145 TEST_F(PrintingContextTest, Base) {
145 if (IsTestCaseDisabled()) 146 if (IsTestCaseDisabled())
146 return; 147 return;
147 148
148 printing::PrintSettings settings; 149 PrintSettings settings;
149 settings.set_device_name(GetDefaultPrinter()); 150 settings.set_device_name(GetDefaultPrinter());
150 // Initialize it. 151 // Initialize it.
151 scoped_ptr<printing::PrintingContext> context( 152 scoped_ptr<PrintingContext> context(PrintingContext::Create(std::string()));
152 printing::PrintingContext::Create(std::string())); 153 EXPECT_EQ(PrintingContext::OK, context->InitWithSettings(settings));
153 EXPECT_EQ(printing::PrintingContext::OK, context->InitWithSettings(settings));
154 154
155 // The print may lie to use and may not support world transformation. 155 // The print may lie to use and may not support world transformation.
156 // Verify right now. 156 // Verify right now.
157 XFORM random_matrix = { 1, 0.1f, 0, 1.5f, 0, 1 }; 157 XFORM random_matrix = { 1, 0.1f, 0, 1.5f, 0, 1 };
158 EXPECT_TRUE(SetWorldTransform(context->context(), &random_matrix)); 158 EXPECT_TRUE(SetWorldTransform(context->context(), &random_matrix));
159 EXPECT_TRUE(ModifyWorldTransform(context->context(), NULL, MWT_IDENTITY)); 159 EXPECT_TRUE(ModifyWorldTransform(context->context(), NULL, MWT_IDENTITY));
160 } 160 }
161 161
162 TEST_F(PrintingContextTest, PrintAll) { 162 TEST_F(PrintingContextTest, PrintAll) {
163 base::MessageLoop message_loop; 163 base::MessageLoop message_loop;
164 if (IsTestCaseDisabled()) 164 if (IsTestCaseDisabled())
165 return; 165 return;
166 166
167 std::string dummy_locale; 167 std::string dummy_locale;
168 printing::PrintingContextWin context(dummy_locale); 168 PrintingContextWin context(dummy_locale);
169 context.SetPrintDialog(&PrintDlgExMock); 169 context.SetPrintDialog(&PrintDlgExMock);
170 context.AskUserForSettings( 170 context.AskUserForSettings(
171 NULL, 123, false, base::Bind(&PrintingContextTest::PrintSettingsCallback, 171 NULL, 123, false, base::Bind(&PrintingContextTest::PrintSettingsCallback,
172 base::Unretained(this))); 172 base::Unretained(this)));
173 EXPECT_EQ(printing::PrintingContext::OK, result()); 173 EXPECT_EQ(PrintingContext::OK, result());
174 printing::PrintSettings settings = context.settings(); 174 PrintSettings settings = context.settings();
175 EXPECT_EQ(settings.ranges().size(), 0); 175 EXPECT_EQ(settings.ranges().size(), 0);
176 } 176 }
177
178 } // namespace printing
OLDNEW
« no previous file with comments | « printing/printing_context_win.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698