1 // OpenTween - Client of Twitter
2 // Copyright (c) 2015 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.Collections.Generic;
26 using System.Threading.Tasks;
27 using System.Windows.Forms;
32 public class ShortcutCommandTest
35 public void Keys_Test()
37 var shortcut = ShortcutCommand.Create(Keys.Control | Keys.S)
40 Assert.Equal(new[] { Keys.Control | Keys.S }, shortcut.Shortcuts);
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));
48 public void Keys_MultipleTest()
50 var shortcut = ShortcutCommand.Create(Keys.Control | Keys.F, Keys.F3)
53 Assert.Equal(new[] { Keys.Control | Keys.F, Keys.F3 }, shortcut.Shortcuts);
55 // Ctrl+F, F3 のどちらの入力も受け付ける
56 Assert.True(shortcut.IsMatch(Keys.Control | Keys.F, FocusedControl.ListTab));
57 Assert.True(shortcut.IsMatch(Keys.F3, FocusedControl.ListTab));
61 public void FocusedOn_Test()
63 var shortcut = ShortcutCommand.Create(Keys.F5)
64 .FocusedOn(FocusedControl.PostBrowser)
67 Assert.Equal(FocusedControl.PostBrowser, shortcut.FocusedOn);
69 // FocusedOn が指定された場合は、そのコントロールにフォーカスがある場合のみ true を返す
70 Assert.False(shortcut.IsMatch(Keys.F5, FocusedControl.ListTab));
71 Assert.True(shortcut.IsMatch(Keys.F5, FocusedControl.PostBrowser));
75 public void FocusedOn_NoneTest()
77 var shortcut = ShortcutCommand.Create(Keys.F5)
78 .FocusedOn(FocusedControl.None)
81 Assert.Equal(FocusedControl.None, shortcut.FocusedOn);
83 // FocusedControl.None がセットされた場合はフォーカス状態に関係なく true を返す
84 Assert.True(shortcut.IsMatch(Keys.F5, FocusedControl.ListTab));
85 Assert.True(shortcut.IsMatch(Keys.F5, FocusedControl.PostBrowser));
89 public void NotFocusedOn_Test()
91 var shortcut = ShortcutCommand.Create(Keys.F5)
92 .NotFocusedOn(FocusedControl.StatusText)
95 Assert.Equal(FocusedControl.StatusText, shortcut.NotFocusedOn);
97 // NotFocusedOn が指定された場合は、そのコントロールにフォーカスがある場合以外は true を返す
98 Assert.False(shortcut.IsMatch(Keys.F5, FocusedControl.StatusText));
99 Assert.True(shortcut.IsMatch(Keys.F5, FocusedControl.ListTab));
103 public void NotFocusedOn_NoneTest()
105 var shortcut = ShortcutCommand.Create(Keys.F5)
106 .NotFocusedOn(FocusedControl.None)
109 Assert.Equal(FocusedControl.None, shortcut.NotFocusedOn);
111 // FocusedControl.None がセットされた場合はフォーカス状態に関係なく true を返す
112 Assert.True(shortcut.IsMatch(Keys.F5, FocusedControl.StatusText));
113 Assert.True(shortcut.IsMatch(Keys.F5, FocusedControl.ListTab));
117 public void OnlyWhen_Test()
121 var shortcut = ShortcutCommand.Create(Keys.F5)
122 .OnlyWhen(() => hoge)
125 // OnlyWhen で指定した条件が true の場合のみ true を返す
126 Assert.False(shortcut.IsMatch(Keys.F5, FocusedControl.ListTab));
129 Assert.True(shortcut.IsMatch(Keys.F5, FocusedControl.ListTab));
133 public async Task RunCommand_Test()
137 var shortcut = ShortcutCommand.Create(Keys.F5)
138 .Do(() => invoked = true);
140 Assert.False(invoked);
142 await shortcut.RunCommand();
144 Assert.True(invoked);
148 public async Task RunCommand_AsyncTest()
152 var shortcut = ShortcutCommand.Create(Keys.F5)
155 await Task.Delay(100);
159 Assert.False(invoked);
161 await shortcut.RunCommand();
163 Assert.True(invoked);
167 public void PreventDefault_Test()
169 var shortcut = ShortcutCommand.Create(Keys.F5)
170 .Do(() => { }, preventDefault: true);
172 Assert.True(shortcut.PreventDefault);
174 shortcut = ShortcutCommand.Create(Keys.F5)
175 .Do(() => { }, preventDefault: false);
177 Assert.False(shortcut.PreventDefault);