1 // OpenTween - Client of Twitter
2 // Copyright (c) 2014 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.Threading;
24 using System.Threading.Tasks;
25 using System.Windows.Forms;
30 public class OTPictureBoxTest
33 public void SizeMode_SetterGetterTest()
35 using var picbox = new OTPictureBox();
36 picbox.SizeMode = PictureBoxSizeMode.Zoom;
38 Assert.Equal(PictureBoxSizeMode.Zoom, picbox.SizeMode);
39 Assert.Equal(PictureBoxSizeMode.Zoom, ((PictureBox)picbox).SizeMode);
43 public void SizeMode_ErrorImageTest()
45 using var picbox = new OTPictureBox();
46 picbox.SizeMode = PictureBoxSizeMode.Zoom;
48 picbox.ShowErrorImage();
50 Assert.Equal(PictureBoxSizeMode.Zoom, picbox.SizeMode);
51 Assert.Equal(PictureBoxSizeMode.CenterImage, ((PictureBox)picbox).SizeMode);
55 public void SizeMode_ErrorImageTest2()
57 using var picbox = new OTPictureBox();
58 picbox.ShowErrorImage();
60 picbox.SizeMode = PictureBoxSizeMode.Zoom;
62 Assert.Equal(PictureBoxSizeMode.Zoom, picbox.SizeMode);
63 Assert.Equal(PictureBoxSizeMode.CenterImage, ((PictureBox)picbox).SizeMode);
67 public void SizeMode_RestoreTest()
69 using var picbox = new OTPictureBox();
70 picbox.SizeMode = PictureBoxSizeMode.Zoom;
72 picbox.ShowErrorImage();
74 picbox.Image = TestUtils.CreateDummyImage();
76 Assert.Equal(PictureBoxSizeMode.Zoom, picbox.SizeMode);
77 Assert.Equal(PictureBoxSizeMode.Zoom, ((PictureBox)picbox).SizeMode);
81 public async Task SetImageFromAsync_Test()
83 using var picbox = new OTPictureBox();
85 var tcs = new TaskCompletionSource<MemoryImage>();
87 var setImageTask = picbox.SetImageFromTask(() => tcs.Task);
89 Assert.Equal(picbox.InitialImage, ((PictureBox)picbox).Image);
91 var image = TestUtils.CreateDummyImage();
95 Assert.Equal(image, picbox.Image);
99 public async Task SetImageFromAsync_ErrorTest()
101 using var picbox = new OTPictureBox();
103 var tcs = new TaskCompletionSource<MemoryImage>();
105 var setImageTask = picbox.SetImageFromTask(() => tcs.Task);
107 Assert.Equal(picbox.InitialImage, ((PictureBox)picbox).Image);
109 tcs.SetException(new InvalidImageException());
112 Assert.Equal(picbox.ErrorImage, ((PictureBox)picbox).Image);
116 public async Task SetImageFromAsync_OutdatedTest()
118 using var picbox = new OTPictureBox();
121 var tcs1 = new TaskCompletionSource<MemoryImage>();
122 var setImageTask1 = picbox.SetImageFromTask(() => tcs1.Task);
125 var tcs2 = new TaskCompletionSource<MemoryImage>();
126 var setImageTask2 = picbox.SetImageFromTask(() => tcs2.Task);
128 Assert.Same(picbox.InitialImage, ((PictureBox)picbox).Image);
131 using var image2 = TestUtils.CreateDummyImage();
132 tcs2.SetResult(image2);
134 Assert.Same(image2, picbox.Image);
136 // 1回目のタスクが完了したとしても、最後に呼んだ SetImageFromTask の画像を表示し続ける
137 using var image1 = TestUtils.CreateDummyImage();
138 tcs1.SetResult(image1);
140 Assert.Same(image2, picbox.Image);
144 public async Task SetImageFromAsync_OutdatedByImageSetterTest()
146 using var picbox = new OTPictureBox();
149 var tcs1 = new TaskCompletionSource<MemoryImage>();
150 var setImageTask1 = picbox.SetImageFromTask(() => tcs1.Task);
152 // 2回目 (set_Image による同期的な更新)
153 using var image2 = TestUtils.CreateDummyImage();
154 picbox.Image = image2;
155 Assert.Same(image2, picbox.Image);
157 // 1回目のタスクが完了したとしても、最後にセットされた Image の画像を表示し続ける
158 using var image1 = TestUtils.CreateDummyImage();
159 tcs1.SetResult(image1);
161 Assert.Same(image2, picbox.Image);