2021-01-12 06:19:50 +00:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2021-01-12 06:19:50 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using NUnit.Framework;
|
2021-11-08 05:55:26 +00:00
|
|
|
using osu.Framework.Input;
|
2021-01-12 06:19:50 +00:00
|
|
|
using osu.Framework.Input.Bindings;
|
|
|
|
using osu.Framework.Platform;
|
|
|
|
using osu.Game.Database;
|
|
|
|
using osu.Game.Input;
|
|
|
|
using osu.Game.Input.Bindings;
|
2021-09-07 06:19:23 +00:00
|
|
|
using osu.Game.Rulesets;
|
2021-01-15 05:22:48 +00:00
|
|
|
using Realms;
|
2021-01-12 06:19:50 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Tests.Database
|
|
|
|
{
|
|
|
|
[TestFixture]
|
|
|
|
public partial class TestRealmKeyBindingStore
|
|
|
|
{
|
|
|
|
private NativeStorage storage;
|
|
|
|
|
|
|
|
private RealmKeyBindingStore keyBindingStore;
|
|
|
|
|
2022-01-24 10:59:58 +00:00
|
|
|
private RealmAccess realm;
|
2021-01-12 06:19:50 +00:00
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void SetUp()
|
|
|
|
{
|
|
|
|
var directory = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()));
|
|
|
|
|
|
|
|
storage = new NativeStorage(directory.FullName);
|
|
|
|
|
2022-01-24 10:59:58 +00:00
|
|
|
realm = new RealmAccess(storage, "test");
|
|
|
|
keyBindingStore = new RealmKeyBindingStore(realm, new ReadableKeyCombinationProvider());
|
2021-01-12 06:19:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestDefaultsPopulationAndQuery()
|
|
|
|
{
|
2021-07-04 08:59:39 +00:00
|
|
|
Assert.That(queryCount(), Is.EqualTo(0));
|
2021-01-12 06:19:50 +00:00
|
|
|
|
|
|
|
KeyBindingContainer testContainer = new TestKeyBindingContainer();
|
|
|
|
|
2021-09-07 06:19:23 +00:00
|
|
|
keyBindingStore.Register(testContainer, Enumerable.Empty<RulesetInfo>());
|
2021-01-12 06:19:50 +00:00
|
|
|
|
2021-07-04 08:59:39 +00:00
|
|
|
Assert.That(queryCount(), Is.EqualTo(3));
|
2021-01-12 06:19:50 +00:00
|
|
|
|
2021-07-04 08:59:39 +00:00
|
|
|
Assert.That(queryCount(GlobalAction.Back), Is.EqualTo(1));
|
|
|
|
Assert.That(queryCount(GlobalAction.Select), Is.EqualTo(2));
|
2021-01-12 06:19:50 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 06:26:32 +00:00
|
|
|
[Test]
|
|
|
|
public void TestDefaultsPopulationRemovesExcess()
|
|
|
|
{
|
|
|
|
Assert.That(queryCount(), Is.EqualTo(0));
|
|
|
|
|
|
|
|
KeyBindingContainer testContainer = new TestKeyBindingContainer();
|
|
|
|
|
|
|
|
// Add some excess bindings for an action which only supports 1.
|
2022-01-25 04:09:47 +00:00
|
|
|
realm.Write(r =>
|
2021-12-13 06:26:32 +00:00
|
|
|
{
|
2022-01-25 04:09:47 +00:00
|
|
|
r.Add(new RealmKeyBinding(GlobalAction.Back, new KeyCombination(InputKey.A)));
|
|
|
|
r.Add(new RealmKeyBinding(GlobalAction.Back, new KeyCombination(InputKey.S)));
|
|
|
|
r.Add(new RealmKeyBinding(GlobalAction.Back, new KeyCombination(InputKey.D)));
|
2022-01-21 08:08:20 +00:00
|
|
|
});
|
2021-12-13 06:26:32 +00:00
|
|
|
|
|
|
|
Assert.That(queryCount(GlobalAction.Back), Is.EqualTo(3));
|
|
|
|
|
|
|
|
keyBindingStore.Register(testContainer, Enumerable.Empty<RulesetInfo>());
|
|
|
|
|
|
|
|
Assert.That(queryCount(GlobalAction.Back), Is.EqualTo(1));
|
|
|
|
}
|
|
|
|
|
2021-07-04 08:59:39 +00:00
|
|
|
private int queryCount(GlobalAction? match = null)
|
|
|
|
{
|
2022-01-25 04:04:05 +00:00
|
|
|
return realm.Run(r =>
|
2021-07-04 08:59:39 +00:00
|
|
|
{
|
2022-01-25 04:04:05 +00:00
|
|
|
var results = r.All<RealmKeyBinding>();
|
2021-07-04 08:59:39 +00:00
|
|
|
if (match.HasValue)
|
|
|
|
results = results.Where(k => k.ActionInt == (int)match.Value);
|
|
|
|
return results.Count();
|
2022-01-21 08:08:20 +00:00
|
|
|
});
|
2021-07-04 08:59:39 +00:00
|
|
|
}
|
2021-01-13 07:53:04 +00:00
|
|
|
|
2021-01-12 06:19:50 +00:00
|
|
|
[Test]
|
|
|
|
public void TestUpdateViaQueriedReference()
|
|
|
|
{
|
|
|
|
KeyBindingContainer testContainer = new TestKeyBindingContainer();
|
|
|
|
|
2021-09-07 06:19:23 +00:00
|
|
|
keyBindingStore.Register(testContainer, Enumerable.Empty<RulesetInfo>());
|
2021-01-12 06:19:50 +00:00
|
|
|
|
2022-01-24 10:59:58 +00:00
|
|
|
realm.Run(outerRealm =>
|
2021-07-04 08:59:39 +00:00
|
|
|
{
|
2022-01-21 08:08:20 +00:00
|
|
|
var backBinding = outerRealm.All<RealmKeyBinding>().Single(k => k.ActionInt == (int)GlobalAction.Back);
|
2021-01-12 06:19:50 +00:00
|
|
|
|
2021-07-04 08:59:39 +00:00
|
|
|
Assert.That(backBinding.KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.Escape }));
|
2021-01-12 06:19:50 +00:00
|
|
|
|
2021-07-04 08:59:39 +00:00
|
|
|
var tsr = ThreadSafeReference.Create(backBinding);
|
2021-01-13 08:59:47 +00:00
|
|
|
|
2022-01-24 10:59:58 +00:00
|
|
|
realm.Run(innerRealm =>
|
2021-07-04 08:59:39 +00:00
|
|
|
{
|
2022-01-21 08:08:20 +00:00
|
|
|
var binding = innerRealm.ResolveReference(tsr);
|
|
|
|
innerRealm.Write(() => binding.KeyCombination = new KeyCombination(InputKey.BackSpace));
|
|
|
|
});
|
2021-01-12 06:19:50 +00:00
|
|
|
|
2021-07-04 08:59:39 +00:00
|
|
|
Assert.That(backBinding.KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.BackSpace }));
|
2021-01-12 06:19:50 +00:00
|
|
|
|
2021-07-04 08:59:39 +00:00
|
|
|
// check still correct after re-query.
|
2022-01-21 08:08:20 +00:00
|
|
|
backBinding = outerRealm.All<RealmKeyBinding>().Single(k => k.ActionInt == (int)GlobalAction.Back);
|
2021-07-04 08:59:39 +00:00
|
|
|
Assert.That(backBinding.KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.BackSpace }));
|
2022-01-21 08:08:20 +00:00
|
|
|
});
|
2021-01-12 06:19:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[TearDown]
|
|
|
|
public void TearDown()
|
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
realm.Dispose();
|
2021-01-12 06:19:50 +00:00
|
|
|
storage.DeleteDirectory(string.Empty);
|
|
|
|
}
|
|
|
|
|
|
|
|
public partial class TestKeyBindingContainer : KeyBindingContainer
|
|
|
|
{
|
|
|
|
public override IEnumerable<IKeyBinding> DefaultKeyBindings =>
|
|
|
|
new[]
|
|
|
|
{
|
|
|
|
new KeyBinding(InputKey.Escape, GlobalAction.Back),
|
|
|
|
new KeyBinding(InputKey.Enter, GlobalAction.Select),
|
|
|
|
new KeyBinding(InputKey.Space, GlobalAction.Select),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|