Skip to content

Commit

Permalink
perf: Avoid screenshotting twice, and make sure to screenshot samples…
Browse files Browse the repository at this point in the history
… without a category
  • Loading branch information
Youssef1313 committed Mar 11, 2024
1 parent ad728fb commit 7574235
Showing 1 changed file with 38 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ public partial class SampleChooserViewModel

private Section _lastSection = Section.Library;
private readonly Stack<Section> _previousSections = new Stack<Section>();
private static readonly Microsoft.UI.Xaml.Media.SolidColorBrush _screenshotBackground =
new Microsoft.UI.Xaml.Media.SolidColorBrush(Microsoft.UI.Colors.White);

private readonly UnitTestDispatcherCompat _dispatcher;

Expand Down Expand Up @@ -248,49 +246,6 @@ private void OnContentVisibilityChanged()
IsAnyContentVisible = ContentVisibility;
}

private class SampleInfo
{
public SampleChooserCategory Category { get; set; }
public SampleChooserContent Sample { get; set; }

public bool Matches(string path)
{
var pathMembers = path.Split(new char[] { '.' });
return Matches(category: pathMembers.ElementAtOrDefault(0), sampleName: pathMembers.ElementAtOrDefault(1));
}

private bool Matches(string category, string sampleName)
{
return !category.IsNullOrEmpty() &&
Category.Category.Equals(category, StringComparison.OrdinalIgnoreCase) &&
(sampleName.IsNullOrEmpty() || (Sample?.ControlName?.Equals(sampleName, StringComparison.OrdinalIgnoreCase) ?? false));
}
}

// Targets can either be a Category or a Sample (formatted as [Category].[SampleName])
private const string _firstTargetToRun = "";

private static readonly string[] _targetsToSkip =
{
/*Will be fixed along with bug #29117 */
"GridView.GridViewEmptyGroups",
"GridView.GridViewGrouped",
"GridView.GridViewGroupedMaxRowsTwo",

/*Will be fixed along with bug #29132 */
"ListView.ListViewGrouped_ItemContainerStyleSelector",

/*Will be fixed along with bug #29134 */
"TimePicker.TimePickerSelector_Simple",

"ScrollViewer.ScrollViewer_Padding",

/* Will befixed along with bug #118190 */
"Animations.DoubleAnimation_TranslateX",
"Animations.DoubleAnimationUsingKeyFrames_TranslateX",
"Animations.EasingDoubleKeyFrame_CompositeTransform",
};

