OSDN Git Service

テストコードの誤りを修正
[opentween/open-tween.git] / OpenTween.Tests / TweetThumbnailTest.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.Collections.Generic;
24 using System.IO;
25 using System.Linq;
26 using System.Reflection;
27 using System.Runtime.InteropServices;
28 using System.Text.RegularExpressions;
29 using System.Threading;
30 using System.Threading.Tasks;
31 using System.Windows.Forms;
32 using NSubstitute;
33 using OpenTween.Thumbnail;
34 using OpenTween.Thumbnail.Services;
35 using Xunit;
36 using Xunit.Extensions;
37
38 namespace OpenTween
39 {
40     public class TweetThumbnailTest
41     {
42         class TestThumbnailService : IThumbnailService
43         {
44             private readonly Regex regex;
45             private readonly string replaceUrl;
46             private readonly string replaceTooltip;
47
48             public TestThumbnailService(string pattern, string replaceUrl, string replaceTooltip)
49             {
50                 this.regex = new Regex(pattern);
51                 this.replaceUrl = replaceUrl;
52                 this.replaceTooltip = replaceTooltip;
53             }
54
55             public override ThumbnailInfo GetThumbnailInfo(string url, PostClass post)
56             {
57                 var match = this.regex.Match(url);
58
59                 if (!match.Success) return null;
60
61                 return new MockThumbnailInfo
62                 {
63                     ImageUrl = url,
64                     ThumbnailUrl = match.Result(this.replaceUrl),
65                     TooltipText = this.replaceTooltip != null ? match.Result(this.replaceTooltip) : null,
66                 };
67             }
68
69             class MockThumbnailInfo : ThumbnailInfo
70             {
71                 public override Task<MemoryImage> LoadThumbnailImageAsync(CancellationToken token)
72                 {
73                     return Task.Factory.StartNew(() => MemoryImage.CopyFromBytes(File.ReadAllBytes("Resources/" + this.ThumbnailUrl)), token);
74                 }
75             }
76         }
77
78         public TweetThumbnailTest()
79         {
80             this.ThumbnailGeneratorSetup();
81             this.MyCommonSetup();
82         }
83
84         public void ThumbnailGeneratorSetup()
85         {
86             ThumbnailGenerator.Services.Clear();
87             ThumbnailGenerator.Services.AddRange(new[]
88             {
89                 new TestThumbnailService(@"^https?://foo.example.com/(.+)$", @"dot.gif", null),
90                 new TestThumbnailService(@"^https?://bar.example.com/(.+)$", @"dot.gif", @"${1}"),
91             });
92         }
93
94         public void MyCommonSetup()
95         {
96             var mockAssembly = Substitute.For<_Assembly>();
97             mockAssembly.GetName().Returns(new AssemblyName("OpenTween"));
98             MyCommon.EntryAssembly = mockAssembly;
99
100             MyCommon.fileVersion = "1.0.0.0";
101         }
102
103         [Fact]
104         public void CreatePictureBoxTest()
105         {
106             using (var thumbBox = new TweetThumbnail())
107             {
108                 var method = typeof(TweetThumbnail).GetMethod("CreatePictureBox", BindingFlags.Instance | BindingFlags.NonPublic);
109                 var picbox = method.Invoke(thumbBox, new[] { "pictureBox1" }) as PictureBox;
110
111                 Assert.NotNull(picbox);
112                 Assert.Equal("pictureBox1", picbox.Name);
113                 Assert.Equal(PictureBoxSizeMode.Zoom, picbox.SizeMode);
114                 Assert.False(picbox.WaitOnLoad);
115                 Assert.Equal(DockStyle.Fill, picbox.Dock);
116
117                 picbox.Dispose();
118             }
119         }
120
121         [Fact(Skip = "Mono環境でたまに InvaliOperationException: out of sync で異常終了する")]
122         public void CancelAsyncTest()
123         {
124             using (var thumbbox = new TweetThumbnail())
125             {
126                 var post = new PostClass();
127
128                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
129                 var task = thumbbox.ShowThumbnailAsync(post);
130
131                 thumbbox.CancelAsync();
132
133                 Assert.Throws<AggregateException>(() => task.Wait());
134                 Assert.True(task.IsCanceled);
135             }
136         }
137
138         [Theory]
139         [InlineData(0)]
140         [InlineData(1)]
141         [InlineData(2)]
142         public void SetThumbnailCountTest(int count)
143         {
144             using (var thumbbox = new TweetThumbnail())
145             {
146                 var method = typeof(TweetThumbnail).GetMethod("SetThumbnailCount", BindingFlags.Instance | BindingFlags.NonPublic);
147                 method.Invoke(thumbbox, new[] { (object)count });
148
149                 Assert.Equal(count, thumbbox.pictureBox.Count);
150
151                 var num = 0;
152                 foreach (var picbox in thumbbox.pictureBox)
153                 {
154                     Assert.Equal("pictureBox" + num, picbox.Name);
155                     num++;
156                 }
157
158                 Assert.Equal(thumbbox.pictureBox, thumbbox.panelPictureBox.Controls.Cast<OTPictureBox>());
159
160                 Assert.Equal(0, thumbbox.scrollBar.Minimum);
161
162                 if (count == 0)
163                     Assert.Equal(0, thumbbox.scrollBar.Maximum);
164                 else
165                     Assert.Equal(count - 1, thumbbox.scrollBar.Maximum);
166             }
167         }
168
169         [Fact]
170         public void ShowThumbnailAsyncTest()
171         {
172             var post = new PostClass
173             {
174                 TextFromApi = "てすと http://foo.example.com/abcd",
175                 Media = new Dictionary<string, string>
176                 {
177                     {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
178                 },
179             };
180
181             using (var thumbbox = new TweetThumbnail())
182             {
183                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
184                 thumbbox.ShowThumbnailAsync(post).Wait();
185
186                 Assert.Equal(0, thumbbox.scrollBar.Maximum);
187                 Assert.False(thumbbox.scrollBar.Enabled);
188
189                 Assert.Equal(1, thumbbox.pictureBox.Count);
190                 Assert.NotNull(thumbbox.pictureBox[0].Image);
191
192                 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
193                 var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag;
194
195                 Assert.Equal("http://foo.example.com/abcd", thumbinfo.ImageUrl);
196                 Assert.Equal("dot.gif", thumbinfo.ThumbnailUrl);
197
198                 Assert.Equal("", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]));
199             }
200         }
201
202         [Fact]
203         public void ShowThumbnailAsyncTest2()
204         {
205             var post = new PostClass
206             {
207                 TextFromApi = "てすと http://foo.example.com/abcd http://bar.example.com/efgh",
208                 Media = new Dictionary<string, string>
209                 {
210                     {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
211                     {"http://bar.example.com/efgh", "http://bar.example.com/efgh"},
212                 },
213             };
214
215             using (var thumbbox = new TweetThumbnail())
216             {
217                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
218                 thumbbox.ShowThumbnailAsync(post).Wait();
219
220                 Assert.Equal(1, thumbbox.scrollBar.Maximum);
221                 Assert.True(thumbbox.scrollBar.Enabled);
222
223                 Assert.Equal(2, thumbbox.pictureBox.Count);
224                 Assert.NotNull(thumbbox.pictureBox[0].Image);
225                 Assert.NotNull(thumbbox.pictureBox[1].Image);
226
227                 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
228                 var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag;
229
230                 Assert.Equal("http://foo.example.com/abcd", thumbinfo.ImageUrl);
231                 Assert.Equal("dot.gif", thumbinfo.ThumbnailUrl);
232
233                 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[1].Tag);
234                 thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[1].Tag;
235
236                 Assert.Equal("http://bar.example.com/efgh", thumbinfo.ImageUrl);
237                 Assert.Equal("dot.gif", thumbinfo.ThumbnailUrl);
238
239                 Assert.Equal("", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]));
240                 Assert.Equal("efgh", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[1]));
241             }
242         }
243
244         [Fact]
245         public void ThumbnailLoadingEventTest()
246         {
247             using (var thumbbox = new TweetThumbnail())
248             {
249                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
250
251                 bool eventCalled;
252                 thumbbox.ThumbnailLoading +=
253                     (s, e) => { eventCalled = true; };
254
255                 var post = new PostClass
256                 {
257                     TextFromApi = "てすと",
258                     Media = new Dictionary<string, string>
259                     {
260                     },
261                 };
262                 eventCalled = false;
263                 thumbbox.ShowThumbnailAsync(post).Wait();
264
265                 Assert.False(eventCalled);
266
267                 var post2 = new PostClass
268                 {
269                     TextFromApi = "てすと http://foo.example.com/abcd",
270                     Media = new Dictionary<string, string>
271                     {
272                         {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
273                     },
274                 };
275                 eventCalled = false;
276                 thumbbox.ShowThumbnailAsync(post2).Wait();
277
278                 Assert.True(eventCalled);
279             }
280         }
281
282         [Fact]
283         public void ScrollTest()
284         {
285             var post = new PostClass
286             {
287                 TextFromApi = "てすと http://foo.example.com/abcd http://foo.example.com/efgh",
288                 Media = new Dictionary<string, string>
289                 {
290                     {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
291                     {"http://foo.example.com/efgh", "http://foo.example.com/efgh"},
292                 },
293             };
294
295             using (var thumbbox = new TweetThumbnail())
296             {
297                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
298                 thumbbox.ShowThumbnailAsync(post).Wait();
299
300                 Assert.Equal(0, thumbbox.scrollBar.Minimum);
301                 Assert.Equal(1, thumbbox.scrollBar.Maximum);
302
303                 thumbbox.scrollBar.Value = 0;
304
305                 thumbbox.ScrollUp();
306                 Assert.Equal(1, thumbbox.scrollBar.Value);
307                 Assert.False(thumbbox.pictureBox[0].Visible);
308                 Assert.True(thumbbox.pictureBox[1].Visible);
309
310                 thumbbox.ScrollUp();
311                 Assert.Equal(1, thumbbox.scrollBar.Value);
312                 Assert.False(thumbbox.pictureBox[0].Visible);
313                 Assert.True(thumbbox.pictureBox[1].Visible);
314
315                 thumbbox.ScrollDown();
316                 Assert.Equal(0, thumbbox.scrollBar.Value);
317                 Assert.True(thumbbox.pictureBox[0].Visible);
318                 Assert.False(thumbbox.pictureBox[1].Visible);
319
320                 thumbbox.ScrollDown();
321                 Assert.Equal(0, thumbbox.scrollBar.Value);
322                 Assert.True(thumbbox.pictureBox[0].Visible);
323                 Assert.False(thumbbox.pictureBox[1].Visible);
324             }
325         }
326     }
327 }