Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic test for PrintDocument.Print using the default print controller #76752

Merged
merged 4 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
elinor-fung marked this conversation as resolved.
Show resolved Hide resolved
// 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