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 code coverage for MonthCalendar #11217

Merged
merged 4 commits into from
Apr 23, 2024
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
@@ -0,0 +1,51 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using static System.Windows.Forms.MonthCalendar;
using Point = System.Drawing.Point;

namespace System.Windows.Forms.Tests;

public class MonthCalendarHitTestInfoTests
{
[WinFormsTheory]
[InlineData(HitArea.Date, true, "2022/01/01")]
[InlineData(HitArea.WeekNumbers, true, "2022/01/01")]
[InlineData(HitArea.Nowhere, false, null)]
public void HitTestInfo_Constructor_SetsPropertiesCorrectly(HitArea hitArea, bool hasDateTime, string dateTimeStr)
{
Point point = new(5, 5);
DateTime? time = hasDateTime ? DateTime.Parse(dateTimeStr) : null;

HitTestInfo hitTestInfo = time.HasValue
? new HitTestInfo(point, hitArea, time.Value)
: new HitTestInfo(point, hitArea);

hitTestInfo.Point.Should().Be(point);
hitTestInfo.HitArea.Should().Be(hitArea);
hitTestInfo.Time.Should().Be(time ?? DateTime.MinValue);
}

[WinFormsTheory]
[InlineData(HitArea.Date, true)]
[InlineData(HitArea.WeekNumbers, true)]
[InlineData(HitArea.Nowhere, false)]
public void HitTestInfo_HitAreaHasValidDateTime_ReturnsExpectedResult(HitArea hitArea, bool expectedResult)
{
bool result = HitTestInfo.HitAreaHasValidDateTime(hitArea);

result.Should().Be(expectedResult);
}

[WinFormsTheory]
[InlineData(null)]
public void HitTestInfo_Constructor_WithNullDateTime_ThrowsException(DateTime? time)
{
Point point = new(5, 5);
HitArea hitArea = HitArea.Date;

Action action = () => new HitTestInfo(point, hitArea, time!.Value);

action.Should().Throw<InvalidOperationException>();
}
}
Zheng-Li01 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Drawing;
using System.Globalization;
using System.Windows.Forms.TestUtilities;
using static System.Windows.Forms.MonthCalendar;
using Point = System.Drawing.Point;
using Size = System.Drawing.Size;

Expand Down Expand Up @@ -4157,7 +4158,7 @@ public void MonthCalendar_SetSelectionRange_DateGreaterThanMaxDate_ThrowsArgumen

public static IEnumerable<object[]> MonthCalendar_FillMonthDayStates_ReturnsExpected_TestData()
{
// This test set of dates is designed for a specifict test case:
// This test set of dates is designed for a specific test case:
// when a calendar has 12 fully visible months + 2 not fully visible.
// This test calendar has (08/29/2021 - 09/10/2022) dates range.

Expand Down Expand Up @@ -4253,6 +4254,203 @@ public void SelectionRange_CopyConstructor_CopiesStartAndEndDatesCorrectly()
copiedRange.End.Should().Be(endDate);
}

[WinFormsFact]
public void GetFocused_Returns_FocusedCellAccessibleObject()
{
using MonthCalendar monthCalendar = new();

MonthCalendarAccessibleObject accessibleObject = new(monthCalendar);
var result = accessibleObject.GetFocused();

result.Should().Be(accessibleObject.FocusedCell);
}

[WinFormsFact]
public void MonthCalendarAccessibleObject_Help_ReturnsExpected()
{
using MonthCalendar monthCalendar = new();

monthCalendar.CreateControl();
MonthCalendarAccessibleObject accessibleObject = new(monthCalendar);
string expectedHelp = "MonthCalendar(Control)";

accessibleObject.Help.Should().Be(expectedHelp);
}

