OSDN Git Service

式形式のメソッドを使用する (IDE0021, IDE0022, IDE0025, IDE0053)
[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         [Fact]
33         public void SizeMode_SetterGetterTest()
34         {
35             using (var picbox = new OTPictureBox())
36             {
37                 picbox.SizeMode = PictureBoxSizeMode.Zoom;
38
39                 Assert.Equal(PictureBoxSizeMode.Zoom, picbox.SizeMode);
40                 Assert.Equal(PictureBoxSizeMode.Zoom, ((PictureBox)picbox).SizeMode);
41             }
42         }
43
44         [Fact]
45         public void SizeMode_ErrorImageTest()
46         {
47             using (var picbox = new OTPictureBox())
48             {
49                 picbox.SizeMode = PictureBoxSizeMode.Zoom;
50
51                 picbox.ShowErrorImage();
52
53                 Assert.Equal(PictureBoxSizeMode.Zoom, picbox.SizeMode);
54                 Assert.Equal(PictureBoxSizeMode.CenterImage, ((PictureBox)picbox).SizeMode);
55             }
56         }
57
58         [Fact]
59         public void SizeMode_ErrorImageTest2()
60         {
61             using (var picbox = new OTPictureBox())
62             {
63                 picbox.ShowErrorImage();
64
65                 picbox.SizeMode = PictureBoxSizeMode.Zoom;
66
67                 Assert.Equal(PictureBoxSizeMode.Zoom, picbox.SizeMode);
68                 Assert.Equal(PictureBoxSizeMode.CenterImage, ((PictureBox)picbox).SizeMode);
69             }
70         }
71
72         [Fact]
73         public void SizeMode_RestoreTest()
74         {
75             using (var picbox = new OTPictureBox())
76             {
77                 picbox.SizeMode = PictureBoxSizeMode.Zoom;
78
79                 picbox.ShowErrorImage();
80
81                 picbox.Image = TestUtils.CreateDummyImage();
82
83                 Assert.Equal(PictureBoxSizeMode.Zoom, picbox.SizeMode);
84                 Assert.Equal(PictureBoxSizeMode.Zoom, ((PictureBox)picbox).SizeMode);
85             }
86         }
87
88         [Fact]
89         public async Task SetImageFromAsync_Test()
90         {
91             using (var picbox = new OTPictureBox())
92             {
93                 // Mono でのテスト実行時にデッドロックする問題の対策
94                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
95
96                 var tcs = new TaskCompletionSource<MemoryImage>();
97
98                 var setImageTask = picbox.SetImageFromTask(() => tcs.Task);
99
100                 Assert.Equal(picbox.InitialImage, ((PictureBox)picbox).Image);
101
102                 var image = TestUtils.CreateDummyImage();
103                 tcs.SetResult(image);
104                 await setImageTask;
105
106                 Assert.Equal(image, picbox.Image);
107             }
108         }
109
110         [Fact]
111         public async Task SetImageFromAsync_ErrorTest()
112         {
113             using (var picbox = new OTPictureBox())
114             {
115                 // Mono でのテスト実行時にデッドロックする問題の対策
116                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
117
118                 var tcs = new TaskCompletionSource<MemoryImage>();
119
120                 var setImageTask = picbox.SetImageFromTask(() => tcs.Task);
121
122                 Assert.Equal(picbox.InitialImage, ((PictureBox)picbox).Image);
123
124                 tcs.SetException(new InvalidImageException());
125                 await setImageTask;
126
127                 Assert.Equal(picbox.ErrorImage, ((PictureBox)picbox).Image);
128             }
129         }
130     }
131 }