From 75dcf72520fe3b1a8eb50130e4bc72a1f85f9177 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 16 Feb 2018 13:47:30 +0900 Subject: [PATCH] Improve testability of API --- osu.Game/Online/API/APIAccess.cs | 5 ++-- osu.Game/Online/API/DummyAPIAccess.cs | 31 ++++++++++++++++++++++++ osu.Game/Online/API/IAPIProvider.cs | 34 +++++++++++++++++++++++++++ osu.Game/OsuGameBase.cs | 1 + osu.Game/osu.Game.csproj | 2 ++ 5 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 osu.Game/Online/API/DummyAPIAccess.cs create mode 100644 osu.Game/Online/API/IAPIProvider.cs diff --git a/osu.Game/Online/API/APIAccess.cs b/osu.Game/Online/API/APIAccess.cs index 1d657b8664..90f3999ddd 100644 --- a/osu.Game/Online/API/APIAccess.cs +++ b/osu.Game/Online/API/APIAccess.cs @@ -7,7 +7,6 @@ using System.Diagnostics; using System.Net; using System.Threading; -using osu.Framework; using osu.Framework.Configuration; using osu.Framework.Logging; using osu.Framework.Threading; @@ -16,7 +15,7 @@ namespace osu.Game.Online.API { - public class APIAccess : IUpdateable + public class APIAccess : IAPIProvider { private readonly OAuth authentication; @@ -34,7 +33,7 @@ public class APIAccess : IUpdateable public string Password; - public Bindable LocalUser = new Bindable(createGuestUser()); + public Bindable LocalUser { get; } = new Bindable(createGuestUser()); public string Token { diff --git a/osu.Game/Online/API/DummyAPIAccess.cs b/osu.Game/Online/API/DummyAPIAccess.cs new file mode 100644 index 0000000000..fc0dc0ef8b --- /dev/null +++ b/osu.Game/Online/API/DummyAPIAccess.cs @@ -0,0 +1,31 @@ +// Copyright (c) 2007-2018 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using osu.Framework.Configuration; +using osu.Game.Users; + +namespace osu.Game.Online.API +{ + public class DummyAPIAccess : IAPIProvider + { + public Bindable LocalUser { get; } = new Bindable(new User + { + Username = @"Dummy", + Id = 1, + }); + + public bool IsLoggedIn => true; + + public void Update() + { + } + + public virtual void Queue(APIRequest request) + { + } + + public void Register(IOnlineComponent component) + { + } + } +} diff --git a/osu.Game/Online/API/IAPIProvider.cs b/osu.Game/Online/API/IAPIProvider.cs new file mode 100644 index 0000000000..b3c8774209 --- /dev/null +++ b/osu.Game/Online/API/IAPIProvider.cs @@ -0,0 +1,34 @@ +// Copyright (c) 2007-2018 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using osu.Framework; +using osu.Framework.Configuration; +using osu.Game.Users; + +namespace osu.Game.Online.API +{ + public interface IAPIProvider : IUpdateable + { + /// + /// The local user. + /// + Bindable LocalUser { get; } + + /// + /// Returns whether the local user is logged in. + /// + bool IsLoggedIn { get; } + + /// + /// Queue a new request. + /// + /// The request to perform. + void Queue(APIRequest request); + + /// + /// Register a component to receive state changes. + /// + /// The component to register. + void Register(IOnlineComponent component); + } +} diff --git a/osu.Game/OsuGameBase.cs b/osu.Game/OsuGameBase.cs index 505577416d..b5d7836f49 100644 --- a/osu.Game/OsuGameBase.cs +++ b/osu.Game/OsuGameBase.cs @@ -105,6 +105,7 @@ private void load() Username = LocalConfig.Get(OsuSetting.Username), Token = LocalConfig.Get(OsuSetting.Token) }); + dependencies.CacheAs(API); dependencies.Cache(RulesetStore = new RulesetStore(contextFactory)); dependencies.Cache(FileStore = new FileStore(contextFactory, Host.Storage)); diff --git a/osu.Game/osu.Game.csproj b/osu.Game/osu.Game.csproj index 02801eb81f..42e54472d1 100644 --- a/osu.Game/osu.Game.csproj +++ b/osu.Game/osu.Game.csproj @@ -281,6 +281,8 @@ + +