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 NUnit.Framework;
27 using System.Reflection;
28 using System.Windows.Forms;
29 using OpenTween.Thumbnail;
30 using OpenTween.Thumbnail.Services;
31 using System.Threading;
32 using System.Threading.Tasks;
37 class TweetThumbnailTest
39 class TestThumbnailService : SimpleThumbnailService
41 protected string tooltip;
43 public TestThumbnailService(string pattern, string replacement, string tooltip)
44 : base(pattern, replacement)
46 this.tooltip = tooltip;
49 public override ThumbnailInfo GetThumbnailInfo(string url, PostClass post)
51 var thumbinfo = base.GetThumbnailInfo(url, post);
53 if (thumbinfo != null && this.tooltip != null)
55 var match = this.regex.Match(url);
56 thumbinfo.TooltipText = match.Result(this.tooltip);
64 public void ThumbnailGeneratorSetup()
66 ThumbnailGenerator.Services.Clear();
67 ThumbnailGenerator.Services.AddRange(new[]
69 new TestThumbnailService(@"^https?://foo.example.com/(.+)$", @"dot.gif", null),
70 new TestThumbnailService(@"^https?://bar.example.com/(.+)$", @"dot.gif", @"${1}"),
75 public void CreatePictureBoxTest()
77 using (var thumbBox = new TweetThumbnail())
79 var method = typeof(TweetThumbnail).GetMethod("CreatePictureBox", BindingFlags.Instance | BindingFlags.NonPublic);
80 var picbox = method.Invoke(thumbBox, new[] { "pictureBox1" }) as PictureBox;
82 Assert.That(picbox, Is.Not.Null);
83 Assert.That(picbox.Name, Is.EqualTo("pictureBox1"));
84 Assert.That(picbox.SizeMode, Is.EqualTo(PictureBoxSizeMode.Zoom));
85 Assert.That(picbox.WaitOnLoad, Is.False);
86 Assert.That(picbox.Dock, Is.EqualTo(DockStyle.Fill));
93 public void CancelAsyncTest()
95 using (var thumbbox = new TweetThumbnail())
97 var post = new PostClass();
99 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
100 var task = thumbbox.ShowThumbnailAsync(post);
102 thumbbox.CancelAsync();
104 Assert.That(task.IsCanceled, Is.True);
109 public void SetThumbnailCountTest(
110 [Values(0, 1, 2)] int count)
112 using (var thumbbox = new TweetThumbnail())
114 var method = typeof(TweetThumbnail).GetMethod("SetThumbnailCount", BindingFlags.Instance | BindingFlags.NonPublic);
115 method.Invoke(thumbbox, new[] { (object)count });
117 Assert.That(thumbbox.pictureBox.Count, Is.EqualTo(count));
120 foreach (var picbox in thumbbox.pictureBox)
122 Assert.That(picbox.Name, Is.EqualTo("pictureBox" + num));
126 Assert.That(thumbbox.Controls, Is.EquivalentTo(new Control[]{ thumbbox.scrollBar }.Concat(thumbbox.pictureBox)));
128 Assert.That(thumbbox.scrollBar.Minimum, Is.EqualTo(0));
129 Assert.That(thumbbox.scrollBar.Maximum, Is.EqualTo(count));
134 public void ShowThumbnailAsyncTest()
136 var post = new PostClass
138 TextFromApi = "てすと http://foo.example.com/abcd",
139 Media = new Dictionary<string, string>
141 {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
145 using (var thumbbox = new TweetThumbnail())
147 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
148 thumbbox.ShowThumbnailAsync(post).Wait();
150 Assert.That(thumbbox.scrollBar.Maximum, Is.EqualTo(0));
151 Assert.That(thumbbox.scrollBar.Enabled, Is.False);
153 Assert.That(thumbbox.pictureBox.Count, Is.EqualTo(1));
154 Assert.That(thumbbox.pictureBox[0].ImageLocation, Is.EqualTo("dot.gif"));
156 var thumbinfo = thumbbox.pictureBox[0].Tag as ThumbnailInfo;
157 Assert.That(thumbinfo, Is.Not.Null);
158 Assert.That(thumbinfo.ImageUrl, Is.EqualTo("http://foo.example.com/abcd"));
159 Assert.That(thumbinfo.ThumbnailUrl, Is.EqualTo("dot.gif"));
161 Assert.That(thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]), Is.EqualTo(""));
166 public void ShowThumbnailAsyncTest2()
168 var post = new PostClass
170 TextFromApi = "てすと http://foo.example.com/abcd http://bar.example.com/efgh",
171 Media = new Dictionary<string, string>
173 {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
174 {"http://bar.example.com/efgh", "http://bar.example.com/efgh"},
178 using (var thumbbox = new TweetThumbnail())
180 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
181 thumbbox.ShowThumbnailAsync(post).Wait();
183 Assert.That(thumbbox.scrollBar.Maximum, Is.EqualTo(1));
184 Assert.That(thumbbox.scrollBar.Enabled, Is.True);
186 Assert.That(thumbbox.pictureBox.Count, Is.EqualTo(2));
187 Assert.That(thumbbox.pictureBox[0].ImageLocation, Is.EqualTo("dot.gif"));
188 Assert.That(thumbbox.pictureBox[1].ImageLocation, Is.EqualTo("dot.gif"));
190 var thumbinfo = thumbbox.pictureBox[0].Tag as ThumbnailInfo;
191 Assert.That(thumbinfo, Is.Not.Null);
192 Assert.That(thumbinfo.ImageUrl, Is.EqualTo("http://foo.example.com/abcd"));
193 Assert.That(thumbinfo.ThumbnailUrl, Is.EqualTo("dot.gif"));
195 thumbinfo = thumbbox.pictureBox[1].Tag as ThumbnailInfo;
196 Assert.That(thumbinfo, Is.Not.Null);
197 Assert.That(thumbinfo.ImageUrl, Is.EqualTo("http://bar.example.com/efgh"));
198 Assert.That(thumbinfo.ThumbnailUrl, Is.EqualTo("dot.gif"));
200 Assert.That(thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]), Is.EqualTo(""));
201 Assert.That(thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[1]), Is.EqualTo("efgh"));
206 public void ThumbnailLoadingEventTest()
208 using (var thumbbox = new TweetThumbnail())
210 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
213 thumbbox.ThumbnailLoading +=
214 (s, e) => { eventCalled = true; };
216 var post = new PostClass
219 Media = new Dictionary<string, string>
224 thumbbox.ShowThumbnailAsync(post).Wait();
226 Assert.That(eventCalled, Is.False);
228 var post2 = new PostClass
230 TextFromApi = "てすと http://foo.example.com/abcd",
231 Media = new Dictionary<string, string>
233 {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
237 thumbbox.ShowThumbnailAsync(post2).Wait();
239 Assert.That(eventCalled, Is.True);
244 public void ScrollTest()
246 var post = new PostClass
248 TextFromApi = "てすと http://foo.example.com/abcd http://foo.example.com/efgh",
249 Media = new Dictionary<string, string>
251 {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
252 {"http://foo.example.com/efgh", "http://foo.example.com/efgh"},
256 using (var thumbbox = new TweetThumbnail())
258 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
259 thumbbox.ShowThumbnailAsync(post).Wait();
261 Assert.That(thumbbox.scrollBar.Minimum, Is.EqualTo(0));
262 Assert.That(thumbbox.scrollBar.Maximum, Is.EqualTo(1));
264 thumbbox.scrollBar.Value = 0;
267 Assert.That(thumbbox.scrollBar.Value, Is.EqualTo(1));
268 Assert.That(thumbbox.pictureBox[0].Visible, Is.False);
269 Assert.That(thumbbox.pictureBox[1].Visible, Is.True);
272 Assert.That(thumbbox.scrollBar.Value, Is.EqualTo(1));
273 Assert.That(thumbbox.pictureBox[0].Visible, Is.False);
274 Assert.That(thumbbox.pictureBox[1].Visible, Is.True);
276 thumbbox.ScrollDown();
277 Assert.That(thumbbox.scrollBar.Value, Is.EqualTo(0));
278 Assert.That(thumbbox.pictureBox[0].Visible, Is.True);
279 Assert.That(thumbbox.pictureBox[1].Visible, Is.False);
281 thumbbox.ScrollDown();
282 Assert.That(thumbbox.scrollBar.Value, Is.EqualTo(0));
283 Assert.That(thumbbox.pictureBox[0].Visible, Is.True);
284 Assert.That(thumbbox.pictureBox[1].Visible, Is.False);