microsoft/TypeAgent

Public

mirrored fromhttps://github.com/microsoft/TypeAgentAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
ecec6489db2ad85ccd1be99c0860dc3b057af2b1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

dotnet/autoShell.Tests/DisplayActionHandlerTests.cs

198lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using System.Text.Json;
5using autoShell.Handlers;
6using autoShell.Logging;
7using autoShell.Services;
8using Moq;
9
10namespace autoShell.Tests;
11
12public class DisplayActionHandlerTests
13{
14 private readonly Mock<IDisplayService> _displayMock = new();
15 private readonly Mock<ILogger> _loggerMock = new();
16 private readonly DisplayActionHandler _handler;
17
18 public DisplayActionHandlerTests()
19 {
20 _handler = new DisplayActionHandler(_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 _handler.Handle("ListResolutions", JsonDocument.Parse("{}").RootElement);
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 _handler.Handle("SetScreenResolution", JsonDocument.Parse("""{"width":1920,"height":1080}""").RootElement);
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, 60)).Returns("ok");
59
60 _handler.Handle("SetScreenResolution", JsonDocument.Parse("""{"width":1920,"height":1080,"refreshRate":60}""").RootElement);
61
62 _displayMock.Verify(d => d.SetResolution(1920, 1080, 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 _handler.Handle("SetScreenResolution", JsonDocument.Parse("""{"width":2560,"height":1440}""").RootElement);
74
75 _displayMock.Verify(d => d.SetResolution(2560, 1440, null), Times.Once);
76 }
77
78 /// <summary>
79 /// Verifies that zero-valued dimensions do not invoke the display service.
80 /// </summary>
81 [Fact]
82 public void SetScreenResolution_ZeroDimensions_DoesNotCallService()
83 {
84 _handler.Handle("SetScreenResolution", JsonDocument.Parse("""{"width":0,"height":0}""").RootElement);
85
86 _displayMock.Verify(d => d.SetResolution(It.IsAny<uint>(), It.IsAny<uint>(), It.IsAny<uint?>()), Times.Never);
87 }
88
89 /// <summary>
90 /// Verifies that missing dimensions do not invoke the display service.
91 /// </summary>
92 [Fact]
93 public void SetScreenResolution_MissingDimensions_DoesNotCallService()
94 {
95 _handler.Handle("SetScreenResolution", JsonDocument.Parse("{}").RootElement);
96
97 _displayMock.Verify(d => d.SetResolution(It.IsAny<uint>(), It.IsAny<uint>(), It.IsAny<uint?>()), Times.Never);
98 }
99
100 /// <summary>
101 /// Verifies that providing only width (no height) does not invoke the display service.
102 /// </summary>
103 [Fact]
104 public void SetScreenResolution_WidthOnly_DoesNotCallService()
105 {
106 _handler.Handle("SetScreenResolution", JsonDocument.Parse("""{"width":1920}""").RootElement);
107
108 _displayMock.Verify(d => d.SetResolution(It.IsAny<uint>(), It.IsAny<uint>(), It.IsAny<uint?>()), Times.Never);
109 }
110
111 /// <summary>
112 /// Verifies that providing only height (no width) does not invoke the display service.
113 /// </summary>
114 [Fact]
115 public void SetScreenResolution_HeightOnly_DoesNotCallService()
116 {
117 _handler.Handle("SetScreenResolution", JsonDocument.Parse("""{"height":1080}""").RootElement);
118
119 _displayMock.Verify(d => d.SetResolution(It.IsAny<uint>(), It.IsAny<uint>(), It.IsAny<uint?>()), Times.Never);
120 }
121
122 /// <summary>
123 /// Verifies that negative dimensions return failure and do not invoke the display service.
124 /// Negative int values would cause uint overflow if not caught.
125 /// </summary>
126 [Fact]
127 public void SetScreenResolution_NegativeDimensions_ReturnsFailure()
128 {
129 var result = _handler.Handle("SetScreenResolution", JsonDocument.Parse("""{"width":-100,"height":1080}""").RootElement);
130
131 Assert.False(result.Success);
132 Assert.Contains("positive", result.Message);
133 _displayMock.Verify(d => d.SetResolution(It.IsAny<uint>(), It.IsAny<uint>(), It.IsAny<uint?>()), Times.Never);
134 }
135
136 /// <summary>
137 /// Verifies that a negative refresh rate returns failure and does not invoke the display service.
138 /// </summary>
139 [Fact]
140 public void SetScreenResolution_NegativeRefreshRate_ReturnsFailure()
141 {
142 var result = _handler.Handle("SetScreenResolution", JsonDocument.Parse("""{"width":1920,"height":1080,"refreshRate":-60}""").RootElement);
143
144 Assert.False(result.Success);
145 Assert.Contains("refresh rate", result.Message, StringComparison.OrdinalIgnoreCase);
146 _displayMock.Verify(d => d.SetResolution(It.IsAny<uint>(), It.IsAny<uint>(), It.IsAny<uint?>()), Times.Never);
147 }
148
149 // --- SetTextSize ---
150
151 /// <summary>
152 /// Verifies that a valid integer text size percentage is forwarded to the display service.
153 /// </summary>
154 [Fact]
155 public void SetTextSize_ValidPercent_CallsService()
156 {
157 _handler.Handle("SetTextSize", JsonDocument.Parse("""{"size":150}""").RootElement);
158
159 _displayMock.Verify(d => d.SetTextSize(150), Times.Once);
160 }
161
162 /// <summary>
163 /// Verifies that non-numeric text size input does not invoke the display service.
164 /// </summary>
165 [Fact]
166 public void SetTextSize_InvalidInput_DoesNotCallService()
167 {
168 _handler.Handle("SetTextSize", JsonDocument.Parse("""{"size":"abc"}""").RootElement);
169
170 _displayMock.Verify(d => d.SetTextSize(It.IsAny<int>()), Times.Never);
171 }
172
173 // --- Unknown key ---
174
175 /// <summary>
176 /// Verifies that an unknown command key does not invoke any display service methods.
177 /// </summary>
178 [Fact]
179 public void Handle_UnknownKey_DoesNothing()
180 {
181 _handler.Handle("UnknownDisplayCmd", JsonDocument.Parse("{}").RootElement);
182
183 _displayMock.VerifyNoOtherCalls();
184 }
185
186 /// <summary>
187 /// Verifies that a JSON object with width, height, and refreshRate is forwarded to the display service.
188 /// </summary>
189 [Fact]
190 public void SetScreenResolution_ObjectFormatWithRefreshRate_CallsServiceWithRefresh()
191 {
192 _displayMock.Setup(d => d.SetResolution(2560, 1440, 144)).Returns("ok");
193
194 _handler.Handle("SetScreenResolution", JsonDocument.Parse("""{"width":2560,"height":1440,"refreshRate":144}""").RootElement);
195
196 _displayMock.Verify(d => d.SetResolution(2560, 1440, 144), Times.Once);
197 }
198}
199