mirror of https://github.com/ppy/SDL3-CS.git
Add simple SDL_tray test
Just to get a feel for the API and to see how it works/behaves across operating systems.
This commit is contained in:
parent
e017626460
commit
3a0f3844b7
|
|
@ -0,0 +1,36 @@
|
|||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
using NUnit.Framework;
|
||||
using static SDL.SDL3;
|
||||
|
||||
namespace SDL.Tests
|
||||
{
|
||||
[Explicit("Uses an interactive tray icon.")]
|
||||
public unsafe class TestTray : TrayTest
|
||||
{
|
||||
private volatile bool running;
|
||||
|
||||
[Test]
|
||||
public void TestBasic()
|
||||
{
|
||||
var checkbox = SDL_InsertTrayEntryAt(RootMenu, -1, "Check box?", SDL_TrayEntryFlags.SDL_TRAYENTRY_CHECKBOX);
|
||||
Assert.That(checkbox != null, SDL_GetError);
|
||||
SetCallback(checkbox, () => Console.WriteLine("Checkbox was toggled."));
|
||||
|
||||
var separator = SDL_InsertTrayEntryAt(RootMenu, -1, (byte*)null, 0);
|
||||
Assert.That(separator != null, SDL_GetError);
|
||||
|
||||
var exit = SDL_InsertTrayEntryAt(RootMenu, -1, "Exit tray", SDL_TrayEntryFlags.SDL_TRAYENTRY_BUTTON);
|
||||
Assert.That(exit != null, SDL_GetError);
|
||||
SetCallback(exit, () => running = false);
|
||||
|
||||
running = true;
|
||||
|
||||
while (running)
|
||||
{
|
||||
SDL_PumpEvents();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace SDL.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public abstract unsafe class TrayTest
|
||||
{
|
||||
private SDL_Tray* tray;
|
||||
protected SDL_TrayMenu* RootMenu { get; private set; }
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
Assert.That(SDL3.SDL_Init(SDL_InitFlags.SDL_INIT_VIDEO), SDL3.SDL_GetError);
|
||||
tray = SDL3.SDL_CreateTray(null, "Test tray");
|
||||
Assert.That(tray != null, SDL3.SDL_GetError);
|
||||
RootMenu = SDL3.SDL_CreateTrayMenu(tray);
|
||||
Assert.That(RootMenu != null, SDL3.SDL_GetError);
|
||||
}
|
||||
|
||||
protected static void SetCallback(SDL_TrayEntry* entry, Action callback)
|
||||
{
|
||||
var objectHandle = new ObjectHandle<Action>(callback, GCHandleType.Normal);
|
||||
SDL3.SDL_SetTrayEntryCallback(entry, &nativeOnSelect, objectHandle.Handle); // this is leaking object handles, fine for tests
|
||||
}
|
||||
|
||||
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
|
||||
private static void nativeOnSelect(IntPtr userdata, SDL_TrayEntry* entry)
|
||||
{
|
||||
var objectHandle = new ObjectHandle<Action>(userdata, true);
|
||||
|
||||
if (objectHandle.GetTarget(out var action))
|
||||
action();
|
||||
else
|
||||
Assert.Fail("Accessing disposed object handle.");
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
if (tray != null)
|
||||
SDL3.SDL_DestroyTray(tray);
|
||||
|
||||
SDL3.SDL_Quit();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue