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: 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