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.Factory.StartNew(() => 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);
121 [Fact(Skip = "Mono環境でたまに InvaliOperationException: out of sync で異常終了する")]
122 public void CancelAsyncTest()
124 using (var thumbbox = new TweetThumbnail())
126 var post = new PostClass();
128 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
129 var task = thumbbox.ShowThumbnailAsync(post);
131 thumbbox.CancelAsync();
133 Assert.Throws<AggregateException>(() => task.Wait());
134 Assert.True(task.IsCanceled);
142 public void SetThumbnailCountTest(int count)
144 using (var thumbbox = new TweetThumbnail())
146 var method = typeof(TweetThumbnail).GetMethod("SetThumbnailCount", BindingFlags.Instance | BindingFlags.NonPublic);
147 method.Invoke(thumbbox, new[] { (object)count });
149 Assert.Equal(count, thumbbox.pictureBox.Count);
152 foreach (var picbox in thumbbox.pictureBox)
154 Assert.Equal("pictureBox" + num, picbox.Name);
158 Assert.Equal(thumbbox.pictureBox, thumbbox.panelPictureBox.Controls.Cast<OTPictureBox>());
160 Assert.Equal(0, thumbbox.scrollBar.Minimum);
163 Assert.Equal(0, thumbbox.scrollBar.Maximum);
165 Assert.Equal(count - 1, thumbbox.scrollBar.Maximum);
170 public void ShowThumbnailAsyncTest()
172 var post = new PostClass
174 TextFromApi = "てすと http://foo.example.com/abcd",
175 Media = new Dictionary<string, string>
177 {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
181 using (var thumbbox = new TweetThumbnail())
183 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
184 thumbbox.ShowThumbnailAsync(post).Wait();
186 Assert.Equal(0, thumbbox.scrollBar.Maximum);
187 Assert.False(thumbbox.scrollBar.Enabled);
189 Assert.Equal(1, thumbbox.pictureBox.Count);
190 Assert.NotNull(thumbbox.pictureBox[0].Image);
192 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
193 var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag;
195 Assert.Equal("http://foo.example.com/abcd", thumbinfo.ImageUrl);
196 Assert.Equal("dot.gif", thumbinfo.ThumbnailUrl);
198 Assert.Equal("", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]));
203 public void ShowThumbnailAsyncTest2()
205 var post = new PostClass
207 TextFromApi = "てすと http://foo.example.com/abcd http://bar.example.com/efgh",
208 Media = new Dictionary<string, string>
210 {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
211 {"http://bar.example.com/efgh", "http://bar.example.com/efgh"},
215 using (var thumbbox = new TweetThumbnail())
217 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
218 thumbbox.ShowThumbnailAsync(post).Wait();
220 Assert.Equal(1, thumbbox.scrollBar.Maximum);
221 Assert.True(thumbbox.scrollBar.Enabled);
223 Assert.Equal(2, thumbbox.pictureBox.Count);
224 Assert.NotNull(thumbbox.pictureBox[0].Image);
225 Assert.NotNull(thumbbox.pictureBox[1].Image);
227 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
228 var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag;
230 Assert.Equal("http://foo.example.com/abcd", thumbinfo.ImageUrl);
231 Assert.Equal("dot.gif", thumbinfo.ThumbnailUrl);
233 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[1].Tag);
234 thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[1].Tag;
236 Assert.Equal("http://bar.example.com/efgh", thumbinfo.ImageUrl);
237 Assert.Equal("dot.gif", thumbinfo.ThumbnailUrl);
239 Assert.Equal("", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]));
240 Assert.Equal("efgh", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[1]));
245 public void ThumbnailLoadingEventTest()
247 using (var thumbbox = new TweetThumbnail())
249 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
252 thumbbox.ThumbnailLoading +=
253 (s, e) => { eventCalled = true; };
255 var post = new PostClass
258 Media = new Dictionary<string, string>
263 thumbbox.ShowThumbnailAsync(post).Wait();
265 Assert.False(eventCalled);
267 var post2 = new PostClass
269 TextFromApi = "てすと http://foo.example.com/abcd",
270 Media = new Dictionary<string, string>
272 {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
276 thumbbox.ShowThumbnailAsync(post2).Wait();
278 Assert.True(eventCalled);
283 public void ScrollTest()
285 var post = new PostClass
287 TextFromApi = "てすと http://foo.example.com/abcd http://foo.example.com/efgh",
288 Media = new Dictionary<string, string>
290 {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
291 {"http://foo.example.com/efgh", "http://foo.example.com/efgh"},
295 using (var thumbbox = new TweetThumbnail())
297 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
298 thumbbox.ShowThumbnailAsync(post).Wait();
300 Assert.Equal(0, thumbbox.scrollBar.Minimum);
301 Assert.Equal(1, thumbbox.scrollBar.Maximum);
303 thumbbox.scrollBar.Value = 0;
306 Assert.Equal(1, thumbbox.scrollBar.Value);
307 Assert.False(thumbbox.pictureBox[0].Visible);
308 Assert.True(thumbbox.pictureBox[1].Visible);
311 Assert.Equal(1, thumbbox.scrollBar.Value);
312 Assert.False(thumbbox.pictureBox[0].Visible);
313 Assert.True(thumbbox.pictureBox[1].Visible);
315 thumbbox.ScrollDown();
316 Assert.Equal(0, thumbbox.scrollBar.Value);
317 Assert.True(thumbbox.pictureBox[0].Visible);
318 Assert.False(thumbbox.pictureBox[1].Visible);
320 thumbbox.ScrollDown();
321 Assert.Equal(0, thumbbox.scrollBar.Value);
322 Assert.True(thumbbox.pictureBox[0].Visible);
323 Assert.False(thumbbox.pictureBox[1].Visible);