OSDN Git Service

サムネイル表示の2枚目以降の画像が正しいサイズで表示されない不具合を修正
[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         [TestFixtureSetUp]
75         public void MyCommonSetup()
76         {
77             MyCommon.fileVersion = "1.0.0.0";
78         }
79
80         [Test]
81         public void CreatePictureBoxTest()
82         {
83             using (var thumbBox = new TweetThumbnail())
84             {
85                 var method = typeof(TweetThumbnail).GetMethod("CreatePictureBox", BindingFlags.Instance | BindingFlags.NonPublic);
86                 var picbox = method.Invoke(thumbBox, new[] { "pictureBox1" }) as PictureBox;
87
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));
93
94                 picbox.Dispose();
95             }
96         }
97
98         [Test]
99         public void CancelAsyncTest()
100         {
101             using (var thumbbox = new TweetThumbnail())
102             {
103                 var post = new PostClass();
104
105                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
106                 var task = thumbbox.ShowThumbnailAsync(post);
107
108                 thumbbox.CancelAsync();
109
110                 Assert.That(task.IsCanceled, Is.True);
111             }
112         }
113
114         [Test]
115         public void SetThumbnailCountTest(
116             [Values(0, 1, 2)] int count)
117         {
118             using (var thumbbox = new TweetThumbnail())
119             {
120                 var method = typeof(TweetThumbnail).GetMethod("SetThumbnailCount", BindingFlags.Instance | BindingFlags.NonPublic);
121                 method.Invoke(thumbbox, new[] { (object)count });
122
123                 Assert.That(thumbbox.pictureBox.Count, Is.EqualTo(count));
124
125                 var num = 0;
126                 foreach (var picbox in thumbbox.pictureBox)
127                 {
128                     Assert.That(picbox.Name, Is.EqualTo("pictureBox" + num));
129                     num++;
130                 }
131
132                 Assert.That(thumbbox.panelPictureBox.Controls, Is.EquivalentTo(thumbbox.pictureBox));
133
134                 Assert.That(thumbbox.scrollBar.Minimum, Is.EqualTo(0));
135                 Assert.That(thumbbox.scrollBar.Maximum, Is.EqualTo(count));
136             }
137         }
138
139         [Test]
140         public void ShowThumbnailAsyncTest()
141         {
142             var post = new PostClass
143             {
144                 TextFromApi = "てすと http://foo.example.com/abcd",
145                 Media = new Dictionary<string, string>
146                 {
147                     {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
148                 },
149             };
150
151             using (var thumbbox = new TweetThumbnail())
152             {
153                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
154                 thumbbox.ShowThumbnailAsync(post).Wait();
155
156                 Assert.That(thumbbox.scrollBar.Maximum, Is.EqualTo(0));
157                 Assert.That(thumbbox.scrollBar.Enabled, Is.False);
158
159                 Assert.That(thumbbox.pictureBox.Count, Is.EqualTo(1));
160                 Assert.That(thumbbox.pictureBox[0].ImageLocation, Is.EqualTo("dot.gif"));
161
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"));
166
167                 Assert.That(thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]), Is.EqualTo(""));
168             }
169         }
170
171         [Test]
172         public void ShowThumbnailAsyncTest2()
173         {
174             var post = new PostClass
175             {
176                 TextFromApi = "てすと http://foo.example.com/abcd http://bar.example.com/efgh",
177                 Media = new Dictionary<string, string>
178                 {
179                     {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
180                     {"http://bar.example.com/efgh", "http://bar.example.com/efgh"},
181                 },
182             };
183
184             using (var thumbbox = new TweetThumbnail())
185             {
186                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
187                 thumbbox.ShowThumbnailAsync(post).Wait();
188
189                 Assert.That(thumbbox.scrollBar.Maximum, Is.EqualTo(1));
190                 Assert.That(thumbbox.scrollBar.Enabled, Is.True);
191
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"));
195
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"));
200
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"));
205
206                 Assert.That(thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]), Is.EqualTo(""));
207                 Assert.That(thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[1]), Is.EqualTo("efgh"));
208             }
209         }
210
211         [Test]
212         public void ThumbnailLoadingEventTest()
213         {
214             using (var thumbbox = new TweetThumbnail())
215             {
216                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
217
218                 bool eventCalled;
219                 thumbbox.ThumbnailLoading +=
220                     (s, e) => { eventCalled = true; };
221
222                 var post = new PostClass
223                 {
224                     TextFromApi = "てすと",
225                     Media = new Dictionary<string, string>
226                     {
227                     },
228                 };
229                 eventCalled = false;
230                 thumbbox.ShowThumbnailAsync(post).Wait();
231
232                 Assert.That(eventCalled, Is.False);
233
234                 var post2 = new PostClass
235                 {
236                     TextFromApi = "てすと http://foo.example.com/abcd",
237                     Media = new Dictionary<string, string>
238                     {
239                         {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
240                     },
241                 };
242                 eventCalled = false;
243                 thumbbox.ShowThumbnailAsync(post2).Wait();
244
245                 Assert.That(eventCalled, Is.True);
246             }
247         }
248
249         [Test]
250         [Ignore("なぜかTravis CIだと通らないテスト")]
251         public void ScrollTest()
252         {
253             var post = new PostClass
254             {
255                 TextFromApi = "てすと http://foo.example.com/abcd http://foo.example.com/efgh",
256                 Media = new Dictionary<string, string>
257                 {
258                     {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
259                     {"http://foo.example.com/efgh", "http://foo.example.com/efgh"},
260                 },
261             };
262
263             using (var thumbbox = new TweetThumbnail())
264             {
265                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
266                 thumbbox.ShowThumbnailAsync(post).Wait();
267
268                 Assert.That(thumbbox.scrollBar.Minimum, Is.EqualTo(0));
269                 Assert.That(thumbbox.scrollBar.Maximum, Is.EqualTo(1));
270
271                 thumbbox.scrollBar.Value = 0;
272
273                 thumbbox.ScrollUp();
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);
277
278                 thumbbox.ScrollUp();
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);
282
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);
287
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);
292             }
293         }
294     }
295 }