From 3a0f3844b774f022474ca15dece55f10481192a7 Mon Sep 17 00:00:00 2001 From: Susko3 Date: Tue, 31 Dec 2024 02:56:57 +0100 Subject: [PATCH 1/2] Add simple SDL_tray test Just to get a feel for the API and to see how it works/behaves across operating systems. --- SDL3-CS.Tests/TestTray.cs | 36 +++++++++++++++++++++++++++ SDL3-CS.Tests/TrayTest.cs | 52 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 SDL3-CS.Tests/TestTray.cs create mode 100644 SDL3-CS.Tests/TrayTest.cs diff --git a/SDL3-CS.Tests/TestTray.cs b/SDL3-CS.Tests/TestTray.cs new file mode 100644 index 0000000..52e9fc0 --- /dev/null +++ b/SDL3-CS.Tests/TestTray.cs @@ -0,0 +1,36 @@ +// Copyright (c) ppy Pty Ltd . 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(); + } + } + } +} diff --git a/SDL3-CS.Tests/TrayTest.cs b/SDL3-CS.Tests/TrayTest.cs new file mode 100644 index 0000000..ad754ce --- /dev/null +++ b/SDL3-CS.Tests/TrayTest.cs @@ -0,0 +1,52 @@ +// Copyright (c) ppy Pty Ltd . 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(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(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(); + } + } +} From 4ee7f7e617c7cd7b7d8b14c50f5a64bd5a06db1f Mon Sep 17 00:00:00 2001 From: Susko3 Date: Tue, 31 Dec 2024 02:57:20 +0100 Subject: [PATCH 2/2] Mark interactive test with `[Explicit]` --- SDL3-CS.Tests/TestPositionalInputVisualisation.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/SDL3-CS.Tests/TestPositionalInputVisualisation.cs b/SDL3-CS.Tests/TestPositionalInputVisualisation.cs index 1515d83..51e3c87 100644 --- a/SDL3-CS.Tests/TestPositionalInputVisualisation.cs +++ b/SDL3-CS.Tests/TestPositionalInputVisualisation.cs @@ -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;