|
| 1 | +// Copyright (c) 2025 .NET Foundation and Contributors. All rights reserved. |
| 2 | +// Licensed to the .NET Foundation under one or more agreements. |
| 3 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 4 | +// See the LICENSE file in the project root for full license information. |
| 5 | + |
| 6 | +using System.Diagnostics.CodeAnalysis; |
| 7 | +using System.Reactive; |
| 8 | +using System.Reactive.Linq; |
| 9 | +using ReactiveUI; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace ReactiveUI.AOTTests; |
| 13 | + |
| 14 | +/// <summary> |
| 15 | +/// Tests to verify that ReactiveUI works correctly in AOT (Ahead-of-Time) compilation scenarios. |
| 16 | +/// These tests ensure that the library doesn't rely on reflection in ways that break with AOT. |
| 17 | +/// </summary> |
| 18 | +public class AOTCompatibilityTests |
| 19 | +{ |
| 20 | + /// <summary> |
| 21 | + /// Tests that ReactiveObjects can be created and property changes work in AOT. |
| 22 | + /// </summary> |
| 23 | + [Fact] |
| 24 | + public void ReactiveObject_PropertyChanges_WorksInAOT() |
| 25 | + { |
| 26 | + var obj = new TestReactiveObject(); |
| 27 | + var propertyChanged = false; |
| 28 | + |
| 29 | + obj.PropertyChanged += (s, e) => propertyChanged = true; |
| 30 | + obj.TestProperty = "New Value"; |
| 31 | + |
| 32 | + Assert.True(propertyChanged); |
| 33 | + Assert.Equal("New Value", obj.TestProperty); |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Tests that ReactiveCommands can be created and executed in AOT. |
| 38 | + /// </summary> |
| 39 | + [Fact] |
| 40 | + [UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "Testing AOT-incompatible ReactiveCommand.Create method")] |
| 41 | + [UnconditionalSuppressMessage("AOT", "IL3050:Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.", Justification = "Testing AOT-incompatible ReactiveCommand.Create method")] |
| 42 | + public void ReactiveCommand_Create_WorksInAOT() |
| 43 | + { |
| 44 | + var executed = false; |
| 45 | + var command = ReactiveCommand.Create(() => executed = true); |
| 46 | + |
| 47 | + command.Execute().Subscribe(); |
| 48 | + |
| 49 | + Assert.True(executed); |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Tests that ReactiveCommands with parameters work in AOT. |
| 54 | + /// </summary> |
| 55 | + [Fact] |
| 56 | + [UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "Testing AOT-incompatible ReactiveCommand.Create method")] |
| 57 | + [UnconditionalSuppressMessage("AOT", "IL3050:Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.", Justification = "Testing AOT-incompatible ReactiveCommand.Create method")] |
| 58 | + public void ReactiveCommand_CreateWithParameter_WorksInAOT() |
| 59 | + { |
| 60 | + string? result = null; |
| 61 | + var command = ReactiveCommand.Create<string>(param => result = param); |
| 62 | + |
| 63 | + command.Execute("test").Subscribe(); |
| 64 | + |
| 65 | + Assert.Equal("test", result); |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Tests that ObservableAsPropertyHelper works in AOT. |
| 70 | + /// </summary> |
| 71 | + [Fact] |
| 72 | + [UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "Testing ToProperty with string-based property names which requires AOT suppression")] |
| 73 | + [UnconditionalSuppressMessage("AOT", "IL3050:Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.", Justification = "Testing ToProperty with string-based property names which requires AOT suppression")] |
| 74 | + public void ObservableAsPropertyHelper_WorksInAOT() |
| 75 | + { |
| 76 | + var obj = new TestReactiveObject(); |
| 77 | + |
| 78 | + // Test string-based property helper (should work in AOT) |
| 79 | + var helper = Observable.Return("computed value") |
| 80 | + .ToProperty(obj, nameof(TestReactiveObject.ComputedProperty)); |
| 81 | + |
| 82 | + Assert.Equal("computed value", helper.Value); |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Tests that WhenAnyValue works with string property names in AOT. |
| 87 | + /// </summary> |
| 88 | + [Fact] |
| 89 | + [UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "Testing WhenAnyValue which requires AOT suppression")] |
| 90 | + [UnconditionalSuppressMessage("AOT", "IL3050:Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.", Justification = "Testing WhenAnyValue which requires AOT suppression")] |
| 91 | + public void WhenAnyValue_StringPropertyNames_WorksInAOT() |
| 92 | + { |
| 93 | + var obj = new TestReactiveObject(); |
| 94 | + string? observedValue = null; |
| 95 | + |
| 96 | + // Using string property names should work in AOT |
| 97 | + obj.WhenAnyValue(x => x.TestProperty) |
| 98 | + .Subscribe(value => observedValue = value); |
| 99 | + |
| 100 | + obj.TestProperty = "test value"; |
| 101 | + |
| 102 | + Assert.Equal("test value", observedValue); |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Tests that interaction requests work in AOT. |
| 107 | + /// </summary> |
| 108 | + [Fact] |
| 109 | + public void Interaction_WorksInAOT() |
| 110 | + { |
| 111 | + var interaction = new Interaction<string, bool>(); |
| 112 | + var called = false; |
| 113 | + |
| 114 | + interaction.RegisterHandler(context => |
| 115 | + { |
| 116 | + called = true; |
| 117 | + context.SetOutput(true); |
| 118 | + }); |
| 119 | + |
| 120 | + var result = interaction.Handle("test").Wait(); |
| 121 | + |
| 122 | + Assert.True(called); |
| 123 | + Assert.True(result); |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Tests that INPC property observation works in AOT. |
| 128 | + /// </summary> |
| 129 | + [Fact] |
| 130 | + public void INPCPropertyObservation_WorksInAOT() |
| 131 | + { |
| 132 | + var obj = new TestReactiveObject(); |
| 133 | + var changes = new List<string?>(); |
| 134 | + |
| 135 | + obj.PropertyChanged += (s, e) => changes.Add(e.PropertyName); |
| 136 | + |
| 137 | + obj.TestProperty = "value1"; |
| 138 | + obj.TestProperty = "value2"; |
| 139 | + |
| 140 | + Assert.Contains(nameof(TestReactiveObject.TestProperty), changes); |
| 141 | + Assert.Equal(2, changes.Count(x => x == nameof(TestReactiveObject.TestProperty))); |
| 142 | + } |
| 143 | + |
| 144 | + /// <summary> |
| 145 | + /// Tests that ReactiveCommand.CreateFromObservable works in AOT scenarios. |
| 146 | + /// </summary> |
| 147 | + [Fact] |
| 148 | + [UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "Testing AOT-incompatible ReactiveCommand.CreateFromObservable method")] |
| 149 | + [UnconditionalSuppressMessage("AOT", "IL3050:Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.", Justification = "Testing AOT-incompatible ReactiveCommand.CreateFromObservable method")] |
| 150 | + public void ReactiveCommand_CreateFromObservable_WorksInAOT() |
| 151 | + { |
| 152 | + var result = 0; |
| 153 | + var command = ReactiveCommand.CreateFromObservable(() => Observable.Return(42)); |
| 154 | + |
| 155 | + command.Subscribe(x => result = x); |
| 156 | + command.Execute().Subscribe(); |
| 157 | + |
| 158 | + Assert.Equal(42, result); |
| 159 | + } |
| 160 | + |
| 161 | + /// <summary> |
| 162 | + /// Tests that string-based property bindings work in AOT (preferred pattern). |
| 163 | + /// </summary> |
| 164 | + [Fact] |
| 165 | + [UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "Testing ToProperty with string-based property names which requires AOT suppression")] |
| 166 | + [UnconditionalSuppressMessage("AOT", "IL3050:Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.", Justification = "Testing ToProperty with string-based property names which requires AOT suppression")] |
| 167 | + public void StringBasedPropertyBinding_WorksInAOT() |
| 168 | + { |
| 169 | + var obj = new TestReactiveObject(); |
| 170 | + var helper = Observable.Return("test") |
| 171 | + .ToProperty(obj, nameof(TestReactiveObject.ComputedProperty)); |
| 172 | + |
| 173 | + Assert.Equal("test", helper.Value); |
| 174 | + } |
| 175 | +} |
0 commit comments