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 end-to-end test framework inspired from iOS interactive tests #8110

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 0 additions & 13 deletions Tests/Directory.Build.props

This file was deleted.

19 changes: 19 additions & 0 deletions Tests/Interactive/Common/Categories.cs
@@ -0,0 +1,19 @@
// MonoGame - Copyright (C) The MonoGame Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.

namespace MonoGame.InteractiveTests
{
/// <summary>
/// Defines test app categories that is provided via a <code>class</code>
/// attribute as well as a human-readable string shown in a UI or provided
/// via command line arg.
/// </summary>
static class Categories
{
public const string Default = General;

public const string General = "General";
public const string Meta = "Meta Tests";
}
}
52 changes: 52 additions & 0 deletions Tests/Interactive/Common/GameDebug.cs
@@ -0,0 +1,52 @@
// MonoGame - Copyright (C) The MonoGame Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.

using System.Collections.Generic;

namespace MonoGame.InteractiveTests
{
/// <summary>
/// Allows tests to output console messages including spammy messages that
/// may be throttled.
///
/// On various platforms, this may be available via console output or via
/// a special console viewer (Console app on Mac; `adb logcat` on Android etc).
/// </summary>
public partial class GameDebug
{
/// <summary>Output a single console message.</summary>
public static void C(string message)
{
System.Console.WriteLine($"MGDBG: {message}");
}

/// <summary>Output an error message to the console.</summary>
public static void E(string message)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we get more descriptive method names for these? LogError or LogDebug

{
System.Console.WriteLine($"****ERROR*****:MGDBG: {message}");
}

/// <summary>Maintains the spam message counts to prevent spamming the console.</summary>
private record MessageCount(int Count)
{
public int Count { get; set; } = Count;
}
private static readonly Dictionary<string, MessageCount> MESSAGES_COUNTS_ = new();

/// <summary>Use this to output spammy messages.</summary>
public static void Spam(string message, int maxNumTimes = 10)
{
if (!MESSAGES_COUNTS_.TryGetValue(message, out var numTimes))
{
numTimes = new(0);
MESSAGES_COUNTS_.Add(message, numTimes);
}

if (numTimes.Count >= maxNumTimes) { return; }

System.Console.WriteLine($"MGDBG: {message} ...#{numTimes.Count}");
numTimes.Count++;
}
}
}
70 changes: 70 additions & 0 deletions Tests/Interactive/Common/InteractiveTest.cs
@@ -0,0 +1,70 @@
// MonoGame - Copyright (C) The MonoGame Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using MonoGame.Framework.Utilities;

namespace MonoGame.InteractiveTests
{
/// <summary>
/// Manages creating of interactive test(s) instance(s).
/// </summary>
public class InteractiveTest
{
public static bool TryCreateFrom(Type type, out InteractiveTest test)
{
test = null;
if (!typeof(TestGame).IsAssignableFrom(type)) { return false; }

var attrs = type.GetCustomAttributes(typeof(InteractiveTestAttribute), false);
if (attrs.Length == 0) { return false; }

var attr = (InteractiveTestAttribute)attrs[0];

test = new InteractiveTest(
type, attr.Name ?? type.Name, attr.Category ?? Categories.Default, attr.Platforms);
return true;
}

private InteractiveTest(Type type, string name, string category, MonoGamePlatform[] platforms)
{
_type = type;
_name = name;
_category = category;
_platforms = platforms;
}

private readonly Type _type;
public Type Type { get { return _type; } }

private readonly string _name;
public string Name { get { return _name; } }

private readonly string _category;
public string Category { get { return _category; } }

private readonly MonoGamePlatform[] _platforms;
public MonoGamePlatform[] Platforms { get { return _platforms; } }

public Game Create()
{
return (Game)Activator.CreateInstance(_type);
}

public bool MatchesPlatform(MonoGamePlatform runtimePlatform)
{
// Empty array matches everything.
if (_platforms.Length == 0) { return true; }

foreach (var testPlatform in _platforms)
{
if (testPlatform == runtimePlatform) { return true; }
}

return false;
}
}
}
48 changes: 48 additions & 0 deletions Tests/Interactive/Common/InteractiveTestAttribute.cs
@@ -0,0 +1,48 @@
// MonoGame - Copyright (C) The MonoGame Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.

