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 MyCommonSetup()
77 MyCommon.fileVersion = "1.0.0.0";
81 public void CreatePictureBoxTest()
83 using (var thumbBox = new TweetThumbnail())
85 var method = typeof(TweetThumbnail).GetMethod("CreatePictureBox", BindingFlags.Instance | BindingFlags.NonPublic);
86 var picbox = method.Invoke(thumbBox, new[] { "pictureBox1" }) as PictureBox;
88 Assert.That(picbox, Is.Not.Null);
89 Assert.That(picbox.Name, Is.EqualTo("pictureBox1"));
90 Assert.That(picbox.SizeMode, Is.EqualTo(PictureBoxSizeMode.Zoom));
91 Assert.That(picbox.WaitOnLoad, Is.False);
92 Assert.That(picbox.Dock, Is.EqualTo(DockStyle.Fill));
99 public void CancelAsyncTest()
101 using (var thumbbox = new TweetThumbnail())
103 var post = new PostClass();
105 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
106 var task = thumbbox.ShowThumbnailAsync(post);
108 thumbbox.CancelAsync();
110 Assert.That(task.IsCanceled, Is.True);
115 public void SetThumbnailCountTest(
116 [Values(0, 1, 2)] int count)
118 using (var thumbbox = new TweetThumbnail())
120 var method = typeof(TweetThumbnail).GetMethod("SetThumbnailCount", BindingFlags.Instance | BindingFlags.NonPublic);
121 method.Invoke(thumbbox, new[] { (object)count });
123 Assert.That(thumbbox.pictureBox.Count, Is.EqualTo(count));
126 foreach (var picbox in thumbbox.pictureBox)
128 Assert.That(picbox.Name, Is.EqualTo("pictureBox" + num));
132 Assert.That(thumbbox.panelPictureBox.Controls, Is.EquivalentTo(thumbbox.pictureBox));
134 Assert.That(thumbbox.scrollBar.Minimum, Is.EqualTo(0));
135 Assert.That(thumbbox.scrollBar.Maximum, Is.EqualTo(count));
140 public void ShowThumbnailAsyncTest()
142 var post = new PostClass
144 TextFromApi = "てすと http://foo.example.com/abcd",
145 Media = new Dictionary<string, string>
147 {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
151 using (var thumbbox = new TweetThumbnail())
153 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
154 thumbbox.ShowThumbnailAsync(post).Wait();
156 Assert.That(thumbbox.scrollBar.Maximum, Is.EqualTo(0));
157 Assert.That(thumbbox.scrollBar.Enabled, Is.False);
159 Assert.That(thumbbox.pictureBox.Count, Is.EqualTo(1));
160 Assert.That(thumbbox.pictureBox[0].ImageLocation, Is.EqualTo("dot.gif"));
162 var thumbinfo = thumbbox.pictureBox[0].Tag as ThumbnailInfo;
163 Assert.That(thumbinfo, Is.Not.Null);
164 Assert.That(thumbinfo.ImageUrl, Is.EqualTo("http://foo.example.com/abcd"));
165 Assert.That(thumbinfo.ThumbnailUrl, Is.EqualTo("dot.gif"));
167 Assert.That(thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]), Is.EqualTo(""));
172 public void ShowThumbnailAsyncTest2()
174 var post = new PostClass
176 TextFromApi = "てすと http://foo.example.com/abcd http://bar.example.com/efgh",
177 Media = new Dictionary<string, string>
179 {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
180 {"http://bar.example.com/efgh", "http://bar.example.com/efgh"},
184 using (var thumbbox = new TweetThumbnail())
186 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
187 thumbbox.ShowThumbnailAsync(post).Wait();
189 Assert.That(thumbbox.scrollBar.Maximum, Is.EqualTo(1));
190 Assert.That(thumbbox.scrollBar.Enabled, Is.True);
192 Assert.That(thumbbox.pictureBox.Count, Is.EqualTo(2));
193 Assert.That(thumbbox.pictureBox[0].ImageLocation, Is.EqualTo("dot.gif"));
194 Assert.That(thumbbox.pictureBox[1].ImageLocation, Is.EqualTo("dot.gif"));
196 var thumbinfo = thumbbox.pictureBox[0].Tag as ThumbnailInfo;
197 Assert.That(thumbinfo, Is.Not.Null);
198 Assert.That(thumbinfo.ImageUrl, Is.EqualTo("http://foo.example.com/abcd"));
199 Assert.That(thumbinfo.ThumbnailUrl, Is.EqualTo("dot.gif"));
201 thumbinfo = thumbbox.pictureBox[1].Tag as ThumbnailInfo;
202 Assert.That(thumbinfo, Is.Not.Null);
203 Assert.That(thumbinfo.ImageUrl, Is.EqualTo("http://bar.example.com/efgh"));
204 Assert.That(thumbinfo.ThumbnailUrl, Is.EqualTo("dot.gif"));
206 Assert.That(thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]), Is.EqualTo(""));
207 Assert.That(thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[1]), Is.EqualTo("efgh"));
212 public void ThumbnailLoadingEventTest()
214 using (var thumbbox = new TweetThumbnail())
216 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
219 thumbbox.ThumbnailLoading +=
220 (s, e) => { eventCalled = true; };
222 var post = new PostClass
225 Media = new Dictionary<string, string>
230 thumbbox.ShowThumbnailAsync(post).Wait();
232 Assert.That(eventCalled, Is.False);
234 var post2 = new PostClass
236 TextFromApi = "てすと http://foo.example.com/abcd",
237 Media = new Dictionary<string, string>
239 {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
243 thumbbox.ShowThumbnailAsync(post2).Wait();
245 Assert.That(eventCalled, Is.True);
250 [Ignore("なぜかTravis CIだと通らないテスト")]
251 public void ScrollTest()
253 var post = new PostClass
255 TextFromApi = "てすと http://foo.example.com/abcd http://foo.example.com/efgh",
256 Media = new Dictionary<string, string>
258 {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
259 {"http://foo.example.com/efgh", "http://foo.example.com/efgh"},
263 using (var thumbbox = new TweetThumbnail())
265 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
266 thumbbox.ShowThumbnailAsync(post).Wait();
268 Assert.That(thumbbox.scrollBar.Minimum, Is.EqualTo(0));
269 Assert.That(thumbbox.scrollBar.Maximum, Is.EqualTo(1));
271 thumbbox.scrollBar.Value = 0;
274 Assert.That(thumbbox.scrollBar.Value, Is.EqualTo(1));
275 Assert.That(thumbbox.pictureBox[0].Visible, Is.False);
276 Assert.That(thumbbox.pictureBox[1].Visible, Is.True);
279 Assert.That(thumbbox.scrollBar.Value, Is.EqualTo(1));
280 Assert.That(thumbbox.pictureBox[0].Visible, Is.False);
281 Assert.That(thumbbox.pictureBox[1].Visible, Is.True);
283 thumbbox.ScrollDown();
284 Assert.That(thumbbox.scrollBar.Value, Is.EqualTo(0));
285 Assert.That(thumbbox.pictureBox[0].Visible, Is.True);
286 Assert.That(thumbbox.pictureBox[1].Visible, Is.False);
288 thumbbox.ScrollDown();
289 Assert.That(thumbbox.scrollBar.Value, Is.EqualTo(0));
290 Assert.That(thumbbox.pictureBox[0].Visible, Is.True);
291 Assert.That(thumbbox.pictureBox[1].Visible, Is.False);