OSDN Git Service

Merge pull request #307 from opentween/detect-rate-limits
[opentween/open-tween.git] / OpenTween.Tests / DetailsListViewTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2023 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.Linq;
24 using OpenTween.OpenTweenCustomControl;
25 using Xunit;
26
27 namespace OpenTween
28 {
29     public class DetailsListViewTest
30     {
31         [WinFormsFact]
32         public void Initialize_Test()
33         {
34             using var listView = new DetailsListView();
35         }
36
37         [WinFormsFact]
38         public void SelectionMark_Test()
39         {
40             using var listView = new DetailsListView();
41
42             listView.RetrieveVirtualItem += (s, e) => e.Item = new();
43             listView.VirtualMode = true;
44             listView.VirtualListSize = 10;
45             listView.CreateControl();
46
47             listView.SelectionMark = 3;
48             Assert.Equal(3, listView.SelectionMark);
49         }
50
51         [WinFormsFact]
52         public void SelectItems_EmptyTest()
53         {
54             using var listView = new DetailsListView();
55
56             listView.RetrieveVirtualItem += (s, e) => e.Item = new();
57             listView.VirtualMode = true;
58             listView.VirtualListSize = 10;
59             listView.CreateControl();
60
61             listView.SelectedIndices.Add(1);
62             Assert.Single(listView.SelectedIndices);
63
64             listView.SelectItems(Array.Empty<int>());
65             Assert.Empty(listView.SelectedIndices);
66         }
67
68         [WinFormsFact]
69         public void SelectItems_SingleTest()
70         {
71             using var listView = new DetailsListView();
72
73             listView.RetrieveVirtualItem += (s, e) => e.Item = new();
74             listView.VirtualMode = true;
75             listView.VirtualListSize = 10;
76             listView.CreateControl();
77
78             listView.SelectedIndices.Add(1);
79             Assert.Single(listView.SelectedIndices);
80
81             listView.SelectItems(new[] { 2 });
82             Assert.Equal(new[] { 2 }, listView.SelectedIndices.Cast<int>());
83         }
84
85         [WinFormsFact]
86         public void SelectItems_Multiple_ClearAndSelectTest()
87         {
88             using var listView = new DetailsListView();
89
90             listView.RetrieveVirtualItem += (s, e) => e.Item = new();
91             listView.VirtualMode = true;
92             listView.VirtualListSize = 10;
93             listView.CreateControl();
94
95             listView.SelectedIndices.Add(2);
96             listView.SelectedIndices.Add(3);
97             Assert.Equal(2, listView.SelectedIndices.Count);
98
99             // Clear して選択し直した方が早いパターン
100             listView.SelectItems(new[] { 5, 6 });
101             Assert.Equal(new[] { 5, 6 }, listView.SelectedIndices.Cast<int>());
102         }
103
104         [WinFormsFact]
105         public void SelectItems_Multiple_DeselectAndSelectTest()
106         {
107             using var listView = new DetailsListView();
108
109             listView.RetrieveVirtualItem += (s, e) => e.Item = new();
110             listView.VirtualMode = true;
111             listView.VirtualListSize = 10;
112             listView.CreateControl();
113
114             listView.SelectedIndices.Add(1);
115             listView.SelectedIndices.Add(2);
116             listView.SelectedIndices.Add(3);
117             Assert.Equal(3, listView.SelectedIndices.Count);
118
119             // 選択範囲の差分だけ更新した方が早いパターン
120             listView.SelectItems(new[] { 2, 3, 4 });
121             Assert.Equal(new[] { 2, 3, 4 }, listView.SelectedIndices.Cast<int>());
122         }
123
124         [WinFormsFact]
125         public void SelectItems_OutOfRangeTest()
126         {
127             using var listView = new DetailsListView();
128
129             listView.RetrieveVirtualItem += (s, e) => e.Item = new();
130             listView.VirtualMode = true;
131             listView.VirtualListSize = 10;
132             listView.CreateControl();
133
134             Assert.Throws<ArgumentOutOfRangeException>(
135                 () => listView.SelectItems(new[] { -1 })
136             );
137             Assert.Throws<ArgumentOutOfRangeException>(
138                 () => listView.SelectItems(new[] { 10 })
139             );
140         }
141     }
142 }