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-2014 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.data.util.converter.Converter.ConversionException;
  29. import com.vaadin.event.ShortcutAction;
  30. import com.vaadin.event.ShortcutAction.KeyCode;
  31. import com.vaadin.event.ShortcutAction.ModifierKey;
  32. import com.vaadin.server.ExternalResource;
  33. import com.vaadin.server.FileResource;
  34. import com.vaadin.server.FontAwesome;
  35. import com.vaadin.server.FontIcon;
  36. import com.vaadin.server.GenericFontIcon;
  37. import com.vaadin.server.Resource;
  38. import com.vaadin.server.ThemeResource;
  39. import com.vaadin.shared.ApplicationConstants;
  40. import com.vaadin.shared.util.SharedUtil;
  41. import com.vaadin.ui.declarative.DesignFormatter;
  42. /**
  43. * Various tests related to formatter.
  44. *
  45. * @since 7.4
  46. * @author Vaadin Ltd
  47. */
  48. public class DesignFormatterTest {
  49. private DesignFormatter formatter;
  50. @Before
  51. public void setUp() {
  52. // initialise with default classes
  53. formatter = new DesignFormatter();
  54. }
  55. @Test
  56. public void testSupportedClasses() {
  57. for (Class<?> type : new Class<?>[] { boolean.class, char.class,
  58. byte.class, short.class, int.class, long.class, float.class,
  59. double.class, Boolean.class, Character.class, Byte.class,
  60. Short.class, Integer.class, Long.class, Float.class,
  61. Double.class, BigDecimal.class, String.class,
  62. ShortcutAction.class, Date.class, FileResource.class,
  63. ExternalResource.class, ThemeResource.class, Resource.class,
  64. TimeZone.class }) {
  65. assertTrue("not supported " + type.getSimpleName(),
  66. formatter.canConvert(type));
  67. }
  68. }
  69. @Test
  70. public void testBoolean() {
  71. assertEquals("", formatter.format(true));
  72. assertEquals("false", formatter.format(false));
  73. assertEquals(true, formatter.parse("true", boolean.class));
  74. assertEquals(true, formatter.parse("foobar", boolean.class));
  75. assertEquals(true, formatter.parse("", boolean.class));
  76. assertEquals(false, formatter.parse("false", boolean.class));
  77. assertEquals(true, formatter.parse("true", Boolean.class));
  78. assertEquals(true, formatter.parse("foobar", Boolean.class));
  79. assertEquals(true, formatter.parse("", Boolean.class));
  80. assertEquals(false, formatter.parse("false", Boolean.class));
  81. }
  82. @Test
  83. public void testIntegral() {
  84. byte b = 123;
  85. assertEquals("123", formatter.format(b));
  86. assertEquals(b, (byte) formatter.parse("123", byte.class));
  87. assertEquals((Byte) b, formatter.parse("123", Byte.class));
  88. b = -123;
  89. assertEquals("-123", formatter.format(b));
  90. assertEquals(b, (byte) formatter.parse("-123", byte.class));
  91. assertEquals((Byte) b, formatter.parse("-123", Byte.class));
  92. short s = 12345;
  93. assertEquals("12345", formatter.format(s));
  94. assertEquals(s, (short) formatter.parse("12345", short.class));
  95. assertEquals((Short) s, formatter.parse("12345", Short.class));
  96. s = -12345;
  97. assertEquals("-12345", formatter.format(s));
  98. assertEquals(s, (short) formatter.parse("-12345", short.class));
  99. assertEquals((Short) s, formatter.parse("-12345", Short.class));
  100. int i = 123456789;
  101. assertEquals("123456789", formatter.format(i));
  102. assertEquals(i, (int) formatter.parse("123456789", int.class));
  103. assertEquals((Integer) i, formatter.parse("123456789", Integer.class));
  104. i = -123456789;
  105. assertEquals("-123456789", formatter.format(i));
  106. assertEquals(i, (int) formatter.parse("-123456789", int.class));
  107. assertEquals((Integer) i, formatter.parse("-123456789", Integer.class));
  108. long l = 123456789123456789L;
  109. assertEquals("123456789123456789", formatter.format(l));
  110. assertEquals(l,
  111. (long) formatter.parse("123456789123456789", long.class));
  112. assertEquals((Long) l,
  113. formatter.parse("123456789123456789", Long.class));
  114. l = -123456789123456789L;
  115. assertEquals("-123456789123456789", formatter.format(l));
  116. assertEquals(l,
  117. (long) formatter.parse("-123456789123456789", long.class));
  118. assertEquals((Long) l,
  119. formatter.parse("-123456789123456789", Long.class));
  120. }
  121. @Test
  122. public void testFloatingPoint() {
  123. float f = 123.4567f;
  124. assertEquals("123.457", formatter.format(f));
  125. float f1 = formatter.parse("123.4567", float.class);
  126. assertEquals(f, f1, 1e-4);
  127. Float f2 = formatter.parse("123.4567", Float.class);
  128. assertEquals(f, f2, 1e-4);
  129. double d = 123456789.123456789;
  130. assertEquals("123456789.123", formatter.format(d));
  131. assertEquals(d, formatter.parse("123456789.123456789", double.class),
  132. 1e-9);
  133. assertEquals(d, formatter.parse("123456789.123456789", Double.class),
  134. 1e-9);
  135. }
  136. @Test
  137. public void testBigDecimal() {
  138. BigDecimal bd = new BigDecimal("123456789123456789.123456789123456789");
  139. assertEquals("123456789123456789.123", formatter.format(bd));
  140. assertEquals(bd, formatter.parse(
  141. "123456789123456789.123456789123456789", BigDecimal.class));
  142. }
  143. @Test
  144. public void testChar() {
  145. char c = '\uABCD';
  146. assertEquals("\uABCD", formatter.format(c));
  147. assertEquals(c, (char) formatter.parse("\uABCD", char.class));
  148. assertEquals((Character) c, formatter.parse("\uABCD", Character.class));
  149. c = 'y';
  150. assertEquals(c, (char) formatter.parse("yes", char.class));
  151. }
  152. @Test
  153. public void testString() {
  154. for (String s : new String[] { "", "foobar", "\uABCD", "驯鹿" }) {
  155. assertEquals(s, formatter.format(s));
  156. assertEquals(s, formatter.parse(s, String.class));
  157. }
  158. }
  159. @Test
  160. public void testDate() throws Exception {
  161. Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2012-02-17");
  162. String formatted = formatter.format(date);
  163. Date result = formatter.parse(formatted, Date.class);
  164. // writing will always give full date string
  165. String timeZone = new SimpleDateFormat("Z").format(date);
  166. assertEquals("2012-02-17 00:00:00" + timeZone, formatted);
  167. assertEquals(date, result);
  168. // try short date as well
  169. result = formatter.parse("2012-02-17", Date.class);
  170. assertEquals(date, result);
  171. }
  172. @Test
  173. public void testShortcutActions() {
  174. ShortcutAction action = new ShortcutAction("&^d");
  175. String formatted = formatter.format(action);
  176. // note the space here - it separates key combination from caption
  177. assertEquals("ctrl-alt-d d", formatted);
  178. ShortcutAction result = formatter
  179. .parse(formatted, ShortcutAction.class);
  180. assertTrue(equals(action, result));
  181. }
  182. @Test
  183. public void testShortcutActionNoCaption() {
  184. ShortcutAction action = new ShortcutAction(null, KeyCode.D, new int[] {
  185. ModifierKey.ALT, ModifierKey.CTRL });
  186. String formatted = formatter.format(action);
  187. assertEquals("ctrl-alt-d", formatted);
  188. ShortcutAction result = formatter
  189. .parse(formatted, ShortcutAction.class);
  190. assertTrue(equals(action, result));
  191. }
  192. @Test
  193. public void testInvalidShortcutAction() {
  194. assertInvalidShortcut("-");
  195. assertInvalidShortcut("foo");
  196. assertInvalidShortcut("atl-ctrl");
  197. assertInvalidShortcut("-a");
  198. }
  199. protected void assertInvalidShortcut(String shortcut) {
  200. try {
  201. formatter.parse(shortcut, ShortcutAction.class);
  202. Assert.fail("Invalid shortcut '" + shortcut + "' should throw");
  203. } catch (ConversionException e) {
  204. // expected
  205. }
  206. }
  207. @Test
  208. public void testTimeZone() {
  209. TimeZone zone = TimeZone.getTimeZone("GMT+2");
  210. String formatted = formatter.format(zone);
  211. assertEquals("GMT+02:00", formatted);
  212. TimeZone result = formatter.parse(formatted, TimeZone.class);
  213. assertEquals(zone, result);
  214. // try shorthand notation as well
  215. result = formatter.parse("GMT+2", TimeZone.class);
  216. assertEquals(zone, result);
  217. }
  218. @Test
  219. public void testExternalResource() {
  220. String url = "://example.com/my%20icon.png?a=b";
  221. for (String scheme : new String[] { "http", "https", "ftp", "ftps" }) {
  222. Resource resource = formatter.parse(scheme + url, Resource.class);
  223. assertTrue(scheme + " url should be parsed as ExternalResource",
  224. resource instanceof ExternalResource);
  225. assertEquals("parsed ExternalResource", scheme + url,
  226. ((ExternalResource) resource).getURL());
  227. String formatted = formatter.format(new ExternalResource(scheme
  228. + url));
  229. assertEquals("formatted ExternalResource", scheme + url, formatted);
  230. }
  231. }
  232. @Test
  233. public void testResourceFormat() {
  234. String httpUrl = "http://example.com/icon.png";
  235. String httpsUrl = "https://example.com/icon.png";
  236. String themePath = "icons/icon.png";
  237. String fontAwesomeUrl = "fonticon://FontAwesome/f0f9";
  238. String someOtherFontUrl = "fonticon://SomeOther/F0F9";
  239. String fileSystemPath = "c:/app/resources/icon.png";
  240. assertEquals(httpUrl, formatter.format(new ExternalResource(httpUrl)));
  241. assertEquals(httpsUrl, 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 = ConversionException.class)
  252. public void testResourceParseException() {
  253. String someRandomResourceUrl = "random://url";
  254. formatter.parse(someRandomResourceUrl, Resource.class);
  255. }
  256. @Test(expected = ConversionException.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(
  280. new ThemeResource(themePath),
  281. formatter.parse(ApplicationConstants.THEME_PROTOCOL_PREFIX
  282. + themePath, 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(
  290. new FileResource(new File(fileSystemPath)).getSourceFile(),
  291. formatter.parse(fileSystemPath, FileResource.class)
  292. .getSourceFile());
  293. }
  294. /**
  295. * A static method to allow comparison two different actions.
  296. *
  297. * @param act
  298. * One action to compare.
  299. * @param other
  300. * Second action to compare.
  301. * @return <b>true</b> when both actions are the same (caption, icon, and
  302. * key combination).
  303. */
  304. public static final boolean equals(ShortcutAction act, 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. }