Skip to content

Commit

Permalink
Add basic test for PrintDocument.Print using the default print cont…
Browse files Browse the repository at this point in the history
…roller (#76752)
  • Loading branch information
elinor-fung authored Oct 10, 2022
1 parent 61687ab commit 43f7699
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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, "_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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -184,6 +185,26 @@ public void EndPrint_SetValue_ReturnsExpected()
}
}

[ConditionalFact(nameof(CanPrintToPdf))]
public void Print_DefaultPrintController_Success()
{
bool endPrintCalled = false;
var endPrintHandler = new PrintEventHandler((sender, e) => endPrintCalled = true);
using (var document = new PrintDocument())
{
document.PrinterSettings.PrinterName = PrintToPdfPrinterName;
document.PrinterSettings.PrintFileName = GetTestFilePath();
document.PrinterSettings.PrintToFile = true;
document.EndPrint += endPrintHandler;
document.Print();
document.EndPrint -= endPrintHandler;
}

// 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")]
[ConditionalFact(Helpers.AnyInstalledPrinters, Helpers.IsDrawingSupported)]
public void PrintPage_SetValue_ReturnsExpected()
Expand Down Expand Up @@ -253,6 +274,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)
Expand Down

0 comments on commit 43f7699

Please sign in to comment.