From a0c688110400714227e20a8ba8d9f284e67b56af Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 5 Aug 2024 12:15:29 -0700 Subject: [PATCH 1/4] Add 'Windows.UI.Xaml' exception types --- .../ExceptionHelpers.Microsoft.cs | 117 ++++++++++++++ src/WinRT.Runtime/ExceptionHelpers.Windows.cs | 118 ++++++++++++++ src/WinRT.Runtime/ExceptionHelpers.cs | 149 ++++-------------- .../MatchingRefApiCompatBaseline.txt | 6 +- 4 files changed, 272 insertions(+), 118 deletions(-) create mode 100644 src/WinRT.Runtime/ExceptionHelpers.Microsoft.cs create mode 100644 src/WinRT.Runtime/ExceptionHelpers.Windows.cs diff --git a/src/WinRT.Runtime/ExceptionHelpers.Microsoft.cs b/src/WinRT.Runtime/ExceptionHelpers.Microsoft.cs new file mode 100644 index 000000000..76f32ca36 --- /dev/null +++ b/src/WinRT.Runtime/ExceptionHelpers.Microsoft.cs @@ -0,0 +1,117 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +using System; + +namespace Microsoft.UI.Xaml +{ + namespace Automation + { +#if EMBED + internal +#else + public +#endif + class ElementNotAvailableException : Exception + { + public ElementNotAvailableException() + : base("The element is not available.") + { + HResult = WinRT.ExceptionHelpers.E_ELEMENTNOTAVAILABLE; + } + + public ElementNotAvailableException(string message) + : base(message) + { + HResult = WinRT.ExceptionHelpers.E_ELEMENTNOTAVAILABLE; + } + + public ElementNotAvailableException(string message, Exception innerException) + : base(message, innerException) + { + HResult = WinRT.ExceptionHelpers.E_ELEMENTNOTAVAILABLE; + } + } + +#if EMBED + internal +#else + public +#endif + class ElementNotEnabledException : Exception + { + public ElementNotEnabledException() + : base("The element is not enabled.") + { + HResult = WinRT.ExceptionHelpers.E_ELEMENTNOTENABLED; + } + + public ElementNotEnabledException(string message) + : base(message) + { + HResult = WinRT.ExceptionHelpers.E_ELEMENTNOTENABLED; + } + + public ElementNotEnabledException(string message, Exception innerException) + : base(message, innerException) + { + HResult = WinRT.ExceptionHelpers.E_ELEMENTNOTENABLED; + } + } + } + namespace Markup + { + +#if EMBED + internal +#else + public +#endif + class XamlParseException : Exception + { + public XamlParseException() + : base("XAML parsing failed.") + { + HResult = WinRT.ExceptionHelpers.E_XAMLPARSEFAILED; + } + + public XamlParseException(string message) + : base(message) + { + HResult = WinRT.ExceptionHelpers.E_XAMLPARSEFAILED; + } + + public XamlParseException(string message, Exception innerException) + : base(message, innerException) + { + HResult = WinRT.ExceptionHelpers.E_XAMLPARSEFAILED; + } + } + } + +#if EMBED + internal +#else + public +#endif + class LayoutCycleException : Exception + { + public LayoutCycleException() + : base("A cycle occurred while laying out the GUI.") + { + HResult = WinRT.ExceptionHelpers.E_LAYOUTCYCLE; + } + + public LayoutCycleException(string message) + : base(message) + { + HResult = WinRT.ExceptionHelpers.E_LAYOUTCYCLE; + } + + public LayoutCycleException(string message, Exception innerException) + : base(message, innerException) + { + HResult = WinRT.ExceptionHelpers.E_LAYOUTCYCLE; + } + } +} diff --git a/src/WinRT.Runtime/ExceptionHelpers.Windows.cs b/src/WinRT.Runtime/ExceptionHelpers.Windows.cs new file mode 100644 index 000000000..852e6e62d --- /dev/null +++ b/src/WinRT.Runtime/ExceptionHelpers.Windows.cs @@ -0,0 +1,118 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +using System; + +namespace Windows.UI.Xaml +{ + namespace Automation + { +#if EMBED + internal +#else + public +#endif + class ElementNotAvailableException : Exception + { + public ElementNotAvailableException() + : base("The element is not available.") + { + HResult = WinRT.ExceptionHelpers.E_ELEMENTNOTAVAILABLE; + } + + public ElementNotAvailableException(string message) + : base(message) + { + HResult = WinRT.ExceptionHelpers.E_ELEMENTNOTAVAILABLE; + } + + public ElementNotAvailableException(string message, Exception innerException) + : base(message, innerException) + { + HResult = WinRT.ExceptionHelpers.E_ELEMENTNOTAVAILABLE; + } + } + +#if EMBED + internal +#else + public +#endif + class ElementNotEnabledException : Exception + { + public ElementNotEnabledException() + : base("The element is not enabled.") + { + HResult = WinRT.ExceptionHelpers.E_ELEMENTNOTENABLED; + } + + public ElementNotEnabledException(string message) + : base(message) + { + HResult = WinRT.ExceptionHelpers.E_ELEMENTNOTENABLED; + } + + public ElementNotEnabledException(string message, Exception innerException) + : base(message, innerException) + { + HResult = WinRT.ExceptionHelpers.E_ELEMENTNOTENABLED; + } + } + } + + namespace Markup + { + +#if EMBED + internal +#else + public +#endif + class XamlParseException : Exception + { + public XamlParseException() + : base("XAML parsing failed.") + { + HResult = WinRT.ExceptionHelpers.E_XAMLPARSEFAILED; + } + + public XamlParseException(string message) + : base(message) + { + HResult = WinRT.ExceptionHelpers.E_XAMLPARSEFAILED; + } + + public XamlParseException(string message, Exception innerException) + : base(message, innerException) + { + HResult = WinRT.ExceptionHelpers.E_XAMLPARSEFAILED; + } + } + } + +#if EMBED + internal +#else + public +#endif + class LayoutCycleException : Exception + { + public LayoutCycleException() + : base("A cycle occurred while laying out the GUI.") + { + HResult = WinRT.ExceptionHelpers.E_LAYOUTCYCLE; + } + + public LayoutCycleException(string message) + : base(message) + { + HResult = WinRT.ExceptionHelpers.E_LAYOUTCYCLE; + } + + public LayoutCycleException(string message, Exception innerException) + : base(message, innerException) + { + HResult = WinRT.ExceptionHelpers.E_LAYOUTCYCLE; + } + } +} diff --git a/src/WinRT.Runtime/ExceptionHelpers.cs b/src/WinRT.Runtime/ExceptionHelpers.cs index 30a8c777b..75cef8a69 100644 --- a/src/WinRT.Runtime/ExceptionHelpers.cs +++ b/src/WinRT.Runtime/ExceptionHelpers.cs @@ -225,16 +225,44 @@ private static Exception GetExceptionForHR(int hr, bool useGlobalErrorState, out ex = !string.IsNullOrEmpty(errorMessage) ? new InvalidOperationException(errorMessage) : new InvalidOperationException(); break; case E_XAMLPARSEFAILED: - ex = !string.IsNullOrEmpty(errorMessage) ? new Microsoft.UI.Xaml.Markup.XamlParseException(errorMessage) : new Microsoft.UI.Xaml.Markup.XamlParseException(); + if (FeatureSwitches.UseWindowsUIXamlProjections) + { + ex = !string.IsNullOrEmpty(errorMessage) ? new Windows.UI.Xaml.Markup.XamlParseException(errorMessage) : new Windows.UI.Xaml.Markup.XamlParseException(); + } + else + { + ex = !string.IsNullOrEmpty(errorMessage) ? new Microsoft.UI.Xaml.Markup.XamlParseException(errorMessage) : new Microsoft.UI.Xaml.Markup.XamlParseException(); + } break; case E_LAYOUTCYCLE: - ex = !string.IsNullOrEmpty(errorMessage) ? new Microsoft.UI.Xaml.LayoutCycleException(errorMessage) : new Microsoft.UI.Xaml.LayoutCycleException(); + if (FeatureSwitches.UseWindowsUIXamlProjections) + { + ex = !string.IsNullOrEmpty(errorMessage) ? new Windows.UI.Xaml.LayoutCycleException(errorMessage) : new Windows.UI.Xaml.LayoutCycleException(); + } + else + { + ex = !string.IsNullOrEmpty(errorMessage) ? new Microsoft.UI.Xaml.LayoutCycleException(errorMessage) : new Microsoft.UI.Xaml.LayoutCycleException(); + } break; case E_ELEMENTNOTAVAILABLE: - ex = !string.IsNullOrEmpty(errorMessage) ? new Microsoft.UI.Xaml.Automation.ElementNotAvailableException(errorMessage) : new Microsoft.UI.Xaml.Automation.ElementNotAvailableException(); + if (FeatureSwitches.UseWindowsUIXamlProjections) + { + ex = !string.IsNullOrEmpty(errorMessage) ? new Windows.UI.Xaml.Automation.ElementNotAvailableException(errorMessage) : new Windows.UI.Xaml.Automation.ElementNotAvailableException(); + } + else + { + ex = !string.IsNullOrEmpty(errorMessage) ? new Microsoft.UI.Xaml.Automation.ElementNotAvailableException(errorMessage) : new Microsoft.UI.Xaml.Automation.ElementNotAvailableException(); + } break; case E_ELEMENTNOTENABLED: - ex = !string.IsNullOrEmpty(errorMessage) ? new Microsoft.UI.Xaml.Automation.ElementNotEnabledException(errorMessage) : new Microsoft.UI.Xaml.Automation.ElementNotEnabledException(); + if (FeatureSwitches.UseWindowsUIXamlProjections) + { + ex = !string.IsNullOrEmpty(errorMessage) ? new Windows.UI.Xaml.Automation.ElementNotEnabledException(errorMessage) : new Windows.UI.Xaml.Automation.ElementNotEnabledException(); + } + else + { + ex = !string.IsNullOrEmpty(errorMessage) ? new Microsoft.UI.Xaml.Automation.ElementNotEnabledException(errorMessage) : new Microsoft.UI.Xaml.Automation.ElementNotEnabledException(); + } break; case ERROR_INVALID_WINDOW_HANDLE: ex = new COMException( @@ -527,116 +555,3 @@ internal static Exception GetExceptionForHR(this Exception innerException, int h } } } - -namespace Microsoft.UI.Xaml -{ - namespace Automation - { -#if EMBED - internal -#else - public -#endif - class ElementNotAvailableException : Exception - { - public ElementNotAvailableException() - : base("The element is not available.") - { - HResult = WinRT.ExceptionHelpers.E_ELEMENTNOTAVAILABLE; - } - - public ElementNotAvailableException(string message) - : base(message) - { - HResult = WinRT.ExceptionHelpers.E_ELEMENTNOTAVAILABLE; - } - - public ElementNotAvailableException(string message, Exception innerException) - : base(message, innerException) - { - HResult = WinRT.ExceptionHelpers.E_ELEMENTNOTAVAILABLE; - } - } - -#if EMBED - internal -#else - public -#endif - class ElementNotEnabledException : Exception - { - public ElementNotEnabledException() - : base("The element is not enabled.") - { - HResult = WinRT.ExceptionHelpers.E_ELEMENTNOTENABLED; - } - - public ElementNotEnabledException(string message) - : base(message) - { - HResult = WinRT.ExceptionHelpers.E_ELEMENTNOTENABLED; - } - - public ElementNotEnabledException(string message, Exception innerException) - : base(message, innerException) - { - HResult = WinRT.ExceptionHelpers.E_ELEMENTNOTENABLED; - } - } - } - namespace Markup - { - -#if EMBED - internal -#else - public -#endif - class XamlParseException : Exception - { - public XamlParseException() - : base("XAML parsing failed.") - { - HResult = WinRT.ExceptionHelpers.E_XAMLPARSEFAILED; - } - - public XamlParseException(string message) - : base(message) - { - HResult = WinRT.ExceptionHelpers.E_XAMLPARSEFAILED; - } - - public XamlParseException(string message, Exception innerException) - : base(message, innerException) - { - HResult = WinRT.ExceptionHelpers.E_XAMLPARSEFAILED; - } - } - } - -#if EMBED - internal -#else - public -#endif - class LayoutCycleException : Exception - { - public LayoutCycleException() - : base("A cycle occurred while laying out the GUI.") - { - HResult = WinRT.ExceptionHelpers.E_LAYOUTCYCLE; - } - - public LayoutCycleException(string message) - : base(message) - { - HResult = WinRT.ExceptionHelpers.E_LAYOUTCYCLE; - } - - public LayoutCycleException(string message, Exception innerException) - : base(message, innerException) - { - HResult = WinRT.ExceptionHelpers.E_LAYOUTCYCLE; - } - } -} diff --git a/src/WinRT.Runtime/MatchingRefApiCompatBaseline.txt b/src/WinRT.Runtime/MatchingRefApiCompatBaseline.txt index 1b3f9a5d2..a226bc617 100644 --- a/src/WinRT.Runtime/MatchingRefApiCompatBaseline.txt +++ b/src/WinRT.Runtime/MatchingRefApiCompatBaseline.txt @@ -273,4 +273,8 @@ TypesMustExist : Type 'WinRT.GeneratedBindableCustomPropertyAttribute' does not TypesMustExist : Type 'Microsoft.UI.Xaml.Data.IBindableCustomPropertyImplementation' does not exist in the reference but it does exist in the implementation. MembersMustExist : Member 'public void ABI.System.Uri.DisposeAbiArray(System.Object)' does not exist in the reference but it does exist in the implementation. TypesMustExist : Type 'WinRT.EnumTypeDetails' does not exist in the reference but it does exist in the implementation. -Total Issues: 274 +TypesMustExist : Type 'Windows.UI.Xaml.LayoutCycleException' does not exist in the reference but it does exist in the implementation. +TypesMustExist : Type 'Windows.UI.Xaml.Automation.ElementNotAvailableException' does not exist in the reference but it does exist in the implementation. +TypesMustExist : Type 'Windows.UI.Xaml.Automation.ElementNotEnabledException' does not exist in the reference but it does exist in the implementation. +TypesMustExist : Type 'Windows.UI.Xaml.Markup.XamlParseException' does not exist in the reference but it does exist in the implementation. +Total Issues: 278 From 3ab785f5f3589be1ef6e9b59f3a6e52f1bfc793b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 5 Aug 2024 12:20:05 -0700 Subject: [PATCH 2/4] Add WUX exceptions unit tests --- src/Tests/AuthoringWuxConsumptionTest/test.cpp | 5 +++++ src/Tests/AuthoringWuxTest/Program.cs | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/Tests/AuthoringWuxConsumptionTest/test.cpp b/src/Tests/AuthoringWuxConsumptionTest/test.cpp index b182b935c..9c54886fa 100644 --- a/src/Tests/AuthoringWuxConsumptionTest/test.cpp +++ b/src/Tests/AuthoringWuxConsumptionTest/test.cpp @@ -64,6 +64,11 @@ TEST(AuthoringWuxTest, PropertyChanged) EXPECT_TRUE(eventTriggered); } +TEST(AuthoringWuxTest, XamlExceptionTypes) +{ + EXPECT_TRUE(XamlExceptionTypes::VerifyExceptionTypes()); +} + int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); diff --git a/src/Tests/AuthoringWuxTest/Program.cs b/src/Tests/AuthoringWuxTest/Program.cs index 004a3cb23..a43657bde 100644 --- a/src/Tests/AuthoringWuxTest/Program.cs +++ b/src/Tests/AuthoringWuxTest/Program.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Collections.Specialized; using System.ComponentModel; +using WinRT; #pragma warning disable CA1416 @@ -168,4 +169,21 @@ void IList.RemoveAt(int index) _list.RemoveAt(index); } } + + public static class XamlExceptionTypes + { + public static bool VerifyExceptionTypes() + { + const int E_XAMLPARSEFAILED = unchecked((int)0x802B000A); + const int E_LAYOUTCYCLE = unchecked((int)0x802B0014); + const int E_ELEMENTNOTENABLED = unchecked((int)0x802B001E); + const int E_ELEMENTNOTAVAILABLE = unchecked((int)0x802B001F); + + return + ExceptionHelpers.GetExceptionForHR(E_XAMLPARSEFAILED)?.GetType() == typeof(Windows.UI.Xaml.Markup.XamlParseException) && + ExceptionHelpers.GetExceptionForHR(E_LAYOUTCYCLE)?.GetType() == typeof(Windows.UI.Xaml.LayoutCycleException) && + ExceptionHelpers.GetExceptionForHR(E_ELEMENTNOTENABLED)?.GetType() == typeof(Windows.UI.Xaml.Automation.ElementNotEnabledException) && + ExceptionHelpers.GetExceptionForHR(E_ELEMENTNOTAVAILABLE)?.GetType() == typeof(Windows.UI.Xaml.Automation.ElementNotAvailableException); + } + } } From 788abf6b470ac256d317e48bd6dbd01d6c2baf85 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 5 Aug 2024 14:47:57 -0700 Subject: [PATCH 3/4] Remove WUX exception types downlevel --- ...elpers.Windows.cs => ExceptionHelpers.Windows.net5.cs} | 0 src/WinRT.Runtime/ExceptionHelpers.cs | 8 ++++++++ 2 files changed, 8 insertions(+) rename src/WinRT.Runtime/{ExceptionHelpers.Windows.cs => ExceptionHelpers.Windows.net5.cs} (100%) diff --git a/src/WinRT.Runtime/ExceptionHelpers.Windows.cs b/src/WinRT.Runtime/ExceptionHelpers.Windows.net5.cs similarity index 100% rename from src/WinRT.Runtime/ExceptionHelpers.Windows.cs rename to src/WinRT.Runtime/ExceptionHelpers.Windows.net5.cs diff --git a/src/WinRT.Runtime/ExceptionHelpers.cs b/src/WinRT.Runtime/ExceptionHelpers.cs index 75cef8a69..e77bcf557 100644 --- a/src/WinRT.Runtime/ExceptionHelpers.cs +++ b/src/WinRT.Runtime/ExceptionHelpers.cs @@ -225,41 +225,49 @@ private static Exception GetExceptionForHR(int hr, bool useGlobalErrorState, out ex = !string.IsNullOrEmpty(errorMessage) ? new InvalidOperationException(errorMessage) : new InvalidOperationException(); break; case E_XAMLPARSEFAILED: +#if NET if (FeatureSwitches.UseWindowsUIXamlProjections) { ex = !string.IsNullOrEmpty(errorMessage) ? new Windows.UI.Xaml.Markup.XamlParseException(errorMessage) : new Windows.UI.Xaml.Markup.XamlParseException(); } else +#endif { ex = !string.IsNullOrEmpty(errorMessage) ? new Microsoft.UI.Xaml.Markup.XamlParseException(errorMessage) : new Microsoft.UI.Xaml.Markup.XamlParseException(); } break; case E_LAYOUTCYCLE: +#if NET if (FeatureSwitches.UseWindowsUIXamlProjections) { ex = !string.IsNullOrEmpty(errorMessage) ? new Windows.UI.Xaml.LayoutCycleException(errorMessage) : new Windows.UI.Xaml.LayoutCycleException(); } else +#endif { ex = !string.IsNullOrEmpty(errorMessage) ? new Microsoft.UI.Xaml.LayoutCycleException(errorMessage) : new Microsoft.UI.Xaml.LayoutCycleException(); } break; case E_ELEMENTNOTAVAILABLE: +#if NET if (FeatureSwitches.UseWindowsUIXamlProjections) { ex = !string.IsNullOrEmpty(errorMessage) ? new Windows.UI.Xaml.Automation.ElementNotAvailableException(errorMessage) : new Windows.UI.Xaml.Automation.ElementNotAvailableException(); } else +#endif { ex = !string.IsNullOrEmpty(errorMessage) ? new Microsoft.UI.Xaml.Automation.ElementNotAvailableException(errorMessage) : new Microsoft.UI.Xaml.Automation.ElementNotAvailableException(); } break; case E_ELEMENTNOTENABLED: +#if NET if (FeatureSwitches.UseWindowsUIXamlProjections) { ex = !string.IsNullOrEmpty(errorMessage) ? new Windows.UI.Xaml.Automation.ElementNotEnabledException(errorMessage) : new Windows.UI.Xaml.Automation.ElementNotEnabledException(); } else +#endif { ex = !string.IsNullOrEmpty(errorMessage) ? new Microsoft.UI.Xaml.Automation.ElementNotEnabledException(errorMessage) : new Microsoft.UI.Xaml.Automation.ElementNotEnabledException(); } From 27f2eb82496e8e3fa1ca014e47d3ff3d6aefd2a6 Mon Sep 17 00:00:00 2001 From: Manodasan Wignarajah Date: Mon, 5 Aug 2024 20:59:48 -0700 Subject: [PATCH 4/4] Fix test --- .../AuthoringWuxConsumptionTest.exe.manifest | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Tests/AuthoringWuxConsumptionTest/AuthoringWuxConsumptionTest.exe.manifest b/src/Tests/AuthoringWuxConsumptionTest/AuthoringWuxConsumptionTest.exe.manifest index 3d69fa84a..e1edf0406 100644 --- a/src/Tests/AuthoringWuxConsumptionTest/AuthoringWuxConsumptionTest.exe.manifest +++ b/src/Tests/AuthoringWuxConsumptionTest/AuthoringWuxConsumptionTest.exe.manifest @@ -18,10 +18,14 @@ name="AuthoringWuxTest.MultipleInterfaceMappingClass" threadingModel="both" xmlns="urn:schemas-microsoft-com:winrt.v1" /> + - \ No newline at end of file +