OSDN Git Service

発言に含まれる短縮URLの展開処理をPostUrlExpanderクラスに移動
[opentween/open-tween.git] / OpenTween.Tests / OTPictureBoxTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2014 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.Linq;
23 using System.Threading;
24 using System.Threading.Tasks;
25 using System.Windows.Forms;
26 using Xunit;
27
28 namespace OpenTween
29 {
30     public class OTPictureBoxTest
31     {
32         [WinFormsFact]
33         public void SizeMode_SetterGetterTest()
34         {
35             using var picbox = new OTPictureBox();
36             picbox.SizeMode = PictureBoxSizeMode.Zoom;
37
38             Assert.Equal(PictureBoxSizeMode.Zoom, picbox.SizeMode);
39             Assert.Equal(PictureBoxSizeMode.Zoom, ((PictureBox)picbox).SizeMode);
40         }
41
42         [WinFormsFact]
43         public void SizeMode_ErrorImageTest()
44         {
45             using var picbox = new OTPictureBox();
46             picbox.SizeMode = PictureBoxSizeMode.Zoom;
47
48             picbox.ShowErrorImage();
49
50             Assert.Equal(PictureBoxSizeMode.Zoom, picbox.SizeMode);
51             Assert.Equal(PictureBoxSizeMode.CenterImage, ((PictureBox)picbox).SizeMode);
52         }
53
54         [WinFormsFact]
55         public void SizeMode_ErrorImageTest2()
56         {
57             using var picbox = new OTPictureBox();
58             picbox.ShowErrorImage();
59
60             picbox.SizeMode = PictureBoxSizeMode.Zoom;
61
62             Assert.Equal(PictureBoxSizeMode.Zoom, picbox.SizeMode);
63             Assert.Equal(PictureBoxSizeMode.CenterImage, ((PictureBox)picbox).SizeMode);
64         }
65
66         [WinFormsFact]
67         public void SizeMode_RestoreTest()
68         {
69             using var picbox = new OTPictureBox();
70             picbox.SizeMode = PictureBoxSizeMode.Zoom;
71
72             picbox.ShowErrorImage();
73
74             picbox.Image = TestUtils.CreateDummyImage();
75
76             Assert.Equal(PictureBoxSizeMode.Zoom, picbox.SizeMode);
77             Assert.Equal(PictureBoxSizeMode.Zoom, ((PictureBox)picbox).SizeMode);
78         }
79
80         [WinFormsFact]
81         public async Task SetImageFromAsync_Test()
82         {
83             using var picbox = new OTPictureBox();
84
85             var tcs = new TaskCompletionSource<MemoryImage>();
86
87             var setImageTask = picbox.SetImageFromTask(() => tcs.Task);
88
89             Assert.Equal(picbox.InitialImage, ((PictureBox)picbox).Image);
90
91             var image = TestUtils.CreateDummyImage();
92             tcs.SetResult(image);
93             await setImageTask;
94
95             Assert.Equal(image, picbox.Image);
96         }
97
98         [WinFormsFact]
99         public async Task SetImageFromAsync_ErrorTest()
100         {
101             using var picbox = new OTPictureBox();
102
103             var tcs = new TaskCompletionSource<MemoryImage>();
104
105             var setImageTask = picbox.SetImageFromTask(() => tcs.Task);
106
107             Assert.Equal(picbox.InitialImage, ((PictureBox)picbox).Image);
108
109             tcs.SetException(new InvalidImageException());
110             await setImageTask;
111
112             Assert.Equal(picbox.ErrorImage, ((PictureBox)picbox).Image);
113         }
114
115         [WinFormsFact]
116         public async Task SetImageFromAsync_OutdatedTest()
117         {
118             using var picbox = new OTPictureBox();
119
120             // 1回目
121             var tcs1 = new TaskCompletionSource<MemoryImage>();
122             var setImageTask1 = picbox.SetImageFromTask(() => tcs1.Task);
123
124             // 2回目
125             var tcs2 = new TaskCompletionSource<MemoryImage>();
126             var setImageTask2 = picbox.SetImageFromTask(() => tcs2.Task);
127
128             Assert.Same(picbox.InitialImage, ((PictureBox)picbox).Image);
129
130             // 2回目のタスクが先に完了する
131             using var image2 = TestUtils.CreateDummyImage();
132             tcs2.SetResult(image2);
133             await setImageTask2;
134             Assert.Same(image2, picbox.Image);
135
136             // 1回目のタスクが完了したとしても、最後に呼んだ SetImageFromTask の画像を表示し続ける
137             using var image1 = TestUtils.CreateDummyImage();
138             tcs1.SetResult(image1);
139             await setImageTask1;
140             Assert.Same(image2, picbox.Image);
141         }
142
143         [WinFormsFact]
144         public async Task SetImageFromAsync_OutdatedByImageSetterTest()
145         {
146             using var picbox = new OTPictureBox();
147
148             // 1回目
149             var tcs1 = new TaskCompletionSource<MemoryImage>();
150             var setImageTask1 = picbox.SetImageFromTask(() => tcs1.Task);
151
152             // 2回目 (set_Image による同期的な更新)
153             using var image2 = TestUtils.CreateDummyImage();
154             picbox.Image = image2;
155             Assert.Same(image2, picbox.Image);
156
157             // 1回目のタスクが完了したとしても、最後にセットされた Image の画像を表示し続ける
158             using var image1 = TestUtils.CreateDummyImage();
159             tcs1.SetResult(image1);
160             await setImageTask1;
161             Assert.Same(image2, picbox.Image);
162         }
163     }
164 }