OSDN Git Service

811ef6a68a2392c8cabd2af7430ef41360e62703
[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 NUnit.Framework;
28 using OpenTween.Api;
29
30 namespace OpenTween
31 {
32     [TestFixture]
33     class ToolStripAPIGaugeTest
34     {
35         [Test]
36         public void GaugeHeightTest()
37         {
38             using (var toolStrip = new ToolStripAPIGauge())
39             {
40                 toolStrip.AutoSize = false;
41                 toolStrip.Size = new Size(100, 10);
42                 toolStrip.ApiLimit = new ApiLimit(150, 150, DateTime.MaxValue);
43
44                 toolStrip.GaugeHeight = 5;
45
46                 Assert.That(toolStrip.apiGaugeBounds, Is.EqualTo(new Rectangle(0, 0, 100, 5)));
47                 Assert.That(toolStrip.timeGaugeBounds, Is.EqualTo(new Rectangle(0, 5, 100, 5)));
48
49                 toolStrip.GaugeHeight = 3;
50
51                 Assert.That(toolStrip.apiGaugeBounds, Is.EqualTo(new Rectangle(0, 2, 100, 3)));
52                 Assert.That(toolStrip.timeGaugeBounds, Is.EqualTo(new Rectangle(0, 5, 100, 3)));
53
54                 toolStrip.GaugeHeight = 0;
55
56                 Assert.That(toolStrip.apiGaugeBounds, Is.EqualTo(Rectangle.Empty));
57                 Assert.That(toolStrip.timeGaugeBounds, Is.EqualTo(Rectangle.Empty));
58             }
59         }
60
61         [Test]
62         public void TextTest()
63         {
64             using (var toolStrip = new ToolStripAPIGauge())
65             {
66                 // toolStrip.ApiLimit の初期値は null
67
68                 Assert.That(toolStrip.Text, Is.EqualTo("API ???/???"));
69                 Assert.That(toolStrip.ToolTipText, Is.EqualTo("API rest ???/???" + Environment.NewLine + "(reset after ??? minutes)"));
70
71                 toolStrip.ApiLimit = new ApiLimit(15, 14, DateTime.Now.AddMinutes(15));
72
73                 Assert.That(toolStrip.Text, Is.EqualTo("API 14/15"));
74                 Assert.That(toolStrip.ToolTipText, Is.EqualTo("API rest 14/15" + Environment.NewLine + "(reset after 15 minutes)"));
75
76                 toolStrip.ApiLimit = null;
77
78                 Assert.That(toolStrip.Text, Is.EqualTo("API ???/???"));
79                 Assert.That(toolStrip.ToolTipText, Is.EqualTo("API rest ???/???" + Environment.NewLine + "(reset after ??? minutes)"));
80             }
81         }
82
83         class TestToolStripAPIGauge : ToolStripAPIGauge
84         {
85             public DateTime Now { get; set; } // 現在時刻
86
87             protected override void UpdateRemainMinutes()
88             {
89                 if (this.ApiLimit != null)
90                     this.remainMinutes = (this.ApiLimit.AccessLimitResetDate - this.Now).TotalMinutes;
91                 else
92                     this.remainMinutes = -1;
93             }
94         }
95
96         [Test]
97         public void GaugeBoundsTest()
98         {
99             using (var toolStrip = new TestToolStripAPIGauge())
100             {
101                 toolStrip.AutoSize = false;
102                 toolStrip.Size = new Size(100, 10);
103                 toolStrip.GaugeHeight = 5;
104
105                 // 現在時刻を偽装
106                 toolStrip.Now = new DateTime(2013, 1, 1, 0, 0, 0);
107
108                 // toolStrip.ApiLimit の初期値は null
109
110                 Assert.That(toolStrip.apiGaugeBounds, Is.EqualTo(Rectangle.Empty));
111                 Assert.That(toolStrip.timeGaugeBounds, Is.EqualTo(Rectangle.Empty));
112
113                 toolStrip.ApiLimit = new ApiLimit(150, 60, toolStrip.Now.AddMinutes(15));
114
115                 Assert.That(toolStrip.apiGaugeBounds, Is.EqualTo(new Rectangle(0, 0, 40, 5))); // 40% (60/150)
116                 Assert.That(toolStrip.timeGaugeBounds, Is.EqualTo(new Rectangle(0, 5, 25, 5))); // 25% (15/60)
117
118                 toolStrip.ApiLimit = null;
119
120                 Assert.That(toolStrip.apiGaugeBounds, Is.EqualTo(Rectangle.Empty));
121                 Assert.That(toolStrip.timeGaugeBounds, Is.EqualTo(Rectangle.Empty));
122             }
123         }
124     }
125 }