internal async Task RecordAllTests(CancellationToken ct, string screenShotPath = "", int totalGroups = 1, int currentGroupIndex = 0, Action doneAction = null)
{
try
Expand Down Expand Up @@ -333,30 +288,8 @@ private async Task RecordAllTestsInner(string folderName, int totalGroups, int c
var initialInactiveStats = Uno.UI.DataBinding.BinderReferenceHolder.GetInactiveViewReferencesStats();
var initialActiveStats = Uno.UI.DataBinding.BinderReferenceHolder.GetReferenceStats();
#endif
var testQuery = from category in _unfilteredCategories
from sample in category.SamplesContent
where !sample.IgnoreInSnapshotTests
// where sample.ControlName.Equals("GridViewVerticalGrouped")
select new SampleInfo
{
Category = category,
Sample = sample,
};

Debug.Assert(
_firstTargetToRun.IsNullOrEmpty() || testQuery.Any(testInfo => testInfo.Matches(_firstTargetToRun)),
"First target to run must be either a Category or a Sample that is present in the app."
);

Debug.Assert(
_targetsToSkip.Where(target => !target.IsNullOrWhiteSpace()).None(target => target.Equals(_firstTargetToRun, StringComparison.OrdinalIgnoreCase)),
"First test to run cannot be skipped"
);

var tests = testQuery
.SkipWhile(testInfo => !_firstTargetToRun.IsNullOrEmpty() && !testInfo.Matches(_firstTargetToRun))
.Where(testInfo => _targetsToSkip.None(testInfo.Matches))
.ToArray();

var tests = GetSampleChooserContentsForSnapshotTests().ToArray();

if (_log.IsEnabled(LogLevel.Debug))
{
Expand All @@ -381,7 +314,7 @@ from sample in category.SamplesContent
var activeStats = Uno.UI.DataBinding.BinderReferenceHolder.GetReferenceStats();
#endif

var fileName = $"{SanitizeScreenshotFileName(sample.Category.Category + "-" + sample.Sample.ControlName)}.png";
var fileName = $"{SanitizeScreenshotFileName(sample.ControlName)}.png";

try
{
Expand All @@ -396,9 +329,9 @@ from sample in category.SamplesContent

ShowNewSection(ct, Section.SamplesContent);

SelectedLibrarySample = sample.Sample;
SelectedLibrarySample = sample;

var content = await UpdateContent(ct, sample.Sample) as FrameworkElement;
var content = await UpdateContent(ct, sample) as FrameworkElement;

ContentPhone = content;

Expand Down Expand Up @@ -495,12 +428,7 @@ internal async Task OpenRuntimeTests(CancellationToken ct)
{
IsSplitVisible = false;

var testQuery = from category in _unfilteredCategories
from sample in category.SamplesContent
where sample.ControlType == typeof(SamplesApp.Samples.UnitTests.UnitTestsPage)
select sample;

var runtimeTests = testQuery.FirstOrDefault();
var runtimeTests = GetContent(typeof(SamplesApp.Samples.UnitTests.UnitTestsPage).GetTypeInfo());

if (runtimeTests == null)
{
Expand Down Expand Up @@ -771,6 +699,19 @@ private void RefreshSamples()
}
}

private IEnumerable<SampleChooserContent> GetSampleChooserContentsForSnapshotTests()
{
foreach (var sample in _allSamples)
{
var typeInfo = sample.GetTypeInfo();
var sampleAttribute = FindSampleAttribute(typeInfo);
if (sampleAttribute is { IgnoreInSnapshotTests: false })
{
yield return GetContent(typeInfo, sampleAttribute);
}
}
}

/// <summary>
/// This method retreives all the categories and sample contents associated with them throughout the app.
/// </summary>
Expand All @@ -788,23 +729,26 @@ orderby contentByCategory.Key.ToLower(CultureInfo.CurrentUICulture)
select new SampleChooserCategory(contentByCategory);

return categories.ToList();

SampleChooserContent GetContent(TypeInfo type, SampleAttribute attribute)
=> new SampleChooserContent
{
ControlName = attribute.Name ?? type.Name,
Categories = attribute.Categories?.Where(c => !string.IsNullOrWhiteSpace(c)).Any() ?? false
? attribute.Categories.Where(c => !string.IsNullOrWhiteSpace(c)).ToArray()
: new[] { type.Namespace.Split('.').Last().TrimStart("Windows_UI_Xaml").TrimStart("Windows_UI") },
ViewModelType = attribute.ViewModelType,
Description = attribute.Description,
ControlType = type.AsType(),
IgnoreInSnapshotTests = attribute.IgnoreInSnapshotTests,
IsManualTest = attribute.IsManualTest,
UsesFrame = attribute.UsesFrame
};
}

private static SampleChooserContent GetContent(TypeInfo type)
=> GetContent(type, FindSampleAttribute(type));

private static SampleChooserContent GetContent(TypeInfo type, SampleAttribute attribute)
=> new SampleChooserContent
{
ControlName = attribute.Name ?? type.Name,
Categories = attribute.Categories?.Where(c => !string.IsNullOrWhiteSpace(c)).Any() ?? false
? attribute.Categories.Where(c => !string.IsNullOrWhiteSpace(c)).ToArray()
: new[] { type.Namespace.Split('.').Last().TrimStart("Windows_UI_Xaml").TrimStart("Windows_UI") },
ViewModelType = attribute.ViewModelType,
Description = attribute.Description,
ControlType = type.AsType(),
IgnoreInSnapshotTests = attribute.IgnoreInSnapshotTests,
IsManualTest = attribute.IsManualTest,
UsesFrame = attribute.UsesFrame
};

private static IEnumerable<TypeInfo> FindDefinedAssemblies(Assembly assembly)
{
try
Expand Down Expand Up @@ -1066,6 +1010,7 @@ private Section ConvertSectionEnum(string value)

public string GetAllSamplesNames()
{
// TODO: This might not be returning samples without a category (i.e, attributed just with [Sample] without any arguments)
var q = from category in _unfilteredCategories
from test in category.SamplesContent
where !test.IgnoreInSnapshotTests && !test.IsManualTest
Expand Down

0 comments on commit 7574235

Please sign in to comment.