using System;
using System.Collections.Generic;
+using System.ComponentModel;
+using System.Drawing;
+using System.Drawing.Imaging;
+using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Xunit;
-using Xunit.Sdk;
-using Xunit.Extensions;
namespace OpenTween
{
- class TestUtils
+ internal static class TestUtils
{
public static void CheckDeepCloning(object obj, object cloneObj)
{
}
}
- public static void FireEvent<T>(T control, string eventName) where T : Control
+ public static async Task NotRaisesAsync<T>(Action<EventHandler<T>> attach, Action<EventHandler<T>> detach, Func<Task> testCode)
+ where T : EventArgs
+ {
+ T? raisedEvent = null;
+
+ void handler(object s, T e)
+ => raisedEvent = e;
+
+ try
+ {
+ attach(handler);
+ await testCode().ConfigureAwait(false);
+
+ if (raisedEvent != null)
+ throw new Xunit.Sdk.RaisesException(typeof(void), raisedEvent.GetType());
+ }
+ finally
+ {
+ detach(handler);
+ }
+ }
+
+ public static void NotPropertyChanged(INotifyPropertyChanged @object, string propertyName, Action testCode)
{
- TestUtils.FireEvent(control, eventName, EventArgs.Empty);
+ void handler(object s, PropertyChangedEventArgs e)
+ {
+ if (s == @object && e.PropertyName == propertyName)
+ throw new Xunit.Sdk.PropertyChangedException(propertyName);
+ }
+
+ try
+ {
+ @object.PropertyChanged += handler;
+ testCode();
+ }
+ finally
+ {
+ @object.PropertyChanged -= handler;
+ }
+ }
+
+ public static MemoryImage CreateDummyImage()
+ {
+ using (var bitmap = new Bitmap(100, 100))
+ using (var stream = new MemoryStream())
+ {
+ bitmap.Save(stream, ImageFormat.Png);
+ stream.Position = 0;
+
+ return MemoryImage.CopyFromStream(stream);
+ }
}
+ public static MemoryImageMediaItem CreateDummyMediaItem()
+ => new MemoryImageMediaItem(CreateDummyImage());
+
+ public static void FireEvent<T>(T control, string eventName) where T : Control
+ => TestUtils.FireEvent(control, eventName, EventArgs.Empty);
+
public static void FireEvent<T>(T control, string eventName, EventArgs e) where T : Control
{
var methodName = "On" + eventName;
method.Invoke(control, new[] { e });
}
- public static async Task<T> ThrowsAsync<T>(Func<Task> testCode) where T : Exception
+ public static void Validate<T>(T control) where T : Control
{
- var expectedType = typeof(T);
- Exception exception = null;
+ var cancelEventArgs = new CancelEventArgs();
+ TestUtils.FireEvent(control, "Validating", cancelEventArgs);
- try
- {
- await testCode();
- }
- catch (Exception ex)
- {
- exception = ex;
- }
+ if (cancelEventArgs.Cancel)
+ return;
+
+ TestUtils.FireEvent(control, "Validated");
+ }
+ }
+}
+
+namespace OpenTween.Setting
+{
+ public class SettingManagerTest
+ {
+ public static SettingCommon Common
+ {
+ get => SettingManager.Common;
+ set => SettingManager.Common = value;
+ }
- if (exception == null)
- throw new ThrowsException(expectedType);
+ public static SettingLocal Local
+ {
+ get => SettingManager.Local;
+ set => SettingManager.Local = value;
+ }
- if (!exception.GetType().Equals(expectedType))
- throw new ThrowsException(expectedType, exception);
+ public static SettingTabs Tabs
+ {
+ get => SettingManager.Tabs;
+ set => SettingManager.Tabs = value;
+ }
- return (T)exception;
+ public static SettingAtIdList AtIdList
+ {
+ get => SettingManager.AtIdList;
+ set => SettingManager.AtIdList = value;
}
}
}