1 // OpenTween - Client of Twitter
2 // Copyright (c) 2013 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;
29 using Xunit.Extensions;
33 public class ToolStripAPIGaugeTest
36 public void ApiEndpointTest()
38 using (var toolStrip = new TestToolStripAPIGauge())
40 var now = DateTimeUtc.Now;
41 toolStrip.DateTimeNow = now;
43 MyCommon.TwitterApiInfo.AccessLimit["endpoint1"] = new ApiLimit(15, 15, now + TimeSpan.FromMinutes(15));
44 MyCommon.TwitterApiInfo.AccessLimit["endpoint2"] = new ApiLimit(180, 18, now + TimeSpan.FromMinutes(5));
46 // toolStrip.ApiEndpoint の初期値は null
48 Assert.Null(toolStrip.ApiEndpoint);
49 Assert.Null(toolStrip.ApiLimit);
51 toolStrip.ApiEndpoint = "endpoint1";
53 Assert.Equal("endpoint1", toolStrip.ApiEndpoint);
54 Assert.Equal(new ApiLimit(15, 15, now + TimeSpan.FromMinutes(15)), toolStrip.ApiLimit);
56 toolStrip.ApiEndpoint = "endpoint2";
58 Assert.Equal("endpoint2", toolStrip.ApiEndpoint);
59 Assert.Equal(new ApiLimit(180, 18, now + TimeSpan.FromMinutes(5)), toolStrip.ApiLimit);
61 MyCommon.TwitterApiInfo.AccessLimit["endpoint2"] = new ApiLimit(180, 17, now + TimeSpan.FromMinutes(5));
62 toolStrip.ApiEndpoint = "endpoint2";
64 Assert.Equal("endpoint2", toolStrip.ApiEndpoint);
65 Assert.Equal(new ApiLimit(180, 17, now + TimeSpan.FromMinutes(5)), toolStrip.ApiLimit);
67 toolStrip.ApiEndpoint = "hoge";
69 Assert.Equal("hoge", toolStrip.ApiEndpoint);
70 Assert.Null(toolStrip.ApiLimit);
72 toolStrip.ApiEndpoint = "";
74 Assert.Null(toolStrip.ApiEndpoint);
75 Assert.Null(toolStrip.ApiLimit);
77 MyCommon.TwitterApiInfo.AccessLimit.Clear();
82 public void GaugeHeightTest()
84 using (var toolStrip = new ToolStripAPIGauge())
86 toolStrip.AutoSize = false;
87 toolStrip.Size = new Size(100, 10);
89 MyCommon.TwitterApiInfo.AccessLimit["endpoint"] = new ApiLimit(15, 15, DateTimeUtc.MaxValue);
90 toolStrip.ApiEndpoint = "endpoint";
92 toolStrip.GaugeHeight = 5;
94 Assert.Equal(new Rectangle(0, 0, 100, 5), toolStrip.apiGaugeBounds);
95 Assert.Equal(new Rectangle(0, 5, 100, 5), toolStrip.timeGaugeBounds);
97 toolStrip.GaugeHeight = 3;
99 Assert.Equal(new Rectangle(0, 2, 100, 3), toolStrip.apiGaugeBounds);
100 Assert.Equal(new Rectangle(0, 5, 100, 3), toolStrip.timeGaugeBounds);
102 toolStrip.GaugeHeight = 0;
104 Assert.Equal(Rectangle.Empty, toolStrip.apiGaugeBounds);
105 Assert.Equal(Rectangle.Empty, toolStrip.timeGaugeBounds);
107 MyCommon.TwitterApiInfo.AccessLimit.Clear();
112 public void TextTest()
114 using (var toolStrip = new ToolStripAPIGauge())
116 MyCommon.TwitterApiInfo.AccessLimit["/statuses/home_timeline"] = new ApiLimit(15, 15, DateTimeUtc.Now + TimeSpan.FromMinutes(15));
117 MyCommon.TwitterApiInfo.AccessLimit["/statuses/user_timeline"] = new ApiLimit(180, 18, DateTimeUtc.Now + TimeSpan.FromMinutes(-2));
118 MyCommon.TwitterApiInfo.AccessLimit["/search/tweets"] = new ApiLimit(180, 90, DateTimeUtc.Now + TimeSpan.FromMinutes(5));
120 // toolStrip.ApiEndpoint の初期値は null
122 Assert.Equal("API ???/???", toolStrip.Text);
123 Assert.Equal("API rest unknown ???/???" + Environment.NewLine + "(reset after ??? minutes)", toolStrip.ToolTipText);
125 toolStrip.ApiEndpoint = "/search/tweets";
127 Assert.Equal("API 90/180", toolStrip.Text);
128 Assert.Equal("API rest /search/tweets 90/180" + Environment.NewLine + "(reset after 5 minutes)", toolStrip.ToolTipText);
130 toolStrip.ApiEndpoint = "/statuses/user_timeline";
132 Assert.Equal("API ???/???", toolStrip.Text);
133 Assert.Equal("API rest /statuses/user_timeline ???/???" + Environment.NewLine + "(reset after ??? minutes)", toolStrip.ToolTipText);
135 MyCommon.TwitterApiInfo.AccessLimit["/statuses/user_timeline"] = new ApiLimit(180, 180, DateTimeUtc.Now + TimeSpan.FromMinutes(15));
136 toolStrip.ApiEndpoint = "/statuses/user_timeline";
138 Assert.Equal("API 180/180", toolStrip.Text);
139 Assert.Equal("API rest /statuses/user_timeline 180/180" + Environment.NewLine + "(reset after 15 minutes)", toolStrip.ToolTipText);
141 MyCommon.TwitterApiInfo.AccessLimit["/statuses/user_timeline"] = new ApiLimit(180, 179, DateTimeUtc.Now + TimeSpan.FromMinutes(15));
142 toolStrip.ApiEndpoint = "/statuses/user_timeline";
144 Assert.Equal("API 179/180", toolStrip.Text);
145 Assert.Equal("API rest /statuses/user_timeline 179/180" + Environment.NewLine + "(reset after 15 minutes)", toolStrip.ToolTipText);
147 toolStrip.ApiEndpoint = "hoge";
149 Assert.Equal("API ???/???", toolStrip.Text);
150 Assert.Equal("API rest hoge ???/???" + Environment.NewLine + "(reset after ??? minutes)", toolStrip.ToolTipText);
152 toolStrip.ApiEndpoint = "";
154 Assert.Equal("API ???/???", toolStrip.Text);
155 Assert.Equal("API rest unknown ???/???" + Environment.NewLine + "(reset after ??? minutes)", toolStrip.ToolTipText);
157 MyCommon.TwitterApiInfo.AccessLimit.Clear();
161 class TestToolStripAPIGauge : ToolStripAPIGauge
163 public DateTimeUtc DateTimeNow = DateTimeUtc.Now;
165 protected override void UpdateRemainMinutes()
167 if (this.ApiLimit != null)
168 // DateTime.Now の代わりに this.DateTimeNow を使用することで任意の日時のテストができるようにする
169 this.remainMinutes = (this.ApiLimit.AccessLimitResetDate - this.DateTimeNow).TotalMinutes;
171 this.remainMinutes = -1;
176 public void GaugeBoundsTest()
178 using (var toolStrip = new TestToolStripAPIGauge())
180 var now = DateTimeUtc.Now;
181 toolStrip.DateTimeNow = now;
183 toolStrip.AutoSize = false;
184 toolStrip.Size = new Size(100, 10);
185 toolStrip.GaugeHeight = 5;
187 // toolStrip.ApiEndpoint の初期値は null
189 Assert.Equal(Rectangle.Empty, toolStrip.apiGaugeBounds);
190 Assert.Equal(Rectangle.Empty, toolStrip.timeGaugeBounds);
192 MyCommon.TwitterApiInfo.AccessLimit["endpoint"] = new ApiLimit(150, 60, now + TimeSpan.FromMinutes(3));
193 toolStrip.ApiEndpoint = "endpoint";
195 Assert.Equal(new Rectangle(0, 0, 40, 5), toolStrip.apiGaugeBounds); // 40% (60/150)
196 Assert.Equal(new Rectangle(0, 5, 20, 5), toolStrip.timeGaugeBounds); // 20% (3/15)
198 toolStrip.ApiEndpoint = "";
200 Assert.Equal(Rectangle.Empty, toolStrip.apiGaugeBounds);
201 Assert.Equal(Rectangle.Empty, toolStrip.timeGaugeBounds);
203 MyCommon.TwitterApiInfo.AccessLimit.Clear();
208 public void OneBillionTest()
210 using var toolStrip = new TestToolStripAPIGauge();
212 var now = new DateTimeUtc(2020, 2, 25, 19, 0, 0);
213 toolStrip.DateTimeNow = now;
215 toolStrip.AutoSize = false;
216 toolStrip.Size = new Size(100, 10);
217 toolStrip.GaugeHeight = 5;
219 MyCommon.TwitterApiInfo.AccessLimit["/statuses/user_timeline"] = new ApiLimit(
220 limitCount: 1_000_000_000,
221 limitRemain: 999_999_999,
222 resetDate: now + TimeSpan.FromMinutes(15)
224 toolStrip.ApiEndpoint = "/statuses/user_timeline";
226 Assert.Equal(new Rectangle(0, 0, 99, 5), toolStrip.apiGaugeBounds); // 99% (999999999/1000000000)
227 Assert.Equal(new Rectangle(0, 5, 100, 5), toolStrip.timeGaugeBounds); // 100% (15/15)
228 Assert.Equal("API 999999999/1000000000", toolStrip.Text);
229 Assert.Equal("API rest /statuses/user_timeline 999999999/1000000000" + Environment.NewLine + "(reset after 15 minutes)", toolStrip.ToolTipText);
231 MyCommon.TwitterApiInfo.AccessLimit.Clear();