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 Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
58 return Task.Run<ThumbnailInfo>(() =>
60 var match = this.regex.Match(url);
62 if (!match.Success) return null;
64 return new MockThumbnailInfo
67 ThumbnailUrl = match.Result(this.replaceUrl),
68 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}"),
98 public 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://foo.example.com/abcd",
130 Media = new List<MediaInfo>
132 new MediaInfo("http://foo.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 TestUtils.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.Equal(1, thumbbox.pictureBox.Count);
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.ImageUrl);
207 Assert.Equal("http://img.example.com/abcd.png", thumbinfo.ThumbnailUrl);
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.ImageUrl);
242 Assert.Equal("http://img.example.com/abcd.png", thumbinfo.ThumbnailUrl);
244 Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[1].Tag);
245 thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[1].Tag;
247 Assert.Equal("http://bar.example.com/efgh", thumbinfo.ImageUrl);
248 Assert.Equal("http://img.example.com/efgh.png", thumbinfo.ThumbnailUrl);
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());
263 thumbbox.ThumbnailLoading +=
264 (s, e) => { eventCalled = true; };
266 var post = new PostClass
269 Media = new List<MediaInfo>
274 await thumbbox.ShowThumbnailAsync(post);
276 Assert.False(eventCalled);
278 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
280 var post2 = new PostClass
282 TextFromApi = "てすと http://foo.example.com/abcd",
283 Media = new List<MediaInfo>
285 new MediaInfo("http://foo.example.com/abcd"),
289 await thumbbox.ShowThumbnailAsync(post2);
291 Assert.True(eventCalled);
296 public async Task ScrollTest()
298 var post = new PostClass
300 TextFromApi = "てすと http://foo.example.com/abcd http://foo.example.com/efgh",
301 Media = new List<MediaInfo>
303 new MediaInfo("http://foo.example.com/abcd"),
304 new MediaInfo("http://foo.example.com/efgh"),
308 using (var thumbbox = new TweetThumbnail())
310 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
311 await thumbbox.ShowThumbnailAsync(post);
313 Assert.Equal(0, thumbbox.scrollBar.Minimum);
314 Assert.Equal(1, thumbbox.scrollBar.Maximum);
316 thumbbox.scrollBar.Value = 0;
318 thumbbox.ScrollDown();
319 Assert.Equal(1, thumbbox.scrollBar.Value);
320 Assert.False(thumbbox.pictureBox[0].Visible);
321 Assert.True(thumbbox.pictureBox[1].Visible);
323 thumbbox.ScrollDown();
324 Assert.Equal(1, thumbbox.scrollBar.Value);
325 Assert.False(thumbbox.pictureBox[0].Visible);
326 Assert.True(thumbbox.pictureBox[1].Visible);
329 Assert.Equal(0, thumbbox.scrollBar.Value);
330 Assert.True(thumbbox.pictureBox[0].Visible);
331 Assert.False(thumbbox.pictureBox[1].Visible);
334 Assert.Equal(0, thumbbox.scrollBar.Value);
335 Assert.True(thumbbox.pictureBox[0].Visible);
336 Assert.False(thumbbox.pictureBox[1].Visible);