Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
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;

public static PageSize A4 { get; } = new PageSize { Width = 21.0, Height = 29.7 }; // cm
public static PageSize Legal { get; } = new PageSize { Width = 21.59, Height = 35.56 }; // cm
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
}

/// <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));
}


/// <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)
{
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()
{
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));
}

[Test]
public void SettingPageSizeToNullThrowsException()
{
var options = new PrintOptions();
Assert.That(() => options.SetPageSize(null), Throws.InstanceOf<ArgumentNullException>());
}

}
}