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

[dotnet] Enhance PrintOptions class to support for predefined and cus… #15144

Merged
merged 13 commits into from
Jan 26, 2025
Merged
29 changes: 28 additions & 1 deletion dotnet/src/webdriver/PrintOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,24 @@ public class PrintOptions
private const double DefaultPageHeight = 21.59;
private const double DefaultPageWidth = 27.94;
private const double CentimetersPerInch = 2.54;

yvsvarma marked this conversation as resolved.
Show resolved Hide resolved
public static PageSize A4 { get; } = new PageSize { Width = 21.0, Height = 29.7 }; // cm
yvsvarma marked this conversation as resolved.
Show resolved Hide resolved
yvsvarma marked this conversation as resolved.
Show resolved Hide resolved
public static PageSize Legal { get; } = new PageSize { Width = 21.59, Height = 35.56 }; // cm
yvsvarma marked this conversation as resolved.
Show resolved Hide resolved
public static PageSize Letter { get; } = new PageSize { Width = 21.59, Height = 27.94 }; // cm
public static PageSize Tabloid { get; } = new PageSize { Width = 27.94, Height = 43.18 }; // cm
private double scale = 1.0;
private PageSize pageSize = new PageSize();
private Margins margins = new Margins();
private readonly HashSet<object> pageRanges = new HashSet<object>();

/// <summary>
/// Initializes a new instance of the <see cref="PrintOptions"/> class with default values.
/// Default page size is set to A4.
/// </summary>
public PrintOptions()
{
this.PageDimensions = A4; // Default to A4 page size
yvsvarma marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
/// Gets or sets the orientation of the pages in the printed document.
/// </summary>
Expand Down Expand Up @@ -99,6 +111,21 @@ public PageSize PageDimensions
set => this.pageSize = value ?? throw new ArgumentNullException(nameof(value));
}


yvsvarma marked this conversation as resolved.
Show resolved Hide resolved
/// <summary>
/// Sets the page size to a predefined or custom size.
/// </summary>
/// <param name="pageSize">The page size to set.</param>
/// <exception cref="ArgumentNullException">Thrown if pageSize is null.</exception>
public void SetPageSize(PageSize pageSize)
yvsvarma marked this conversation as resolved.
Show resolved Hide resolved
{
if (pageSize == null)
{
throw new ArgumentNullException(nameof(pageSize), "Page size cannot be null.");
}
this.PageDimensions = pageSize;
}

/// <summary>
/// Gets or sets the margins for each page in the doucment.
/// </summary>
Expand Down
51 changes: 51 additions & 0 deletions dotnet/test/common/PrintTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,56 @@ public void MarginsCannotHaveNegativeValues()
Assert.That(() => new PrintOptions.Margins { Left = -1 }, Throws.TypeOf<ArgumentOutOfRangeException>());
Assert.That(() => new PrintOptions.Margins { Right = -1 }, Throws.TypeOf<ArgumentOutOfRangeException>());
}

[Test]
public void DefaultPageSizeIsA4()
yvsvarma marked this conversation as resolved.
Show resolved Hide resolved
{
var options = new PrintOptions();

Assert.That(options.PageDimensions.Width, Is.EqualTo(PrintOptions.A4.Width));
Assert.That(options.PageDimensions.Height, Is.EqualTo(PrintOptions.A4.Height));
}

[Test]
public void CanSetPredefinedPageSizes()
{
var options = new PrintOptions();

options.SetPageSize(PrintOptions.A4);
Assert.That(options.PageDimensions.Width, Is.EqualTo(PrintOptions.A4.Width));
Assert.That(options.PageDimensions.Height, Is.EqualTo(PrintOptions.A4.Height));

options.SetPageSize(PrintOptions.Legal);
Assert.That(options.PageDimensions.Width, Is.EqualTo(PrintOptions.Legal.Width));
Assert.That(options.PageDimensions.Height, Is.EqualTo(PrintOptions.Legal.Height));

options.SetPageSize(PrintOptions.Letter);
Assert.That(options.PageDimensions.Width, Is.EqualTo(PrintOptions.Letter.Width));
Assert.That(options.PageDimensions.Height, Is.EqualTo(PrintOptions.Letter.Height));

options.SetPageSize(PrintOptions.Tabloid);
Assert.That(options.PageDimensions.Width, Is.EqualTo(PrintOptions.Tabloid.Width));
Assert.That(options.PageDimensions.Height, Is.EqualTo(PrintOptions.Tabloid.Height));
}

[Test]
public void CanSetCustomPageSize()
{
var options = new PrintOptions();
var customPageSize = new PrintOptions.PageSize { Width = 25.0, Height = 30.0 };

options.SetPageSize(customPageSize);

Assert.That(options.PageDimensions.Width, Is.EqualTo(25.0));
Assert.That(options.PageDimensions.Height, Is.EqualTo(30.0));
}

yvsvarma marked this conversation as resolved.
Show resolved Hide resolved
[Test]
public void SettingPageSizeToNullThrowsException()
{
var options = new PrintOptions();
Assert.That(() => options.SetPageSize(null), Throws.InstanceOf<ArgumentNullException>());
}

}
}