Facepunch.Steamworks/Facepunch.Steamworks.Test/UgcEditor.cs

184 lines
4.6 KiB
C#
Raw Normal View History

2019-04-26 16:54:47 +03:00
using System;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Steamworks.Data;
namespace Steamworks
{
[TestClass]
[DeploymentItem( "steam_api64.dll" )]
2020-02-19 15:29:07 +03:00
[DeploymentItem( "steam_api.dll" )]
public class UgcEditor
2019-04-26 16:54:47 +03:00
{
[TestMethod]
public async Task CreateFile()
{
var result = await Ugc.Editor.NewCommunityFile
.WithTitle( "Unit Test Created Item" )
.WithDescription( "This item was created by Facepunch Steamworks unit tests.\n\n" +
"It should have technically been deleted so you should never get to " +
"read this unless something terrible has happened." )
2019-05-07 18:38:54 +03:00
.WithTag( "Arsehole" )
.WithTag( "Spiteful" )
.WithTag( "Fat-Head" )
2019-04-26 16:54:47 +03:00
.SubmitAsync();
Assert.IsTrue( result.Success );
Assert.AreNotEqual( result.FileId.Value, 0 );
var deleted = await SteamUGC.DeleteFileAsync( result.FileId );
Assert.IsTrue( deleted );
}
2019-04-26 17:23:05 +03:00
2019-06-20 15:10:38 +03:00
[TestMethod]
public async Task CreateChineseFile()
{
string fileName = "这是我的项目";
string description = "此项目由Facepunch Steamworks单元测试创建";
var result = await Ugc.Editor.NewCommunityFile
.WithTitle( fileName )
.WithDescription( description )
.WithTag( "Arsehole" )
.WithTag( "Spiteful" )
.WithTag( "Fat-Head" )
.SubmitAsync();
Console.WriteLine( $"Title: {fileName}" );
Console.WriteLine( $"Description: {description}" );
Assert.IsTrue( result.Success );
Assert.AreNotEqual( result.FileId.Value, 0 );
var file = await Steamworks.SteamUGC.QueryFileAsync( result.FileId );
Console.WriteLine( $"FileId: {result.FileId}" );
Console.WriteLine( $"Title: {file.Value.Title}" );
Console.WriteLine( $"Description: {file.Value.Description}" );
Assert.AreEqual( file.Value.Title, fileName );
Assert.AreEqual( file.Value.Description, description );
var deleted = await SteamUGC.DeleteFileAsync( result.FileId );
Assert.IsTrue( deleted );
}
2019-04-26 17:40:27 +03:00
class ProgressBar : IProgress<float>
{
float Value = 0;
public void Report( float value )
{
if ( Value >= value ) return;
Value = value;
Console.WriteLine( value );
}
}
2019-04-26 17:23:05 +03:00
[TestMethod]
2019-05-02 17:23:47 +03:00
public async Task UploadBigishFile()
2019-04-26 17:23:05 +03:00
{
var created = Ugc.Editor.NewCommunityFile
.WithTitle( "Unit Test Upload Item" )
.WithDescription( "This item was created by Facepunch Steamworks unit tests.\n\n" +
"It should have technically been deleted so you should never get to " +
"read this unless something terrible has happened." )
//.WithTag( "Apple" )
//.WithTag( "Banana" )
;
// Make a folder
var testFolder = new System.IO.DirectoryInfo( "WorkshopUpload" );
if ( !testFolder.Exists ) testFolder.Create();
created = created.WithContent( testFolder.FullName );
// Upload a file of random bytes
var rand = new Random();
2019-05-02 17:23:47 +03:00
var testFile = new byte[1024 * 1024 * 32];
2019-04-26 17:23:05 +03:00
rand.NextBytes( testFile );
System.IO.File.WriteAllBytes( testFolder.FullName + "/testfile1.bin", testFile );
Console.WriteLine( testFolder.FullName );
try
{
2019-04-26 17:40:27 +03:00
var done = await created.SubmitAsync( new ProgressBar() );
2019-04-26 17:23:05 +03:00
Assert.IsTrue( done.Success );
Console.WriteLine( "item.Id: {0}", done.FileId );
var deleted = await SteamUGC.DeleteFileAsync( done.FileId );
Assert.IsTrue( deleted );
}
finally
{
System.IO.File.Delete( testFolder.FullName + "/testfile.bin" );
}
}
2019-05-10 12:43:03 +03:00
[TestMethod]
public async Task CreateAndThenEditFile()
{
PublishedFileId fileid = default;
//
// Make a file
//
{
var result = await Ugc.Editor.NewCommunityFile
.WithTitle( "Unedited File" )
.SubmitAsync();
Assert.IsTrue( result.Success );
Assert.AreNotEqual( result.FileId.Value, 0 );
fileid = result.FileId;
}
await Task.Delay( 1000 );
//
// Edit it
//
{
var editor = new Ugc.Editor( fileid );
editor = editor.WithTitle( "An Edited File" );
var result = await editor.SubmitAsync();
Assert.IsTrue( result.Success );
Assert.AreEqual( result.FileId, fileid );
}
await Task.Delay( 1000 );
//
// Make sure the edited file matches
//
{
var details = await SteamUGC.QueryFileAsync( fileid ) ?? throw new Exception( "Somethign went wrong" );
Assert.AreEqual( details.Id, fileid );
Assert.AreEqual( details.Title, "An Edited File" );
}
//
// Clean up
//
var deleted = await SteamUGC.DeleteFileAsync( fileid );
Assert.IsTrue( deleted );
}
2019-04-26 16:54:47 +03:00
}
}