OSDN Git Service

Merge pull request #307 from opentween/detect-rate-limits
[opentween/open-tween.git] / OpenTween.Tests / ShortcutCommandTest.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.Linq;
25 using System.Text;
26 using System.Threading.Tasks;
27 using System.Windows.Forms;
28 using Xunit;
29
30 namespace OpenTween
31 {
32     public class ShortcutCommandTest
33     {
34         [Fact]
35         public void Keys_Test()
36         {
37             var shortcut = ShortcutCommand.Create(Keys.Control | Keys.S)
38                 .Do(() => { });
39
40             Assert.Equal(new[] { Keys.Control | Keys.S }, shortcut.Shortcuts);
41
42             Assert.False(shortcut.IsMatch(Keys.S, FocusedControl.ListTab));
43             Assert.False(shortcut.IsMatch(Keys.Control | Keys.F, FocusedControl.ListTab));
44             Assert.True(shortcut.IsMatch(Keys.Control | Keys.S, FocusedControl.ListTab));
45         }
46
47         [Fact]
48         public void Keys_MultipleTest()
49         {
50             var shortcut = ShortcutCommand.Create(Keys.Control | Keys.F, Keys.F3)
51                 .Do(() => { });
52
53             Assert.Equal(new[] { Keys.Control | Keys.F, Keys.F3 }, shortcut.Shortcuts);
54
55             // Ctrl+F, F3 のどちらの入力も受け付ける
56             Assert.True(shortcut.IsMatch(Keys.Control | Keys.F, FocusedControl.ListTab));
57             Assert.True(shortcut.IsMatch(Keys.F3, FocusedControl.ListTab));
58         }
59
60         [Fact]
61         public void FocusedOn_Test()
62         {
63             var shortcut = ShortcutCommand.Create(Keys.F5)
64                 .FocusedOn(FocusedControl.PostBrowser)
65                 .Do(() => { });
66
67             Assert.Equal(FocusedControl.PostBrowser, shortcut.FocusedOn);
68
69             // FocusedOn が指定された場合は、そのコントロールにフォーカスがある場合のみ true を返す
70             Assert.False(shortcut.IsMatch(Keys.F5, FocusedControl.ListTab));
71             Assert.True(shortcut.IsMatch(Keys.F5, FocusedControl.PostBrowser));
72         }
73
74         [Fact]
75         public void FocusedOn_NoneTest()
76         {
77             var shortcut = ShortcutCommand.Create(Keys.F5)
78                 .FocusedOn(FocusedControl.None)
79                 .Do(() => { });
80
81             Assert.Equal(FocusedControl.None, shortcut.FocusedOn);
82
83             // FocusedControl.None がセットされた場合はフォーカス状態に関係なく true を返す
84             Assert.True(shortcut.IsMatch(Keys.F5, FocusedControl.ListTab));
85             Assert.True(shortcut.IsMatch(Keys.F5, FocusedControl.PostBrowser));
86         }
87
88         [Fact]
89         public void NotFocusedOn_Test()
90         {
91             var shortcut = ShortcutCommand.Create(Keys.F5)
92                 .NotFocusedOn(FocusedControl.StatusText)
93                 .Do(() => { });
94
95             Assert.Equal(FocusedControl.StatusText, shortcut.NotFocusedOn);
96
97             // NotFocusedOn が指定された場合は、そのコントロールにフォーカスがある場合以外は true を返す
98             Assert.False(shortcut.IsMatch(Keys.F5, FocusedControl.StatusText));
99             Assert.True(shortcut.IsMatch(Keys.F5, FocusedControl.ListTab));
100         }
101
102         [Fact]
103         public void NotFocusedOn_NoneTest()
104         {
105             var shortcut = ShortcutCommand.Create(Keys.F5)
106                 .NotFocusedOn(FocusedControl.None)
107                 .Do(() => { });
108
109             Assert.Equal(FocusedControl.None, shortcut.NotFocusedOn);
110
111             // FocusedControl.None がセットされた場合はフォーカス状態に関係なく true を返す
112             Assert.True(shortcut.IsMatch(Keys.F5, FocusedControl.StatusText));
113             Assert.True(shortcut.IsMatch(Keys.F5, FocusedControl.ListTab));
114         }
115
116         [Fact]
117         public void OnlyWhen_Test()
118         {
119             var hoge = false;
120
121             var shortcut = ShortcutCommand.Create(Keys.F5)
122                 .OnlyWhen(() => hoge)
123                 .Do(() => { });
124
125             // OnlyWhen で指定した条件が true の場合のみ true を返す
126             Assert.False(shortcut.IsMatch(Keys.F5, FocusedControl.ListTab));
127
128             hoge = true;
129             Assert.True(shortcut.IsMatch(Keys.F5, FocusedControl.ListTab));
130         }
131
132         [Fact]
133         public async Task RunCommand_Test()
134         {
135             var invoked = false;
136
137             var shortcut = ShortcutCommand.Create(Keys.F5)
138                 .Do(() => invoked = true);
139
140             Assert.False(invoked);
141
142             await shortcut.RunCommand();
143
144             Assert.True(invoked);
145         }
146
147         [Fact]
148         public async Task RunCommand_AsyncTest()
149         {
150             var invoked = false;
151
152             var shortcut = ShortcutCommand.Create(Keys.F5)
153                 .Do(async () =>
154                 {
155                     await Task.Delay(100);
156                     invoked = true;
157                 });
158
159             Assert.False(invoked);
160
161             await shortcut.RunCommand();
162
163             Assert.True(invoked);
164         }
165
166         [Fact]
167         public void PreventDefault_Test()
168         {
169             var shortcut = ShortcutCommand.Create(Keys.F5)
170                 .Do(() => { }, preventDefault: true);
171
172             Assert.True(shortcut.PreventDefault);
173
174             shortcut = ShortcutCommand.Create(Keys.F5)
175                 .Do(() => { }, preventDefault: false);
176
177             Assert.False(shortcut.PreventDefault);
178         }
179     }
180 }