OSDN Git Service

部分文字列に対するアサーションをAssert.StartsWithで行う (xUnit2009)
[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.Net.Http;
27 using System.Reflection;
28 using System.Runtime.InteropServices;
29 using System.Text.RegularExpressions;
30 using System.Threading;
31 using System.Threading.Tasks;
32 using System.Windows.Forms;
33 using Moq;
34 using OpenTween.Models;
35 using OpenTween.Thumbnail;
36 using OpenTween.Thumbnail.Services;
37 using Xunit;
38 using Xunit.Extensions;
39
40 namespace OpenTween
41 {
42     public class TweetThumbnailTest
43     {
44         class TestThumbnailService : IThumbnailService
45         {
46             private readonly Regex regex;
47             private readonly string replaceUrl;
48             private readonly string replaceTooltip;
49
50             public TestThumbnailService(string pattern, string replaceUrl, string replaceTooltip)
51             {
52                 this.regex = new Regex(pattern);
53                 this.replaceUrl = replaceUrl;
54                 this.replaceTooltip = replaceTooltip;
55             }
56
57             public override async Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
58             {
59                 var match = this.regex.Match(url);
60
61                 if (!match.Success) return null;
62
63                 if (url.StartsWith("http://slow.example.com/", StringComparison.Ordinal))
64                     await Task.Delay(1000, token).ConfigureAwait(false);
65
66                 return new MockThumbnailInfo
67                 {
68                     MediaPageUrl = url,
69                     ThumbnailImageUrl = match.Result(this.replaceUrl),
70                     TooltipText = this.replaceTooltip != null ? match.Result(this.replaceTooltip) : null,
71                 };
72             }
73
74             class MockThumbnailInfo : ThumbnailInfo
75             {
76                 public override Task<MemoryImage> LoadThumbnailImageAsync(HttpClient http, CancellationToken cancellationToken)
77                 {
78                     return Task.FromResult(TestUtils.CreateDummyImage());
79                 }
80             }
81         }
82
83         public TweetThumbnailTest()
84         {
85             this.ThumbnailGeneratorSetup();
86             this.MyCommonSetup();
87         }
88
89         private void ThumbnailGeneratorSetup()
90         {
91             ThumbnailGenerator.Services.Clear();
92             ThumbnailGenerator.Services.AddRange(new[]
93             {
94                 new TestThumbnailService(@"^https?://foo.example.com/(.+)$", @"http://img.example.com/${1}.png", null),
95                 new TestThumbnailService(@"^https?://bar.example.com/(.+)$", @"http://img.example.com/${1}.png", @"${1}"),
96                 new TestThumbnailService(@"^https?://slow.example.com/(.+)$", @"http://img.example.com/${1}.png", null),
97             });
98         }
99
100         private void MyCommonSetup()
101         {
102             var mockAssembly = new Mock<_Assembly>();
103             mockAssembly.Setup(m => m.GetName()).Returns(new AssemblyName("OpenTween"));
104
105             MyCommon.EntryAssembly = mockAssembly.Object;
106         }
107
108         [Fact]
109         public void CreatePictureBoxTest()
110         {
111             using (var thumbBox = new TweetThumbnail())
112             {
113                 var method = typeof(TweetThumbnail).GetMethod("CreatePictureBox", BindingFlags.Instance | BindingFlags.NonPublic);
114                 var picbox = method.Invoke(thumbBox, new[] { "pictureBox1" }) as PictureBox;
115
116                 Assert.NotNull(picbox);
117                 Assert.Equal("pictureBox1", picbox.Name);
118                 Assert.Equal(PictureBoxSizeMode.Zoom, picbox.SizeMode);
119                 Assert.False(picbox.WaitOnLoad);
120                 Assert.Equal(DockStyle.Fill, picbox.Dock);
121
122                 picbox.Dispose();
123             }
124         }
125
126         [Fact]
127         public async Task CancelAsyncTest()
128         {
129             var post = new PostClass
130             {
131                 TextFromApi = "てすと http://slow.example.com/abcd",
132                 Media = new List<MediaInfo>
133                 {
134                     new MediaInfo("http://slow.example.com/abcd"),
135                 },
136             };
137
138             using (var thumbbox = new TweetThumbnail())
139             using (var tokenSource = new CancellationTokenSource())
140             {
141                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
142                 var task = thumbbox.ShowThumbnailAsync(post, tokenSource.Token);
143
144                 tokenSource.Cancel();
145
146                 await Assert.ThrowsAnyAsync<OperationCanceledException>(async () => await task);
147                 Assert.True(task.IsCanceled);
148             }
149         }
150
151         [Theory]
152         [InlineData(0)]
153         [InlineData(1)]
154         [InlineData(2)]
155         public void SetThumbnailCountTest(int count)
156         {
157             using (var thumbbox = new TweetThumbnail())
158             {
159                 var method = typeof(TweetThumbnail).GetMethod("SetThumbnailCount", BindingFlags.Instance | BindingFlags.NonPublic);
160                 method.Invoke(thumbbox, new[] { (object)count });
161
162                 Assert.Equal(count, thumbbox.pictureBox.Count);
163
164                 var num = 0;
165                 foreach (var picbox in thumbbox.pictureBox)
166                 {
167                     Assert.Equal("pictureBox" + num, picbox.Name);
168                     num++;
169                 }
170
171                 Assert.Equal(thumbbox.pictureBox, thumbbox.panelPictureBox.Controls.Cast<OTPictureBox>());
172
173                 Assert.Equal(0, thumbbox.scrollBar.Minimum);
174
175                 if (count == 0)
176                     Assert.Equal(0, thumbbox.scrollBar.Maximum);
177                 else
178                     Assert.Equal(count - 1, thumbbox.scrollBar.Maximum);
179             }
180         }
181
182         [Fact]
183         public async Task ShowThumbnailAsyncTest()
184         {
185             var post = new PostClass
186             {
187                 TextFromApi = "てすと http://foo.example.com/abcd",
188                 Media = new List<MediaInfo>
189                 {
190                     new MediaInfo("http://foo.example.com/abcd"),
191                 },
192             };
193
194             using (var thumbbox = new TweetThumbnail())
195             {
196                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
197                 await thumbbox.ShowThumbnailAsync(post);
198
199                 Assert.Equal(0, thumbbox.scrollBar.Maximum);
200                 Assert.False(thumbbox.scrollBar.Enabled);
201
202                 Assert.Equal(1, thumbbox.pictureBox.Count);
203                 Assert.NotNull(thumbbox.pictureBox[0].Image);
204
205                 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
206                 var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag;
207
208                 Assert.Equal("http://foo.example.com/abcd", thumbinfo.MediaPageUrl);
209                 Assert.Equal("http://img.example.com/abcd.png", thumbinfo.ThumbnailImageUrl);
210
211                 Assert.Equal("", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]));
212             }
213         }
214
215         [Fact]
216         public async Task ShowThumbnailAsyncTest2()
217         {
218             var post = new PostClass
219             {
220                 TextFromApi = "てすと http://foo.example.com/abcd http://bar.example.com/efgh",
221                 Media = new List<MediaInfo>
222                 {
223                     new MediaInfo("http://foo.example.com/abcd"),
224                     new MediaInfo("http://bar.example.com/efgh"),
225                 },
226             };
227
228             using (var thumbbox = new TweetThumbnail())
229             {
230                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
231                 await thumbbox.ShowThumbnailAsync(post);
232
233                 Assert.Equal(1, thumbbox.scrollBar.Maximum);
234                 Assert.True(thumbbox.scrollBar.Enabled);
235
236                 Assert.Equal(2, thumbbox.pictureBox.Count);
237                 Assert.NotNull(thumbbox.pictureBox[0].Image);
238                 Assert.NotNull(thumbbox.pictureBox[1].Image);
239
240                 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
241                 var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag;
242
243                 Assert.Equal("http://foo.example.com/abcd", thumbinfo.MediaPageUrl);
244                 Assert.Equal("http://img.example.com/abcd.png", thumbinfo.ThumbnailImageUrl);
245
246                 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[1].Tag);
247                 thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[1].Tag;
248
249                 Assert.Equal("http://bar.example.com/efgh", thumbinfo.MediaPageUrl);
250                 Assert.Equal("http://img.example.com/efgh.png", thumbinfo.ThumbnailImageUrl);
251
252                 Assert.Equal("", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]));
253                 Assert.Equal("efgh", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[1]));
254             }
255         }
256
257         [Fact]
258         public async Task ThumbnailLoadingEventTest()
259         {
260             using (var thumbbox = new TweetThumbnail())
261             {
262                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
263
264                 var post = new PostClass
265                 {
266                     TextFromApi = "てすと",
267                     Media = new List<MediaInfo>
268                     {
269                     },
270                 };
271                 await TestUtils.NotRaisesAsync<EventArgs>(
272                     x => thumbbox.ThumbnailLoading += x,
273                     x => thumbbox.ThumbnailLoading -= x,
274                     () => thumbbox.ShowThumbnailAsync(post)
275                 );
276
277                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
278
279                 var post2 = new PostClass
280                 {
281                     TextFromApi = "てすと http://foo.example.com/abcd",
282                     Media = new List<MediaInfo>
283                     {
284                         new MediaInfo("http://foo.example.com/abcd"),
285                     },
286                 };
287
288                 await Assert.RaisesAsync<EventArgs>(
289                     x => thumbbox.ThumbnailLoading += x,
290                     x => thumbbox.ThumbnailLoading -= x,
291                     () => thumbbox.ShowThumbnailAsync(post2)
292                 );
293             }
294         }
295
296         [Fact]
297         public async Task ScrollTest()
298         {
299             var post = new PostClass
300             {
301                 TextFromApi = "てすと http://foo.example.com/abcd http://foo.example.com/efgh",
302                 Media = new List<MediaInfo>
303                 {
304                     new MediaInfo("http://foo.example.com/abcd"),
305                     new MediaInfo("http://foo.example.com/efgh"),
306                 },
307             };
308
309             using (var thumbbox = new TweetThumbnail())
310             {
311                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
312                 await thumbbox.ShowThumbnailAsync(post);
313
314                 Assert.Equal(0, thumbbox.scrollBar.Minimum);
315                 Assert.Equal(1, thumbbox.scrollBar.Maximum);
316
317                 thumbbox.scrollBar.Value = 0;
318
319                 thumbbox.ScrollDown();
320                 Assert.Equal(1, thumbbox.scrollBar.Value);
321                 Assert.False(thumbbox.pictureBox[0].Visible);
322                 Assert.True(thumbbox.pictureBox[1].Visible);
323
324                 thumbbox.ScrollDown();
325                 Assert.Equal(1, thumbbox.scrollBar.Value);
326                 Assert.False(thumbbox.pictureBox[0].Visible);
327                 Assert.True(thumbbox.pictureBox[1].Visible);
328
329                 thumbbox.ScrollUp();
330                 Assert.Equal(0, thumbbox.scrollBar.Value);
331                 Assert.True(thumbbox.pictureBox[0].Visible);
332                 Assert.False(thumbbox.pictureBox[1].Visible);
333
334                 thumbbox.ScrollUp();
335                 Assert.Equal(0, thumbbox.scrollBar.Value);
336                 Assert.True(thumbbox.pictureBox[0].Visible);
337                 Assert.False(thumbbox.pictureBox[1].Visible);
338             }
339         }
340     }
341 }