OSDN Git Service

IApiConnectionLegacyを削除
[opentween/open-tween.git] / OpenTween.Tests / ToolStripAPIGaugeTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2013 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 OpenTween.Api;
28 using Xunit;
29 using Xunit.Extensions;
30
31 namespace OpenTween
32 {
33     public class ToolStripAPIGaugeTest
34     {
35         [Fact]
36         public void ApiEndpointTest()
37         {
38             using var toolStrip = new TestToolStripAPIGauge();
39             var now = DateTimeUtc.Now;
40             toolStrip.DateTimeNow = now;
41
42             MyCommon.TwitterApiInfo.AccessLimit["endpoint1"] = new ApiLimit(15, 15, now + TimeSpan.FromMinutes(15));
43             MyCommon.TwitterApiInfo.AccessLimit["endpoint2"] = new ApiLimit(180, 18, now + TimeSpan.FromMinutes(5));
44
45             // toolStrip.ApiEndpoint の初期値は null
46
47             Assert.Null(toolStrip.ApiEndpoint);
48             Assert.Null(toolStrip.ApiLimit);
49
50             toolStrip.ApiEndpoint = "endpoint1";
51
52             Assert.Equal("endpoint1", toolStrip.ApiEndpoint);
53             Assert.Equal(new ApiLimit(15, 15, now + TimeSpan.FromMinutes(15)), toolStrip.ApiLimit);
54
55             toolStrip.ApiEndpoint = "endpoint2";
56
57             Assert.Equal("endpoint2", toolStrip.ApiEndpoint);
58             Assert.Equal(new ApiLimit(180, 18, now + TimeSpan.FromMinutes(5)), toolStrip.ApiLimit);
59
60             MyCommon.TwitterApiInfo.AccessLimit["endpoint2"] = new ApiLimit(180, 17, now + TimeSpan.FromMinutes(5));
61             toolStrip.ApiEndpoint = "endpoint2";
62
63             Assert.Equal("endpoint2", toolStrip.ApiEndpoint);
64             Assert.Equal(new ApiLimit(180, 17, now + TimeSpan.FromMinutes(5)), toolStrip.ApiLimit);
65
66             toolStrip.ApiEndpoint = "hoge";
67
68             Assert.Equal("hoge", toolStrip.ApiEndpoint);
69             Assert.Null(toolStrip.ApiLimit);
70
71             toolStrip.ApiEndpoint = "";
72
73             Assert.Null(toolStrip.ApiEndpoint);
74             Assert.Null(toolStrip.ApiLimit);
75
76             MyCommon.TwitterApiInfo.AccessLimit.Clear();
77         }
78
79         [Fact]
80         public void GaugeHeightTest()
81         {
82             using var toolStrip = new ToolStripAPIGauge();
83             toolStrip.AutoSize = false;
84             toolStrip.Size = new Size(100, 10);
85
86             MyCommon.TwitterApiInfo.AccessLimit["endpoint"] = new ApiLimit(15, 15, DateTimeUtc.MaxValue);
87             toolStrip.ApiEndpoint = "endpoint";
88
89             toolStrip.GaugeHeight = 5;
90
91             Assert.Equal(new Rectangle(0, 0, 100, 5), toolStrip.ApiGaugeBounds);
92             Assert.Equal(new Rectangle(0, 5, 100, 5), toolStrip.TimeGaugeBounds);
93
94             toolStrip.GaugeHeight = 3;
95
96             Assert.Equal(new Rectangle(0, 2, 100, 3), toolStrip.ApiGaugeBounds);
97             Assert.Equal(new Rectangle(0, 5, 100, 3), toolStrip.TimeGaugeBounds);
98
99             toolStrip.GaugeHeight = 0;
100
101             Assert.Equal(Rectangle.Empty, toolStrip.ApiGaugeBounds);
102             Assert.Equal(Rectangle.Empty, toolStrip.TimeGaugeBounds);
103
104             MyCommon.TwitterApiInfo.AccessLimit.Clear();
105         }
106
107         [Fact]
108         public void TextTest()
109         {
110             using var toolStrip = new ToolStripAPIGauge();
111
112             MyCommon.TwitterApiInfo.AccessLimit["/statuses/home_timeline"] = new ApiLimit(15, 15, DateTimeUtc.Now + TimeSpan.FromMinutes(15));
113             MyCommon.TwitterApiInfo.AccessLimit["/statuses/user_timeline"] = new ApiLimit(180, 18, DateTimeUtc.Now + TimeSpan.FromMinutes(-2));
114             MyCommon.TwitterApiInfo.AccessLimit["/search/tweets"] = new ApiLimit(180, 90, DateTimeUtc.Now + TimeSpan.FromMinutes(5));
115
116             // toolStrip.ApiEndpoint の初期値は null
117
118             Assert.Equal("API ???/???", toolStrip.Text);
119             Assert.Equal("API rest unknown ???/???" + Environment.NewLine + "(reset after ??? minutes)", toolStrip.ToolTipText);
120
121             toolStrip.ApiEndpoint = "/search/tweets";
122
123             Assert.Equal("API 90/180", toolStrip.Text);
124             Assert.Equal("API rest /search/tweets 90/180" + Environment.NewLine + "(reset after 5 minutes)", toolStrip.ToolTipText);
125
126             toolStrip.ApiEndpoint = "/statuses/user_timeline";
127
128             Assert.Equal("API ???/???", toolStrip.Text);
129             Assert.Equal("API rest /statuses/user_timeline ???/???" + Environment.NewLine + "(reset after ??? minutes)", toolStrip.ToolTipText);
130
131             MyCommon.TwitterApiInfo.AccessLimit["/statuses/user_timeline"] = new ApiLimit(180, 180, DateTimeUtc.Now + TimeSpan.FromMinutes(15));
132             toolStrip.ApiEndpoint = "/statuses/user_timeline";
133
134             Assert.Equal("API 180/180", toolStrip.Text);
135             Assert.Equal("API rest /statuses/user_timeline 180/180" + Environment.NewLine + "(reset after 15 minutes)", toolStrip.ToolTipText);
136
137             MyCommon.TwitterApiInfo.AccessLimit["/statuses/user_timeline"] = new ApiLimit(180, 179, DateTimeUtc.Now + TimeSpan.FromMinutes(15));
138             toolStrip.ApiEndpoint = "/statuses/user_timeline";
139
140             Assert.Equal("API 179/180", toolStrip.Text);
141             Assert.Equal("API rest /statuses/user_timeline 179/180" + Environment.NewLine + "(reset after 15 minutes)", toolStrip.ToolTipText);
142
143             toolStrip.ApiEndpoint = "hoge";
144
145             Assert.Equal("API ???/???", toolStrip.Text);
146             Assert.Equal("API rest hoge ???/???" + Environment.NewLine + "(reset after ??? minutes)", toolStrip.ToolTipText);
147
148             toolStrip.ApiEndpoint = "";
149
150             Assert.Equal("API ???/???", toolStrip.Text);
151             Assert.Equal("API rest unknown ???/???" + Environment.NewLine + "(reset after ??? minutes)", toolStrip.ToolTipText);
152
153             MyCommon.TwitterApiInfo.AccessLimit.Clear();
154         }
155
156         private class TestToolStripAPIGauge : ToolStripAPIGauge
157         {
158             public DateTimeUtc DateTimeNow = DateTimeUtc.Now;
159
160             protected override void UpdateRemainMinutes()
161             {
162                 if (this.ApiLimit != null)
163                     // DateTime.Now の代わりに this.DateTimeNow を使用することで任意の日時のテストができるようにする
164                     this.remainMinutes = (this.ApiLimit.AccessLimitResetDate - this.DateTimeNow).TotalMinutes;
165                 else
166                     this.remainMinutes = -1;
167             }
168         }
169
170         [Fact]
171         public void GaugeBoundsTest()
172         {
173             using var toolStrip = new TestToolStripAPIGauge();
174
175             var now = DateTimeUtc.Now;
176             toolStrip.DateTimeNow = now;
177
178             toolStrip.AutoSize = false;
179             toolStrip.Size = new Size(100, 10);
180             toolStrip.GaugeHeight = 5;
181
182             // toolStrip.ApiEndpoint の初期値は null
183
184             Assert.Equal(Rectangle.Empty, toolStrip.ApiGaugeBounds);
185             Assert.Equal(Rectangle.Empty, toolStrip.TimeGaugeBounds);
186
187             MyCommon.TwitterApiInfo.AccessLimit["endpoint"] = new ApiLimit(150, 60, now + TimeSpan.FromMinutes(3));
188             toolStrip.ApiEndpoint = "endpoint";
189
190             Assert.Equal(new Rectangle(0, 0, 40, 5), toolStrip.ApiGaugeBounds); // 40% (60/150)
191             Assert.Equal(new Rectangle(0, 5, 20, 5), toolStrip.TimeGaugeBounds); // 20% (3/15)
192
193             toolStrip.ApiEndpoint = "";
194
195             Assert.Equal(Rectangle.Empty, toolStrip.ApiGaugeBounds);
196             Assert.Equal(Rectangle.Empty, toolStrip.TimeGaugeBounds);
197
198             MyCommon.TwitterApiInfo.AccessLimit.Clear();
199         }
200
201         [Fact]
202         public void OneBillionTest()
203         {
204             using var toolStrip = new TestToolStripAPIGauge();
205
206             var now = new DateTimeUtc(2020, 2, 25, 19, 0, 0);
207             toolStrip.DateTimeNow = now;
208
209             toolStrip.AutoSize = false;
210             toolStrip.Size = new Size(100, 10);
211             toolStrip.GaugeHeight = 5;
212
213             MyCommon.TwitterApiInfo.AccessLimit["/statuses/user_timeline"] = new ApiLimit(
214                 AccessLimitCount: 1_000_000_000,
215                 AccessLimitRemain: 999_999_999,
216                 AccessLimitResetDate: now + TimeSpan.FromMinutes(15)
217             );
218             toolStrip.ApiEndpoint = "/statuses/user_timeline";
219
220             Assert.Equal(new Rectangle(0, 0, 99, 5), toolStrip.ApiGaugeBounds); // 99% (999999999/1000000000)
221             Assert.Equal(new Rectangle(0, 5, 100, 5), toolStrip.TimeGaugeBounds); // 100% (15/15)
222             Assert.Equal("API 999999999/1000000000", toolStrip.Text);
223             Assert.Equal("API rest /statuses/user_timeline 999999999/1000000000" + Environment.NewLine + "(reset after 15 minutes)", toolStrip.ToolTipText);
224
225             MyCommon.TwitterApiInfo.AccessLimit.Clear();
226         }
227     }
228 }