-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsControllerTests.cs
More file actions
314 lines (266 loc) · 12 KB
/
Copy pathSettingsControllerTests.cs
File metadata and controls
314 lines (266 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using SwitchifyPc.Core.Settings;
using SwitchifyPc.Core.Startup;
using SwitchifyPc.Core.Ui;
using SwitchifyPc.Core.Updates;
using SwitchifyPc.Core.Pairing;
namespace SwitchifyPc.Tests;
public sealed class SettingsControllerTests
{
[Fact]
public async Task LoadPopulatesAllSettings()
{
SettingsViewModel viewModel = new();
FakeStartupSettings startup = new(new SystemStartupSettings(
Supported: true,
StartWithSystem: true,
StartsHidden: true,
Reason: null,
Registration: null));
FakePointerSettings pointer = new(new PointerMovementSettings(150));
FakeCursorOverlaySettings cursor = new(CursorOverlaySettingsModel.Default with
{
Enabled = false,
Color = "blue"
});
FakeUpdates updates = new(UpdateState.CreateInitial("0.2.0") with
{
Info = UpdateState.CreateInitial("0.2.0").Info with
{
Status = UpdateCheckStatus.UpToDate,
LatestVersion = "0.2.0"
}
});
MemoryPairingStore pairing = new(new PairingState(
"desktop-1",
[
new PairedDevice("device-1", "Pixel 9", "secret-token", 3_723_000, 3_724_000)
]));
await new SettingsController(viewModel, startup, pointer, cursor, updates, pairing).LoadAsync();
Assert.True(viewModel.StartWithSystem);
Assert.Equal(150, viewModel.PointerScalePercent);
Assert.False(viewModel.CursorOverlayEnabled);
Assert.Equal("Blue", viewModel.CursorOverlayColor);
Assert.Equal("Switchify PC is up to date.", viewModel.UpdateStatusMessage);
Assert.Equal("1 paired device.", viewModel.PairedDevicesMessage);
PairedDeviceSettingsView device = Assert.Single(viewModel.PairedDevices);
Assert.Equal("Pixel 9", device.DeviceName);
Assert.DoesNotContain("secret", device.ToString(), StringComparison.OrdinalIgnoreCase);
}
[Fact]
public async Task SetStartWithSystemDelegatesAndUpdatesViewModel()
{
SettingsViewModel viewModel = new();
FakeStartupSettings startup = new(new SystemStartupSettings(false, false, true, "unpackaged"));
SettingsController controller = CreateController(viewModel, startup: startup);
await controller.SetStartWithSystemAsync(true);
Assert.True(startup.LastSetValue);
Assert.True(viewModel.StartWithSystem);
Assert.Equal("Switchify PC will start hidden when you sign in.", viewModel.StartWithSystemMessage);
}
[Fact]
public void SetPointerScaleSavesNormalizedValueAndUpdatesDerivedPercentages()
{
SettingsViewModel viewModel = new();
FakePointerSettings pointer = new(PointerMovementSettingsModel.Default);
SettingsController controller = CreateController(viewModel, pointer: pointer);
PointerMovementSettings saved = controller.SetPointerScalePercent(212);
Assert.Equal(200, saved.ScalePercent);
Assert.Equal(new PointerMovementSettings(200), pointer.Saved);
Assert.Equal(200, viewModel.PointerScalePercent);
Assert.Equal("9%", viewModel.PointerSmall);
Assert.Equal("24%", viewModel.PointerMedium);
Assert.Equal("50%", viewModel.PointerLarge);
}
[Fact]
public async Task CursorOverlayActionsSaveNormalizedSettingsAndUpdateViewModel()
{
SettingsViewModel viewModel = new();
FakeCursorOverlaySettings cursor = new(CursorOverlaySettingsModel.Default);
SettingsController controller = CreateController(viewModel, cursor: cursor);
await controller.LoadAsync();
CursorOverlaySettings saved = controller.SetCursorOverlayColor("blue");
saved = controller.SetCursorOverlayVisibility("whileControlling");
saved = controller.SetCursorOverlaySize("not-a-size");
saved = controller.SetCursorOverlayEnabled(false);
saved = controller.SetCursorOverlayCrosshairs(true);
Assert.Equal(CursorOverlaySettingsModel.Default.Size, saved.Size);
Assert.Equal("blue", saved.Color);
Assert.False(saved.Enabled);
Assert.True(saved.Crosshairs);
Assert.Equal("whileControlling", saved.Visibility);
Assert.Equal(saved, cursor.Saved);
Assert.Equal("Blue", viewModel.CursorOverlayColor);
Assert.Equal("While controlling", viewModel.CursorOverlayVisibility);
Assert.False(viewModel.CursorOverlayEnabled);
Assert.True(viewModel.CursorOverlayCrosshairs);
}
[Fact]
public async Task UpdateActionsDelegateAndRefreshViewModel()
{
SettingsViewModel viewModel = new();
UpdateState initial = UpdateState.CreateInitial("0.2.0");
UpdateState available = initial with
{
Info = initial.Info with
{
Status = UpdateCheckStatus.UpdateAvailable,
LatestVersion = "0.2.1"
}
};
UpdateState downloaded = available with
{
Download = new UpdateDownloadProgress(UpdateDownloadStatus.Downloaded, 100, 100, 100)
};
FakeUpdates updates = new(initial)
{
CheckResult = available,
DownloadResult = downloaded,
InstallResult = UpdateInstallResult.Success()
};
SettingsController controller = CreateController(viewModel, updates: updates);
await controller.CheckForUpdatesAsync();
Assert.Equal("Update available: v0.2.1.", viewModel.UpdateStatusMessage);
Assert.True(viewModel.CanDownloadUpdate);
await controller.DownloadUpdateAsync();
Assert.Equal("Update downloaded and ready to install.", viewModel.UpdateDownloadMessage);
Assert.True(viewModel.CanInstallUpdate);
UpdateInstallResult result = await controller.InstallDownloadedUpdateAsync();
Assert.True(result.Ok);
Assert.Equal(1, updates.InstallCalls);
Assert.True(viewModel.CanInstallUpdate);
}
[Fact]
public async Task ForgetPairedDeviceRemovesPersistedDeviceAndRefreshesViewModel()
{
SettingsViewModel viewModel = new();
MemoryPairingStore pairingStore = new(new PairingState(
"desktop-1",
[
new PairedDevice("device-1", "Pixel 9", "secret-token-1", 3_723_000, null),
new PairedDevice("device-2", "Galaxy Tab", "secret-token-2", 3_724_000, null)
]));
SettingsController controller = CreateController(viewModel, pairingStore: pairingStore);
await controller.LoadAsync();
bool removed = await controller.ForgetPairedDeviceAsync("device-1");
Assert.True(removed);
PairingState state = await pairingStore.LoadAsync();
Assert.DoesNotContain(state.PairedDevices, device => device.DeviceId == "device-1");
PairedDevice remaining = Assert.Single(state.PairedDevices);
Assert.Equal("device-2", remaining.DeviceId);
PairedDeviceSettingsView view = Assert.Single(viewModel.PairedDevices);
Assert.Equal("Galaxy Tab", view.DeviceName);
Assert.Equal("1 paired device.", viewModel.PairedDevicesMessage);
}
[Fact]
public async Task ForgetPairedDeviceReturnsFalseForMissingDevice()
{
SettingsViewModel viewModel = new();
MemoryPairingStore pairingStore = new(new PairingState(
"desktop-1",
[
new PairedDevice("device-1", "Pixel 9", "secret-token", 3_723_000, null)
]));
SettingsController controller = CreateController(viewModel, pairingStore: pairingStore);
await controller.LoadAsync();
bool removed = await controller.ForgetPairedDeviceAsync("missing-device");
Assert.False(removed);
Assert.Single((await pairingStore.LoadAsync()).PairedDevices);
Assert.Single(viewModel.PairedDevices);
}
[Fact]
public void ApplyUpdateStateUpdatesViewModelFromPushEvents()
{
SettingsViewModel viewModel = new();
SettingsController controller = CreateController(viewModel);
controller.ApplyUpdateState(UpdateState.CreateInitial("0.2.0") with
{
Info = UpdateState.CreateInitial("0.2.0").Info with
{
Status = UpdateCheckStatus.CheckFailed,
Reason = UpdateFailureReason.NetworkError
}
});
Assert.Equal("Could not check for updates.", viewModel.UpdateStatusMessage);
}
private static SettingsController CreateController(
SettingsViewModel viewModel,
FakeStartupSettings? startup = null,
FakePointerSettings? pointer = null,
FakeCursorOverlaySettings? cursor = null,
FakeUpdates? updates = null,
IPairingStore? pairingStore = null)
{
return new SettingsController(
viewModel,
startup ?? new FakeStartupSettings(new SystemStartupSettings(false, false, true, "unpackaged")),
pointer ?? new FakePointerSettings(PointerMovementSettingsModel.Default),
cursor ?? new FakeCursorOverlaySettings(CursorOverlaySettingsModel.Default),
updates ?? new FakeUpdates(UpdateState.CreateInitial("0.2.0")),
pairingStore ?? new MemoryPairingStore());
}
private sealed class FakeStartupSettings(SystemStartupSettings initial) : ISystemStartupSettingsService
{
private SystemStartupSettings settings = initial;
public bool? LastSetValue { get; private set; }
public Task<SystemStartupSettings> GetSettingsAsync() => Task.FromResult(settings);
public Task<SystemStartupSettings> SetStartWithSystemAsync(bool enabled)
{
LastSetValue = enabled;
settings = new SystemStartupSettings(
Supported: true,
StartWithSystem: enabled,
StartsHidden: true,
Reason: null,
Registration: null);
return Task.FromResult(settings);
}
}
private sealed class FakePointerSettings(PointerMovementSettings initial) : IPointerMovementSettingsStore
{
private PointerMovementSettings settings = initial;
public PointerMovementSettings? Saved { get; private set; }
public PointerMovementSettings Load() => settings;
public PointerMovementSettings Save(PointerMovementSettings next)
{
settings = PointerMovementSettingsModel.Normalize(next);
Saved = settings;
return settings;
}
}
private sealed class FakeCursorOverlaySettings(CursorOverlaySettings initial) : ICursorOverlaySettingsStore
{
private CursorOverlaySettings settings = initial;
public CursorOverlaySettings? Saved { get; private set; }
public CursorOverlaySettings Load() => settings;
public CursorOverlaySettings Save(CursorOverlaySettings next)
{
settings = CursorOverlaySettingsModel.Normalize(next);
Saved = settings;
return settings;
}
}
private sealed class FakeUpdates(UpdateState initial) : IUpdateSettingsService
{
private UpdateState state = initial;
public UpdateState CheckResult { get; init; } = initial;
public UpdateState DownloadResult { get; init; } = initial;
public UpdateInstallResult InstallResult { get; init; } = UpdateInstallResult.Failure(UpdateInstallFailureReason.NotDownloaded);
public int InstallCalls { get; private set; }
public UpdateState GetState() => state;
public Task<UpdateState> CheckForUpdatesAsync(CancellationToken cancellationToken = default)
{
state = CheckResult;
return Task.FromResult(state);
}
public Task<UpdateState> DownloadUpdateAsync(CancellationToken cancellationToken = default)
{
state = DownloadResult;
return Task.FromResult(state);
}
public Task<UpdateInstallResult> InstallDownloadedUpdateAsync(CancellationToken cancellationToken = default)
{
InstallCalls++;
return Task.FromResult(InstallResult);
}
}
}