|
| 1 | +// Callbacks must keep their captured objects alive while replacing or clearing |
| 2 | +// themselves. Unregistering a shortcut must release its callback even if the |
| 3 | +// caller still owns the shortcut. |
| 4 | +// |
| 5 | +// The registration case needs a desktop session and skips if the platform cannot |
| 6 | +// register the accelerator. The self-replacement cases run without registration. |
| 7 | + |
| 8 | +#include <cstdlib> |
| 9 | +#include <iostream> |
| 10 | +#include <memory> |
| 11 | + |
| 12 | +#include "../src/shortcut.h" |
| 13 | +#include "../src/shortcut_manager.h" |
| 14 | +#include "../src/window_manager.h" |
| 15 | + |
| 16 | +namespace { |
| 17 | + |
| 18 | +int g_failures = 0; |
| 19 | + |
| 20 | +void Check(bool condition, const char* message) { |
| 21 | + if (!condition) { |
| 22 | + std::cerr << "FAIL: " << message << std::endl; |
| 23 | + ++g_failures; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +void TestShortcutSelfReplacement() { |
| 28 | + nativeapi::Shortcut shortcut(1, "Ctrl+A", nullptr); |
| 29 | + auto captured = std::make_shared<int>(42); |
| 30 | + std::weak_ptr<int> weak = captured; |
| 31 | + int replacements = 0; |
| 32 | + shortcut.SetCallback([&, captured] { |
| 33 | + shortcut.SetCallback([&] { ++replacements; }); |
| 34 | + Check(!weak.expired(), "active shortcut callback was destroyed during replacement"); |
| 35 | + }); |
| 36 | + captured.reset(); |
| 37 | + shortcut.Invoke(); |
| 38 | + Check(weak.expired(), "replaced shortcut callback was retained"); |
| 39 | + shortcut.Invoke(); |
| 40 | + Check(replacements == 1, "replacement callback was not installed"); |
| 41 | + shortcut.SetCallback(nullptr); |
| 42 | +} |
| 43 | + |
| 44 | +void TestHookSelfRemoval() { |
| 45 | + auto& manager = nativeapi::WindowManager::GetInstance(); |
| 46 | + for (bool show : {true, false}) { |
| 47 | + auto captured = std::make_shared<int>(42); |
| 48 | + std::weak_ptr<int> weak = captured; |
| 49 | + auto callback = [&, captured](nativeapi::WindowId) { |
| 50 | + if (show) { |
| 51 | + manager.SetWillShowHook(std::nullopt); |
| 52 | + } else { |
| 53 | + manager.SetWillHideHook(std::nullopt); |
| 54 | + } |
| 55 | + Check(!weak.expired(), "active window hook was destroyed during removal"); |
| 56 | + }; |
| 57 | + if (show) { |
| 58 | + manager.SetWillShowHook(std::move(callback)); |
| 59 | + } else { |
| 60 | + manager.SetWillHideHook(std::move(callback)); |
| 61 | + } |
| 62 | + captured.reset(); |
| 63 | + if (show) { |
| 64 | + manager.HandleWillShow(0); |
| 65 | + } else { |
| 66 | + manager.HandleWillHide(0); |
| 67 | + } |
| 68 | + Check(weak.expired(), "removed window hook was retained"); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +void TestUnregisterClearsRetainedShortcut() { |
| 73 | + auto& manager = nativeapi::ShortcutManager::GetInstance(); |
| 74 | + int calls = 0; |
| 75 | + auto captured = std::make_shared<int>(42); |
| 76 | + std::weak_ptr<int> weak = captured; |
| 77 | + auto shortcut = manager.Register("Ctrl+Alt+Shift+F12", [&, captured] { ++calls; }); |
| 78 | + captured.reset(); |
| 79 | + if (!shortcut) { |
| 80 | + std::cout << "SKIP: platform could not register a global shortcut" << std::endl; |
| 81 | + return; |
| 82 | + } |
| 83 | + shortcut->Invoke(); |
| 84 | + Check(calls == 1, "registered shortcut callback did not run"); |
| 85 | + Check(manager.Unregister(shortcut->GetId()), "shortcut unregister failed"); |
| 86 | + shortcut->Invoke(); |
| 87 | + Check(calls == 1, "retained shortcut invoked after unregister"); |
| 88 | + Check(weak.expired(), "unregistered shortcut retained its captured state"); |
| 89 | +} |
| 90 | +} // namespace |
| 91 | + |
| 92 | +int main() { |
| 93 | + std::cout << "callback_lifetime_test" << std::endl; |
| 94 | + |
| 95 | + TestShortcutSelfReplacement(); |
| 96 | + TestHookSelfRemoval(); |
| 97 | + TestUnregisterClearsRetainedShortcut(); |
| 98 | + if (g_failures > 0) { |
| 99 | + std::cerr << g_failures << " check(s) failed" << std::endl; |
| 100 | + return EXIT_FAILURE; |
| 101 | + } |
| 102 | + std::cout << "All checks passed." << std::endl; |
| 103 | + return EXIT_SUCCESS; |
| 104 | +} |
0 commit comments