forked from ppy/SDL3-CS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ppy#190 from Susko3/SDL_tray-tests
Add simple `SDL_tray` test
- Loading branch information
Showing
3 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |