OSDN Git Service

90aa99bcf9e8cb26dedd67c164dce346bb3680be
[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             {
40                 var now = DateTimeUtc.Now;
41                 toolStrip.DateTimeNow = now;
42
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));
45
46                 // toolStrip.ApiEndpoint の初期値は null
47
48                 Assert.Null(toolStrip.ApiEndpoint);
49                 Assert.Null(toolStrip.ApiLimit);
50
51                 toolStrip.ApiEndpoint = "endpoint1";
52
53                 Assert.Equal("endpoint1", toolStrip.ApiEndpoint);
54                 Assert.Equal(new ApiLimit(15, 15, now + TimeSpan.FromMinutes(15)), toolStrip.ApiLimit);
55
56                 toolStrip.ApiEndpoint = "endpoint2";
57
58                 Assert.Equal("endpoint2", toolStrip.ApiEndpoint);
59                 Assert.Equal(new ApiLimit(180, 18, now + TimeSpan.FromMinutes(5)), toolStrip.ApiLimit);
60
61                 MyCommon.TwitterApiInfo.AccessLimit["endpoint2"] = new ApiLimit(180, 17, now + TimeSpan.FromMinutes(5));
62                 toolStrip.ApiEndpoint = "endpoint2";
63
64                 Assert.Equal("endpoint2", toolStrip.ApiEndpoint);
65                 Assert.Equal(new ApiLimit(180, 17, now + TimeSpan.FromMinutes(5)), toolStrip.ApiLimit);
66
67                 toolStrip.ApiEndpoint = "hoge";
68
69                 Assert.Equal("hoge", toolStrip.ApiEndpoint);
70                 Assert.Null(toolStrip.ApiLimit);
71
72                 toolStrip.ApiEndpoint = "";
73
74                 Assert.Null(toolStrip.ApiEndpoint);
75                 Assert.Null(toolStrip.ApiLimit);
76
77                 MyCommon.TwitterApiInfo.AccessLimit.Clear();
78             }
79         }
80
81         [Fact]
82         public void GaugeHeightTest()
83         {
84             using (var toolStrip = new ToolStripAPIGauge())
85             {
86                 toolStrip.AutoSize = false;
87                 toolStrip.Size = new Size(100, 10);
88
89                 MyCommon.TwitterApiInfo.AccessLimit["endpoint"] = new ApiLimit(15, 15, DateTimeUtc.MaxValue);
90                 toolStrip.ApiEndpoint = "endpoint";
91
92                 toolStrip.GaugeHeight = 5;
93
94                 Assert.Equal(new Rectangle(0, 0, 100, 5), toolStrip.apiGaugeBounds);
95                 Assert.Equal(new Rectangle(0, 5, 100, 5), toolStrip.timeGaugeBounds);
96
97                 toolStrip.GaugeHeight = 3;
98
99                 Assert.Equal(new Rectangle(0, 2, 100, 3), toolStrip.apiGaugeBounds);
100                 Assert.Equal(new Rectangle(0, 5, 100, 3), toolStrip.timeGaugeBounds);
101
102                 toolStrip.GaugeHeight = 0;
103
104                 Assert.Equal(Rectangle.Empty, toolStrip.apiGaugeBounds);
105                 Assert.Equal(Rectangle.Empty, toolStrip.timeGaugeBounds);
106
107                 MyCommon.TwitterApiInfo.AccessLimit.Clear();
108             }
109         }
110
111         [Fact]
112         public void TextTest()
113         {
114             using (var toolStrip = new ToolStripAPIGauge())
115             {
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));
119
120                 // toolStrip.ApiEndpoint の初期値は null
121
122                 Assert.Equal("API ???/???", toolStrip.Text);
123                 Assert.Equal("API rest unknown ???/???" + Environment.NewLine + "(reset after ??? minutes)", toolStrip.ToolTipText);
124
125                 toolStrip.ApiEndpoint = "/search/tweets";
126
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);
129
130                 toolStrip.ApiEndpoint = "/statuses/user_timeline";
131
132                 Assert.Equal("API ???/???", toolStrip.Text);
133                 Assert.Equal("API rest /statuses/user_timeline ???/???" + Environment.NewLine + "(reset after ??? minutes)", toolStrip.ToolTipText);
134
135                 MyCommon.TwitterApiInfo.AccessLimit["/statuses/user_timeline"] = new ApiLimit(180, 180, DateTimeUtc.Now + TimeSpan.FromMinutes(15));
136                 toolStrip.ApiEndpoint = "/statuses/user_timeline";
137
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);
140
141                 MyCommon.TwitterApiInfo.AccessLimit["/statuses/user_timeline"] = new ApiLimit(180, 179, DateTimeUtc.Now + TimeSpan.FromMinutes(15));
142                 toolStrip.ApiEndpoint = "/statuses/user_timeline";
143
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);
146
147                 toolStrip.ApiEndpoint = "hoge";
148
149                 Assert.Equal("API ???/???", toolStrip.Text);
150                 Assert.Equal("API rest hoge ???/???" + Environment.NewLine + "(reset after ??? minutes)", toolStrip.ToolTipText);
151
152                 toolStrip.ApiEndpoint = "";
153
154                 Assert.Equal("API ???/???", toolStrip.Text);
155                 Assert.Equal("API rest unknown ???/???" + Environment.NewLine + "(reset after ??? minutes)", toolStrip.ToolTipText);
156
157                 MyCommon.TwitterApiInfo.AccessLimit.Clear();
158             }
159         }
160
161         class TestToolStripAPIGauge : ToolStripAPIGauge
162         {
163             public DateTimeUtc DateTimeNow = DateTimeUtc.Now;
164
165             protected override void UpdateRemainMinutes()
166             {
167                 if (this.ApiLimit != null)
168                     // DateTime.Now の代わりに this.DateTimeNow を使用することで任意の日時のテストができるようにする
169                     this.remainMinutes = (this.ApiLimit.AccessLimitResetDate - this.DateTimeNow).TotalMinutes;
170                 else
171                     this.remainMinutes = -1;
172             }
173         }
174
175         [Fact]
176         public void GaugeBoundsTest()
177         {
178             using (var toolStrip = new TestToolStripAPIGauge())
179             {
180                 var now = DateTimeUtc.Now;
181                 toolStrip.DateTimeNow = now;
182
183                 toolStrip.AutoSize = false;
184                 toolStrip.Size = new Size(100, 10);
185                 toolStrip.GaugeHeight = 5;
186
187                 // toolStrip.ApiEndpoint の初期値は null
188
189                 Assert.Equal(Rectangle.Empty, toolStrip.apiGaugeBounds);
190                 Assert.Equal(Rectangle.Empty, toolStrip.timeGaugeBounds);
191
192                 MyCommon.TwitterApiInfo.AccessLimit["endpoint"] = new ApiLimit(150, 60, now + TimeSpan.FromMinutes(3));
193                 toolStrip.ApiEndpoint = "endpoint";
194
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)
197
198                 toolStrip.ApiEndpoint = "";
199
200                 Assert.Equal(Rectangle.Empty, toolStrip.apiGaugeBounds);
201                 Assert.Equal(Rectangle.Empty, toolStrip.timeGaugeBounds);
202
203                 MyCommon.TwitterApiInfo.AccessLimit.Clear();
204             }
205         }
206     }
207 }