1 // OpenTween - Client of Twitter
2 // Copyright (c) 2012 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
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)
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
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.
23 using System.Collections.Generic;
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;
34 using OpenTween.Thumbnail;
35 using OpenTween.Thumbnail.Services;
37 using Xunit.Extensions;
41 public class TweetThumbnailTest
43 class TestThumbnailService : IThumbnailService
45 private readonly Regex regex;
46 private readonly string replaceUrl;
47 private readonly string replaceTooltip;
49 public TestThumbnailService(string pattern, string replaceUrl, string replaceTooltip)
51 this.regex = new Regex(pattern);
52 this.replaceUrl = replaceUrl;
53 this.replaceTooltip = replaceTooltip;
56 public override async Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
58 var match = this.regex.Match(url);
60 if (!match.Success) return null;
62 if (url.StartsWith("http://slow.example.com/", StringComparison.Ordinal))
63 await Task.Delay(1000, token).ConfigureAwait(false);
65 return new MockThumbnailInfo
68 ThumbnailUrl = match.Result(this.replaceUrl),
69 TooltipText = this.replaceTooltip != null ? match.Result(this.replaceTooltip) : null,
73 class MockThumbnailInfo : ThumbnailInfo
75 public override Task<MemoryImage> LoadThumbnailImageAsync(HttpClient http, CancellationToken cancellationToken)
77 return Task.FromResult(TestUtils.CreateDummyImage());
82 public TweetThumbnailTest()
84 this.ThumbnailGeneratorSetup();
88 public void ThumbnailGeneratorSetup()
90 ThumbnailGenerator.Services.Clear();
91 ThumbnailGenerator.Services.AddRange(new[]
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 new TestThumbnailService(@"^https?://slow.example.com/(.+)$", @"http://img.example.com/${1}.png", null),
99 public void MyCommonSetup()
101 var mockAssembly = new Mock<_Assembly>();
102 mockAssembly.Setup(m => m.GetName()).Returns(new AssemblyName("OpenTween"));
104 MyCommon.EntryAssembly = mockAssembly.Object;
108 public void CreatePictureBoxTest()
110 using (var thumbBox = new TweetThumbnail())
112 var method = typeof(TweetThumbnail).GetMethod("CreatePictureBox", BindingFlags.Instance | BindingFlags.NonPublic);
113 var picbox = method.Invoke(thumbBox, new[] { "pictureBox1" }) as PictureBox;
115 Assert.NotNull(picbox);
116 Assert.Equal("pictureBox1", picbox.Name);
117 Assert.Equal(PictureBoxSizeMode.Zoom, picbox.SizeMode);
118 Assert.False(picbox.WaitOnLoad);
119 Assert.Equal(DockStyle.Fill, picbox.Dock);
126 public async Task CancelAsyncTest()
128 var post = new PostClass
130 TextFromApi = "てすと http://slow.example.com/abcd",
131 Media = new List<MediaInfo>
133 new MediaInfo("http://slow.example.com/abcd"),
137 using (var thumbbox = new TweetThumbnail())
138 using (var tokenSource = new CancellationTokenSource())
140 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
141 var task = thumbbox.ShowThumbnailAsync(post, tokenSource.Token);
143 tokenSource.Cancel();
145 await TestUtils.ThrowsAnyAsync<OperationCanceledException>(async () => await task);
146 Assert.True(task.IsCanceled);
154 public void SetThumbnailCountTest(int count)
156 using (var thumbbox = new TweetThumbnail())
158 var method = typeof(TweetThumbnail).GetMethod("SetThumbnailCount", BindingFlags.Instance | BindingFlags.NonPublic);
159 method.Invoke(thumbbox, new[] { (object)count });
161 Assert.Equal(count, thumbbox.pictureBox.Count);
164 foreach (var picbox in thumbbox.pictureBox)
166 Assert.Equal("pictureBox" + num, picbox.Name);
170 Assert.Equal(thumbbox.pictureBox, thumbbox.panelPictureBox.Controls.Cast<OTPictureBox>());
172 Assert.Equal(0, thumbbox.scrollBar.Minimum);
175 Assert.Equal(0, thumbbox.scrollBar.Maximum);
177 Assert.Equal(count - 1, thumbbox.scrollBar.Maximum);
182 public async Task ShowThumbnailAsyncTest()
184 var post = new PostClass
186 TextFromApi = "てすと http://foo.example.com/abcd",
187 Media = new List<MediaInfo>
189 new MediaInfo("http://foo.example.com/abcd"),
193 using (var thumbbox = new TweetThumbnail())
195 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
196 await thumbbox.ShowThumbnailAsync(post);
198 Assert.Equal(0, thumbbox.scrollBar.Maximum);
199 Assert.False(thumbbox.scrollBar.Enabled);
201 Assert.Equal(1, thumbbox.pictureBox.Count);
202 Assert.NotNull(thumbbox.pictureBox[0].Image);
204 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
205 var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag;
207 Assert.Equal("http://foo.example.com/abcd", thumbinfo.ImageUrl);
208 Assert.Equal("http://img.example.com/abcd.png", thumbinfo.ThumbnailUrl);
210 Assert.Equal("", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]));
215 public async Task ShowThumbnailAsyncTest2()
217 var post = new PostClass
219 TextFromApi = "てすと http://foo.example.com/abcd http://bar.example.com/efgh",
220 Media = new List<MediaInfo>
222 new MediaInfo("http://foo.example.com/abcd"),
223 new MediaInfo("http://bar.example.com/efgh"),
227 using (var thumbbox = new TweetThumbnail())
229 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
230 await thumbbox.ShowThumbnailAsync(post);
232 Assert.Equal(1, thumbbox.scrollBar.Maximum);
233 Assert.True(thumbbox.scrollBar.Enabled);
235 Assert.Equal(2, thumbbox.pictureBox.Count);
236 Assert.NotNull(thumbbox.pictureBox[0].Image);
237 Assert.NotNull(thumbbox.pictureBox[1].Image);
239 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
240 var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag;
242 Assert.Equal("http://foo.example.com/abcd", thumbinfo.ImageUrl);
243 Assert.Equal("http://img.example.com/abcd.png", thumbinfo.ThumbnailUrl);
245 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[1].Tag);
246 thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[1].Tag;
248 Assert.Equal("http://bar.example.com/efgh", thumbinfo.ImageUrl);
249 Assert.Equal("http://img.example.com/efgh.png", thumbinfo.ThumbnailUrl);
251 Assert.Equal("", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]));
252 Assert.Equal("efgh", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[1]));
257 public async Task ThumbnailLoadingEventTest()
259 using (var thumbbox = new TweetThumbnail())
261 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
264 thumbbox.ThumbnailLoading +=
265 (s, e) => { eventCalled = true; };
267 var post = new PostClass
270 Media = new List<MediaInfo>
275 await thumbbox.ShowThumbnailAsync(post);
277 Assert.False(eventCalled);
279 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
281 var post2 = new PostClass
283 TextFromApi = "てすと http://foo.example.com/abcd",
284 Media = new List<MediaInfo>
286 new MediaInfo("http://foo.example.com/abcd"),
290 await thumbbox.ShowThumbnailAsync(post2);
292 Assert.True(eventCalled);
297 public async Task ScrollTest()
299 var post = new PostClass
301 TextFromApi = "てすと http://foo.example.com/abcd http://foo.example.com/efgh",
302 Media = new List<MediaInfo>
304 new MediaInfo("http://foo.example.com/abcd"),
305 new MediaInfo("http://foo.example.com/efgh"),
309 using (var thumbbox = new TweetThumbnail())
311 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
312 await thumbbox.ShowThumbnailAsync(post);
314 Assert.Equal(0, thumbbox.scrollBar.Minimum);
315 Assert.Equal(1, thumbbox.scrollBar.Maximum);
317 thumbbox.scrollBar.Value = 0;
319 thumbbox.ScrollDown();
320 Assert.Equal(1, thumbbox.scrollBar.Value);
321 Assert.False(thumbbox.pictureBox[0].Visible);
322 Assert.True(thumbbox.pictureBox[1].Visible);
324 thumbbox.ScrollDown();
325 Assert.Equal(1, thumbbox.scrollBar.Value);
326 Assert.False(thumbbox.pictureBox[0].Visible);
327 Assert.True(thumbbox.pictureBox[1].Visible);
330 Assert.Equal(0, thumbbox.scrollBar.Value);
331 Assert.True(thumbbox.pictureBox[0].Visible);
332 Assert.False(thumbbox.pictureBox[1].Visible);
335 Assert.Equal(0, thumbbox.scrollBar.Value);
336 Assert.True(thumbbox.pictureBox[0].Visible);
337 Assert.False(thumbbox.pictureBox[1].Visible);