microsoft/TypeAgent
Publicmirrored fromhttps://github.com/microsoft/TypeAgentAvailable
dotnet/autoShell.Tests/DisplayCommandHandlerTests.cs
145lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using autoShell.Handlers; |
| 5 | using autoShell.Logging; |
| 6 | using autoShell.Services; |
| 7 | using Moq; |
| 8 | using Newtonsoft.Json.Linq; |
| 9 | |
| 10 | namespace autoShell.Tests; |
| 11 | |
| 12 | public class DisplayCommandHandlerTests |
| 13 | { |
| 14 | private readonly Mock<IDisplayService> _displayMock = new(); |
| 15 | private readonly Mock<ILogger> _loggerMock = new(); |
| 16 | private readonly DisplayCommandHandler _handler; |
| 17 | |
| 18 | public DisplayCommandHandlerTests() |
| 19 | { |
| 20 | _handler = new DisplayCommandHandler(_displayMock.Object, _loggerMock.Object); |
| 21 | } |
| 22 | // --- ListResolutions --- |
| 23 | |
| 24 | /// <summary> |
| 25 | /// Verifies that the ListResolutions command calls the display service to list available resolutions. |
| 26 | /// </summary> |
| 27 | [Fact] |
| 28 | public void ListResolutions_CallsService() |
| 29 | { |
| 30 | _displayMock.Setup(d => d.ListResolutions()).Returns("[{\"Width\":1920}]"); |
| 31 | |
| 32 | Handle("ListResolutions", ""); |
| 33 | |
| 34 | _displayMock.Verify(d => d.ListResolutions(), Times.Once); |
| 35 | } |
| 36 | |
| 37 | // --- SetScreenResolution --- |
| 38 | |
| 39 | /// <summary> |
| 40 | /// Verifies that a "WIDTHxHEIGHT" string is parsed and forwarded to the display service. |
| 41 | /// </summary> |
| 42 | [Fact] |
| 43 | public void SetScreenResolution_StringFormat_CallsServiceWithParsedValues() |
| 44 | { |
| 45 | _displayMock.Setup(d => d.SetResolution(1920, 1080, null)).Returns("ok"); |
| 46 | |
| 47 | Handle("SetScreenResolution", "1920x1080"); |
| 48 | |
| 49 | _displayMock.Verify(d => d.SetResolution(1920, 1080, null), Times.Once); |
| 50 | } |
| 51 | |
| 52 | /// <summary> |
| 53 | /// Verifies that a "WIDTHxHEIGHT@RATE" string includes the refresh rate in the service call. |
| 54 | /// </summary> |
| 55 | [Fact] |
| 56 | public void SetScreenResolution_WithRefreshRate_CallsServiceWithRefresh() |
| 57 | { |
| 58 | _displayMock.Setup(d => d.SetResolution(1920, 1080, (uint)60)).Returns("ok"); |
| 59 | |
| 60 | Handle("SetScreenResolution", "1920x1080@60"); |
| 61 | |
| 62 | _displayMock.Verify(d => d.SetResolution(1920, 1080, (uint)60), Times.Once); |
| 63 | } |
| 64 | |
| 65 | /// <summary> |
| 66 | /// Verifies that a JSON object with width and height properties is forwarded to the display service. |
| 67 | /// </summary> |
| 68 | [Fact] |
| 69 | public void SetScreenResolution_ObjectFormat_CallsServiceWithValues() |
| 70 | { |
| 71 | _displayMock.Setup(d => d.SetResolution(2560, 1440, null)).Returns("ok"); |
| 72 | |
| 73 | var rawValue = JObject.FromObject(new { width = 2560, height = 1440 }); |
| 74 | _handler.Handle("SetScreenResolution", "", rawValue); |
| 75 | |
| 76 | _displayMock.Verify(d => d.SetResolution(2560, 1440, null), Times.Once); |
| 77 | } |
| 78 | |
| 79 | /// <summary> |
| 80 | /// Verifies that an invalid resolution string does not invoke the display service. |
| 81 | /// </summary> |
| 82 | [Fact] |
| 83 | public void SetScreenResolution_InvalidFormat_DoesNotCallService() |
| 84 | { |
| 85 | Handle("SetScreenResolution", "invalid"); |
| 86 | |
| 87 | _displayMock.Verify(d => d.SetResolution(It.IsAny<uint>(), It.IsAny<uint>(), It.IsAny<uint?>()), Times.Never); |
| 88 | } |
| 89 | |
| 90 | // --- SetTextSize --- |
| 91 | |
| 92 | /// <summary> |
| 93 | /// Verifies that a valid integer text size percentage is forwarded to the display service. |
| 94 | /// </summary> |
| 95 | [Fact] |
| 96 | public void SetTextSize_ValidPercent_CallsService() |
| 97 | { |
| 98 | Handle("SetTextSize", "150"); |
| 99 | |
| 100 | _displayMock.Verify(d => d.SetTextSize(150), Times.Once); |
| 101 | } |
| 102 | |
| 103 | /// <summary> |
| 104 | /// Verifies that non-numeric text size input does not invoke the display service. |
| 105 | /// </summary> |
| 106 | [Fact] |
| 107 | public void SetTextSize_InvalidInput_DoesNotCallService() |
| 108 | { |
| 109 | Handle("SetTextSize", "abc"); |
| 110 | |
| 111 | _displayMock.Verify(d => d.SetTextSize(It.IsAny<int>()), Times.Never); |
| 112 | } |
| 113 | |
| 114 | // --- Unknown key --- |
| 115 | |
| 116 | /// <summary> |
| 117 | /// Verifies that an unknown command key does not invoke any display service methods. |
| 118 | /// </summary> |
| 119 | [Fact] |
| 120 | public void Handle_UnknownKey_DoesNothing() |
| 121 | { |
| 122 | Handle("UnknownDisplayCmd", "value"); |
| 123 | |
| 124 | _displayMock.VerifyNoOtherCalls(); |
| 125 | } |
| 126 | |
| 127 | /// <summary> |
| 128 | /// Verifies that a JSON object with width, height, and refreshRate is forwarded to the display service. |
| 129 | /// </summary> |
| 130 | [Fact] |
| 131 | public void SetScreenResolution_ObjectFormatWithRefreshRate_CallsServiceWithRefresh() |
| 132 | { |
| 133 | _displayMock.Setup(d => d.SetResolution(2560, 1440, (uint)144)).Returns("ok"); |
| 134 | |
| 135 | var rawValue = JObject.FromObject(new { width = 2560, height = 1440, refreshRate = 144 }); |
| 136 | _handler.Handle("SetScreenResolution", "", rawValue); |
| 137 | |
| 138 | _displayMock.Verify(d => d.SetResolution(2560, 1440, (uint)144), Times.Once); |
| 139 | } |
| 140 | |
| 141 | private void Handle(string key, string value) |
| 142 | { |
| 143 | _handler.Handle(key, value, JToken.FromObject(value)); |
| 144 | } |
| 145 | } |
| 146 | |