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

fix: mobile ui-tests not working #644

Merged
merged 1 commit into from
Jul 12, 2023
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
@@ -1,18 +1,85 @@
using Android.App;
using Android.Widget;
using System;
using System.IO;
using System.Threading;
using Android.App;
using Android.OS;
using Android.Content.PM;
using Android.Views;
using Java.Interop;

namespace Uno.Toolkit.Samples
namespace Uno.Toolkit.Samples.Droid;

[Activity(
MainLauncher = true,
ConfigurationChanges = global::Uno.UI.ActivityHelper.AllConfigChanges,
// SoftInput.AdjustNothing is required by SafeArea
WindowSoftInputMode = SoftInput.AdjustNothing | SoftInput.StateHidden
)]
public class MainActivity : Windows.UI.Xaml.ApplicationActivity
{
[Activity(
MainLauncher = true,
ConfigurationChanges = global::Uno.UI.ActivityHelper.AllConfigChanges,
WindowSoftInputMode = SoftInput.AdjustPan | SoftInput.StateHidden
)]
public class MainActivity : Windows.UI.Xaml.ApplicationActivity
#if USE_UITESTS
private HandlerThread _pixelCopyHandlerThread;

[Export("NavBackFromNestedPage")]
public void NavBackFromNestedPage() => App.NavBackFromNestedPage();

[Export("ForceNavigation")]
public void ForceNavigation(string sampleName) => App.ForceNavigation(sampleName);

[Export("ExitNestedSample")]
public void ExitNestedSampleBackdoor() => App.ExitNestedSample();

[Export("NavigateToNestedSample")]
public void NavigateToNestedSample(string pageName) => App.NavigateToNestedSample(pageName);

[Export("GetDisplayScreenScaling")]
public string GetDisplayScreenScaling(string value) => App.GetDisplayScreenScaling(value);

/// <summary>
/// Returns a base64 encoded PNG file
/// </summary>
[Export("GetScreenshot")]
public string GetScreenshot(string displayId)
{
// Get true size of screen, including status bar and bottom navigation bar
var metrics = Resources.DisplayMetrics;
var bitmap = Android.Graphics.Bitmap.CreateBitmap(metrics.WidthPixels, metrics.HeightPixels, Android.Graphics.Bitmap.Config.Argb8888);

if (_pixelCopyHandlerThread == null)
{
_pixelCopyHandlerThread = new Android.OS.HandlerThread("ScreenshotHelper");
_pixelCopyHandlerThread.Start();
}

var listener = new PixelCopyListener();

// PixelCopy.Request returns the actual rendering of the screen location for the app, including OpenGL content.
// Setting srcRect to null ensures that the entire screen, including status bar and bottom navigation bar, are captured.
#pragma warning disable CA1416 // Validate platform compatibility
PixelCopy.Request(Window, srcRect: null, bitmap, listener, new Android.OS.Handler(_pixelCopyHandlerThread.Looper));
#pragma warning restore CA1416 // Validate platform compatibility

listener.WaitOne();

using var memoryStream = new MemoryStream();
bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 100, memoryStream);

return Convert.ToBase64String(memoryStream.ToArray());
}

class PixelCopyListener : Java.Lang.Object, PixelCopy.IOnPixelCopyFinishedListener
{
private ManualResetEvent _event = new ManualResetEvent(false);

public void WaitOne()
{
_event.WaitOne();
}

public void OnPixelCopyFinished(int copyResult)
{
_event.Set();
}
}
#endif
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'net7.0-android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="'$(TargetFramework)'=='net7.0-macos'">10.14</SupportedOSPlatformVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Debug' or '$(IsUiAutomationMappingEnabled)'=='True'">
<IsUiAutomationMappingEnabled>True</IsUiAutomationMappingEnabled>
<DefineConstants>$(DefineConstants);USE_UITESTS</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(RuntimeIdentifier)'==''">
<!-- Default values for command line builds -->
<RuntimeIdentifier Condition="'$(TargetFramework)' == 'net7.0-ios'">iossimulator-x64</RuntimeIdentifier>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,85 @@
using Android.OS;
using Android.Content.PM;
using Android.Views;
using Java.Interop;
using Uno.Toolkit.Samples;
using System.IO;
using System;
using System.Threading;

namespace Uno.Toolkit.WinUI.Samples.Droid;

[Activity(
MainLauncher = true,
ConfigurationChanges = global::Uno.UI.ActivityHelper.AllConfigChanges,
// SoftInput.AdjustNothing is required by SafeArea
WindowSoftInputMode = SoftInput.AdjustNothing | SoftInput.StateHidden
)]
public class MainActivity : Microsoft.UI.Xaml.ApplicationActivity
{
}
#if USE_UITESTS
private HandlerThread _pixelCopyHandlerThread;

[Export("NavBackFromNestedPage")]
public void NavBackFromNestedPage() => App.NavBackFromNestedPage();

[Export("ForceNavigation")]
public void ForceNavigation(string sampleName) => App.ForceNavigation(sampleName);

[Export("ExitNestedSample")]
public void ExitNestedSampleBackdoor() => App.ExitNestedSample();

[Export("NavigateToNestedSample")]
public void NavigateToNestedSample(string pageName) => App.NavigateToNestedSample(pageName);

[Export("GetDisplayScreenScaling")]
public string GetDisplayScreenScaling(string value) => App.GetDisplayScreenScaling(value);

/// <summary>
/// Returns a base64 encoded PNG file
/// </summary>
[Export("GetScreenshot")]
public string GetScreenshot(string displayId)
{
// Get true size of screen, including status bar and bottom navigation bar
var metrics = Resources.DisplayMetrics;
var bitmap = Android.Graphics.Bitmap.CreateBitmap(metrics.WidthPixels, metrics.HeightPixels, Android.Graphics.Bitmap.Config.Argb8888);

if (_pixelCopyHandlerThread == null)
{
_pixelCopyHandlerThread = new Android.OS.HandlerThread("ScreenshotHelper");
_pixelCopyHandlerThread.Start();
}

var listener = new PixelCopyListener();

// PixelCopy.Request returns the actual rendering of the screen location for the app, including OpenGL content.
// Setting srcRect to null ensures that the entire screen, including status bar and bottom navigation bar, are captured.
#pragma warning disable CA1416 // Validate platform compatibility
PixelCopy.Request(Window, srcRect: null, bitmap, listener, new Android.OS.Handler(_pixelCopyHandlerThread.Looper));
#pragma warning restore CA1416 // Validate platform compatibility

listener.WaitOne();

using var memoryStream = new MemoryStream();
bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 100, memoryStream);

return Convert.ToBase64String(memoryStream.ToArray());
}

class PixelCopyListener : Java.Lang.Object, PixelCopy.IOnPixelCopyFinishedListener
{
private ManualResetEvent _event = new ManualResetEvent(false);

public void WaitOne()
{
_event.WaitOne();
}

public void OnPixelCopyFinished(int copyResult)
{
_event.Set();
}
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

<DefineConstants>$(DefineConstants);IS_WINUI</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Debug' or '$(IsUiAutomationMappingEnabled)'=='True'">
<IsUiAutomationMappingEnabled>True</IsUiAutomationMappingEnabled>
<DefineConstants>$(DefineConstants);USE_UITESTS</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" />
Expand Down
Loading