[WinFormsTheory]
[InlineData("42")]
public void CalendarWeekNumberCellAccessibleObject_Name_ReturnsExpected(string weekNumber)
{
using MonthCalendar monthCalendar = new();

monthCalendar.CreateControl();
MonthCalendarAccessibleObject monthCalendarAccessibleObject = new(monthCalendar);
CalendarAccessibleObject calendarAccessibleObject = new(monthCalendarAccessibleObject, 1, "Main Calendar");
CalendarBodyAccessibleObject calendarBodyAccessibleObject = new(calendarAccessibleObject, monthCalendarAccessibleObject, 1);
CalendarRowAccessibleObject calendarRowAccessibleObject = new(calendarBodyAccessibleObject, monthCalendarAccessibleObject, 1, 1);

CalendarWeekNumberCellAccessibleObject accessibleObject = new(
calendarRowAccessibleObject,
calendarBodyAccessibleObject,
monthCalendarAccessibleObject,
calendarIndex: 0,
rowIndex: 0,
columnIndex: 0,
weekNumber);

string name = accessibleObject.Name;

name.Should().Be($"Week {weekNumber}");
}

[WinFormsFact]
public void CalendarTodayLinkAccessibleObject_Bounds_ReturnsExpected()
{
using MonthCalendar monthCalendar = new();

monthCalendar.CreateControl();
var controlAccessibleObject = (MonthCalendarAccessibleObject)monthCalendar.AccessibilityObject;
CalendarTodayLinkAccessibleObject todayLinkAccessibleObject = new(controlAccessibleObject);
Rectangle actual = todayLinkAccessibleObject.Bounds;
Rectangle expected = controlAccessibleObject.GetCalendarPartRectangle(MCGRIDINFO_PART.MCGIP_FOOTER);

actual.Should().Be(expected);
}

[WinFormsFact]
public void CalendarPreviousButtonAccessibleObject_Bounds_ReturnsExpected()
{
using MonthCalendar monthCalendar = new();

monthCalendar.CreateControl();
var controlAccessibleObject = (MonthCalendarAccessibleObject)monthCalendar.AccessibilityObject;
CalendarPreviousButtonAccessibleObject previousButtonAccessibleObject = new(controlAccessibleObject);
Rectangle bounds = previousButtonAccessibleObject.Bounds;
Rectangle expectedBounds = new(13, 42, 16, 16);

bounds.Should().Be(expectedBounds);
}

private (CalendarRowAccessibleObject, CalendarCellAccessibleObject) CreateCalendarObjects(MonthCalendar control, int calendarIndex = 0, int rowIndex = 0, int columnIndex = 0)
{
MonthCalendarAccessibleObject controlAccessibleObject = (MonthCalendarAccessibleObject)control.AccessibilityObject;
CalendarAccessibleObject calendarAccessibleObject = new(controlAccessibleObject, calendarIndex, "Main Calendar");
CalendarBodyAccessibleObject bodyAccessibleObject = new(calendarAccessibleObject, controlAccessibleObject, calendarIndex);
CalendarRowAccessibleObject rowAccessibleObject = new(bodyAccessibleObject, controlAccessibleObject, calendarIndex, rowIndex);
CalendarCellAccessibleObject cellAccessibleObject = new(rowAccessibleObject, bodyAccessibleObject, controlAccessibleObject, calendarIndex, rowIndex, columnIndex);

return (rowAccessibleObject, cellAccessibleObject);
}

[WinFormsFact]
public void CalendarRowAccessibleObject_Bounds_ReturnsExpected()
{
using MonthCalendar monthCalendar = new();

monthCalendar.CreateControl();
var controlAccessibleObject = (MonthCalendarAccessibleObject)monthCalendar.AccessibilityObject;
var (rowAccessibleObject, _) = CreateCalendarObjects(monthCalendar);
Rectangle actual = rowAccessibleObject.Bounds;
Rectangle expected = controlAccessibleObject.GetCalendarPartRectangle(MCGRIDINFO_PART.MCGIP_CALENDARROW, 0, 0);

actual.Should().Be(expected);
}

