OSDN Git Service

TweetThumbnailTest.CancelTest() で検証する例外の型を変更
[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.Run(() => 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]
122         public async Task CancelAsyncTest()
123         {
124             var post = new PostClass
125             {
126                 TextFromApi = "てすと http://foo.example.com/abcd",
127                 Media = new Dictionary<string, string>
128                 {
129                     {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
130                 },
131             };
132
133             using (var thumbbox = new TweetThumbnail())
134             using (var tokenSource = new CancellationTokenSource())
135             {
136                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
137                 var task = thumbbox.ShowThumbnailAsync(post, tokenSource.Token);
138
139                 tokenSource.Cancel();
140
141                 await TestUtils.ThrowsAnyAsync<OperationCanceledException>(async () => await task);
142                 Assert.True(task.IsCanceled);
143             }
144         }
145
146         [Theory]
147         [InlineData(0)]
148         [InlineData(1)]
149         [InlineData(2)]
150         public void SetThumbnailCountTest(int count)
151         {
152             using (var thumbbox = new TweetThumbnail())
153             {
154                 var method = typeof(TweetThumbnail).GetMethod("SetThumbnailCount", BindingFlags.Instance | BindingFlags.NonPublic);
155                 method.Invoke(thumbbox, new[] { (object)count });
156
157                 Assert.Equal(count, thumbbox.pictureBox.Count);
158
159                 var num = 0;
160                 foreach (var picbox in thumbbox.pictureBox)
161                 {
162                     Assert.Equal("pictureBox" + num, picbox.Name);
163                     num++;
164                 }
165
166                 Assert.Equal(thumbbox.pictureBox, thumbbox.panelPictureBox.Controls.Cast<OTPictureBox>());
167
168                 Assert.Equal(0, thumbbox.scrollBar.Minimum);
169
170                 if (count == 0)
171                     Assert.Equal(0, thumbbox.scrollBar.Maximum);
172                 else
173                     Assert.Equal(count - 1, thumbbox.scrollBar.Maximum);
174             }
175         }
176
177         [Fact]
178         public async Task ShowThumbnailAsyncTest()
179         {
180             var post = new PostClass
181             {
182                 TextFromApi = "てすと http://foo.example.com/abcd",
183                 Media = new Dictionary<string, string>
184                 {
185                     {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
186                 },
187             };
188
189             using (var thumbbox = new TweetThumbnail())
190             {
191                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
192                 await thumbbox.ShowThumbnailAsync(post);
193
194                 Assert.Equal(0, thumbbox.scrollBar.Maximum);
195                 Assert.False(thumbbox.scrollBar.Enabled);
196
197                 Assert.Equal(1, thumbbox.pictureBox.Count);
198                 Assert.NotNull(thumbbox.pictureBox[0].Image);
199
200                 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
201                 var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag;
202
203                 Assert.Equal("http://foo.example.com/abcd", thumbinfo.ImageUrl);
204                 Assert.Equal("dot.gif", thumbinfo.ThumbnailUrl);
205
206                 Assert.Equal("", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]));
207             }
208         }
209
210         [Fact]
211         public async Task ShowThumbnailAsyncTest2()
212         {
213             var post = new PostClass
214             {
215                 TextFromApi = "てすと http://foo.example.com/abcd http://bar.example.com/efgh",
216                 Media = new Dictionary<string, string>
217                 {
218                     {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
219                     {"http://bar.example.com/efgh", "http://bar.example.com/efgh"},
220                 },
221             };
222
223             using (var thumbbox = new TweetThumbnail())
224             {
225                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
226                 await thumbbox.ShowThumbnailAsync(post);
227
228                 Assert.Equal(1, thumbbox.scrollBar.Maximum);
229                 Assert.True(thumbbox.scrollBar.Enabled);
230
231                 Assert.Equal(2, thumbbox.pictureBox.Count);
232                 Assert.NotNull(thumbbox.pictureBox[0].Image);
233                 Assert.NotNull(thumbbox.pictureBox[1].Image);
234
235                 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
236                 var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag;
237
238                 Assert.Equal("http://foo.example.com/abcd", thumbinfo.ImageUrl);
239                 Assert.Equal("dot.gif", thumbinfo.ThumbnailUrl);
240
241                 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[1].Tag);
242                 thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[1].Tag;
243
244                 Assert.Equal("http://bar.example.com/efgh", thumbinfo.ImageUrl);
245                 Assert.Equal("dot.gif", thumbinfo.ThumbnailUrl);
246
247                 Assert.Equal("", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]));
248                 Assert.Equal("efgh", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[1]));
249             }
250         }
251
252         [Fact]
253         public async Task ThumbnailLoadingEventTest()
254         {
255             using (var thumbbox = new TweetThumbnail())
256             {
257                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
258
259                 bool eventCalled;
260                 thumbbox.ThumbnailLoading +=
261                     (s, e) => { eventCalled = true; };
262
263                 var post = new PostClass
264                 {
265                     TextFromApi = "てすと",
266                     Media = new Dictionary<string, string>
267                     {
268                     },
269                 };
270                 eventCalled = false;
271                 await thumbbox.ShowThumbnailAsync(post);
272
273                 Assert.False(eventCalled);
274
275                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
276
277                 var post2 = new PostClass
278                 {
279                     TextFromApi = "てすと http://foo.example.com/abcd",
280                     Media = new Dictionary<string, string>
281                     {
282                         {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
283                     },
284                 };
285                 eventCalled = false;
286                 await thumbbox.ShowThumbnailAsync(post2);
287
288                 Assert.True(eventCalled);
289             }
290         }
291
292         [Fact]
293         public async Task ScrollTest()
294         {
295             var post = new PostClass
296             {
297                 TextFromApi = "てすと http://foo.example.com/abcd http://foo.example.com/efgh",
298                 Media = new Dictionary<string, string>
299                 {
300                     {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
301                     {"http://foo.example.com/efgh", "http://foo.example.com/efgh"},
302                 },
303             };
304
305             using (var thumbbox = new TweetThumbnail())
306             {
307                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
308                 await thumbbox.ShowThumbnailAsync(post);
309
310                 Assert.Equal(0, thumbbox.scrollBar.Minimum);
311                 Assert.Equal(1, thumbbox.scrollBar.Maximum);
312
313                 thumbbox.scrollBar.Value = 0;
314
315                 thumbbox.ScrollUp();
316                 Assert.Equal(1, thumbbox.scrollBar.Value);
317                 Assert.False(thumbbox.pictureBox[0].Visible);
318                 Assert.True(thumbbox.pictureBox[1].Visible);
319
320                 thumbbox.ScrollUp();
321                 Assert.Equal(1, thumbbox.scrollBar.Value);
322                 Assert.False(thumbbox.pictureBox[0].Visible);
323                 Assert.True(thumbbox.pictureBox[1].Visible);
324
325                 thumbbox.ScrollDown();
326                 Assert.Equal(0, thumbbox.scrollBar.Value);
327                 Assert.True(thumbbox.pictureBox[0].Visible);
328                 Assert.False(thumbbox.pictureBox[1].Visible);
329
330                 thumbbox.ScrollDown();
331                 Assert.Equal(0, thumbbox.scrollBar.Value);
332                 Assert.True(thumbbox.pictureBox[0].Visible);
333                 Assert.False(thumbbox.pictureBox[1].Visible);
334             }
335         }
336     }
337 }