OSDN Git Service

式形式のメソッドを使用する (IDE0021, IDE0022, IDE0025, IDE0053)
[opentween/open-tween.git] / OpenTween.Tests / OTBaseFormTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2015 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.Drawing;
25 using System.Linq;
26 using System.Text;
27 using System.Threading.Tasks;
28 using System.Windows.Forms;
29 using Xunit;
30
31 namespace OpenTween
32 {
33     public class OTBaseFormTest
34     {
35         private class TestForm : OTBaseForm { }
36
37         public OTBaseFormTest()
38             => this.SetupSynchronizationContext();
39
40         protected void SetupSynchronizationContext()
41             => WindowsFormsSynchronizationContext.AutoInstall = false;
42
43         [Fact]
44         public async Task InvokeAsync_Test()
45         {
46             using (var form = new TestForm())
47             {
48                 await Task.Run(async () =>
49                 {
50                     await form.InvokeAsync(() => form.Text = "hoge");
51                 });
52
53                 Assert.Equal("hoge", form.Text);
54             }
55         }
56
57         [Fact]
58         public async Task InvokeAsync_ReturnValueTest()
59         {
60             using (var form = new TestForm())
61             {
62                 form.Text = "hoge";
63
64                 await Task.Run(async () =>
65                 {
66                     var ret = await form.InvokeAsync(() => form.Text);
67                     Assert.Equal("hoge", ret);
68                 });
69             }
70         }
71
72         [Fact]
73         public async Task InvokeAsync_TaskTest()
74         {
75             using (var form = new TestForm())
76             {
77                 await Task.Run(async () =>
78                 {
79                     await form.InvokeAsync(async () =>
80                     {
81                         await Task.Delay(1);
82                         form.Text = "hoge";
83                     });
84                 });
85
86                 Assert.Equal("hoge", form.Text);
87             }
88         }
89
90         [Fact]
91         public async Task InvokeAsync_TaskWithValueTest()
92         {
93             using (var form = new TestForm())
94             {
95                 form.Text = "hoge";
96
97                 await Task.Run(async () =>
98                 {
99                     var ret = await form.InvokeAsync(async () =>
100                     {
101                         await Task.Delay(1);
102                         return form.Text;
103                     });
104
105                     Assert.Equal("hoge", ret);
106                 });
107             }
108         }
109
110         [Fact]
111         public void ScaleChildControl_ListViewTest()
112         {
113             using (var listview = new ListView { Width = 200, Height = 200 })
114             {
115                 listview.Columns.AddRange(new[]
116                 {
117                     new ColumnHeader { Width = 60 },
118                     new ColumnHeader { Width = 140 },
119                 });
120
121                 OTBaseForm.ScaleChildControl(listview, new SizeF(1.25f, 1.25f));
122
123                 Assert.Equal(75, listview.Columns[0].Width);
124                 Assert.Equal(175, listview.Columns[1].Width);
125             }
126         }
127
128         [Fact]
129         public void ScaleChildControl_VScrollBarTest()
130         {
131             using (var scrollBar = new VScrollBar { Width = 20, Height = 200 })
132             {
133                 OTBaseForm.ScaleChildControl(scrollBar, new SizeF(2.0f, 2.0f));
134
135                 Assert.Equal(40, scrollBar.Width);
136             }
137         }
138     }
139 }