1 // OpenTween - Client of Twitter
2 // Copyright (c) 2012 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
23 using System.ComponentModel;
25 using System.Drawing.Imaging;
28 using System.Net.Http;
29 using System.Reflection;
30 using System.Threading.Tasks;
31 using System.Windows.Forms;
32 using OpenTween.Connection;
37 internal static class TestUtils
39 public static void CheckDeepCloning(object obj, object cloneObj)
41 Assert.Equal(obj, cloneObj);
42 Assert.NotSame(obj, cloneObj);
44 foreach (var field in obj.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
46 var objValue = field.GetValue(obj);
47 var cloneValue = field.GetValue(cloneObj);
49 Assert.Equal(objValue, cloneValue);
50 if (objValue == null && cloneValue == null) continue;
51 if (field.FieldType.IsValueType || field.FieldType == typeof(string)) continue;
53 Assert.NotSame(objValue, cloneValue);
57 public static async Task NotRaisesAsync<T>(Action<EventHandler<T>> attach, Action<EventHandler<T>> detach, Func<Task> testCode)
60 T? raisedEvent = null;
62 void Handler(object s, T e)
68 await testCode().ConfigureAwait(false);
70 if (raisedEvent != null)
71 throw Xunit.Sdk.RaisesException.ForIncorrectType(typeof(void), raisedEvent.GetType());
79 public static void NotPropertyChanged(INotifyPropertyChanged @object, string propertyName, Action testCode)
81 void Handler(object s, PropertyChangedEventArgs e)
83 if (s == @object && e.PropertyName == propertyName)
84 throw Xunit.Sdk.PropertyChangedException.ForUnsetProperty(propertyName);
89 @object.PropertyChanged += Handler;
94 @object.PropertyChanged -= Handler;
98 public static MemoryImage CreateDummyImage()
100 using var bitmap = new Bitmap(100, 100);
101 using var stream = new MemoryStream();
102 bitmap.Save(stream, ImageFormat.Png);
105 return MemoryImage.CopyFromStream(stream);
108 public static MemoryImageMediaItem CreateDummyMediaItem()
109 => new(CreateDummyImage());
111 public static void FireEvent<T>(T control, string eventName)
113 => TestUtils.FireEvent(control, eventName, EventArgs.Empty);
115 public static void FireEvent<T>(T control, string eventName, EventArgs e)
118 var methodName = "On" + eventName;
119 var method = typeof(T).GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic);
121 method.Invoke(control, new[] { e });
124 public static void Validate<T>(T control)
127 var cancelEventArgs = new CancelEventArgs();
128 TestUtils.FireEvent(control, "Validating", cancelEventArgs);
130 if (cancelEventArgs.Cancel)
133 TestUtils.FireEvent(control, "Validated");
136 public static IDisposable FreezeTime(DateTimeUtc datetime)
138 DateTimeUtc.UseFakeNow = true;
139 DateTimeUtc.FakeNow = datetime;
140 DateTimeUtc.FakeNowDrift = TimeSpan.Zero;
141 return new RestoreFreezedTime();
144 public static void DriftTime(TimeSpan drift)
145 => DateTimeUtc.FakeNowDrift += drift;
147 private sealed class RestoreFreezedTime : IDisposable
149 public void Dispose()
150 => DateTimeUtc.UseFakeNow = false;
153 public static DateTimeUtc LocalTime(int year, int month, int day, int hour, int minute, int second)
154 => new(new DateTimeOffset(year, month, day, hour, minute, second, TimeZoneInfo.Local.BaseUtcOffset));
156 public static async Task<ApiResponse> CreateApiResponse(string path)
159 using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
161 buffer = new byte[stream.Length];
162 await stream.ReadAsync(buffer, 0, buffer.Length);
164 var responseMessage = new HttpResponseMessage
166 StatusCode = HttpStatusCode.OK,
167 Content = new ByteArrayContent(buffer),
169 return new ApiResponse(responseMessage);
174 #pragma warning disable SA1403
176 namespace OpenTween.Setting
178 public class SettingManagerTest
180 public static SettingCommon Common
182 get => SettingManager.Instance.Common;
183 set => SettingManager.Instance.Common = value;
186 public static SettingLocal Local
188 get => SettingManager.Instance.Local;
189 set => SettingManager.Instance.Local = value;
192 public static SettingTabs Tabs
194 get => SettingManager.Instance.Tabs;
195 set => SettingManager.Instance.Tabs = value;
198 public static SettingAtIdList AtIdList
200 get => SettingManager.Instance.AtIdList;
201 set => SettingManager.Instance.AtIdList = value;