maui-linux/Xamarin.Forms.Core.iOS.UITests/BaseTestFixture.cs

140 строки
2.7 KiB
C#
Исходник Обычный вид История

2016-03-22 23:02:25 +03:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
2016-03-22 23:02:25 +03:00
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using Xamarin.Forms.Controls;
2016-03-22 23:02:25 +03:00
using Xamarin.UITest;
using Xamarin.UITest.Queries;
namespace Xamarin.Forms.Core.UITests
{
internal abstract class BaseTestFixture
{
// TODO: Landscape tests
public static IApp App { get; set; }
2016-03-22 23:02:25 +03:00
public string PlatformViewType { get; protected set; }
public static AppRect ScreenBounds { get; set; }
2016-03-22 23:02:25 +03:00
[TestFixtureTearDown]
protected virtual void FixtureTeardown()
2016-03-22 23:02:25 +03:00
{
}
static int s_testsrun;
const int ConsecutiveTestLimit = 10;
2016-03-22 23:02:25 +03:00
// Until we get more of our memory leak issues worked out, restart the app
// after a specified number of tests so we don't get bogged down in GC
// (or booted by jetsam)
public void EnsureMemory()
2016-03-22 23:02:25 +03:00
{
s_testsrun += 1;
if (s_testsrun >= ConsecutiveTestLimit)
{
s_testsrun = 0;
2016-03-22 23:02:25 +03:00
CoreUITestsSetup.LaunchApp();
FixtureSetup();
}
2016-03-22 23:02:25 +03:00
}
[SetUp]
protected virtual void TestSetup()
2016-03-22 23:02:25 +03:00
{
EnsureMemory();
2016-03-22 23:02:25 +03:00
}
[TearDown]
protected virtual void TestTearDown()
2016-03-22 23:02:25 +03:00
{
}
protected abstract void NavigateToGallery();
#pragma warning disable 618
[TestFixtureSetUp]
#pragma warning restore 618
protected virtual void FixtureSetup()
2016-03-22 23:02:25 +03:00
{
ResetApp();
int maxAttempts = 2;
int attempts = 0;
while (attempts < maxAttempts)
{
attempts += 1;
try
{
NavigateToGallery();
return;
}
catch (Exception ex)
{
var debugMessage = $"NavigateToGallery failed: {ex}";
Debug.WriteLine(debugMessage);
Console.WriteLine(debugMessage);
if (attempts < maxAttempts)
{
// Something has failed and we're stuck in a place where we can't navigate
// to the test. Usually this is because we're getting network/HTTP errors
// communicating with the server on the device. So we'll try restarting the app.
CoreUITestsSetup.LaunchApp();
}
else
{
// But if it's still not working after [maxAttempts], we've got assume this is a legit
// problem that restarting won't fix
throw;
}
}
}
2016-03-22 23:02:25 +03:00
}
protected void ResetApp()
{
#if __IOS__
App.Invoke("reset:", string.Empty);
#endif
#if __ANDROID__
App.Invoke("Reset");
#endif
}
}
}
#if UITEST
namespace Xamarin.Forms.Core.UITests
{
using NUnit.Framework;
[SetUpFixture]
public class CoreUITestsSetup
{
[SetUp]
public void RunBeforeAnyTests()
{
LaunchApp();
}
public static void LaunchApp()
{
BaseTestFixture.App = null;
BaseTestFixture.App = AppSetup.Setup();
BaseTestFixture.App.SetOrientationPortrait();
BaseTestFixture.ScreenBounds = BaseTestFixture.App.RootViewRect();
}
2016-03-22 23:02:25 +03:00
}
}
#endif