Skip to content

Commit

Permalink
Merge pull request ppy#190 from Susko3/SDL_tray-tests
Browse files Browse the repository at this point in the history
Add simple `SDL_tray` test
  • Loading branch information
smoogipoo authored Jan 1, 2025
2 parents 365fe5c + 4ee7f7e commit 20e10af
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
2 changes: 2 additions & 0 deletions SDL3-CS.Tests/TestPositionalInputVisualisation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// See the LICENCE file in the repository root for full licence text.

using System.Drawing;
using NUnit.Framework;
using static SDL.SDL3;

namespace SDL.Tests
{
[Explicit("Uses an interactive window.")]
public unsafe class TestPositionalInputVisualisation : MainCallbacksTest
{
private SDL_Window* window;
Expand Down
36 changes: 36 additions & 0 deletions SDL3-CS.Tests/TestTray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. 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();
}
}
}
}
52 changes: 52 additions & 0 deletions SDL3-CS.Tests/TrayTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. 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();
}
}
}

0 comments on commit 20e10af

Please sign in to comment.