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

Side by Side Diff: printing/printing_context_win_unittest.cc

Issue 11530005: Wrapers for PRINTER_INFO_* and DRIVER_INFO_*. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years 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 "printing/backend/printing_info_win.h"
13 #include "printing/printing_test.h" 14 #include "printing/printing_test.h"
14 #include "printing/printing_context.h" 15 #include "printing/printing_context.h"
15 #include "printing/printing_context_win.h" 16 #include "printing/printing_context_win.h"
16 #include "printing/print_settings.h" 17 #include "printing/print_settings.h"
17 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
18 19
19 // This test is automatically disabled if no printer is available. 20 // This test is automatically disabled if no printer is available.
20 class PrintingContextTest : public PrintingTest<testing::Test> { 21 class PrintingContextTest : public PrintingTest<testing::Test> {
21 public: 22 public:
22 void PrintSettingsCallback(printing::PrintingContext::Result result) { 23 void PrintSettingsCallback(printing::PrintingContext::Result result) {
(...skipping 20 matching lines...) Expand all
43 lppd->lpPageRanges[0].nFromPage = 1; 44 lppd->lpPageRanges[0].nFromPage = 1;
44 lppd->lpPageRanges[0].nToPage = 5; 45 lppd->lpPageRanges[0].nToPage = 5;
45 46
46 // Painful paperwork. 47 // Painful paperwork.
47 std::wstring printer_name = PrintingContextTest::GetDefaultPrinter(); 48 std::wstring printer_name = PrintingContextTest::GetDefaultPrinter();
48 HANDLE printer; 49 HANDLE printer;
49 if (!OpenPrinter(const_cast<wchar_t*>(printer_name.c_str()), &printer, NULL)) 50 if (!OpenPrinter(const_cast<wchar_t*>(printer_name.c_str()), &printer, NULL))
50 return E_FAIL; 51 return E_FAIL;
51 52
52 scoped_array<uint8> buffer; 53 scoped_array<uint8> buffer;
53 DEVMODE* dev_mode = NULL; 54 const DEVMODE* dev_mode = NULL;
54 PRINTER_INFO_2* info_2 = NULL;
55 HRESULT result = S_OK; 55 HRESULT result = S_OK;
56 lppd->hDC = NULL; 56 lppd->hDC = NULL;
57 lppd->hDevMode = NULL; 57 lppd->hDevMode = NULL;
58 lppd->hDevNames = NULL; 58 lppd->hDevNames = NULL;
59 59
60 printing::PrintingContextWin::GetPrinterHelper(printer, 2, &buffer); 60 printing::PrinterInfo2 info_2;
61 if (buffer.get()) { 61 if (info_2.Init(printer)) {
62 info_2 = reinterpret_cast<PRINTER_INFO_2*>(buffer.get()); 62 dev_mode = info_2.get()->pDevMode;
63 if (info_2->pDevMode != NULL)
64 dev_mode = info_2->pDevMode;
65 } 63 }
66 if (!dev_mode) { 64 if (!dev_mode) {
67 result = E_FAIL; 65 result = E_FAIL;
68 goto Cleanup; 66 goto Cleanup;
69 } 67 }
70 68
71 if (!printing::PrintingContextWin::AllocateContext(printer_name, dev_mode, 69 if (!printing::PrintingContextWin::AllocateContext(printer_name, dev_mode,
72 &lppd->hDC)) { 70 &lppd->hDC)) {
73 result = E_FAIL; 71 result = E_FAIL;
74 goto Cleanup; 72 goto Cleanup;
75 } 73 }
76 74
77 size_t dev_mode_size = dev_mode->dmSize + dev_mode->dmDriverExtra; 75 size_t dev_mode_size = dev_mode->dmSize + dev_mode->dmDriverExtra;
78 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, dev_mode_size); 76 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, dev_mode_size);
79 if (!lppd->hDevMode) { 77 if (!lppd->hDevMode) {
80 result = E_FAIL; 78 result = E_FAIL;
81 goto Cleanup; 79 goto Cleanup;
82 } 80 }
83 void* dev_mode_ptr = GlobalLock(lppd->hDevMode); 81 void* dev_mode_ptr = GlobalLock(lppd->hDevMode);
84 if (!dev_mode_ptr) { 82 if (!dev_mode_ptr) {
85 result = E_FAIL; 83 result = E_FAIL;
86 goto Cleanup; 84 goto Cleanup;
87 } 85 }
88 memcpy(dev_mode_ptr, dev_mode, dev_mode_size); 86 memcpy(dev_mode_ptr, dev_mode, dev_mode_size);
89 GlobalUnlock(lppd->hDevMode); 87 GlobalUnlock(lppd->hDevMode);
90 dev_mode_ptr = NULL; 88 dev_mode_ptr = NULL;
91 89
92 size_t driver_size = 2 + sizeof(wchar_t) * lstrlen(info_2->pDriverName); 90 size_t driver_size = 2 + sizeof(wchar_t) * lstrlen(info_2.get()->pDriverName);
93 size_t printer_size = 2 + sizeof(wchar_t) * lstrlen(info_2->pPrinterName); 91 size_t printer_size = 2 + sizeof(wchar_t) *
94 size_t port_size = 2 + sizeof(wchar_t) * lstrlen(info_2->pPortName); 92 lstrlen(info_2.get()->pPrinterName);
93 size_t port_size = 2 + sizeof(wchar_t) * lstrlen(info_2.get()->pPortName);
95 size_t dev_names_size = sizeof(DEVNAMES) + driver_size + printer_size + 94 size_t dev_names_size = sizeof(DEVNAMES) + driver_size + printer_size +
96 port_size; 95 port_size;
97 lppd->hDevNames = GlobalAlloc(GHND, dev_names_size); 96 lppd->hDevNames = GlobalAlloc(GHND, dev_names_size);
98 if (!lppd->hDevNames) { 97 if (!lppd->hDevNames) {
99 result = E_FAIL; 98 result = E_FAIL;
100 goto Cleanup; 99 goto Cleanup;
101 } 100 }
102 void* dev_names_ptr = GlobalLock(lppd->hDevNames); 101 void* dev_names_ptr = GlobalLock(lppd->hDevNames);
103 if (!dev_names_ptr) { 102 if (!dev_names_ptr) {
104 result = E_FAIL; 103 result = E_FAIL;
105 goto Cleanup; 104 goto Cleanup;
106 } 105 }
107 DEVNAMES* dev_names = reinterpret_cast<DEVNAMES*>(dev_names_ptr); 106 DEVNAMES* dev_names = reinterpret_cast<DEVNAMES*>(dev_names_ptr);
108 dev_names->wDefault = 1; 107 dev_names->wDefault = 1;
109 dev_names->wDriverOffset = sizeof(DEVNAMES); 108 dev_names->wDriverOffset = sizeof(DEVNAMES);
110 memcpy(reinterpret_cast<uint8*>(dev_names_ptr) + dev_names->wDriverOffset, 109 memcpy(reinterpret_cast<uint8*>(dev_names_ptr) + dev_names->wDriverOffset,
111 info_2->pDriverName, driver_size); 110 info_2.get()->pDriverName, driver_size);
112 dev_names->wDeviceOffset = dev_names->wDriverOffset + driver_size; 111 dev_names->wDeviceOffset = dev_names->wDriverOffset + driver_size;
113 memcpy(reinterpret_cast<uint8*>(dev_names_ptr) + dev_names->wDeviceOffset, 112 memcpy(reinterpret_cast<uint8*>(dev_names_ptr) + dev_names->wDeviceOffset,
114 info_2->pPrinterName, printer_size); 113 info_2.get()->pPrinterName, printer_size);
115 dev_names->wOutputOffset = dev_names->wDeviceOffset + printer_size; 114 dev_names->wOutputOffset = dev_names->wDeviceOffset + printer_size;
116 memcpy(reinterpret_cast<uint8*>(dev_names_ptr) + dev_names->wOutputOffset, 115 memcpy(reinterpret_cast<uint8*>(dev_names_ptr) + dev_names->wOutputOffset,
117 info_2->pPortName, port_size); 116 info_2.get()->pPortName, port_size);
118 GlobalUnlock(lppd->hDevNames); 117 GlobalUnlock(lppd->hDevNames);
119 dev_names_ptr = NULL; 118 dev_names_ptr = NULL;
120 119
121 Cleanup: 120 Cleanup:
122 // Note: This section does proper deallocation/free of DC/global handles. We 121 // Note: This section does proper deallocation/free of DC/global handles. We
123 // did not use ScopedHGlobal or ScopedHandle because they did not 122 // did not use ScopedHGlobal or ScopedHandle because they did not
124 // perform what we need. Goto's are used based on Windows programming 123 // perform what we need. Goto's are used based on Windows programming
125 // idiom, to avoid deeply nested if's, and try-catch-finally is not 124 // idiom, to avoid deeply nested if's, and try-catch-finally is not
126 // allowed in Chromium. 125 // allowed in Chromium.
127 if (FAILED(result)) { 126 if (FAILED(result)) {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 std::string dummy_locale; 165 std::string dummy_locale;
167 printing::PrintingContextWin context(dummy_locale); 166 printing::PrintingContextWin context(dummy_locale);
168 context.SetPrintDialog(&PrintDlgExMock); 167 context.SetPrintDialog(&PrintDlgExMock);
169 context.AskUserForSettings( 168 context.AskUserForSettings(
170 NULL, 123, false, base::Bind(&PrintingContextTest::PrintSettingsCallback, 169 NULL, 123, false, base::Bind(&PrintingContextTest::PrintSettingsCallback,
171 base::Unretained(this))); 170 base::Unretained(this)));
172 EXPECT_EQ(printing::PrintingContext::OK, result()); 171 EXPECT_EQ(printing::PrintingContext::OK, result());
173 printing::PrintSettings settings = context.settings(); 172 printing::PrintSettings settings = context.settings();
174 EXPECT_EQ(settings.ranges.size(), 0); 173 EXPECT_EQ(settings.ranges.size(), 0);
175 } 174 }
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