Skip to content

Commit

Permalink
Merge pull request #6002 from unoplatform/dev/cdb/ci-fix
Browse files Browse the repository at this point in the history
fix(test): Ensure `[ActivePlatform]` in UI Tests works with NUnit's `[TestCase]`
  • Loading branch information
jeromelaban authored May 13, 2021
2 parents e1f887c + d4ab54c commit d551c61
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 20 deletions.
60 changes: 41 additions & 19 deletions src/SamplesApp/SamplesApp.UITests/SampleControlUITestBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -75,7 +76,7 @@ public void BeforeEachTest()

// Check if the test needs to be ignore or not
// If nothing specified, it is considered as a global test
var platforms = GetActivePlatforms();
var platforms = GetActivePlatforms().Distinct().ToArray();
if (platforms.Length != 0)
{
// Otherwise, we need to define on which platform the test is running and compare it with targeted platform
Expand Down Expand Up @@ -217,35 +218,56 @@ private static T[] GetCurrentTestAttributes<T>() where T : Attribute
return methodInfo?.GetCustomAttributes(typeof(T), true) is T[] array ? array : new T[0];
}

private Platform[] GetActivePlatforms()
private IEnumerable<Platform> GetActivePlatforms()
{
if (TestContext.CurrentContext.Test.Properties["ActivePlatforms"].FirstOrDefault() is Platform[] platforms)
var currentTest = TestContext.CurrentContext.Test;
if (currentTest.ClassName == null)
{
if (platforms.Length != 0)
{
return platforms;
}
yield break;
}
else
if (Type.GetType(currentTest.ClassName) is { } classType)
{
if (Type.GetType(TestContext.CurrentContext.Test.ClassName) is Type classType)
if (classType.GetCustomAttributes(typeof(ActivePlatformsAttribute), false) is
ActivePlatformsAttribute[] classAttributes)
{
if (classType.GetCustomAttributes(typeof(ActivePlatformsAttribute), false) is ActivePlatformsAttribute[] attributes)
foreach (var attr in classAttributes)
{
if (
attributes.Length != 0
&& attributes[0]
.Properties["ActivePlatforms"]
.OfType<object>()
.FirstOrDefault() is Platform[] platforms2)
if (attr.Platforms == null)
{
continue;
}

foreach (var platform in attr.Platforms)
{
return platforms2;
yield return platform;
}
}
}

if (currentTest.MethodName is { })
{
var testMethodInfo = classType.GetMethod(currentTest.MethodName);

if (testMethodInfo is { } mi &&
mi.GetCustomAttributes(typeof(ActivePlatformsAttribute), false) is
ActivePlatformsAttribute[] methodAttributes)
{
foreach (var attr in methodAttributes)
{
if (attr.Platforms == null)
{
continue;
}

foreach (var platform in attr.Platforms)
{
yield return platform;
}
}
}
}
}

return Array.Empty<Platform>();
}
}

protected void Run(string metadataName, bool waitForSampleControl = true, bool skipInitialScreenshot = false, int sampleLoadTimeout = 5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,18 @@ namespace SamplesApp.UITests.TestFramework
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false)]
internal class ActivePlatformsAttribute : PropertyAttribute
{
public Platform[] Platforms
{
get
{
var property = Properties["ActivePlatforms"] as IList<object>;
return property?.FirstOrDefault() as Platform[];
}
}

public ActivePlatformsAttribute(params Platform[] platforms)
{
base.Properties.Add("ActivePlatforms", platforms);
Properties.Add("ActivePlatforms", platforms);
}
}
}

0 comments on commit d551c61

Please sign in to comment.