Facepunch.Steamworks/Facepunch.Steamworks.Test/Client/WorkshopTest.cs

542 lines
19 KiB
C#
Raw Normal View History

2016-10-05 18:27:21 +03:00
using System;
using System.Text;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
2018-01-23 15:33:49 +03:00
using System.Diagnostics;
using Facepunch.Steamworks.Callbacks;
2016-10-05 18:27:21 +03:00
namespace Facepunch.Steamworks.Test
{
[TestClass]
2016-10-25 12:29:35 +03:00
[DeploymentItem( "steam_api64.dll" )]
2016-10-05 18:27:21 +03:00
public class WorkshopTest
{
[TestMethod]
public void Query()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
Assert.IsTrue( client.IsValid );
var Query = client.Workshop.CreateQuery();
2016-11-08 14:13:04 +03:00
Query.Order = Workshop.Order.RankedByTrend;
2016-10-05 18:27:21 +03:00
Query.Run();
// Block, wait for result
// (don't do this in realtime)
Query.Block();
Assert.IsFalse( Query.IsRunning );
2016-10-05 18:52:41 +03:00
Assert.IsTrue( Query.TotalResults > 0 );
Assert.IsTrue( Query.Items.Length > 0 );
2016-10-05 18:27:21 +03:00
2016-10-06 16:43:52 +03:00
Console.WriteLine( "Query.TotalResults: {0}", Query.TotalResults );
Console.WriteLine( "Query.Items.Length: {0}", Query.Items.Length );
2016-10-05 18:27:21 +03:00
// results
Console.WriteLine( "Searching" );
2016-10-05 18:52:41 +03:00
Query.Order = Workshop.Order.RankedByTextSearch;
2016-10-06 18:07:43 +03:00
Query.QueryType = Workshop.QueryType.MicrotransactionItems;
2016-10-13 12:31:42 +03:00
Query.SearchText = "black";
2016-10-06 14:43:38 +03:00
Query.RequireTags.Add( "LongTShirt Skin" );
2016-10-05 18:27:21 +03:00
Query.Run();
// Block, wait for result
// (don't do this in realtime)
Query.Block();
2016-10-06 14:13:15 +03:00
Console.WriteLine( "Query.TotalResults: {0}", Query.TotalResults );
Console.WriteLine( "Query.Items.Length: {0}", Query.Items.Length );
2016-10-05 18:52:41 +03:00
Assert.IsTrue( Query.TotalResults > 0 );
Assert.IsTrue( Query.Items.Length > 0 );
foreach ( var item in Query.Items )
{
Console.WriteLine( "{0}", item.Title );
2016-10-13 12:37:13 +03:00
Console.WriteLine( "\t WebsiteViews: {0}", item.WebsiteViews );
Console.WriteLine( "\t VotesUp: {0}", item.VotesUp );
Console.WriteLine( "\t PreviewUrl: {0}", item.PreviewImageUrl );
2016-10-25 13:16:05 +03:00
Assert.IsTrue( item.PreviewImageUrl.Contains( "http" ) );
2016-10-05 18:52:41 +03:00
}
2016-10-06 14:43:38 +03:00
}
}
2016-11-08 14:13:04 +03:00
[TestMethod]
public void QueryMyFiles()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
Assert.IsTrue( client.IsValid );
var Query = client.Workshop.CreateQuery();
Query.UserId = client.SteamId;
Query.UserQueryType = Workshop.UserQueryType.Published;
Query.Run();
// Block, wait for result
// (don't do this in realtime)
Query.Block();
Assert.IsFalse( Query.IsRunning );
Assert.IsTrue( Query.TotalResults > 0 );
Assert.IsTrue( Query.Items.Length > 0 );
Console.WriteLine( "Query.TotalResults: {0}", Query.TotalResults );
Console.WriteLine( "Query.Items.Length: {0}", Query.Items.Length );
foreach ( var item in Query.Items )
{
Console.WriteLine( "{0}", item.Title );
Console.WriteLine( "\t WebsiteViews: {0}", item.WebsiteViews );
Console.WriteLine( "\t VotesUp: {0}", item.VotesUp );
Console.WriteLine( "\t PreviewUrl: {0}", item.PreviewImageUrl );
Console.WriteLine( "\t Directory: {0}", item.Directory );
Assert.IsTrue( item.PreviewImageUrl.Contains( "http" ) );
}
}
}
2016-10-06 14:43:38 +03:00
[TestMethod]
public void QueryTagRequire()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
Assert.IsTrue( client.IsValid );
2016-10-05 18:52:41 +03:00
2016-10-06 14:43:38 +03:00
using ( var Query = client.Workshop.CreateQuery() )
2016-10-05 18:27:21 +03:00
{
2016-10-06 14:43:38 +03:00
Query.RequireTags.Add( "LongTShirt Skin" );
Query.Run();
Query.Block();
Assert.IsFalse( Query.IsRunning );
Assert.IsTrue( Query.TotalResults > 0 );
Assert.IsTrue( Query.Items.Length > 0 );
Console.WriteLine( "Query.TotalResults: {0}", Query.TotalResults );
Console.WriteLine( "Query.Items.Length: {0}", Query.Items.Length );
Assert.IsTrue( Query.TotalResults > 0 );
Assert.IsTrue( Query.Items.Length > 0 );
foreach ( var item in Query.Items )
{
Console.WriteLine( "{0}", item.Title );
Console.WriteLine( "\t{0}", string.Join( ";", item.Tags ) );
Assert.IsTrue( item.Tags.Contains( "longtshirt skin" ) );
2016-10-06 14:43:38 +03:00
}
}
}
}
2016-10-10 17:46:56 +03:00
[TestMethod]
public void QueryTagRequireMultiple()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
Assert.IsTrue( client.IsValid );
using ( var Query = client.Workshop.CreateQuery() )
{
Query.RequireTags.Add( "LongTShirt Skin" );
Query.RequireTags.Add( "version2" );
Query.RequireAllTags = true;
Query.Run();
Query.Block();
Assert.IsFalse( Query.IsRunning );
Assert.IsTrue( Query.TotalResults > 0 );
Assert.IsTrue( Query.Items.Length > 0 );
Console.WriteLine( "Query.TotalResults: {0}", Query.TotalResults );
Console.WriteLine( "Query.Items.Length: {0}", Query.Items.Length );
Assert.IsTrue( Query.TotalResults > 0 );
Assert.IsTrue( Query.Items.Length > 0 );
foreach ( var item in Query.Items )
{
Console.WriteLine( "{0}", item.Title );
Console.WriteLine( "\t{0}", string.Join( ";", item.Tags ) );
Assert.IsTrue( item.Tags.Contains( "longtshirt skin" ) );
2016-10-10 17:46:56 +03:00
Assert.IsTrue( item.Tags.Contains( "version2" ) );
}
}
}
}
2016-10-06 14:43:38 +03:00
[TestMethod]
public void QueryTagExclude()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
Assert.IsTrue( client.IsValid );
using ( var Query = client.Workshop.CreateQuery() )
{
Query.RequireTags.Add( "LongTShirt Skin" );
Query.ExcludeTags.Add( "version2" );
Query.Run();
Query.Block();
Assert.IsFalse( Query.IsRunning );
Assert.IsTrue( Query.TotalResults > 0 );
Assert.IsTrue( Query.Items.Length > 0 );
Console.WriteLine( "Query.TotalResults: {0}", Query.TotalResults );
Console.WriteLine( "Query.Items.Length: {0}", Query.Items.Length );
Assert.IsTrue( Query.TotalResults > 0 );
Assert.IsTrue( Query.Items.Length > 0 );
foreach ( var item in Query.Items )
{
Console.WriteLine( "{0}", item.Title );
Console.WriteLine( "\t{0}", string.Join( ";", item.Tags ) );
Assert.IsTrue( item.Tags.Contains( "longtshirt skin" ) );
2016-10-06 14:43:38 +03:00
Assert.IsFalse( item.Tags.Contains( "version2" ) );
}
2016-10-05 18:27:21 +03:00
}
}
}
2016-10-06 14:57:18 +03:00
[TestMethod]
public void QueryFile()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
Assert.IsTrue( client.IsValid );
using ( var Query = client.Workshop.CreateQuery() )
{
Query.FileId.Add( 751993251 );
Query.Run();
Assert.IsTrue( Query.IsRunning );
Query.Block();
Assert.IsFalse( Query.IsRunning );
Assert.AreEqual( Query.TotalResults, 1 );
Assert.AreEqual( Query.Items.Length, 1 );
Console.WriteLine( "Query.TotalResults: {0}", Query.TotalResults );
Console.WriteLine( "Query.Items.Length: {0}", Query.Items.Length );
Assert.AreEqual<ulong>( Query.Items[0].Id, 751993251 );
}
}
}
2016-12-01 14:07:47 +03:00
[TestMethod]
public void QueryCallback()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
Assert.IsTrue( client.IsValid );
using ( var Query = client.Workshop.CreateQuery() )
{
var gotCallback = false;
Query.OnResult = ( q ) =>
{
Assert.AreEqual( q.Items.Length, 1 );
Console.WriteLine( "Query.TotalResults: {0}", q.TotalResults );
Console.WriteLine( "Query.Items.Length: {0}", q.Items.Length );
gotCallback = true;
};
Query.FileId.Add( 751993251 );
Query.Run();
Assert.IsTrue( Query.IsRunning );
client.UpdateWhile( () => gotCallback == false );
Assert.IsFalse( Query.IsRunning );
Assert.AreEqual( Query.TotalResults, 1 );
Assert.AreEqual<ulong>( Query.Items[0].Id, 751993251 );
}
}
}
2016-10-06 14:57:18 +03:00
[TestMethod]
public void QueryFiles()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
Assert.IsTrue( client.IsValid );
using ( var Query = client.Workshop.CreateQuery() )
{
Query.FileId.Add( 751993251 );
Query.FileId.Add( 747266909 );
Query.Run();
Assert.IsTrue( Query.IsRunning );
Query.Block();
Assert.IsFalse( Query.IsRunning );
Assert.AreEqual( Query.TotalResults, 2 );
Assert.AreEqual( Query.Items.Length, 2 );
Console.WriteLine( "Query.TotalResults: {0}", Query.TotalResults );
Console.WriteLine( "Query.Items.Length: {0}", Query.Items.Length );
Assert.IsTrue( Query.Items.Any( x => x.Id == 751993251 ) );
Assert.IsTrue( Query.Items.Any( x => x.Id == 747266909 ) );
}
}
}
[TestMethod]
public void Query_255()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
Assert.IsTrue( client.IsValid );
using ( var Query = client.Workshop.CreateQuery() )
{
Query.PerPage = 255;
Query.Run();
Assert.IsTrue( Query.IsRunning );
Query.Block();
Assert.IsFalse( Query.IsRunning );
Assert.AreEqual( Query.Items.Length, 255 );
Console.WriteLine( "Query.TotalResults: {0}", Query.TotalResults );
Console.WriteLine( "Query.Items.Length: {0}", Query.Items.Length );
}
}
}
[TestMethod]
public void Query_28()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
Assert.IsTrue( client.IsValid );
using ( var Query = client.Workshop.CreateQuery() )
{
Query.PerPage = 28;
Query.Run();
Query.Block();
var firstPage = Query.Items;
Assert.AreEqual( firstPage.Length, 28 );
Console.WriteLine( "Page 2" );
Query.Page++;
Query.Run();
Query.Block();
var secondPage = Query.Items;
Assert.AreEqual( secondPage.Length, 28 );
Console.WriteLine( "Page 3" );
Query.Page++;
Query.Run();
Query.Block();
var thirdPage = Query.Items;
Assert.AreEqual( thirdPage.Length, 28 );
foreach ( var i in firstPage )
{
Assert.IsFalse( secondPage.Any( x => x.Id == i.Id ) );
Assert.IsFalse( thirdPage.Any( x => x.Id == i.Id ) );
}
foreach ( var i in secondPage )
{
Assert.IsFalse( firstPage.Any( x => x.Id == i.Id ) );
Assert.IsFalse( thirdPage.Any( x => x.Id == i.Id ) );
}
foreach ( var i in thirdPage )
{
Assert.IsFalse( secondPage.Any( x => x.Id == i.Id ) );
Assert.IsFalse( firstPage.Any( x => x.Id == i.Id ) );
}
}
}
}
2016-10-06 15:05:22 +03:00
[TestMethod]
public void DownloadFile()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
Assert.IsTrue( client.IsValid );
2017-01-18 18:15:41 +03:00
var item = client.Workshop.GetItem( 844466101 );
2016-10-06 15:05:22 +03:00
2018-01-23 15:33:49 +03:00
var time = Stopwatch.StartNew();
2016-10-28 13:57:56 +03:00
if ( !item.Installed )
{
item.Download();
2016-10-06 15:05:22 +03:00
2017-01-18 18:15:41 +03:00
while ( !item.Installed )
2016-10-06 16:43:52 +03:00
{
2016-10-28 13:57:56 +03:00
Thread.Sleep( 500 );
client.Update();
Console.WriteLine( "Download Progress: {0}", item.DownloadProgress );
2018-01-23 15:33:49 +03:00
if (time.Elapsed.Seconds > 30)
throw new Exception("item.Installed Took Too Long");
2016-10-06 16:43:52 +03:00
}
2016-10-28 13:57:56 +03:00
}
2016-10-06 16:43:52 +03:00
2016-10-28 13:57:56 +03:00
Assert.IsNotNull( item.Directory );
Assert.AreNotEqual( 0, item.Size );
2016-10-06 15:05:22 +03:00
2016-10-28 13:57:56 +03:00
Console.WriteLine( "item.Installed: {0}", item.Installed );
Console.WriteLine( "item.Downloading: {0}", item.Downloading );
Console.WriteLine( "item.DownloadPending: {0}", item.DownloadPending );
Console.WriteLine( "item.Directory: {0}", item.Directory );
Console.WriteLine( "item.Size: {0}mb", (item.Size / 1024 / 1024) );
2016-10-06 15:05:22 +03:00
}
}
2016-10-06 18:07:43 +03:00
[TestMethod]
[TestCategory( "Run Manually" )]
public void CreatePublish()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
Assert.IsTrue( client.IsValid );
var item = client.Workshop.CreateItem( Workshop.ItemType.Microtransaction );
item.Title = "Facepunch.Steamworks Unit test";
2016-10-11 17:10:20 +03:00
item.Tags.Add( "Apple" );
item.Tags.Add( "Banana" );
2016-10-06 18:07:43 +03:00
// Make a folder
var testFolder = new System.IO.DirectoryInfo("BlahBlah");
if (!testFolder.Exists) testFolder.Create();
item.Folder = testFolder.FullName;
// Upload a file of random bytes
var rand = new Random();
var testFile = new byte[1024 * 1024 * 32];
rand.NextBytes(testFile);
System.IO.File.WriteAllBytes( testFolder.FullName + "/testfile1.bin", testFile);
Console.WriteLine(item.Folder);
item.OnChangesSubmitted += result =>
{
Console.WriteLine( "OnChangesSubmitted called: " + result );
Assert.AreEqual( Result.OK, result );
};
2016-10-26 19:08:39 +03:00
try
2016-10-06 18:07:43 +03:00
{
2016-10-26 19:08:39 +03:00
item.Publish();
while ( item.Publishing )
{
client.Update();
Thread.Sleep( 10 );
Console.WriteLine("Progress: " + item.Progress);
Console.WriteLine("BytesUploaded: " + item.BytesUploaded);
Console.WriteLine("BytesTotal: " + item.BytesTotal);
2016-10-26 19:08:39 +03:00
}
Assert.IsFalse( item.Publishing );
Assert.AreNotEqual( 0, item.Id );
2016-10-28 13:57:56 +03:00
Assert.IsNull( item.Error, item.Error );
2016-10-26 19:08:39 +03:00
Console.WriteLine( "item.Id: {0}", item.Id );
2016-10-06 18:07:43 +03:00
2016-10-26 19:08:39 +03:00
using ( var Query = client.Workshop.CreateQuery() )
{
Query.FileId.Add( item.Id );
Query.Run();
Query.Block();
2016-10-06 18:07:43 +03:00
2016-10-26 19:08:39 +03:00
var itemInfo = Query.Items[0];
2016-10-06 18:07:43 +03:00
2016-10-26 19:08:39 +03:00
Assert.AreEqual( itemInfo.Id, item.Id );
Assert.AreEqual( itemInfo.OwnerId, client.SteamId );
Assert.AreEqual( itemInfo.Title, item.Title );
Assert.IsTrue( itemInfo.Tags.Contains( "apple" ), "Missing Tag" );
Assert.IsTrue( itemInfo.Tags.Contains( "banana" ), "Missing Tag" );
2016-10-26 19:08:39 +03:00
}
}
finally
{
Console.WriteLine( "Deleting: {0}", item.Id );
item.Delete();
System.IO.File.Delete(testFolder.FullName + "/testfile.bin");
2016-10-26 19:08:39 +03:00
}
2016-10-06 18:07:43 +03:00
}
}
2016-10-12 12:20:02 +03:00
[TestMethod]
public void UserQuery()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
Assert.IsTrue( client.IsValid );
var Query = client.Workshop.CreateQuery();
Query.UserId = 76561197960279927;
Query.UserQueryType = Workshop.UserQueryType.Published;
Query.Run();
// Block, wait for result
// (don't do this in realtime)
Query.Block();
Assert.IsFalse( Query.IsRunning );
Assert.IsTrue( Query.TotalResults > 0 );
Assert.IsTrue( Query.Items.Length > 0 );
Console.WriteLine( "Query.TotalResults: {0}", Query.TotalResults );
Console.WriteLine( "Query.Items.Length: {0}", Query.Items.Length );
foreach ( var item in Query.Items )
{
Console.WriteLine( "{0}", item.Title );
Assert.AreEqual<ulong>( item.OwnerId, 76561197960279927 );
}
}
}
2016-10-05 18:27:21 +03:00
}
}