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();
39 var now = DateTimeUtc.Now;
40 toolStrip.DateTimeNow = now;
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));
45 // toolStrip.ApiEndpoint の初期値は null
47 Assert.Null(toolStrip.ApiEndpoint);
48 Assert.Null(toolStrip.ApiLimit);
50 toolStrip.ApiEndpoint = "endpoint1";
52 Assert.Equal("endpoint1", toolStrip.ApiEndpoint);
53 Assert.Equal(new ApiLimit(15, 15, now + TimeSpan.FromMinutes(15)), toolStrip.ApiLimit);
55 toolStrip.ApiEndpoint = "endpoint2";
57 Assert.Equal("endpoint2", toolStrip.ApiEndpoint);
58 Assert.Equal(new ApiLimit(180, 18, now + TimeSpan.FromMinutes(5)), toolStrip.ApiLimit);
60 MyCommon.TwitterApiInfo.AccessLimit["endpoint2"] = new ApiLimit(180, 17, now + TimeSpan.FromMinutes(5));
61 toolStrip.ApiEndpoint = "endpoint2";
63 Assert.Equal("endpoint2", toolStrip.ApiEndpoint);
64 Assert.Equal(new ApiLimit(180, 17, now + TimeSpan.FromMinutes(5)), toolStrip.ApiLimit);
66 toolStrip.ApiEndpoint = "hoge";
68 Assert.Equal("hoge", toolStrip.ApiEndpoint);
69 Assert.Null(toolStrip.ApiLimit);
71 toolStrip.ApiEndpoint = "";
73 Assert.Null(toolStrip.ApiEndpoint);
74 Assert.Null(toolStrip.ApiLimit);
76 MyCommon.TwitterApiInfo.AccessLimit.Clear();
80 public void GaugeHeightTest()
82 using var toolStrip = new ToolStripAPIGauge();
83 toolStrip.AutoSize = false;
84 toolStrip.Size = new Size(100, 10);
86 MyCommon.TwitterApiInfo.AccessLimit["endpoint"] = new ApiLimit(15, 15, DateTimeUtc.MaxValue);
87 toolStrip.ApiEndpoint = "endpoint";
89 toolStrip.GaugeHeight = 5;
91 Assert.Equal(new Rectangle(0, 0, 100, 5), toolStrip.ApiGaugeBounds);
92 Assert.Equal(new Rectangle(0, 5, 100, 5), toolStrip.TimeGaugeBounds);
94 toolStrip.GaugeHeight = 3;
96 Assert.Equal(new Rectangle(0, 2, 100, 3), toolStrip.ApiGaugeBounds);
97 Assert.Equal(new Rectangle(0, 5, 100, 3), toolStrip.TimeGaugeBounds);
99 toolStrip.GaugeHeight = 0;
101 Assert.Equal(Rectangle.Empty, toolStrip.ApiGaugeBounds);
102 Assert.Equal(Rectangle.Empty, toolStrip.TimeGaugeBounds);
104 MyCommon.TwitterApiInfo.AccessLimit.Clear();
108 public void TextTest()
110 using var toolStrip = new ToolStripAPIGauge();
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));
116 // toolStrip.ApiEndpoint の初期値は null
118 Assert.Equal("API ???/???", toolStrip.Text);
119 Assert.Equal("API rest unknown ???/???" + Environment.NewLine + "(reset after ??? minutes)", toolStrip.ToolTipText);
121 toolStrip.ApiEndpoint = "/search/tweets";
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);
126 toolStrip.ApiEndpoint = "/statuses/user_timeline";
128 Assert.Equal("API ???/???", toolStrip.Text);
129 Assert.Equal("API rest /statuses/user_timeline ???/???" + Environment.NewLine + "(reset after ??? minutes)", toolStrip.ToolTipText);
131 MyCommon.TwitterApiInfo.AccessLimit["/statuses/user_timeline"] = new ApiLimit(180, 180, DateTimeUtc.Now + TimeSpan.FromMinutes(15));
132 toolStrip.ApiEndpoint = "/statuses/user_timeline";
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);
137 MyCommon.TwitterApiInfo.AccessLimit["/statuses/user_timeline"] = new ApiLimit(180, 179, DateTimeUtc.Now + TimeSpan.FromMinutes(15));
138 toolStrip.ApiEndpoint = "/statuses/user_timeline";
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);
143 toolStrip.ApiEndpoint = "hoge";
145 Assert.Equal("API ???/???", toolStrip.Text);
146 Assert.Equal("API rest hoge ???/???" + Environment.NewLine + "(reset after ??? minutes)", toolStrip.ToolTipText);
148 toolStrip.ApiEndpoint = "";
150 Assert.Equal("API ???/???", toolStrip.Text);
151 Assert.Equal("API rest unknown ???/???" + Environment.NewLine + "(reset after ??? minutes)", toolStrip.ToolTipText);
153 MyCommon.TwitterApiInfo.AccessLimit.Clear();
156 private class TestToolStripAPIGauge : ToolStripAPIGauge
158 public DateTimeUtc DateTimeNow = DateTimeUtc.Now;
160 protected override void UpdateRemainMinutes()
162 if (this.ApiLimit != null)
163 // DateTime.Now の代わりに this.DateTimeNow を使用することで任意の日時のテストができるようにする
164 this.remainMinutes = (this.ApiLimit.AccessLimitResetDate - this.DateTimeNow).TotalMinutes;
166 this.remainMinutes = -1;
171 public void GaugeBoundsTest()
173 using var toolStrip = new TestToolStripAPIGauge();
175 var now = DateTimeUtc.Now;
176 toolStrip.DateTimeNow = now;
178 toolStrip.AutoSize = false;
179 toolStrip.Size = new Size(100, 10);
180 toolStrip.GaugeHeight = 5;
182 // toolStrip.ApiEndpoint の初期値は null
184 Assert.Equal(Rectangle.Empty, toolStrip.ApiGaugeBounds);
185 Assert.Equal(Rectangle.Empty, toolStrip.TimeGaugeBounds);
187 MyCommon.TwitterApiInfo.AccessLimit["endpoint"] = new ApiLimit(150, 60, now + TimeSpan.FromMinutes(3));
188 toolStrip.ApiEndpoint = "endpoint";
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)
193 toolStrip.ApiEndpoint = "";
195 Assert.Equal(Rectangle.Empty, toolStrip.ApiGaugeBounds);
196 Assert.Equal(Rectangle.Empty, toolStrip.TimeGaugeBounds);
198 MyCommon.TwitterApiInfo.AccessLimit.Clear();
202 public void OneBillionTest()
204 using var toolStrip = new TestToolStripAPIGauge();
206 var now = new DateTimeUtc(2020, 2, 25, 19, 0, 0);
207 toolStrip.DateTimeNow = now;
209 toolStrip.AutoSize = false;
210 toolStrip.Size = new Size(100, 10);
211 toolStrip.GaugeHeight = 5;
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)
218 toolStrip.ApiEndpoint = "/statuses/user_timeline";
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);
225 MyCommon.TwitterApiInfo.AccessLimit.Clear();