using System;
using System.Collections.Generic;
using MonoGame.Framework.Utilities;

namespace MonoGame.InteractiveTests
{
/// <summary>
/// Attribute specified on test classes that are automatically
/// discovered and shown/provided via some UI / command-line arg.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class InteractiveTestAttribute : Attribute
{
public InteractiveTestAttribute(string name, string category,
MonoGamePlatform[] platforms = null)
{
_name = name;
_category = category;

// Empty array matches everything.
if (platforms == null) { platforms = new MonoGamePlatform[] { }; }

_platforms = platforms;
}

/// <summary>Human-readable name of the test</summary>
private readonly string _name;

public string Name { get { return _name; } }

/// <summary>Category of the test. See <see cref="Categories"/></summary>
private readonly string _category;

public string Category { get { return _category; } }

/// <summary>
/// Supported platforms that this test can run on (empty array
/// allows running on any platform.
/// </summary>
private readonly MonoGamePlatform[] _platforms;

public MonoGamePlatform[] Platforms { get { return _platforms; } }
}
}
70 changes: 70 additions & 0 deletions Tests/Interactive/Common/InteractiveTests.cs
@@ -0,0 +1,70 @@
// MonoGame - Copyright (C) The MonoGame Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.

using System.Collections.Generic;
using System.Reflection;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Framework.Utilities;

namespace MonoGame.InteractiveTests
{
/// <summary>
/// Creates a <see cref="InteractiveTest"/> from applicable types in our binary.
/// Also allows filtering of tests based on platforms/command-line args.
/// </summary>
public class InteractiveTests
{
private readonly List<InteractiveTest> _interactiveTests = new();

private readonly List<InteractiveTest> _filteredTests = new();

public InteractiveTests()
{
_interactiveTests.Clear();
var assembly = Assembly.GetExecutingAssembly();
foreach (var type in assembly.GetTypes())
{
InteractiveTest test;
if (!InteractiveTest.TryCreateFrom(type, out test)) { continue; }

if (test.MatchesPlatform(PlatformInfo.MonoGamePlatform)) { _interactiveTests.Add(test); }
}

GameDebug.C($"--Discovered {_interactiveTests.Count} tests.");
}

public IReadOnlyList<InteractiveTest> Tests { get { return _interactiveTests; } }

/// <summary>
/// Parses the passed-in arg and returns an `InteractiveTest` game. See HelpStr for more details.
/// </summary>
public IReadOnlyList<InteractiveTest> Parse(string[] args)
{
_filteredTests.Clear();
if (args == null || args.Length == 0) { return _filteredTests; }

foreach (var test in _interactiveTests)
{
foreach (var testName in args)
{
var name = testName.ToLower();
if (test.Category.ToLower().Contains(name)) { _filteredTests.Add(test); }
else if (test.Name.ToLower().Contains(name)) { _filteredTests.Add(test); }
}
}

return _filteredTests;
}

public string HelpStr()
{
var testStr = "";
foreach (var test in _interactiveTests) { testStr += $"{test.Name}\n"; }

return $@"Interactive tests available:
{testStr}
";
}
}
}
13 changes: 13 additions & 0 deletions Tests/Interactive/Common/MonoGame.Interactive.Common.projitems
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
<HasSharedItems>true</HasSharedItems>
</PropertyGroup>
<PropertyGroup Label="Configuration">
<Import_RootNamespace>MonoGame</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)\**\*.cs" />
</ItemGroup>
</Project>
11 changes: 11 additions & 0 deletions Tests/Interactive/Common/MonoGame.Interactive.Common.shproj
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Globals">
<ProjectGuid>085B407C-726C-43FE-BB55-64E2B6D143FE</ProjectGuid>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
<Import Project="MonoGame.Interactive.Common.projitems" Label="Shared" />
</Project>