A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using System;
5using System.Collections.Generic;
6using System.Linq;
7using System.Runtime.CompilerServices;
8using NUnit.Framework;
9using osu.Framework.Allocation;
10using osu.Framework.Configuration;
11using osu.Framework.Input.Handlers;
12using osu.Framework.Input.Handlers.Mouse;
13using osu.Framework.Platform;
14using osu.Framework.Testing;
15
16namespace osu.Framework.Tests.Configuration
17{
18 [TestFixture]
19 public class InputConfigManagerTest
20 {
21 private Storage storage;
22
23 [Test]
24 public void TestOldConfigPersists()
25 {
26 using (var host = new TestHeadlessGameHost(bypassCleanup: true))
27 {
28 host.Run(new TestGame((h, config) =>
29 {
30 storage = h.Storage;
31#pragma warning disable 618
32 config.SetValue(FrameworkSetting.CursorSensitivity, 5.0);
33#pragma warning restore 618
34 }));
35 }
36
37 // test with only FrameworkConfigManager configuration file present
38 storage.Delete(InputConfigManager.FILENAME);
39
40 double sensitivity = 0;
41
42 using (var host = new TestHeadlessGameHost())
43 {
44 host.Run(new TestGame((h, config) => sensitivity = h.AvailableInputHandlers.OfType<MouseHandler>().First().Sensitivity.Value));
45 }
46
47 Assert.AreEqual(5, sensitivity);
48 }
49
50 [Test]
51 public void TestNewConfigPersists()
52 {
53 using (var host = new TestHeadlessGameHost(bypassCleanup: true))
54 {
55 host.Run(new TestGame((h, config) =>
56 {
57 storage = h.Storage;
58 h.AvailableInputHandlers.OfType<MouseHandler>().First().Sensitivity.Value = 5;
59 }));
60 }
61
62 // test with only InputConfigManager configuration file present
63 storage.Delete(FrameworkConfigManager.FILENAME);
64
65 double sensitivity = 0;
66
67 using (var host = new TestHeadlessGameHost())
68 {
69 host.Run(new TestGame((h, config) => sensitivity = h.AvailableInputHandlers.OfType<MouseHandler>().First().Sensitivity.Value));
70 }
71
72 Assert.AreEqual(5, sensitivity);
73 }
74
75 public class TestHeadlessGameHost : TestRunHeadlessGameHost
76 {
77 public TestHeadlessGameHost([CallerMemberName] string caller = "", bool bypassCleanup = false)
78 : base(caller, bypassCleanup: bypassCleanup)
79 {
80 }
81
82 protected override IEnumerable<InputHandler> CreateAvailableInputHandlers() => new[]
83 {
84 new MouseHandler()
85 };
86 }
87
88 private class TestGame : Game
89 {
90 private readonly Action<GameHost, FrameworkConfigManager> action;
91
92 [Resolved]
93 private FrameworkConfigManager config { get; set; }
94
95 public TestGame(Action<GameHost, FrameworkConfigManager> action)
96 {
97 this.action = action;
98 }
99
100 protected override void LoadComplete()
101 {
102 base.LoadComplete();
103 action(Host, config);
104 }
105
106 protected override void Update()
107 {
108 base.Update();
109 Exit();
110 }
111 }
112 }
113}