You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DesignFormatterTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.design;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertTrue;
  19. import java.io.File;
  20. import java.math.BigDecimal;
  21. import java.text.SimpleDateFormat;
  22. import java.util.Date;
  23. import java.util.HashSet;
  24. import java.util.TimeZone;
  25. import org.junit.Assert;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28. import com.vaadin.event.ShortcutAction;
  29. import com.vaadin.event.ShortcutAction.KeyCode;
  30. import com.vaadin.event.ShortcutAction.ModifierKey;
  31. import com.vaadin.server.ExternalResource;
  32. import com.vaadin.server.FileResource;
  33. import com.vaadin.server.FontAwesome;
  34. import com.vaadin.server.FontIcon;
  35. import com.vaadin.server.GenericFontIcon;
  36. import com.vaadin.server.Resource;
  37. import com.vaadin.server.ThemeResource;
  38. import com.vaadin.shared.ApplicationConstants;
  39. import com.vaadin.shared.util.SharedUtil;
  40. import com.vaadin.ui.declarative.DesignFormatter;
  41. /**
  42. * Various tests related to formatter.
  43. *
  44. * @since 7.4
  45. * @author Vaadin Ltd
  46. */
  47. public class DesignFormatterTest {
  48. private DesignFormatter formatter;
  49. @Before
  50. public void setUp() {
  51. // initialise with default classes
  52. formatter = new DesignFormatter();
  53. }
  54. @Test
  55. public void testSupportedClasses() {
  56. for (Class<?> type : new Class<?>[] { boolean.class, char.class,
  57. byte.class, short.class, int.class, long.class, float.class,
  58. double.class, Boolean.class, Character.class, Byte.class,
  59. Short.class, Integer.class, Long.class, Float.class,
  60. Double.class, BigDecimal.class, String.class,
  61. ShortcutAction.class, Date.class, FileResource.class,
  62. ExternalResource.class, ThemeResource.class, Resource.class,
  63. TimeZone.class }) {
  64. assertTrue("not supported " + type.getSimpleName(),
  65. formatter.canConvert(type));
  66. }
  67. }
  68. @Test
  69. public void testBoolean() {
  70. assertEquals("", formatter.format(true));
  71. assertEquals("false", formatter.format(false));
  72. assertEquals(true, formatter.parse("true", boolean.class));
  73. assertEquals(true, formatter.parse("foobar", boolean.class));
  74. assertEquals(true, formatter.parse("", boolean.class));
  75. assertEquals(false, formatter.parse("false", boolean.class));
  76. assertEquals(true, formatter.parse("true", Boolean.class));
  77. assertEquals(true, formatter.parse("foobar", Boolean.class));
  78. assertEquals(true, formatter.parse("", Boolean.class));
  79. assertEquals(false, formatter.parse("false", Boolean.class));
  80. }
  81. @Test
  82. public void testIntegral() {
  83. byte b = 123;
  84. assertEquals("123", formatter.format(b));
  85. assertEquals(b, (byte) formatter.parse("123", byte.class));
  86. assertEquals((Byte) b, formatter.parse("123", Byte.class));
  87. b = -123;
  88. assertEquals("-123", formatter.format(b));
  89. assertEquals(b, (byte) formatter.parse("-123", byte.class));
  90. assertEquals((Byte) b, formatter.parse("-123", Byte.class));
  91. short s = 12345;
  92. assertEquals("12345", formatter.format(s));
  93. assertEquals(s, (short) formatter.parse("12345", short.class));
  94. assertEquals((Short) s, formatter.parse("12345", Short.class));
  95. s = -12345;
  96. assertEquals("-12345", formatter.format(s));
  97. assertEquals(s, (short) formatter.parse("-12345", short.class));
  98. assertEquals((Short) s, formatter.parse("-12345", Short.class));
  99. int i = 123456789;
  100. assertEquals("123456789", formatter.format(i));
  101. assertEquals(i, (int) formatter.parse("123456789", int.class));
  102. assertEquals((Integer) i, formatter.parse("123456789", Integer.class));
  103. i = -123456789;
  104. assertEquals("-123456789", formatter.format(i));
  105. assertEquals(i, (int) formatter.parse("-123456789", int.class));
  106. assertEquals((Integer) i, formatter.parse("-123456789", Integer.class));
  107. long l = 123456789123456789L;
  108. assertEquals("123456789123456789", formatter.format(l));
  109. assertEquals(l,
  110. (long) formatter.parse("123456789123456789", long.class));
  111. assertEquals((Long) l,
  112. formatter.parse("123456789123456789", Long.class));
  113. l = -123456789123456789L;
  114. assertEquals("-123456789123456789", formatter.format(l));
  115. assertEquals(l,
  116. (long) formatter.parse("-123456789123456789", long.class));
  117. assertEquals((Long) l,
  118. formatter.parse("-123456789123456789", Long.class));
  119. }
  120. @Test
  121. public void testFloatingPoint() {
  122. float f = 123.4567f;
  123. assertEquals("123.457", formatter.format(f));
  124. float f1 = formatter.parse("123.4567", float.class);
  125. assertEquals(f, f1, 1e-4);
  126. Float f2 = formatter.parse("123.4567", Float.class);
  127. assertEquals(f, f2, 1e-4);
  128. double d = 123456789.123456789;
  129. assertEquals("123456789.123", formatter.format(d));
  130. assertEquals(d, formatter.parse("123456789.123456789", double.class),
  131. 1e-9);
  132. assertEquals(d, formatter.parse("123456789.123456789", Double.class),
  133. 1e-9);
  134. }
  135. @Test
  136. public void testBigDecimal() {
  137. BigDecimal bd = new BigDecimal("123456789123456789.123456789123456789");
  138. assertEquals("123456789123456789.123", formatter.format(bd));
  139. assertEquals(bd, formatter.parse(
  140. "123456789123456789.123456789123456789", BigDecimal.class));
  141. }
  142. @Test
  143. public void testChar() {
  144. char c = '\uABCD';
  145. assertEquals("\uABCD", formatter.format(c));
  146. assertEquals(c, (char) formatter.parse("\uABCD", char.class));
  147. assertEquals((Character) c, formatter.parse("\uABCD", Character.class));
  148. c = 'y';
  149. assertEquals(c, (char) formatter.parse("yes", char.class));
  150. }
  151. @Test
  152. public void testString() {
  153. for (String s : new String[] { "", "foobar", "\uABCD", "驯鹿" }) {
  154. assertEquals(s, formatter.format(s));
  155. assertEquals(s, formatter.parse(s, String.class));
  156. }
  157. }
  158. @Test
  159. public void testDate() throws Exception {
  160. Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2012-02-17");
  161. String formatted = formatter.format(date);
  162. Date result = formatter.parse(formatted, Date.class);
  163. // writing will always give full date string
  164. String timeZone = new SimpleDateFormat("Z").format(date);
  165. assertEquals("2012-02-17 00:00:00" + timeZone, formatted);
  166. assertEquals(date, result);
  167. // try short date as well
  168. result = formatter.parse("2012-02-17", Date.class);
  169. assertEquals(date, result);
  170. }
  171. @Test
  172. public void testShortcutActions() {
  173. ShortcutAction action = new ShortcutAction("&^d");
  174. String formatted = formatter.format(action);
  175. // note the space here - it separates key combination from caption
  176. assertEquals("ctrl-alt-d d", formatted);
  177. ShortcutAction result = formatter.parse(formatted,
  178. ShortcutAction.class);
  179. assertTrue(equals(action, result));
  180. }
  181. @Test
  182. public void testShortcutActionNoCaption() {
  183. ShortcutAction action = new ShortcutAction(null, KeyCode.D,
  184. new int[] { ModifierKey.ALT, ModifierKey.CTRL });
  185. String formatted = formatter.format(action);
  186. assertEquals("ctrl-alt-d", formatted);
  187. ShortcutAction result = formatter.parse(formatted,
  188. ShortcutAction.class);
  189. assertTrue(equals(action, result));
  190. }
  191. @Test
  192. public void testInvalidShortcutAction() {
  193. assertInvalidShortcut("-");
  194. assertInvalidShortcut("foo");
  195. assertInvalidShortcut("atl-ctrl");
  196. assertInvalidShortcut("-a");
  197. }
  198. protected void assertInvalidShortcut(String shortcut) {
  199. try {
  200. formatter.parse(shortcut, ShortcutAction.class);
  201. Assert.fail("Invalid shortcut '" + shortcut + "' should throw");
  202. } catch (IllegalArgumentException e) {
  203. // expected
  204. }
  205. }
  206. @Test
  207. public void testTimeZone() {
  208. TimeZone zone = TimeZone.getTimeZone("GMT+2");
  209. String formatted = formatter.format(zone);
  210. assertEquals("GMT+02:00", formatted);
  211. TimeZone result = formatter.parse(formatted, TimeZone.class);
  212. assertEquals(zone, result);
  213. // try shorthand notation as well
  214. result = formatter.parse("GMT+2", TimeZone.class);
  215. assertEquals(zone, result);
  216. }
  217. @Test
  218. public void testExternalResource() {
  219. String url = "://example.com/my%20icon.png?a=b";
  220. for (String scheme : new String[] { "http", "https", "ftp", "ftps" }) {
  221. Resource resource = formatter.parse(scheme + url, Resource.class);
  222. assertTrue(scheme + " url should be parsed as ExternalResource",
  223. resource instanceof ExternalResource);
  224. assertEquals("parsed ExternalResource", scheme + url,
  225. ((ExternalResource) resource).getURL());
  226. String formatted = formatter
  227. .format(new ExternalResource(scheme + url));
  228. assertEquals("formatted ExternalResource", scheme + url, formatted);
  229. }
  230. }
  231. @Test
  232. public void testResourceFormat() {
  233. String httpUrl = "http://example.com/icon.png";
  234. String httpsUrl = "https://example.com/icon.png";
  235. String themePath = "icons/icon.png";
  236. String fontAwesomeUrl = "fonticon://FontAwesome/f0f9";
  237. String someOtherFontUrl = "fonticon://SomeOther/F0F9";
  238. String fileSystemPath = "c:/app/resources/icon.png";
  239. assertEquals(httpUrl, formatter.format(new ExternalResource(httpUrl)));
  240. assertEquals(httpsUrl,
  241. formatter.format(new ExternalResource(httpsUrl)));
  242. assertEquals(ApplicationConstants.THEME_PROTOCOL_PREFIX + themePath,
  243. formatter.format(new ThemeResource(themePath)));
  244. assertEquals(fontAwesomeUrl, formatter.format(FontAwesome.AMBULANCE));
  245. assertEquals(someOtherFontUrl.toLowerCase(),
  246. formatter.format(new GenericFontIcon("SomeOther", 0xf0f9))
  247. .toLowerCase());
  248. assertEquals(fileSystemPath,
  249. formatter.format(new FileResource(new File(fileSystemPath))));
  250. }
  251. @Test(expected = IllegalArgumentException.class)
  252. public void testResourceParseException() {
  253. String someRandomResourceUrl = "random://url";
  254. formatter.parse(someRandomResourceUrl, Resource.class);
  255. }
  256. @Test(expected = IllegalArgumentException.class)
  257. public void testResourceFormatException() {
  258. formatter.format(new Resource() { // must use unknown resource type
  259. @Override
  260. public String getMIMEType() {
  261. // TODO Auto-generated method stub
  262. return null;
  263. }
  264. });
  265. }
  266. @Test
  267. public void testResourceParse() {
  268. String httpUrl = "http://example.com/icon.png";
  269. String httpsUrl = "https://example.com/icon.png";
  270. String themePath = "icons/icon.png";
  271. String fontAwesomeUrl = "fonticon://FontAwesome/f0f9";
  272. String someOtherFont = "fonticon://SomeOther/F0F9";
  273. String fontAwesomeUrlOld = "font://AMBULANCE";
  274. String fileSystemPath = "c:\\app\\resources\\icon.png";
  275. assertEquals(new ExternalResource(httpUrl).getURL(),
  276. formatter.parse(httpUrl, ExternalResource.class).getURL());
  277. assertEquals(new ExternalResource(httpsUrl).getURL(),
  278. formatter.parse(httpsUrl, ExternalResource.class).getURL());
  279. assertEquals(new ThemeResource(themePath),
  280. formatter.parse(
  281. ApplicationConstants.THEME_PROTOCOL_PREFIX + themePath,
  282. ThemeResource.class));
  283. assertEquals(FontAwesome.AMBULANCE,
  284. formatter.parse(fontAwesomeUrlOld, FontAwesome.class));
  285. assertEquals(FontAwesome.AMBULANCE,
  286. formatter.parse(fontAwesomeUrl, FontAwesome.class));
  287. assertEquals(new GenericFontIcon("SomeOther", 0xF0F9),
  288. formatter.parse(someOtherFont, FontIcon.class));
  289. assertEquals(new FileResource(new File(fileSystemPath)).getSourceFile(),
  290. formatter.parse(fileSystemPath, FileResource.class)
  291. .getSourceFile());
  292. }
  293. /**
  294. * A static method to allow comparison two different actions.
  295. *
  296. * @param act
  297. * One action to compare.
  298. * @param other
  299. * Second action to compare.
  300. * @return <b>true</b> when both actions are the same (caption, icon, and
  301. * key combination).
  302. */
  303. public static final boolean equals(ShortcutAction act,
  304. ShortcutAction other) {
  305. if (SharedUtil.equals(other.getCaption(), act.getCaption())
  306. && SharedUtil.equals(other.getIcon(), act.getIcon())
  307. && act.getKeyCode() == other.getKeyCode()
  308. && act.getModifiers().length == other.getModifiers().length) {
  309. HashSet<Integer> thisSet = new HashSet<Integer>(
  310. act.getModifiers().length);
  311. // this is a bit tricky comparison, but there is no nice way of
  312. // making int[] into a Set
  313. for (int mod : act.getModifiers()) {
  314. thisSet.add(mod);
  315. }
  316. for (int mod : other.getModifiers()) {
  317. thisSet.remove(mod);
  318. }
  319. return thisSet.isEmpty();
  320. }
  321. return false;
  322. }
  323. }