From 7e7c38708d2915f7cf00dd70664961af5614df1d Mon Sep 17 00:00:00 2001 From: Jeremy Kuhne Date: Mon, 28 Apr 2025 11:06:42 -0700 Subject: [PATCH 1/2] Fix Color value in PageSettings This was inadvertently flipped when changing to CsWin32. Fixes #13367 --- .../System/Drawing/Printing/PageSettings.cs | 2 +- src/System.Drawing.Common/tests/Helpers.cs | 42 +++++++++++++++++++ .../Drawing/Printing/PageSettingsTests.cs | 17 ++++++++ .../Drawing/Printing/PrintDocumentTests.cs | 33 ++------------- 4 files changed, 63 insertions(+), 31 deletions(-) diff --git a/src/System.Drawing.Common/src/System/Drawing/Printing/PageSettings.cs b/src/System.Drawing.Common/src/System/Drawing/Printing/PageSettings.cs index eaa9d765a8f..cbf8f47d02f 100644 --- a/src/System.Drawing.Common/src/System/Drawing/Printing/PageSettings.cs +++ b/src/System.Drawing.Common/src/System/Drawing/Printing/PageSettings.cs @@ -53,7 +53,7 @@ public Rectangle Bounds public bool Color { get => _color.IsDefault - ? _printerSettings.GetModeField(ModeField.Color, (short)DEVMODE_COLOR.DMCOLOR_MONOCHROME) == (short)DEVMODE_COLOR.DMCOLOR_MONOCHROME + ? _printerSettings.GetModeField(ModeField.Color, (short)DEVMODE_COLOR.DMCOLOR_MONOCHROME) == (short)DEVMODE_COLOR.DMCOLOR_COLOR : (bool)_color; set => _color = value; } diff --git a/src/System.Drawing.Common/tests/Helpers.cs b/src/System.Drawing.Common/tests/Helpers.cs index 89bcdddbae5..23cb91e7442 100644 --- a/src/System.Drawing.Common/tests/Helpers.cs +++ b/src/System.Drawing.Common/tests/Helpers.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics.CodeAnalysis; using System.Drawing.Printing; using System.Text; using Windows.Win32; @@ -21,6 +22,47 @@ public unsafe static class Helpers public static bool AreAnyPrintersInstalled() => s_anyInstalledPrinters; + private const string PrintToPdfPrinterName = "Microsoft Print to PDF"; + + /// + /// Checks if PDF printing is supported by verifying installed printers. + /// + /// if a PDF printer is installed; otherwise, . + public static bool CanPrintToPdf() + { + foreach (string name in InstalledPrinters) + { + if (name.StartsWith(PrintToPdfPrinterName, StringComparison.Ordinal)) + { + return true; + } + } + + return false; + } + + /// + /// Attempts to get the name of the PDF printer installed on the system. + /// + /// + /// When this method returns, contains the name of the PDF printer if found; otherwise, . + /// + /// if a PDF printer is found; otherwise, . + public static bool TryGetPdfPrinterName([NotNullWhen(true)] out string? printerName) + { + foreach (string name in InstalledPrinters) + { + if (name.StartsWith(PrintToPdfPrinterName, StringComparison.Ordinal)) + { + printerName = name; + return true; + } + } + + printerName = null; + return false; + } + public static string GetTestBitmapPath(string fileName) => GetTestPath("bitmaps", fileName); public static string GetTestFontPath(string fileName) => GetTestPath("fonts", fileName); public static string GetTestColorProfilePath(string fileName) => GetTestPath("colorProfiles", fileName); diff --git a/src/System.Drawing.Common/tests/System/Drawing/Printing/PageSettingsTests.cs b/src/System.Drawing.Common/tests/System/Drawing/Printing/PageSettingsTests.cs index e14c72f99ec..5a583a1e914 100644 --- a/src/System.Drawing.Common/tests/System/Drawing/Printing/PageSettingsTests.cs +++ b/src/System.Drawing.Common/tests/System/Drawing/Printing/PageSettingsTests.cs @@ -63,4 +63,21 @@ public void Clone_Success() Assert.Equal(ps.PaperSource.Kind, clone.PaperSource.Kind); Assert.Equal(ps.PaperSource.SourceName, clone.PaperSource.SourceName); } + + [ConditionalFact(Helpers.AnyInstalledPrinters)] + public void PrintToPDF_DefaultPageSettings_IsColor() + { + // Regression test for https://github.com/dotnet/winforms/issues/13367 + if (!Helpers.TryGetPdfPrinterName(out string? printerName)) + { + return; + } + + PrinterSettings printerSettings = new() + { + PrinterName = printerName + }; + + printerSettings.DefaultPageSettings.Color.Should().BeTrue("PDF printer should support color printing."); + } } diff --git a/src/System.Drawing.Common/tests/System/Drawing/Printing/PrintDocumentTests.cs b/src/System.Drawing.Common/tests/System/Drawing/Printing/PrintDocumentTests.cs index edacdcb97d5..9078f524fae 100644 --- a/src/System.Drawing.Common/tests/System/Drawing/Printing/PrintDocumentTests.cs +++ b/src/System.Drawing.Common/tests/System/Drawing/Printing/PrintDocumentTests.cs @@ -160,10 +160,10 @@ public void EndPrint_SetValue_ReturnsExpected() Assert.False(flag); } - [ConditionalFact(nameof(CanPrintToPdf))] + [ConditionalFact(nameof(Helpers.CanPrintToPdf))] public void Print_DefaultPrintController_Success() { - if (!CanPrintToPdf()) + if (!Helpers.TryGetPdfPrinterName(out string? printerName)) { return; } @@ -172,7 +172,7 @@ public void Print_DefaultPrintController_Success() PrintEventHandler endPrintHandler = new((sender, e) => endPrintCalled = true); using (PrintDocument document = new()) { - document.PrinterSettings.PrinterName = GetPdfPrinterName(); + document.PrinterSettings.PrinterName = printerName; document.PrinterSettings.PrintFileName = GetTestFilePath(); document.PrinterSettings.PrintToFile = true; document.EndPrint += endPrintHandler; @@ -247,33 +247,6 @@ private void AssertDefaultPageSettings(PageSettings pageSettings) Assert.True(pageSettings.PrinterSettings.IsDefaultPrinter); } - private const string PrintToPdfPrinterName = "Microsoft Print to PDF"; - private static bool CanPrintToPdf() - { - foreach (string name in Helpers.InstalledPrinters) - { - if (name.StartsWith(PrintToPdfPrinterName, StringComparison.Ordinal)) - { - return true; - } - } - - return false; - } - - private static string GetPdfPrinterName() - { - foreach (string name in Helpers.InstalledPrinters) - { - if (name.StartsWith(PrintToPdfPrinterName, StringComparison.Ordinal)) - { - return name; - } - } - - throw new InvalidOperationException("No PDF printer installed"); - } - private class TestPrintController : PrintController { public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs e) From a1cdb5a95ba7d42158155c901f3d44cafbf3b3a6 Mon Sep 17 00:00:00 2001 From: Jeremy Kuhne Date: Mon, 28 Apr 2025 13:23:11 -0700 Subject: [PATCH 2/2] Fix test attribute --- .../tests/System/Drawing/Printing/PrintDocumentTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Drawing.Common/tests/System/Drawing/Printing/PrintDocumentTests.cs b/src/System.Drawing.Common/tests/System/Drawing/Printing/PrintDocumentTests.cs index 9078f524fae..5a082482e2a 100644 --- a/src/System.Drawing.Common/tests/System/Drawing/Printing/PrintDocumentTests.cs +++ b/src/System.Drawing.Common/tests/System/Drawing/Printing/PrintDocumentTests.cs @@ -160,7 +160,7 @@ public void EndPrint_SetValue_ReturnsExpected() Assert.False(flag); } - [ConditionalFact(nameof(Helpers.CanPrintToPdf))] + [ConditionalFact(typeof(Helpers), nameof(Helpers.CanPrintToPdf))] public void Print_DefaultPrintController_Success() { if (!Helpers.TryGetPdfPrinterName(out string? printerName))