From 80daff62588bea27003bebb1527f65d1d060ade0 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 22 May 2025 06:00:54 +0000 Subject: [PATCH 1/3] Initial plan for issue From 1e95b78788554b27d9d42f2d65e66909ae2e5cc6 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 22 May 2025 06:02:57 +0000 Subject: [PATCH 2/3] Fix SharedMemory::IsValid() to check memory view validity Co-authored-by: MuyuanMS <116717757+MuyuanMS@users.noreply.github.com> --- dev/AppLifecycle/SharedMemory.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/AppLifecycle/SharedMemory.h b/dev/AppLifecycle/SharedMemory.h index fbf709fc0f..4452aed1e7 100644 --- a/dev/AppLifecycle/SharedMemory.h +++ b/dev/AppLifecycle/SharedMemory.h @@ -82,7 +82,7 @@ class SharedMemory bool IsValid() { - return (m_name.size() != 0); + return (m_name.size() != 0) && (m_view != nullptr); } protected: From 4ec7cbca635ab3f41ccf11d2d4fa7c9dfef27df8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 22 May 2025 06:04:21 +0000 Subject: [PATCH 3/3] Add test for unregister and re-register AppInstance key scenario Co-authored-by: MuyuanMS <116717757+MuyuanMS@users.noreply.github.com> --- .../AppInstanceRegisterKeyTests.cpp | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 test/AppLifecycle/AppInstanceRegisterKeyTests.cpp diff --git a/test/AppLifecycle/AppInstanceRegisterKeyTests.cpp b/test/AppLifecycle/AppInstanceRegisterKeyTests.cpp new file mode 100644 index 0000000000..2b6b6bb8d7 --- /dev/null +++ b/test/AppLifecycle/AppInstanceRegisterKeyTests.cpp @@ -0,0 +1,65 @@ +// Copyright (c) Microsoft Corporation and Contributors. +// Licensed under the MIT License. + +#include "pch.h" +#include +#include "Shared.h" + +using namespace WEX::Common; +using namespace WEX::Logging; +using namespace WEX::TestExecution; + +using namespace winrt; +using namespace winrt::Microsoft::Windows::AppLifecycle; +using namespace winrt::Windows::ApplicationModel::Activation; +using namespace winrt::Windows::Foundation; +using namespace winrt::Windows::Foundation::Collections; +using namespace winrt::Windows::Management::Deployment; +using namespace winrt::Windows::Storage; +using namespace winrt::Windows::System; + +namespace Test::AppLifecycle +{ + class AppInstanceRegisterKeyTests + { + private: + const std::wstring c_testKeyName = L"TestKey_ReRegister"; + + public: + BEGIN_TEST_CLASS(AppInstanceRegisterKeyTests) + TEST_CLASS_PROPERTY(L"ThreadingModel", L"MTA") + TEST_CLASS_PROPERTY(L"RunAs:Class", L"RestrictedUser") + END_TEST_CLASS() + + TEST_CLASS_SETUP(ClassInit) + { + ::Test::Bootstrap::Setup(); + return true; + } + + TEST_CLASS_CLEANUP(ClassUninit) + { + ::Test::Bootstrap::Cleanup(); + return true; + } + + TEST_METHOD(UnregisterAndReRegisterKey) + { + // First register a key + auto instance = AppInstance::FindOrRegisterForKey(c_testKeyName); + VERIFY_IS_NOT_NULL(instance); + VERIFY_IS_TRUE(instance.IsCurrent()); + VERIFY_ARE_EQUAL(instance.Key(), c_testKeyName); + + // Unregister the key + instance.UnregisterKey(); + VERIFY_ARE_EQUAL(instance.Key(), L""); + + // Re-register the same key + auto reRegisteredInstance = AppInstance::FindOrRegisterForKey(c_testKeyName); + VERIFY_IS_NOT_NULL(reRegisteredInstance); + VERIFY_IS_TRUE(reRegisteredInstance.IsCurrent()); + VERIFY_ARE_EQUAL(reRegisteredInstance.Key(), c_testKeyName); + } + }; +} \ No newline at end of file