2020-09-11 15:20:30 +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.IO ;
using System.Linq ;
using System.Threading.Tasks ;
using NUnit.Framework ;
using osu.Framework.Allocation ;
using osu.Framework.Platform ;
using osu.Game.IO.Archives ;
using osu.Game.Skinning ;
using SharpCompress.Archives.Zip ;
namespace osu.Game.Tests.Skins.IO
{
2020-09-18 17:05:33 +08:00
public class ImportSkinTest : ImportTest
2020-09-11 15:20:30 +08:00
{
[Test]
public async Task TestBasicImport ( )
{
2020-09-17 12:01:34 +08:00
using ( HeadlessGameHost host = new CleanRunHeadlessGameHost ( nameof ( ImportSkinTest ) ) )
2020-09-11 15:20:30 +08:00
{
try
{
2020-09-18 17:05:33 +08:00
var osu = LoadOsuIntoHost ( host ) ;
2020-09-11 15:20:30 +08:00
2020-09-18 17:05:33 +08:00
var imported = await loadSkinIntoOsu ( osu , new ZipArchiveReader ( createOsk ( "test skin" , "skinner" ) , "skin.osk" ) ) ;
2020-09-11 15:20:30 +08:00
Assert . That ( imported . Name , Is . EqualTo ( "test skin" ) ) ;
Assert . That ( imported . Creator , Is . EqualTo ( "skinner" ) ) ;
}
finally
{
host . Exit ( ) ;
}
}
}
[Test]
public async Task TestImportTwiceWithSameMetadata ( )
{
2020-09-17 12:01:34 +08:00
using ( HeadlessGameHost host = new CleanRunHeadlessGameHost ( nameof ( ImportSkinTest ) ) )
2020-09-11 15:20:30 +08:00
{
try
{
2020-09-18 17:05:33 +08:00
var osu = LoadOsuIntoHost ( host ) ;
2020-09-11 15:20:30 +08:00
2020-09-18 17:05:33 +08:00
var imported = await loadSkinIntoOsu ( osu , new ZipArchiveReader ( createOsk ( "test skin" , "skinner" ) , "skin.osk" ) ) ;
var imported2 = await loadSkinIntoOsu ( osu , new ZipArchiveReader ( createOsk ( "test skin" , "skinner" ) , "skin2.osk" ) ) ;
2020-09-11 15:20:30 +08:00
Assert . That ( imported2 . ID , Is . Not . EqualTo ( imported . ID ) ) ;
Assert . That ( osu . Dependencies . Get < SkinManager > ( ) . GetAllUserSkins ( ) . Count , Is . EqualTo ( 1 ) ) ;
// the first should be overwritten by the second import.
Assert . That ( osu . Dependencies . Get < SkinManager > ( ) . GetAllUserSkins ( ) . First ( ) . Files . First ( ) . FileInfoID , Is . EqualTo ( imported2 . Files . First ( ) . FileInfoID ) ) ;
}
finally
{
host . Exit ( ) ;
}
}
}
2020-09-11 15:29:14 +08:00
[Test]
public async Task TestImportTwiceWithNoMetadata ( )
{
2020-09-17 12:01:34 +08:00
using ( HeadlessGameHost host = new CleanRunHeadlessGameHost ( nameof ( ImportSkinTest ) ) )
2020-09-11 15:29:14 +08:00
{
try
{
2020-09-18 17:05:33 +08:00
var osu = LoadOsuIntoHost ( host ) ;
2020-09-11 15:29:14 +08:00
// if a user downloads two skins that do have skin.ini files but don't have any creator metadata in the skin.ini, they should both import separately just for safety.
2020-09-18 17:05:33 +08:00
var imported = await loadSkinIntoOsu ( osu , new ZipArchiveReader ( createOsk ( string . Empty , string . Empty ) , "download.osk" ) ) ;
var imported2 = await loadSkinIntoOsu ( osu , new ZipArchiveReader ( createOsk ( string . Empty , string . Empty ) , "download.osk" ) ) ;
2020-09-11 15:29:14 +08:00
Assert . That ( imported2 . ID , Is . Not . EqualTo ( imported . ID ) ) ;
Assert . That ( osu . Dependencies . Get < SkinManager > ( ) . GetAllUserSkins ( ) . Count , Is . EqualTo ( 2 ) ) ;
Assert . That ( osu . Dependencies . Get < SkinManager > ( ) . GetAllUserSkins ( ) . First ( ) . Files . First ( ) . FileInfoID , Is . EqualTo ( imported . Files . First ( ) . FileInfoID ) ) ;
Assert . That ( osu . Dependencies . Get < SkinManager > ( ) . GetAllUserSkins ( ) . Last ( ) . Files . First ( ) . FileInfoID , Is . EqualTo ( imported2 . Files . First ( ) . FileInfoID ) ) ;
}
finally
{
host . Exit ( ) ;
}
}
}
2020-09-11 15:20:30 +08:00
[Test]
public async Task TestImportTwiceWithDifferentMetadata ( )
{
2020-09-17 12:01:34 +08:00
using ( HeadlessGameHost host = new CleanRunHeadlessGameHost ( nameof ( ImportSkinTest ) ) )
2020-09-11 15:20:30 +08:00
{
try
{
2020-09-18 17:05:33 +08:00
var osu = LoadOsuIntoHost ( host ) ;
2020-09-11 15:20:30 +08:00
2020-09-18 17:05:33 +08:00
var imported = await loadSkinIntoOsu ( osu , new ZipArchiveReader ( createOsk ( "test skin v2" , "skinner" ) , "skin.osk" ) ) ;
var imported2 = await loadSkinIntoOsu ( osu , new ZipArchiveReader ( createOsk ( "test skin v2.1" , "skinner" ) , "skin2.osk" ) ) ;
2020-09-11 15:20:30 +08:00
Assert . That ( imported2 . ID , Is . Not . EqualTo ( imported . ID ) ) ;
Assert . That ( osu . Dependencies . Get < SkinManager > ( ) . GetAllUserSkins ( ) . Count , Is . EqualTo ( 2 ) ) ;
Assert . That ( osu . Dependencies . Get < SkinManager > ( ) . GetAllUserSkins ( ) . First ( ) . Files . First ( ) . FileInfoID , Is . EqualTo ( imported . Files . First ( ) . FileInfoID ) ) ;
Assert . That ( osu . Dependencies . Get < SkinManager > ( ) . GetAllUserSkins ( ) . Last ( ) . Files . First ( ) . FileInfoID , Is . EqualTo ( imported2 . Files . First ( ) . FileInfoID ) ) ;
}
finally
{
host . Exit ( ) ;
}
}
}
2021-03-25 03:55:15 +08:00
[Test]
public async Task TestImportUpperCasedOskArchive ( )
{
using ( HeadlessGameHost host = new CleanRunHeadlessGameHost ( nameof ( ImportSkinTest ) ) )
{
try
{
var osu = LoadOsuIntoHost ( host ) ;
var imported = await loadSkinIntoOsu ( osu , new ZipArchiveReader ( createOsk ( "name 1" , "author 1" ) , "skin1.OsK" ) ) ;
Assert . That ( imported . Name , Is . EqualTo ( "name 1" ) ) ;
Assert . That ( imported . Creator , Is . EqualTo ( "author 1" ) ) ;
var imported2 = await loadSkinIntoOsu ( osu , new ZipArchiveReader ( createOsk ( "name 1" , "author 1" ) , "skin1.oSK" ) ) ;
Assert . That ( imported2 . Hash , Is . EqualTo ( imported . Hash ) ) ;
}
finally
{
host . Exit ( ) ;
}
}
}
2020-09-11 15:20:30 +08:00
private MemoryStream createOsk ( string name , string author )
{
var zipStream = new MemoryStream ( ) ;
using var zip = ZipArchive . Create ( ) ;
zip . AddEntry ( "skin.ini" , generateSkinIni ( name , author ) ) ;
zip . SaveTo ( zipStream ) ;
return zipStream ;
}
private MemoryStream generateSkinIni ( string name , string author )
{
var stream = new MemoryStream ( ) ;
var writer = new StreamWriter ( stream ) ;
writer . WriteLine ( "[General]" ) ;
writer . WriteLine ( $"Name: {name}" ) ;
writer . WriteLine ( $"Author: {author}" ) ;
writer . WriteLine ( ) ;
writer . WriteLine ( $"# unique {Guid.NewGuid()}" ) ;
writer . Flush ( ) ;
return stream ;
}
2020-09-18 17:05:33 +08:00
private async Task < SkinInfo > loadSkinIntoOsu ( OsuGameBase osu , ArchiveReader archive = null )
2020-09-11 15:20:30 +08:00
{
var skinManager = osu . Dependencies . Get < SkinManager > ( ) ;
return await skinManager . Import ( archive ) ;
}
}
}