1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 13:22:55 +08:00

Merge pull request #4511 from peppy/better-dummy-api

Fix AccountCreationOverlay tests and better complete dummy api's behaviour
This commit is contained in:
Dan Balasescu 2019-03-22 15:11:29 +09:00 committed by GitHub
commit 9d32b97b81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 19 deletions

View File

@ -3,9 +3,13 @@
using System;
using System.Collections.Generic;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Online.API;
using osu.Game.Overlays;
using osu.Game.Overlays.AccountCreation;
using osu.Game.Users;
namespace osu.Game.Tests.Visual
{
@ -21,12 +25,32 @@ namespace osu.Game.Tests.Visual
typeof(AccountCreationScreen),
};
[Cached(typeof(IAPIProvider))]
private DummyAPIAccess api = new DummyAPIAccess();
public TestCaseAccountCreationOverlay()
{
var accountCreation = new AccountCreationOverlay();
Child = accountCreation;
Container userPanelArea;
AccountCreationOverlay accountCreation;
accountCreation.State = Visibility.Visible;
Children = new Drawable[]
{
api,
accountCreation = new AccountCreationOverlay(),
userPanelArea = new Container
{
Padding = new MarginPadding(10),
AutoSizeAxes = Axes.Both,
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
},
};
api.Logout();
api.LocalUser.BindValueChanged(user => { userPanelArea.Child = new UserPanel(user.NewValue) { Width = 200 }; }, true);
AddStep("show", () => accountCreation.State = Visibility.Visible);
AddStep("logout", () => api.Logout());
}
}
}

View File

@ -16,6 +16,8 @@ namespace osu.Game.Tests.Visual
[BackgroundDependencyLoader]
private void load()
{
Add(api);
AddStep("load disclaimer", () => LoadScreen(new Disclaimer()));
AddStep("toggle support", () =>

View File

@ -266,20 +266,18 @@ namespace osu.Game.Online.API
get => state;
private set
{
APIState oldState = state;
APIState newState = value;
if (state == value)
return;
APIState oldState = state;
state = value;
if (oldState != newState)
log.Add($@"We just went {state}!");
Scheduler.Add(delegate
{
log.Add($@"We just went {newState}!");
Scheduler.Add(delegate
{
components.ForEach(c => c.APIStateChanged(this, newState));
OnStateChange?.Invoke(oldState, newState);
});
}
components.ForEach(c => c.APIStateChanged(this, state));
OnStateChange?.Invoke(oldState, state);
});
}
}

View File

@ -1,12 +1,15 @@
// 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.Collections.Generic;
using System.Threading;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Users;
namespace osu.Game.Online.API
{
public class DummyAPIAccess : IAPIProvider
public class DummyAPIAccess : Component, IAPIProvider
{
public Bindable<User> LocalUser { get; } = new Bindable<User>(new User
{
@ -20,7 +23,23 @@ namespace osu.Game.Online.API
public string Endpoint => "http://localhost";
public APIState State => LocalUser.Value.Id == 1 ? APIState.Offline : APIState.Online;
private APIState state = APIState.Online;
private readonly List<IOnlineComponent> components = new List<IOnlineComponent>();
public APIState State
{
get => state;
private set
{
if (state == value)
return;
state = value;
Scheduler.Add(() => components.ForEach(c => c.APIStateChanged(this, value)));
}
}
public virtual void Queue(APIRequest request)
{
@ -28,28 +47,36 @@ namespace osu.Game.Online.API
public void Register(IOnlineComponent component)
{
// todo: add support
Scheduler.Add(delegate { components.Add(component); });
component.APIStateChanged(this, state);
}
public void Unregister(IOnlineComponent component)
{
// todo: add support
Scheduler.Add(delegate { components.Remove(component); });
}
public void Login(string username, string password)
{
LocalUser.Value = new User
{
Username = @"Dummy",
Username = username,
Id = 1001,
};
State = APIState.Online;
}
public void Logout()
{
LocalUser.Value = new GuestUser();
State = APIState.Offline;
}
public RegistrationRequest.RegistrationRequestErrors CreateAccount(string email, string username, string password) => null;
public RegistrationRequest.RegistrationRequestErrors CreateAccount(string email, string username, string password)
{
Thread.Sleep(200);
return null;
}
}
}