OSDN Git Service

PictureBox.Visible の変更に何故か時間が掛かる問題の回避
[opentween/open-tween.git] / OpenTween.Tests / TweetThumbnailTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2012 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 // 
5 // This file is part of OpenTween.
6 // 
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)
10 // any later version.
11 // 
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
15 // for more details. 
16 // 
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.
21
22 using System;
23 using System.Collections.Generic;
24 using System.Linq;
25 using System.Text;
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;
33
34 namespace OpenTween
35 {
36     [TestFixture]
37     class TweetThumbnailTest
38     {
39         class TestThumbnailService : SimpleThumbnailService
40         {
41             protected string tooltip;
42
43             public TestThumbnailService(string pattern, string replacement, string tooltip)
44                 : base(pattern, replacement)
45             {
46                 this.tooltip = tooltip;
47             }
48
49             public override ThumbnailInfo GetThumbnailInfo(string url, PostClass post)
50             {
51                 var thumbinfo = base.GetThumbnailInfo(url, post);
52
53                 if (thumbinfo != null && this.tooltip != null)
54                 {
55                     var match = this.regex.Match(url);
56                     thumbinfo.TooltipText = match.Result(this.tooltip);
57                 }
58
59                 return thumbinfo;
60             }
61         }
62
63         [TestFixtureSetUp]
64         public void ThumbnailGeneratorSetup()
65         {
66             ThumbnailGenerator.Services.Clear();
67             ThumbnailGenerator.Services.AddRange(new[]
68             {
69                 new TestThumbnailService(@"^https?://foo.example.com/(.+)$", @"dot.gif", null),
70                 new TestThumbnailService(@"^https?://bar.example.com/(.+)$", @"dot.gif", @"${1}"),
71             });
72         }
73
74         [Test]
75         public void CreatePictureBoxTest()
76         {
77             using (var thumbBox = new TweetThumbnail())
78             {
79                 var method = typeof(TweetThumbnail).GetMethod("CreatePictureBox", BindingFlags.Instance | BindingFlags.NonPublic);
80                 var picbox = method.Invoke(thumbBox, new[] { "pictureBox1" }) as PictureBox;
81
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));
87
88                 picbox.Dispose();
89             }
90         }
91
92         [Test]
93         public void CancelAsyncTest()
94         {
95             using (var thumbbox = new TweetThumbnail())
96             {
97                 var post = new PostClass();
98
99                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
100                 var task = thumbbox.ShowThumbnailAsync(post);
101
102                 thumbbox.CancelAsync();
103
104                 Assert.That(task.IsCanceled, Is.True);
105             }
106         }
107
108         [Test]
109         public void SetThumbnailCountTest(
110             [Values(0, 1, 2)] int count)
111         {
112             using (var thumbbox = new TweetThumbnail())
113             {
114                 var method = typeof(TweetThumbnail).GetMethod("SetThumbnailCount", BindingFlags.Instance | BindingFlags.NonPublic);
115                 method.Invoke(thumbbox, new[] { (object)count });
116
117                 Assert.That(thumbbox.pictureBox.Count, Is.EqualTo(count));
118
119                 var num = 0;
120                 foreach (var picbox in thumbbox.pictureBox)
121                 {
122                     Assert.That(picbox.Name, Is.EqualTo("pictureBox" + num));
123                     num++;
124                 }
125
126                 Assert.That(thumbbox.Controls, Is.EquivalentTo(new Control[]{ thumbbox.scrollBar }.Concat(thumbbox.pictureBox)));
127
128                 Assert.That(thumbbox.scrollBar.Minimum, Is.EqualTo(0));
129                 Assert.That(thumbbox.scrollBar.Maximum, Is.EqualTo(count));
130             }
131         }
132
133         [Test]
134         public void ShowThumbnailAsyncTest()
135         {
136             var post = new PostClass
137             {
138                 TextFromApi = "てすと http://foo.example.com/abcd",
139                 Media = new Dictionary<string, string>
140                 {
141                     {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
142                 },
143             };
144
145             using (var thumbbox = new TweetThumbnail())
146             {
147                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
148                 thumbbox.ShowThumbnailAsync(post).Wait();
149
150                 Assert.That(thumbbox.scrollBar.Maximum, Is.EqualTo(0));
151                 Assert.That(thumbbox.scrollBar.Enabled, Is.False);
152
153                 Assert.That(thumbbox.pictureBox.Count, Is.EqualTo(1));
154                 Assert.That(thumbbox.pictureBox[0].ImageLocation, Is.EqualTo("dot.gif"));
155
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"));
160
161                 Assert.That(thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]), Is.EqualTo(""));
162             }
163         }
164
165         [Test]
166         public void ShowThumbnailAsyncTest2()
167         {
168             var post = new PostClass
169             {
170                 TextFromApi = "てすと http://foo.example.com/abcd http://bar.example.com/efgh",
171                 Media = new Dictionary<string, string>
172                 {
173                     {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
174                     {"http://bar.example.com/efgh", "http://bar.example.com/efgh"},
175                 },
176             };
177
178             using (var thumbbox = new TweetThumbnail())
179             {
180                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
181                 thumbbox.ShowThumbnailAsync(post).Wait();
182
183                 Assert.That(thumbbox.scrollBar.Maximum, Is.EqualTo(1));
184                 Assert.That(thumbbox.scrollBar.Enabled, Is.True);
185
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"));
189
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"));
194
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"));
199
200                 Assert.That(thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]), Is.EqualTo(""));
201                 Assert.That(thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[1]), Is.EqualTo("efgh"));
202             }
203         }
204
205         [Test]
206         public void ThumbnailLoadingEventTest()
207         {
208             using (var thumbbox = new TweetThumbnail())
209             {
210                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
211
212                 bool eventCalled;
213                 thumbbox.ThumbnailLoading +=
214                     (s, e) => { eventCalled = true; };
215
216                 var post = new PostClass
217                 {
218                     TextFromApi = "てすと",
219                     Media = new Dictionary<string, string>
220                     {
221                     },
222                 };
223                 eventCalled = false;
224                 thumbbox.ShowThumbnailAsync(post).Wait();
225
226                 Assert.That(eventCalled, Is.False);
227
228                 var post2 = new PostClass
229                 {
230                     TextFromApi = "てすと http://foo.example.com/abcd",
231                     Media = new Dictionary<string, string>
232                     {
233                         {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
234                     },
235                 };
236                 eventCalled = false;
237                 thumbbox.ShowThumbnailAsync(post2).Wait();
238
239                 Assert.That(eventCalled, Is.True);
240             }
241         }
242
243         [Test]
244         public void ScrollTest()
245         {
246             var post = new PostClass
247             {
248                 TextFromApi = "てすと http://foo.example.com/abcd http://foo.example.com/efgh",
249                 Media = new Dictionary<string, string>
250                 {
251                     {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
252                     {"http://foo.example.com/efgh", "http://foo.example.com/efgh"},
253                 },
254             };
255
256             using (var thumbbox = new TweetThumbnail())
257             {
258                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
259                 thumbbox.ShowThumbnailAsync(post).Wait();
260
261                 Assert.That(thumbbox.scrollBar.Minimum, Is.EqualTo(0));
262                 Assert.That(thumbbox.scrollBar.Maximum, Is.EqualTo(1));
263
264                 thumbbox.scrollBar.Value = 0;
265
266                 thumbbox.ScrollUp();
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);
270
271                 thumbbox.ScrollUp();
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);
275
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);
280
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);
285             }
286         }
287     }
288 }