OSDN Git Service

新兵装開発資材輸送を船団護衛せよ!のカウンターを実装する
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer.Test / ShipLabelTest.cs
1 // Copyright (C) 2016 Kazuhiro Fujieda <fujieda@users.osdn.me>\r
2 //\r
3 // Licensed under the Apache License, Version 2.0 (the "License");\r
4 // you may not use this file except in compliance with the License.\r
5 // You may obtain a copy of the License at\r
6 //\r
7 //    http://www.apache.org/licenses/LICENSE-2.0\r
8 //\r
9 // Unless required by applicable law or agreed to in writing, software\r
10 // distributed under the License is distributed on an "AS IS" BASIS,\r
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12 // See the License for the specific language governing permissions and\r
13 // limitations under the License.\r
14 \r
15 using System;\r
16 using System.Collections.Generic;\r
17 using System.Drawing;\r
18 using System.Reflection;\r
19 using System.Windows.Forms;\r
20 using ExpressionToCodeLib;\r
21 using KancolleSniffer.Model;\r
22 using KancolleSniffer.View;\r
23 using Microsoft.VisualStudio.TestTools.UnitTesting;\r
24 \r
25 namespace KancolleSniffer.Test\r
26 {\r
27     [TestClass]\r
28     public class ShipLabelTest\r
29     {\r
30         [TestClass]\r
31         public class TruncateTest\r
32         {\r
33             private class TestData : Dictionary<int, Pair[]>\r
34             {\r
35             }\r
36 \r
37             private class Pair\r
38             {\r
39                 public readonly string Origin;\r
40                 public readonly string Result;\r
41 \r
42                 public Pair()\r
43                 {\r
44                     Origin = "";\r
45                     Result = "";\r
46                 }\r
47 \r
48                 public Pair(string origin, string result)\r
49                 {\r
50                     Origin = origin;\r
51                     Result = result;\r
52                 }\r
53             }\r
54 \r
55             /// <summary>\r
56             /// 文字数制限がない\r
57             /// </summary>\r
58             [TestMethod]\r
59             public void Unlimited()\r
60             {\r
61                 var label = new ShipLabel.Name(Point.Empty, ShipNameWidth.Max){Parent = new Panel()};\r
62                 var str = "一二三四五六七八九〇一二三四五六七八九〇一二三四五六七八九〇";\r
63                 label.SetName(str);\r
64                 Assert.AreEqual(str, label.Text);\r
65             }\r
66 \r
67             /// <summary>\r
68             /// 明石タイマー表示中の艦娘の名前を縮める\r
69             /// </summary>\r
70             [TestMethod]\r
71             public void ForAkashiTimer()\r
72             {\r
73                 var data = new TestData\r
74                 {\r
75                     {\r
76                         100,\r
77                         new[]\r
78                         {\r
79                             new Pair("夕立改二", "夕立改二"),\r
80                             new Pair("千代田航改", "千代田航"),\r
81                             new Pair("千代田航改二", "千代田航"),\r
82                             new Pair("Bismarck改", "Bismarck"),\r
83                             new Pair("Bismarck zwei", "Bismarck"),\r
84                             new Pair("Bismarck drei", "Bismarck"),\r
85                             new Pair("Prinz Eugen", "Prinz Eug"),\r
86                             new Pair("Prinz Eugen改", "Prinz Eug"),\r
87                             new Pair("Graf Zeppelin", "Graf Zep"),\r
88                             new Pair("Graf Zeppelin改", "Graf Zep"),\r
89                             new Pair("Libeccio改", "Libeccio")\r
90                         }\r
91                     },\r
92                     {\r
93                         125,\r
94                         new[]\r
95                         {\r
96                             new Pair(),\r
97                             new Pair(),\r
98                             new Pair(),\r
99                             new Pair(),\r
100                             new Pair(),\r
101                             new Pair(),\r
102                             new Pair(),\r
103                             new Pair(),\r
104                             new Pair("Graf Zeppelin", "Graf Zepp"),\r
105                             new Pair("Graf Zeppelin改", "Graf Zepp"),\r
106                             new Pair("Libeccio改", "Libeccio改")\r
107                         }\r
108                     }\r
109                 };\r
110                 TestTruncate(data, ShipNameWidth.AkashiTimer);\r
111             }\r
112 \r
113             /// <summary>\r
114             /// 入渠中の艦娘名の名前を縮める\r
115             /// </summary>\r
116             [TestMethod]\r
117             public void ForNDock()\r
118             {\r
119                 var data = new TestData\r
120                 {\r
121                     {\r
122                         100, new[]\r
123                         {\r
124                             new Pair("千歳航改二", "千歳航改二"),\r
125                             new Pair("千代田航改二", "千代田航改"),\r
126                             new Pair("Graf Zeppelin", "Graf Zeppeli"),\r
127                             new Pair("Graf Zeppelin改", "Graf Zeppeli")\r
128                         }\r
129                     },\r
130                     {\r
131                         125, new[]\r
132                         {\r
133                             new Pair(),\r
134                             new Pair(),\r
135                             new Pair("Graf Zeppelin", "Graf Zeppelin"),\r
136                             new Pair("Graf Zeppelin改", "Graf Zeppelin")\r
137                         }\r
138                     }\r
139                 };\r
140                 TestTruncate(data, ShipNameWidth.NDock);\r
141             }\r
142 \r
143             /// <summary>\r
144             /// 一覧ウィンドウの要修復一覧の艦娘の名前を縮める\r
145             /// </summary>\r
146             [TestMethod]\r
147             public void ForRepairListFull()\r
148             {\r
149                 var data = new TestData\r
150                 {\r
151                     {\r
152                         100,\r
153                         new[]\r
154                         {\r
155                             new Pair("Graf Zeppelin", "Graf Zeppelin"),\r
156                             new Pair("Graf Zeppelin改", "Graf Zeppelin"),\r
157                             new Pair("千代田航改二", "千代田航改")\r
158                         }\r
159                     },\r
160                     {\r
161                         125,\r
162                         new[]\r
163                         {\r
164                             new Pair(),\r
165                             new Pair(),\r
166                             new Pair("千代田航改二", "千代田航改二")\r
167                         }\r
168                     }\r
169                 };\r
170                 TestTruncate(data, ShipNameWidth.RepairListFull);\r
171             }\r
172 \r
173             /// <summary>\r
174             /// メインパネルの艦娘の名前を縮める\r
175             /// </summary>\r
176             [TestMethod]\r
177             public void ForMainPanel()\r
178             {\r
179                 var data = new TestData\r
180                 {\r
181                     {\r
182                         100,\r
183                         new[]\r
184                         {\r
185                             new Pair("Commandant Teste", "Commandant Tes")\r
186                         }\r
187                     },\r
188                     {\r
189                         125,\r
190                         new[]\r
191                         {\r
192                             new Pair("Commandant Teste", "Commandant Test")\r
193                         }\r
194                     }\r
195                 };\r
196                 TestTruncate(data, ShipNameWidth.MainPanel);\r
197             }\r
198 \r
199             /// <summary>\r
200             /// 一覧ウィンドウの艦娘一覧の名前を縮める\r
201             /// </summary>\r
202             [TestMethod]\r
203             public void ForShipList()\r
204             {\r
205                 var data = new TestData\r
206                 {\r
207                     {\r
208                         100,\r
209                         new[]\r
210                         {\r
211                             new Pair("Commandant Teste", "Commandant T"),\r
212                             new Pair("Graf Zeppelin改", "Graf Zeppelin")\r
213                         }\r
214                     },\r
215                     {\r
216                         125,\r
217                         new[]\r
218                         {\r
219                             new Pair(),\r
220                             new Pair("Graf Zeppelin改", "Graf Zeppelin改")\r
221                         }\r
222                     }\r
223                 };\r
224                 TestTruncate(data, ShipNameWidth.ShipList);\r
225             }\r
226 \r
227             private static void TestTruncate(TestData data, ShipNameWidth width)\r
228             {\r
229                 foreach (var zoom in data.Keys)\r
230                 {\r
231                     SetScaleFactor(zoom);\r
232                     var label = new ShipLabel.Name(Point.Empty, width) {Font = ShipLabel.Name.BaseFont};\r
233                     for (var i = 0; i < data[zoom].Length; i++)\r
234                     {\r
235                         var entry = data[zoom][i];\r
236                         if (string.IsNullOrEmpty(entry.Origin))\r
237                             entry = data[100][i];\r
238                         label.SetName(entry.Origin);\r
239                         Assert.AreEqual(entry.Result, label.Text, $"{entry.Origin}: scale {zoom}");\r
240                     }\r
241                 }\r
242             }\r
243 \r
244             private static void SetScaleFactor(int zoom)\r
245             {\r
246                 var form = new Form {AutoScaleMode = AutoScaleMode.Font};\r
247                 if (zoom == 100)\r
248                 {\r
249                     Scaler.Factor = new SizeF(1, 1);\r
250                     ShipLabel.Name.BaseFont = form.Font;\r
251                     ShipLabel.Name.LatinFont = LatinFont(100);\r
252                     return;\r
253                 }\r
254                 var prev = form.CurrentAutoScaleDimensions;\r
255                 form.Font = new Font(form.Font.FontFamily, form.Font.Size * zoom / 100);\r
256                 ShipLabel.Name.BaseFont = form.Font;\r
257                 ShipLabel.Name.LatinFont = LatinFont(zoom);\r
258                 var cur = form.CurrentAutoScaleDimensions;\r
259                 Scaler.Factor = new SizeF(cur.Width / prev.Width, cur.Height / prev.Height);\r
260             }\r
261 \r
262             private static Font LatinFont(int zoom)\r
263             {\r
264                 return new Font("Tahoma", 8f * zoom / 100);\r
265             }\r
266         }\r
267 \r
268         /// <summary>\r
269         /// prefixを加える\r
270         /// </summary>\r
271         [TestMethod]\r
272         public void SetName()\r
273         {\r
274             var label = new ShipLabel.Name(Point.Empty, ShipNameWidth.AkashiTimer) {Parent = new Panel()};\r
275             Scaler.Factor = new SizeF(1, 1);\r
276             label.Set(new ShipStatus\r
277             {\r
278                 Spec = new ShipSpec {Name = "綾波改二"},\r
279                 Escaped = true\r
280             });\r
281             PAssert.That(() => label.Text == "[避]綾波改二");\r
282             label.Set(new ShipStatus\r
283             {\r
284                 Spec = new ShipSpec {Name = "朝潮改二丁"},\r
285                 Escaped = true\r
286             });\r
287             PAssert.That(() => label.Text == "[避]朝潮改二");\r
288         }\r
289 \r
290         /// <summary>\r
291         /// %表示の小数部を切り捨てる\r
292         /// </summary>\r
293         [TestMethod]\r
294         public void RoundOffFractionOfPercent()\r
295         {\r
296             var label = new ShipLabel.Hp {Parent = new Panel()};\r
297             label.SetHp(104, 105);\r
298             label.ToggleHpPercent();\r
299             PAssert.That(() => label.Text == "99%");\r
300         }\r
301 \r
302 \r
303         /// <summary>\r
304         /// 装備スロットの状況を調べる\r
305         /// </summary>\r
306         [TestMethod]\r
307         public void SlotStatus()\r
308         {\r
309             var ship = new ShipStatus\r
310             {\r
311                 Id = 1,\r
312                 Slot = new[] {new ItemStatus(), new ItemStatus(), new ItemStatus()}, SlotEx = new ItemStatus(0),\r
313                 Spec = new ShipSpec {SlotNum = 3}\r
314             };\r
315             Assert.AreEqual(2, GetSlotStatus(ship)); // NormalEmpty\r
316             ship.SlotEx.Id = -1;\r
317             Assert.AreEqual(2 | 4, GetSlotStatus(ship)); // | ExtraEmpty\r
318             ship.SlotEx.Id = 1;\r
319             Assert.AreEqual(2, GetSlotStatus(ship)); // NormalEmpty\r
320             ship.Slot[0].Id = ship.Slot[1].Id = ship.Slot[2].Id = 1;\r
321             Assert.AreEqual(0, GetSlotStatus(ship)); // Equipped\r
322             ship.Slot[2].Id = -1;\r
323             Assert.AreEqual(1, GetSlotStatus(ship)); // SemiEquipped\r
324             ship.Spec.SlotNum = 2;\r
325             Assert.AreEqual(0, GetSlotStatus(ship)); // Equipped\r
326             ship.Spec.SlotNum = 0;\r
327             Assert.AreEqual(0, GetSlotStatus(ship)); // Equipped (まるゆ)\r
328         }\r
329 \r
330         private static int GetSlotStatus(ShipStatus ship)\r
331         {\r
332             var method =\r
333                 typeof(ShipLabel.Name).GetMethod("GetSlotStatus", BindingFlags.NonPublic | BindingFlags.Static);\r
334             // ReSharper disable once PossibleNullReferenceException\r
335             return (int)Convert.ChangeType(method.Invoke(null, new object[] {ship}), TypeCode.Int32);\r
336         }\r
337     }\r
338 }