From 9d45d35b528f66d96333f9edde6fc75fbad7d883 Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Thu, 6 Oct 2022 22:15:13 -0700 Subject: [PATCH 1/4] Add basic test for PrintDocument.Print using the default print controller --- .../tests/Printing/PrintDocumentTests.cs | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Drawing.Common/tests/Printing/PrintDocumentTests.cs b/src/libraries/System.Drawing.Common/tests/Printing/PrintDocumentTests.cs index 64bff89aaa0134..a9c1348d86194e 100644 --- a/src/libraries/System.Drawing.Common/tests/Printing/PrintDocumentTests.cs +++ b/src/libraries/System.Drawing.Common/tests/Printing/PrintDocumentTests.cs @@ -23,11 +23,12 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // +using System.IO; using Xunit; namespace System.Drawing.Printing.Tests { - public class PrintDocumentTests + public class PrintDocumentTests : FileCleanupTestBase { private readonly PageSettings _pageSettings = new PageSettings() { @@ -184,6 +185,21 @@ public void EndPrint_SetValue_ReturnsExpected() } } + [ConditionalFact(nameof(CanPrintToPdf))] + public void Print_DefaultPrintController_Success() + { + string printFilePath = GetTestFilePath(); + using (var document = new PrintDocument()) + { + document.PrinterSettings.PrinterName = PrintToPdfPrinterName; + document.PrinterSettings.PrintFileName = printFilePath; + document.PrinterSettings.PrintToFile = true; + document.Print(); + } + + Assert.True(File.Exists(printFilePath)); + } + [ActiveIssue("https://github.com/dotnet/runtime/issues/26428")] [ConditionalFact(Helpers.AnyInstalledPrinters, Helpers.IsDrawingSupported)] public void PrintPage_SetValue_ReturnsExpected() @@ -253,6 +269,23 @@ private void AssertDefaultPageSettings(PageSettings pageSettings) Assert.True(pageSettings.PrinterSettings.IsDefaultPrinter); } + private const string PrintToPdfPrinterName = "Microsoft Print to PDF"; + private static bool CanPrintToPdf() + { + if (!PlatformDetection.IsWindows || !PlatformDetection.IsDrawingSupported) + return false; + + foreach (string name in PrinterSettings.InstalledPrinters) + { + if (name == PrintToPdfPrinterName) + { + return true; + } + } + + return false; + } + private class TestPrintController : PrintController { public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs e) From 1f1cdc6638ee36cbf85b70078fe07596476095d8 Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Fri, 7 Oct 2022 15:20:53 -0700 Subject: [PATCH 2/4] Check for EndPrint instead of file existence --- .../tests/Printing/PrintDocumentTests.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Drawing.Common/tests/Printing/PrintDocumentTests.cs b/src/libraries/System.Drawing.Common/tests/Printing/PrintDocumentTests.cs index a9c1348d86194e..7a96b12c432444 100644 --- a/src/libraries/System.Drawing.Common/tests/Printing/PrintDocumentTests.cs +++ b/src/libraries/System.Drawing.Common/tests/Printing/PrintDocumentTests.cs @@ -188,16 +188,21 @@ public void EndPrint_SetValue_ReturnsExpected() [ConditionalFact(nameof(CanPrintToPdf))] public void Print_DefaultPrintController_Success() { - string printFilePath = GetTestFilePath(); + bool endPrintCalled = false; + var endPrintHandler = new PrintEventHandler((sender, e) => endPrintCalled = true); using (var document = new PrintDocument()) { document.PrinterSettings.PrinterName = PrintToPdfPrinterName; - document.PrinterSettings.PrintFileName = printFilePath; + document.PrinterSettings.PrintFileName = GetTestFilePath(); document.PrinterSettings.PrintToFile = true; + document.EndPrint += endPrintHandler; document.Print(); + document.EndPrint -= endPrintHandler; } - Assert.True(File.Exists(printFilePath)); + // File may not have finished saving to disk when Print returns, + // so we check for EndPrint being called instead of file existence. + Assert.True(endPrintCalled); } [ActiveIssue("https://github.com/dotnet/runtime/issues/26428")] From eb3e2753a63ea71c68cdfe84e4c83391e5a62f0a Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Fri, 7 Oct 2022 15:23:41 -0700 Subject: [PATCH 3/4] Fix Debug.Assert placement in StandardPrintController --- .../src/System/Drawing/Printing/DefaultPrintController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/DefaultPrintController.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/DefaultPrintController.cs index 6999d9048e66cc..ee11c2d298a579 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/DefaultPrintController.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/DefaultPrintController.cs @@ -22,13 +22,13 @@ public class StandardPrintController : PrintController public override void OnStartPrint(PrintDocument document, PrintEventArgs e) { Debug.Assert(_dc == null && _graphics == null, "PrintController methods called in the wrong order?"); - Debug.Assert(_modeHandle != null); base.OnStartPrint(document, e); // the win32 methods below SuppressUnmanagedCodeAttributes so assertin on UnmanagedCodePermission is redundant if (!document.PrinterSettings.IsValid) throw new InvalidPrinterException(document.PrinterSettings); + Debug.Assert(_modeHandle != null); _dc = document.PrinterSettings.CreateDeviceContext(_modeHandle); Interop.Gdi32.DOCINFO info = new Interop.Gdi32.DOCINFO(); info.lpszDocName = document.DocumentName; From 2c298a61f4265dc99fdc68bd44447df780f76503 Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Fri, 7 Oct 2022 15:39:20 -0700 Subject: [PATCH 4/4] Update src/libraries/System.Drawing.Common/src/System/Drawing/Printing/DefaultPrintController.cs --- .../src/System/Drawing/Printing/DefaultPrintController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/DefaultPrintController.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/DefaultPrintController.cs index ee11c2d298a579..1a78f2e4ebe7e2 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/DefaultPrintController.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/DefaultPrintController.cs @@ -28,7 +28,7 @@ public override void OnStartPrint(PrintDocument document, PrintEventArgs e) if (!document.PrinterSettings.IsValid) throw new InvalidPrinterException(document.PrinterSettings); - Debug.Assert(_modeHandle != null); + Debug.Assert(_modeHandle != null, "_modeHandle should have been set by PrintController.OnStartPrint"); _dc = document.PrinterSettings.CreateDeviceContext(_modeHandle); Interop.Gdi32.DOCINFO info = new Interop.Gdi32.DOCINFO(); info.lpszDocName = document.DocumentName;