OSDN Git Service

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