[WinFormsTheory]
[InlineData(0, "Week 23, Monday")]
[InlineData(1, null)]
public void CalendarCellAccessibleObject_Description_ReturnsExpected(int view, string expected)
{
using MonthCalendar monthCalendar = new();

monthCalendar.CreateControl();
monthCalendar.FirstDayOfWeek = Day.Monday;
monthCalendar.SelectionStart = new DateTime(2021, 6, 16);
PInvoke.SendMessage(monthCalendar, PInvoke.MCM_SETCURRENTVIEW, 0, view);

var (_, cellAccessibleObject) = CreateCalendarObjects(monthCalendar);

cellAccessibleObject.Description.Should().Be(expected);
}

[WinFormsFact]
public void CalendarCellAccessibleObject_Select_SetsSelectionRange()
{
using MonthCalendar monthCalendar = new();

monthCalendar.CreateControl();
var (_, cellAccessibleObject) = CreateCalendarObjects(monthCalendar);

DateTime startDate = new(2022, 10, 1);
DateTime endDate = new(2022, 10, 7);
cellAccessibleObject.TestAccessor().Dynamic._dateRange = new SelectionRange(startDate, endDate);

cellAccessibleObject.Select(AccessibleSelection.TakeSelection);

monthCalendar.SelectionStart.Should().Be(startDate);
monthCalendar.SelectionEnd.Should().Be(endDate);
}

[WinFormsTheory]
[InlineData(AccessibleStates.Focusable | AccessibleStates.Selectable | AccessibleStates.Selected)]
public void CalendarCellAccessibleObject_State_ReturnsExpected(AccessibleStates expectedState)
{
using MonthCalendar monthCalendar = new();

monthCalendar.CreateControl();
var controlAccessibleObject = (MonthCalendarAccessibleObject)monthCalendar.AccessibilityObject;
var (_, cellAccessibleObject) = CreateCalendarObjects(monthCalendar);

bool shouldFocus = expectedState.HasFlag(AccessibleStates.Focused);
bool selectExactRange = expectedState.HasFlag(AccessibleStates.Selected);

if (shouldFocus)
{
monthCalendar.Focus();
}

if (selectExactRange)
{
monthCalendar.SetSelectionRange(cellAccessibleObject.DateRange.Start, cellAccessibleObject.DateRange.End);
}
else
{
monthCalendar.SetSelectionRange(cellAccessibleObject.DateRange.Start.AddDays(-1), cellAccessibleObject.DateRange.End.AddDays(1));
}

cellAccessibleObject.State.Should().Be(expectedState);
}

[WinFormsTheory]
[InlineData(AccessibleSelection.AddSelection)]
[InlineData(AccessibleSelection.RemoveSelection)]
[InlineData(AccessibleSelection.TakeSelection)]
public void CalendarCellAccessibleObject_Select_Invoke_SetsSelectionRange(AccessibleSelection selectionFlag)
{
using MonthCalendar monthCalendar = new();
monthCalendar.CreateControl();
var (_, cellAccessibleObject) = CreateCalendarObjects(monthCalendar);

DateTime expectedStart = cellAccessibleObject.DateRange.Start;
DateTime expectedEnd = cellAccessibleObject.DateRange.End;

cellAccessibleObject.Select(selectionFlag);
monthCalendar.SelectionStart.Should().Be(expectedStart);
monthCalendar.SelectionEnd.Should().Be(expectedEnd);
}

[WinFormsFact]
public void CalendarCellAccessibleObject_Role_ReturnsExpected()
{
using MonthCalendar monthCalendar = new();

monthCalendar.CreateControl();
var controlAccessibleObject = (MonthCalendarAccessibleObject)monthCalendar.AccessibilityObject;
var (_, cellAccessibleObject) = CreateCalendarObjects(monthCalendar);

cellAccessibleObject.Role.Should().Be(AccessibleRole.Cell);
}

private class SubMonthCalendar : MonthCalendar
{
public new bool CanEnableIme => base.CanEnableIme;
Expand Down