OSDN Git Service

静的フィールドに関するnull参照の警告が出ている箇所を修正
[opentween/open-tween.git] / OpenTween.Tests / TabsDialogTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2012 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.Reflection;
26 using System.Text;
27 using System.Windows.Forms;
28 using OpenTween.Models;
29 using Xunit;
30 using Xunit.Extensions;
31
32 namespace OpenTween
33 {
34     public class TabsDialogTest
35     {
36         private readonly TabInformations tabinfo;
37
38         public TabsDialogTest()
39         {
40             this.tabinfo = (TabInformations)Activator.CreateInstance(typeof(TabInformations), true);
41
42             // タブを追加
43             this.tabinfo.AddTab(new HomeTabModel("Recent"));
44             this.tabinfo.AddTab(new MentionsTabModel("Reply"));
45             this.tabinfo.AddTab(new DirectMessagesTabModel("DM"));
46             this.tabinfo.AddTab(new FavoritesTabModel("Favorites"));
47             this.tabinfo.AddTab(new FilterTabModel("MyTab1"));
48
49             // 一応 TabInformation.GetInstance() でも取得できるようにする
50             var field = typeof(TabInformations).GetField("_instance",
51                 BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.SetField);
52             field.SetValue(null, this.tabinfo);
53         }
54
55         [Fact]
56         public void OKButtonEnabledTest()
57         {
58             using (var dialog = new TabsDialog(this.tabinfo))
59             {
60                 Assert.False(dialog.OK_Button.Enabled);
61
62                 dialog.TabList.SelectedIndex = 0;
63
64                 Assert.True(dialog.OK_Button.Enabled);
65
66                 dialog.TabList.SelectedIndex = -1;
67
68                 Assert.False(dialog.OK_Button.Enabled);
69             }
70         }
71
72         [Fact]
73         public void MultiSelectTest()
74         {
75             using (var dialog = new TabsDialog(this.tabinfo))
76             {
77                 // MultiSelect = false (default)
78                 var firstItem = (TabsDialog.TabListItem)dialog.TabList.Items[0];
79                 Assert.Null(firstItem.Tab); // 「(新規タブ)」
80                 Assert.Equal(SelectionMode.One, dialog.TabList.SelectionMode);
81
82                 dialog.MultiSelect = true;
83                 firstItem = (TabsDialog.TabListItem)dialog.TabList.Items[0];
84                 Assert.NotNull(firstItem.Tab);
85                 Assert.Equal(SelectionMode.MultiExtended, dialog.TabList.SelectionMode);
86
87                 dialog.MultiSelect = false;
88                 firstItem = (TabsDialog.TabListItem)dialog.TabList.Items[0];
89                 Assert.Null(firstItem.Tab);
90                 Assert.Equal(SelectionMode.One, dialog.TabList.SelectionMode);
91             }
92         }
93
94         [Fact]
95         public void DoubleClickTest()
96         {
97             using (var dialog = new TabsDialog(this.tabinfo))
98             {
99                 dialog.TabList.SelectedIndex = -1;
100                 TestUtils.FireEvent(dialog.TabList, "DoubleClick");
101
102                 Assert.Equal(DialogResult.None, dialog.DialogResult);
103                 Assert.False(dialog.IsDisposed);
104
105                 dialog.TabList.SelectedIndex = 1;
106                 TestUtils.FireEvent(dialog.TabList, "DoubleClick");
107
108                 Assert.Equal(DialogResult.OK, dialog.DialogResult);
109                 Assert.True(dialog.IsDisposed);
110             }
111         }
112
113         [Fact]
114         public void SelectableTabTest()
115         {
116             using (var dialog = new TabsDialog(this.tabinfo))
117             {
118                 dialog.MultiSelect = false;
119
120                 var item = (TabsDialog.TabListItem)dialog.TabList.Items[0];
121                 Assert.Null(item.Tab);
122
123                 item = (TabsDialog.TabListItem)dialog.TabList.Items[1];
124                 Assert.Equal(this.tabinfo.Tabs["Reply"], item.Tab);
125
126                 item = (TabsDialog.TabListItem)dialog.TabList.Items[2];
127                 Assert.Equal(this.tabinfo.Tabs["MyTab1"], item.Tab);
128             }
129         }
130
131         [Fact]
132         public void SelectedTabTest()
133         {
134             using (var dialog = new TabsDialog(this.tabinfo))
135             {
136                 dialog.MultiSelect = false;
137
138                 dialog.TabList.SelectedIndex = 0;
139                 Assert.Null(dialog.SelectedTab);
140
141                 dialog.TabList.SelectedIndex = 1;
142                 Assert.Equal(this.tabinfo.Tabs["Reply"], dialog.SelectedTab);
143             }
144         }
145
146         [Fact]
147         public void SelectedTabsTest()
148         {
149             using (var dialog = new TabsDialog(this.tabinfo))
150             {
151                 dialog.MultiSelect = true;
152
153                 dialog.TabList.SelectedIndices.Clear();
154                 var selectedTabs = dialog.SelectedTabs;
155                 Assert.Empty(selectedTabs);
156
157                 dialog.TabList.SelectedIndices.Add(0);
158                 selectedTabs = dialog.SelectedTabs;
159                 Assert.Equal(new[] { this.tabinfo.Tabs["Reply"] }, selectedTabs);
160
161                 dialog.TabList.SelectedIndices.Add(1);
162                 selectedTabs = dialog.SelectedTabs;
163                 Assert.Equal(new[] { this.tabinfo.Tabs["Reply"], this.tabinfo.Tabs["MyTab1"] }, selectedTabs);
164             }
165         }
166     }
167 }