2021-01-12 14:19:50 +08: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.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using NUnit.Framework;
|
|
|
|
using osu.Framework.Input.Bindings;
|
|
|
|
using osu.Framework.Platform;
|
|
|
|
using osu.Game.Database;
|
|
|
|
using osu.Game.Input;
|
|
|
|
using osu.Game.Input.Bindings;
|
2021-01-15 13:22:48 +08:00
|
|
|
using Realms;
|
2021-01-12 14:19:50 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Tests.Database
|
|
|
|
{
|
|
|
|
[TestFixture]
|
|
|
|
public class TestRealmKeyBindingStore
|
|
|
|
{
|
|
|
|
private NativeStorage storage;
|
|
|
|
|
|
|
|
private RealmKeyBindingStore keyBindingStore;
|
|
|
|
|
|
|
|
private RealmContextFactory realmContextFactory;
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void SetUp()
|
|
|
|
{
|
|
|
|
var directory = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()));
|
|
|
|
|
|
|
|
storage = new NativeStorage(directory.FullName);
|
|
|
|
|
|
|
|
realmContextFactory = new RealmContextFactory(storage);
|
|
|
|
keyBindingStore = new RealmKeyBindingStore(realmContextFactory);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestDefaultsPopulationAndQuery()
|
|
|
|
{
|
2021-01-13 15:53:04 +08:00
|
|
|
Assert.That(query().Count, Is.EqualTo(0));
|
2021-01-12 14:19:50 +08:00
|
|
|
|
|
|
|
KeyBindingContainer testContainer = new TestKeyBindingContainer();
|
|
|
|
|
|
|
|
keyBindingStore.Register(testContainer);
|
|
|
|
|
2021-01-13 15:53:04 +08:00
|
|
|
Assert.That(query().Count, Is.EqualTo(3));
|
2021-01-12 14:19:50 +08:00
|
|
|
|
2021-01-13 17:07:35 +08:00
|
|
|
Assert.That(query().Where(k => k.ActionInt == (int)GlobalAction.Back).Count, Is.EqualTo(1));
|
|
|
|
Assert.That(query().Where(k => k.ActionInt == (int)GlobalAction.Select).Count, Is.EqualTo(2));
|
2021-01-12 14:19:50 +08:00
|
|
|
}
|
|
|
|
|
2021-01-13 16:35:00 +08:00
|
|
|
private IQueryable<RealmKeyBinding> query() => realmContextFactory.Context.All<RealmKeyBinding>();
|
2021-01-13 15:53:04 +08:00
|
|
|
|
2021-01-12 14:19:50 +08:00
|
|
|
[Test]
|
|
|
|
public void TestUpdateViaQueriedReference()
|
|
|
|
{
|
|
|
|
KeyBindingContainer testContainer = new TestKeyBindingContainer();
|
|
|
|
|
|
|
|
keyBindingStore.Register(testContainer);
|
|
|
|
|
2021-01-13 17:07:35 +08:00
|
|
|
var backBinding = query().Single(k => k.ActionInt == (int)GlobalAction.Back);
|
2021-01-12 14:19:50 +08:00
|
|
|
|
2021-01-13 17:07:35 +08:00
|
|
|
Assert.That(backBinding.KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.Escape }));
|
2021-01-12 14:19:50 +08:00
|
|
|
|
2021-01-15 13:22:48 +08:00
|
|
|
var tsr = ThreadSafeReference.Create(backBinding);
|
2021-01-13 16:59:47 +08:00
|
|
|
|
2021-01-15 13:22:48 +08:00
|
|
|
using (var usage = realmContextFactory.GetForWrite())
|
2021-01-13 16:59:47 +08:00
|
|
|
{
|
2021-01-15 13:22:48 +08:00
|
|
|
var binding = usage.Realm.ResolveReference(tsr);
|
2021-01-13 17:07:35 +08:00
|
|
|
binding.KeyCombination = new KeyCombination(InputKey.BackSpace);
|
2021-01-15 13:22:48 +08:00
|
|
|
|
|
|
|
usage.Commit();
|
|
|
|
}
|
2021-01-12 14:19:50 +08:00
|
|
|
|
2021-01-13 17:07:35 +08:00
|
|
|
Assert.That(backBinding.KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.BackSpace }));
|
2021-01-12 14:19:50 +08:00
|
|
|
|
|
|
|
// check still correct after re-query.
|
2021-01-13 17:07:35 +08:00
|
|
|
backBinding = query().Single(k => k.ActionInt == (int)GlobalAction.Back);
|
|
|
|
Assert.That(backBinding.KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.BackSpace }));
|
2021-01-12 14:19:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[TearDown]
|
|
|
|
public void TearDown()
|
|
|
|
{
|
2021-01-15 13:26:06 +08:00
|
|
|
realmContextFactory.Dispose();
|
2021-01-12 14:19:50 +08:00
|
|
|
storage.DeleteDirectory(string.Empty);
|
|
|
|
}
|
|
|
|
|
|
|
|
public 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),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|