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();
141 await TestUtils.ThrowsAnyAsync<OperationCanceledException>(async () => await task);
142 Assert.True(task.IsCanceled);
150 public void SetThumbnailCountTest(int count)
152 using (var thumbbox = new TweetThumbnail())
154 var method = typeof(TweetThumbnail).GetMethod("SetThumbnailCount", BindingFlags.Instance | BindingFlags.NonPublic);
155 method.Invoke(thumbbox, new[] { (object)count });
157 Assert.Equal(count, thumbbox.pictureBox.Count);
160 foreach (var picbox in thumbbox.pictureBox)
162 Assert.Equal("pictureBox" + num, picbox.Name);
166 Assert.Equal(thumbbox.pictureBox, thumbbox.panelPictureBox.Controls.Cast<OTPictureBox>());
168 Assert.Equal(0, thumbbox.scrollBar.Minimum);
171 Assert.Equal(0, thumbbox.scrollBar.Maximum);
173 Assert.Equal(count - 1, thumbbox.scrollBar.Maximum);
178 public async Task ShowThumbnailAsyncTest()
180 var post = new PostClass
182 TextFromApi = "てすと http://foo.example.com/abcd",
183 Media = new Dictionary<string, string>
185 {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
189 using (var thumbbox = new TweetThumbnail())
191 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
192 await thumbbox.ShowThumbnailAsync(post);
194 Assert.Equal(0, thumbbox.scrollBar.Maximum);
195 Assert.False(thumbbox.scrollBar.Enabled);
197 Assert.Equal(1, thumbbox.pictureBox.Count);
198 Assert.NotNull(thumbbox.pictureBox[0].Image);
200 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
201 var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag;
203 Assert.Equal("http://foo.example.com/abcd", thumbinfo.ImageUrl);
204 Assert.Equal("dot.gif", thumbinfo.ThumbnailUrl);
206 Assert.Equal("", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]));
211 public async Task ShowThumbnailAsyncTest2()
213 var post = new PostClass
215 TextFromApi = "てすと http://foo.example.com/abcd http://bar.example.com/efgh",
216 Media = new Dictionary<string, string>
218 {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
219 {"http://bar.example.com/efgh", "http://bar.example.com/efgh"},
223 using (var thumbbox = new TweetThumbnail())
225 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
226 await thumbbox.ShowThumbnailAsync(post);
228 Assert.Equal(1, thumbbox.scrollBar.Maximum);
229 Assert.True(thumbbox.scrollBar.Enabled);
231 Assert.Equal(2, thumbbox.pictureBox.Count);
232 Assert.NotNull(thumbbox.pictureBox[0].Image);
233 Assert.NotNull(thumbbox.pictureBox[1].Image);
235 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
236 var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag;
238 Assert.Equal("http://foo.example.com/abcd", thumbinfo.ImageUrl);
239 Assert.Equal("dot.gif", thumbinfo.ThumbnailUrl);
241 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[1].Tag);
242 thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[1].Tag;
244 Assert.Equal("http://bar.example.com/efgh", thumbinfo.ImageUrl);
245 Assert.Equal("dot.gif", thumbinfo.ThumbnailUrl);
247 Assert.Equal("", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]));
248 Assert.Equal("efgh", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[1]));
253 public async Task ThumbnailLoadingEventTest()
255 using (var thumbbox = new TweetThumbnail())
257 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
260 thumbbox.ThumbnailLoading +=
261 (s, e) => { eventCalled = true; };
263 var post = new PostClass
266 Media = new Dictionary<string, string>
271 await thumbbox.ShowThumbnailAsync(post);
273 Assert.False(eventCalled);
275 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
277 var post2 = new PostClass
279 TextFromApi = "てすと http://foo.example.com/abcd",
280 Media = new Dictionary<string, string>
282 {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
286 await thumbbox.ShowThumbnailAsync(post2);
288 Assert.True(eventCalled);
293 public async Task ScrollTest()
295 var post = new PostClass
297 TextFromApi = "てすと http://foo.example.com/abcd http://foo.example.com/efgh",
298 Media = new Dictionary<string, string>
300 {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
301 {"http://foo.example.com/efgh", "http://foo.example.com/efgh"},
305 using (var thumbbox = new TweetThumbnail())
307 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
308 await thumbbox.ShowThumbnailAsync(post);
310 Assert.Equal(0, thumbbox.scrollBar.Minimum);
311 Assert.Equal(1, thumbbox.scrollBar.Maximum);
313 thumbbox.scrollBar.Value = 0;
316 Assert.Equal(1, thumbbox.scrollBar.Value);
317 Assert.False(thumbbox.pictureBox[0].Visible);
318 Assert.True(thumbbox.pictureBox[1].Visible);
321 Assert.Equal(1, thumbbox.scrollBar.Value);
322 Assert.False(thumbbox.pictureBox[0].Visible);
323 Assert.True(thumbbox.pictureBox[1].Visible);
325 thumbbox.ScrollDown();
326 Assert.Equal(0, thumbbox.scrollBar.Value);
327 Assert.True(thumbbox.pictureBox[0].Visible);
328 Assert.False(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);