|
| 1 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 2 | +using System; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Uno.UI.Hosting; |
| 5 | + |
| 6 | +namespace Uno.UI.Tests.Hosting |
| 7 | +{ |
| 8 | + [TestClass] |
| 9 | + public class Given_UnoPlatformHost |
| 10 | + { |
| 11 | + [TestMethod] |
| 12 | + public void When_Exception_In_Initialize_Should_Throw_Actual_Exception() |
| 13 | + { |
| 14 | + // Arrange |
| 15 | + var host = new TestHost_ThrowsInInitialize(); |
| 16 | + |
| 17 | + // Act & Assert |
| 18 | + var exception = Assert.ThrowsException<InvalidOperationException>(() => host.Run()); |
| 19 | + Assert.AreEqual("Test exception in Initialize", exception.Message); |
| 20 | + } |
| 21 | + |
| 22 | + [TestMethod] |
| 23 | + public void When_Exception_In_InitializeAsync_Should_Throw_Actual_Exception() |
| 24 | + { |
| 25 | + // Arrange |
| 26 | + var host = new TestHost_ThrowsInInitializeAsync(); |
| 27 | + |
| 28 | + // Act & Assert |
| 29 | + var exception = Assert.ThrowsException<InvalidOperationException>(() => host.Run()); |
| 30 | + Assert.AreEqual("Test exception in InitializeAsync", exception.Message); |
| 31 | + } |
| 32 | + |
| 33 | + [TestMethod] |
| 34 | + public void When_Run_With_Async_RunLoop_Should_Throw_InvalidOperationException() |
| 35 | + { |
| 36 | + // Arrange |
| 37 | + var host = new TestHost_AsyncRunLoop(); |
| 38 | + |
| 39 | + // Act & Assert |
| 40 | + var exception = Assert.ThrowsException<InvalidOperationException>(() => host.Run()); |
| 41 | + Assert.IsTrue(exception.Message.Contains("requires calling 'await host.RunAsync()' instead of 'host.Run()'")); |
| 42 | + } |
| 43 | + |
| 44 | + [TestMethod] |
| 45 | + public void When_Run_With_Synchronous_Completion_Should_Not_Throw() |
| 46 | + { |
| 47 | + // Arrange |
| 48 | + var host = new TestHost_SynchronousCompletion(); |
| 49 | + |
| 50 | + // Act & Assert - should not throw |
| 51 | + host.Run(); |
| 52 | + Assert.IsTrue(host.InitializeCalled); |
| 53 | + } |
| 54 | + |
| 55 | + [TestMethod] |
| 56 | + public async Task When_RunAsync_With_Exception_Should_Throw_Actual_Exception() |
| 57 | + { |
| 58 | + // Arrange |
| 59 | + var host = new TestHost_ThrowsInInitialize(); |
| 60 | + |
| 61 | + // Act & Assert |
| 62 | + var exception = await Assert.ThrowsExceptionAsync<InvalidOperationException>(() => host.RunAsync()); |
| 63 | + Assert.AreEqual("Test exception in Initialize", exception.Message); |
| 64 | + } |
| 65 | + |
| 66 | + // Test host implementations |
| 67 | + private class TestHost_ThrowsInInitialize : UnoPlatformHost |
| 68 | + { |
| 69 | + protected override void Initialize() |
| 70 | + { |
| 71 | + throw new InvalidOperationException("Test exception in Initialize"); |
| 72 | + } |
| 73 | + |
| 74 | + protected override Task RunLoop() |
| 75 | + { |
| 76 | + return Task.CompletedTask; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private class TestHost_ThrowsInInitializeAsync : UnoPlatformHost |
| 81 | + { |
| 82 | + protected override void Initialize() |
| 83 | + { |
| 84 | + // No-op |
| 85 | + } |
| 86 | + |
| 87 | + protected override Task InitializeAsync() |
| 88 | + { |
| 89 | + throw new InvalidOperationException("Test exception in InitializeAsync"); |
| 90 | + } |
| 91 | + |
| 92 | + protected override Task RunLoop() |
| 93 | + { |
| 94 | + return Task.CompletedTask; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private class TestHost_AsyncRunLoop : UnoPlatformHost |
| 99 | + { |
| 100 | + protected override void Initialize() |
| 101 | + { |
| 102 | + // No-op |
| 103 | + } |
| 104 | + |
| 105 | + protected override async Task RunLoop() |
| 106 | + { |
| 107 | + await Task.Delay(100); // Simulate async operation |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private class TestHost_SynchronousCompletion : UnoPlatformHost |
| 112 | + { |
| 113 | + public bool InitializeCalled { get; private set; } |
| 114 | + |
| 115 | + protected override void Initialize() |
| 116 | + { |
| 117 | + InitializeCalled = true; |
| 118 | + } |
| 119 | + |
| 120 | + protected override Task RunLoop() |
| 121 | + { |
| 122 | + return Task.CompletedTask; |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments