OSDN Git Service

Merge pull request #307 from opentween/detect-rate-limits
[opentween/open-tween.git] / OpenTween.Tests / TestUtils.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2012 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
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)
10 // any later version.
11 //
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
15 // for more details.
16 //
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.
21
22 using System;
23 using System.ComponentModel;
24 using System.Drawing;
25 using System.Drawing.Imaging;
26 using System.IO;
27 using System.Net;
28 using System.Net.Http;
29 using System.Reflection;
30 using System.Threading.Tasks;
31 using System.Windows.Forms;
32 using OpenTween.Connection;
33 using Xunit;
34
35 namespace OpenTween
36 {
37     internal static class TestUtils
38     {
39         public static void CheckDeepCloning(object obj, object cloneObj)
40         {
41             Assert.Equal(obj, cloneObj);
42             Assert.NotSame(obj, cloneObj);
43
44             foreach (var field in obj.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
45             {
46                 var objValue = field.GetValue(obj);
47                 var cloneValue = field.GetValue(cloneObj);
48
49                 Assert.Equal(objValue, cloneValue);
50                 if (objValue == null && cloneValue == null) continue;
51                 if (field.FieldType.IsValueType || field.FieldType == typeof(string)) continue;
52
53                 Assert.NotSame(objValue, cloneValue);
54             }
55         }
56
57         public static async Task NotRaisesAsync<T>(Action<EventHandler<T>> attach, Action<EventHandler<T>> detach, Func<Task> testCode)
58             where T : EventArgs
59         {
60             T? raisedEvent = null;
61
62             void Handler(object s, T e)
63                 => raisedEvent = e;
64
65             try
66             {
67                 attach(Handler);
68                 await testCode().ConfigureAwait(false);
69
70                 if (raisedEvent != null)
71                     throw Xunit.Sdk.RaisesException.ForIncorrectType(typeof(void), raisedEvent.GetType());
72             }
73             finally
74             {
75                 detach(Handler);
76             }
77         }
78
79         public static void NotPropertyChanged(INotifyPropertyChanged @object, string propertyName, Action testCode)
80         {
81             void Handler(object s, PropertyChangedEventArgs e)
82             {
83                 if (s == @object && e.PropertyName == propertyName)
84                     throw Xunit.Sdk.PropertyChangedException.ForUnsetProperty(propertyName);
85             }
86
87             try
88             {
89                 @object.PropertyChanged += Handler;
90                 testCode();
91             }
92             finally
93             {
94                 @object.PropertyChanged -= Handler;
95             }
96         }
97
98         public static MemoryImage CreateDummyImage()
99         {
100             using var bitmap = new Bitmap(100, 100);
101             using var stream = new MemoryStream();
102             bitmap.Save(stream, ImageFormat.Png);
103             stream.Position = 0;
104
105             return MemoryImage.CopyFromStream(stream);
106         }
107
108         public static MemoryImageMediaItem CreateDummyMediaItem()
109             => new(CreateDummyImage());
110
111         public static void FireEvent<T>(T control, string eventName)
112             where T : Control
113             => TestUtils.FireEvent(control, eventName, EventArgs.Empty);
114
115         public static void FireEvent<T>(T control, string eventName, EventArgs e)
116             where T : Control
117         {
118             var methodName = "On" + eventName;
119             var method = typeof(T).GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic);
120
121             method.Invoke(control, new[] { e });
122         }
123
124         public static void Validate<T>(T control)
125             where T : Control
126         {
127             var cancelEventArgs = new CancelEventArgs();
128             TestUtils.FireEvent(control, "Validating", cancelEventArgs);
129
130             if (cancelEventArgs.Cancel)
131                 return;
132
133             TestUtils.FireEvent(control, "Validated");
134         }
135
136         public static IDisposable FreezeTime(DateTimeUtc datetime)
137         {
138             DateTimeUtc.UseFakeNow = true;
139             DateTimeUtc.FakeNow = datetime;
140             DateTimeUtc.FakeNowDrift = TimeSpan.Zero;
141             return new RestoreFreezedTime();
142         }
143
144         public static void DriftTime(TimeSpan drift)
145             => DateTimeUtc.FakeNowDrift += drift;
146
147         private sealed class RestoreFreezedTime : IDisposable
148         {
149             public void Dispose()
150                 => DateTimeUtc.UseFakeNow = false;
151         }
152
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));
155
156         public static async Task<ApiResponse> CreateApiResponse(string path)
157         {
158             byte[] buffer;
159             using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
160             {
161                 buffer = new byte[stream.Length];
162                 await stream.ReadAsync(buffer, 0, buffer.Length);
163             }
164             var responseMessage = new HttpResponseMessage
165             {
166                 StatusCode = HttpStatusCode.OK,
167                 Content = new ByteArrayContent(buffer),
168             };
169             return new ApiResponse(responseMessage);
170         }
171     }
172 }
173
174 #pragma warning disable SA1403
175
176 namespace OpenTween.Setting
177 {
178     public class SettingManagerTest
179     {
180         public static SettingCommon Common
181         {
182             get => SettingManager.Instance.Common;
183             set => SettingManager.Instance.Common = value;
184         }
185
186         public static SettingLocal Local
187         {
188             get => SettingManager.Instance.Local;
189             set => SettingManager.Instance.Local = value;
190         }
191
192         public static SettingTabs Tabs
193         {
194             get => SettingManager.Instance.Tabs;
195             set => SettingManager.Instance.Tabs = value;
196         }
197
198         public static SettingAtIdList AtIdList
199         {
200             get => SettingManager.Instance.AtIdList;
201             set => SettingManager.Instance.AtIdList = value;
202         }
203     }
204 }