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.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;
33 using OpenTween.Thumbnail;
34 using OpenTween.Thumbnail.Services;
36 using Xunit.Extensions;
40 public class TweetThumbnailTest
42 class TestThumbnailService : IThumbnailService
44 private readonly Regex regex;
45 private readonly string replaceUrl;
46 private readonly string replaceTooltip;
48 public TestThumbnailService(string pattern, string replaceUrl, string replaceTooltip)
50 this.regex = new Regex(pattern);
51 this.replaceUrl = replaceUrl;
52 this.replaceTooltip = replaceTooltip;
55 public override ThumbnailInfo GetThumbnailInfo(string url, PostClass post)
57 var match = this.regex.Match(url);
59 if (!match.Success) return null;
61 return new MockThumbnailInfo
64 ThumbnailUrl = match.Result(this.replaceUrl),
65 TooltipText = this.replaceTooltip != null ? match.Result(this.replaceTooltip) : null,
69 class MockThumbnailInfo : ThumbnailInfo
71 public override Task<MemoryImage> LoadThumbnailImageAsync(CancellationToken token)
73 return Task.Run(() => MemoryImage.CopyFromBytes(File.ReadAllBytes("Resources/" + this.ThumbnailUrl)), token);
78 public TweetThumbnailTest()
80 this.ThumbnailGeneratorSetup();
84 public void ThumbnailGeneratorSetup()
86 ThumbnailGenerator.Services.Clear();
87 ThumbnailGenerator.Services.AddRange(new[]
89 new TestThumbnailService(@"^https?://foo.example.com/(.+)$", @"dot.gif", null),
90 new TestThumbnailService(@"^https?://bar.example.com/(.+)$", @"dot.gif", @"${1}"),
94 public void MyCommonSetup()
96 var mockAssembly = Substitute.For<_Assembly>();
97 mockAssembly.GetName().Returns(new AssemblyName("OpenTween"));
98 MyCommon.EntryAssembly = mockAssembly;
100 MyCommon.fileVersion = "1.0.0.0";
104 public void CreatePictureBoxTest()
106 using (var thumbBox = new TweetThumbnail())
108 var method = typeof(TweetThumbnail).GetMethod("CreatePictureBox", BindingFlags.Instance | BindingFlags.NonPublic);
109 var picbox = method.Invoke(thumbBox, new[] { "pictureBox1" }) as PictureBox;
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);
122 public async Task CancelAsyncTest()
124 var post = new PostClass
126 TextFromApi = "てすと http://foo.example.com/abcd",
127 Media = new Dictionary<string, string>
129 {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
133 using (var thumbbox = new TweetThumbnail())
134 using (var tokenSource = new CancellationTokenSource())
136 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
137 var task = thumbbox.ShowThumbnailAsync(post, tokenSource.Token);
139 tokenSource.Cancel();
142 if (Type.GetType("Mono.Runtime") != null)
143 await TestUtils.ThrowsAsync<OperationCanceledException>(async () => await task);
145 await TestUtils.ThrowsAsync<TaskCanceledException>(async () => await task);
147 Assert.True(task.IsCanceled);
155 public void SetThumbnailCountTest(int count)
157 using (var thumbbox = new TweetThumbnail())
159 var method = typeof(TweetThumbnail).GetMethod("SetThumbnailCount", BindingFlags.Instance | BindingFlags.NonPublic);
160 method.Invoke(thumbbox, new[] { (object)count });
162 Assert.Equal(count, thumbbox.pictureBox.Count);
165 foreach (var picbox in thumbbox.pictureBox)
167 Assert.Equal("pictureBox" + num, picbox.Name);
171 Assert.Equal(thumbbox.pictureBox, thumbbox.panelPictureBox.Controls.Cast<OTPictureBox>());
173 Assert.Equal(0, thumbbox.scrollBar.Minimum);
176 Assert.Equal(0, thumbbox.scrollBar.Maximum);
178 Assert.Equal(count - 1, thumbbox.scrollBar.Maximum);
183 public async Task ShowThumbnailAsyncTest()
185 var post = new PostClass
187 TextFromApi = "てすと http://foo.example.com/abcd",
188 Media = new Dictionary<string, string>
190 {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
194 using (var thumbbox = new TweetThumbnail())
196 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
197 await thumbbox.ShowThumbnailAsync(post);
199 Assert.Equal(0, thumbbox.scrollBar.Maximum);
200 Assert.False(thumbbox.scrollBar.Enabled);
202 Assert.Equal(1, thumbbox.pictureBox.Count);
203 Assert.NotNull(thumbbox.pictureBox[0].Image);
205 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
206 var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag;
208 Assert.Equal("http://foo.example.com/abcd", thumbinfo.ImageUrl);
209 Assert.Equal("dot.gif", thumbinfo.ThumbnailUrl);
211 Assert.Equal("", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]));
216 public async Task ShowThumbnailAsyncTest2()
218 var post = new PostClass
220 TextFromApi = "てすと http://foo.example.com/abcd http://bar.example.com/efgh",
221 Media = new Dictionary<string, string>
223 {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
224 {"http://bar.example.com/efgh", "http://bar.example.com/efgh"},
228 using (var thumbbox = new TweetThumbnail())
230 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
231 await thumbbox.ShowThumbnailAsync(post);
233 Assert.Equal(1, thumbbox.scrollBar.Maximum);
234 Assert.True(thumbbox.scrollBar.Enabled);
236 Assert.Equal(2, thumbbox.pictureBox.Count);
237 Assert.NotNull(thumbbox.pictureBox[0].Image);
238 Assert.NotNull(thumbbox.pictureBox[1].Image);
240 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
241 var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag;
243 Assert.Equal("http://foo.example.com/abcd", thumbinfo.ImageUrl);
244 Assert.Equal("dot.gif", thumbinfo.ThumbnailUrl);
246 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[1].Tag);
247 thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[1].Tag;
249 Assert.Equal("http://bar.example.com/efgh", thumbinfo.ImageUrl);
250 Assert.Equal("dot.gif", thumbinfo.ThumbnailUrl);
252 Assert.Equal("", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]));
253 Assert.Equal("efgh", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[1]));
258 public async Task ThumbnailLoadingEventTest()
260 using (var thumbbox = new TweetThumbnail())
262 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
265 thumbbox.ThumbnailLoading +=
266 (s, e) => { eventCalled = true; };
268 var post = new PostClass
271 Media = new Dictionary<string, string>
276 await thumbbox.ShowThumbnailAsync(post);
278 Assert.False(eventCalled);
280 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
282 var post2 = new PostClass
284 TextFromApi = "てすと http://foo.example.com/abcd",
285 Media = new Dictionary<string, string>
287 {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
291 await thumbbox.ShowThumbnailAsync(post2);
293 Assert.True(eventCalled);
298 public async Task ScrollTest()
300 var post = new PostClass
302 TextFromApi = "てすと http://foo.example.com/abcd http://foo.example.com/efgh",
303 Media = new Dictionary<string, string>
305 {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
306 {"http://foo.example.com/efgh", "http://foo.example.com/efgh"},
310 using (var thumbbox = new TweetThumbnail())
312 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
313 await thumbbox.ShowThumbnailAsync(post);
315 Assert.Equal(0, thumbbox.scrollBar.Minimum);
316 Assert.Equal(1, thumbbox.scrollBar.Maximum);
318 thumbbox.scrollBar.Value = 0;
321 Assert.Equal(1, thumbbox.scrollBar.Value);
322 Assert.False(thumbbox.pictureBox[0].Visible);
323 Assert.True(thumbbox.pictureBox[1].Visible);
326 Assert.Equal(1, thumbbox.scrollBar.Value);
327 Assert.False(thumbbox.pictureBox[0].Visible);
328 Assert.True(thumbbox.pictureBox[1].Visible);
330 thumbbox.ScrollDown();
331 Assert.Equal(0, thumbbox.scrollBar.Value);
332 Assert.True(thumbbox.pictureBox[0].Visible);
333 Assert.False(thumbbox.pictureBox[1].Visible);
335 thumbbox.ScrollDown();
336 Assert.Equal(0, thumbbox.scrollBar.Value);
337 Assert.True(thumbbox.pictureBox[0].Visible);
338 Assert.False(thumbbox.pictureBox[1].Visible);