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.Models;
35 using OpenTween.Thumbnail;
36 using OpenTween.Thumbnail.Services;
38 using Xunit.Extensions;
42 public class TweetThumbnailTest
44 class TestThumbnailService : IThumbnailService
46 private readonly Regex regex;
47 private readonly string replaceUrl;
48 private readonly string replaceTooltip;
50 public TestThumbnailService(string pattern, string replaceUrl, string replaceTooltip)
52 this.regex = new Regex(pattern);
53 this.replaceUrl = replaceUrl;
54 this.replaceTooltip = replaceTooltip;
57 public override async Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
59 var match = this.regex.Match(url);
61 if (!match.Success) return null;
63 if (url.StartsWith("http://slow.example.com/", StringComparison.Ordinal))
64 await Task.Delay(1000, token).ConfigureAwait(false);
66 return new MockThumbnailInfo
69 ThumbnailImageUrl = match.Result(this.replaceUrl),
70 TooltipText = this.replaceTooltip != null ? match.Result(this.replaceTooltip) : null,
74 class MockThumbnailInfo : ThumbnailInfo
76 public override Task<MemoryImage> LoadThumbnailImageAsync(HttpClient http, CancellationToken cancellationToken)
77 => Task.FromResult(TestUtils.CreateDummyImage());
81 public TweetThumbnailTest()
83 this.ThumbnailGeneratorSetup();
87 private void ThumbnailGeneratorSetup()
89 ThumbnailGenerator.Services.Clear();
90 ThumbnailGenerator.Services.AddRange(new[]
92 new TestThumbnailService(@"^https?://foo.example.com/(.+)$", @"http://img.example.com/${1}.png", null),
93 new TestThumbnailService(@"^https?://bar.example.com/(.+)$", @"http://img.example.com/${1}.png", @"${1}"),
94 new TestThumbnailService(@"^https?://slow.example.com/(.+)$", @"http://img.example.com/${1}.png", null),
98 private void MyCommonSetup()
100 var mockAssembly = new Mock<_Assembly>();
101 mockAssembly.Setup(m => m.GetName()).Returns(new AssemblyName("OpenTween"));
103 MyCommon.EntryAssembly = mockAssembly.Object;
107 public void CreatePictureBoxTest()
109 using (var thumbBox = new TweetThumbnail())
111 var method = typeof(TweetThumbnail).GetMethod("CreatePictureBox", BindingFlags.Instance | BindingFlags.NonPublic);
112 var picbox = method.Invoke(thumbBox, new[] { "pictureBox1" }) as PictureBox;
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);
125 public async Task CancelAsyncTest()
127 var post = new PostClass
129 TextFromApi = "てすと http://slow.example.com/abcd",
130 Media = new List<MediaInfo>
132 new MediaInfo("http://slow.example.com/abcd"),
136 using (var thumbbox = new TweetThumbnail())
137 using (var tokenSource = new CancellationTokenSource())
139 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
140 var task = thumbbox.ShowThumbnailAsync(post, tokenSource.Token);
142 tokenSource.Cancel();
144 await Assert.ThrowsAnyAsync<OperationCanceledException>(async () => await task);
145 Assert.True(task.IsCanceled);
153 public void SetThumbnailCountTest(int count)
155 using (var thumbbox = new TweetThumbnail())
157 var method = typeof(TweetThumbnail).GetMethod("SetThumbnailCount", BindingFlags.Instance | BindingFlags.NonPublic);
158 method.Invoke(thumbbox, new[] { (object)count });
160 Assert.Equal(count, thumbbox.pictureBox.Count);
163 foreach (var picbox in thumbbox.pictureBox)
165 Assert.Equal("pictureBox" + num, picbox.Name);
169 Assert.Equal(thumbbox.pictureBox, thumbbox.panelPictureBox.Controls.Cast<OTPictureBox>());
171 Assert.Equal(0, thumbbox.scrollBar.Minimum);
174 Assert.Equal(0, thumbbox.scrollBar.Maximum);
176 Assert.Equal(count - 1, thumbbox.scrollBar.Maximum);
181 public async Task ShowThumbnailAsyncTest()
183 var post = new PostClass
185 TextFromApi = "てすと http://foo.example.com/abcd",
186 Media = new List<MediaInfo>
188 new MediaInfo("http://foo.example.com/abcd"),
192 using (var thumbbox = new TweetThumbnail())
194 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
195 await thumbbox.ShowThumbnailAsync(post);
197 Assert.Equal(0, thumbbox.scrollBar.Maximum);
198 Assert.False(thumbbox.scrollBar.Enabled);
200 Assert.Single(thumbbox.pictureBox);
201 Assert.NotNull(thumbbox.pictureBox[0].Image);
203 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
204 var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag;
206 Assert.Equal("http://foo.example.com/abcd", thumbinfo.MediaPageUrl);
207 Assert.Equal("http://img.example.com/abcd.png", thumbinfo.ThumbnailImageUrl);
209 Assert.Equal("", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]));
214 public async Task ShowThumbnailAsyncTest2()
216 var post = new PostClass
218 TextFromApi = "てすと http://foo.example.com/abcd http://bar.example.com/efgh",
219 Media = new List<MediaInfo>
221 new MediaInfo("http://foo.example.com/abcd"),
222 new MediaInfo("http://bar.example.com/efgh"),
226 using (var thumbbox = new TweetThumbnail())
228 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
229 await thumbbox.ShowThumbnailAsync(post);
231 Assert.Equal(1, thumbbox.scrollBar.Maximum);
232 Assert.True(thumbbox.scrollBar.Enabled);
234 Assert.Equal(2, thumbbox.pictureBox.Count);
235 Assert.NotNull(thumbbox.pictureBox[0].Image);
236 Assert.NotNull(thumbbox.pictureBox[1].Image);
238 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
239 var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag;
241 Assert.Equal("http://foo.example.com/abcd", thumbinfo.MediaPageUrl);
242 Assert.Equal("http://img.example.com/abcd.png", thumbinfo.ThumbnailImageUrl);
244 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[1].Tag);
245 thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[1].Tag;
247 Assert.Equal("http://bar.example.com/efgh", thumbinfo.MediaPageUrl);
248 Assert.Equal("http://img.example.com/efgh.png", thumbinfo.ThumbnailImageUrl);
250 Assert.Equal("", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]));
251 Assert.Equal("efgh", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[1]));
256 public async Task ThumbnailLoadingEventTest()
258 using (var thumbbox = new TweetThumbnail())
260 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
262 var post = new PostClass
265 Media = new List<MediaInfo>
269 await TestUtils.NotRaisesAsync<EventArgs>(
270 x => thumbbox.ThumbnailLoading += x,
271 x => thumbbox.ThumbnailLoading -= x,
272 () => thumbbox.ShowThumbnailAsync(post)
275 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
277 var post2 = new PostClass
279 TextFromApi = "てすと http://foo.example.com/abcd",
280 Media = new List<MediaInfo>
282 new MediaInfo("http://foo.example.com/abcd"),
286 await Assert.RaisesAsync<EventArgs>(
287 x => thumbbox.ThumbnailLoading += x,
288 x => thumbbox.ThumbnailLoading -= x,
289 () => thumbbox.ShowThumbnailAsync(post2)
295 public async Task ScrollTest()
297 var post = new PostClass
299 TextFromApi = "てすと http://foo.example.com/abcd http://foo.example.com/efgh",
300 Media = new List<MediaInfo>
302 new MediaInfo("http://foo.example.com/abcd"),
303 new MediaInfo("http://foo.example.com/efgh"),
307 using (var thumbbox = new TweetThumbnail())
309 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
310 await thumbbox.ShowThumbnailAsync(post);
312 Assert.Equal(0, thumbbox.scrollBar.Minimum);
313 Assert.Equal(1, thumbbox.scrollBar.Maximum);
315 thumbbox.scrollBar.Value = 0;
317 thumbbox.ScrollDown();
318 Assert.Equal(1, thumbbox.scrollBar.Value);
319 Assert.False(thumbbox.pictureBox[0].Visible);
320 Assert.True(thumbbox.pictureBox[1].Visible);
322 thumbbox.ScrollDown();
323 Assert.Equal(1, thumbbox.scrollBar.Value);
324 Assert.False(thumbbox.pictureBox[0].Visible);
325 Assert.True(thumbbox.pictureBox[1].Visible);
328 Assert.Equal(0, thumbbox.scrollBar.Value);
329 Assert.True(thumbbox.pictureBox[0].Visible);
330 Assert.False(thumbbox.pictureBox[1].Visible);
333 Assert.Equal(0, thumbbox.scrollBar.Value);
334 Assert.True(thumbbox.pictureBox[0].Visible);
335 Assert.False(thumbbox.pictureBox[1].Visible);