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.

GridLayoutDeclarativeTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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.server.component.gridlayout;
  17. import java.io.ByteArrayInputStream;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.IOException;
  20. import org.junit.Assert;
  21. import org.junit.Test;
  22. import com.vaadin.shared.ui.ContentMode;
  23. import com.vaadin.shared.ui.MarginInfo;
  24. import com.vaadin.tests.server.component.DeclarativeMarginTestBase;
  25. import com.vaadin.ui.Alignment;
  26. import com.vaadin.ui.Button;
  27. import com.vaadin.ui.Component;
  28. import com.vaadin.ui.GridLayout;
  29. import com.vaadin.ui.Label;
  30. import com.vaadin.ui.declarative.Design;
  31. import com.vaadin.ui.declarative.DesignContext;
  32. public class GridLayoutDeclarativeTest
  33. extends DeclarativeMarginTestBase<GridLayout> {
  34. @Test
  35. public void testMargins() {
  36. testMargins("vaadin-grid-layout", new MarginInfo(false));
  37. }
  38. @Test
  39. public void testSimpleGridLayout() {
  40. Button b1 = new Button("Button 0,0");
  41. Button b2 = new Button("Button 0,1");
  42. Button b3 = new Button("Button 1,0");
  43. Button b4 = new Button("Button 1,1");
  44. b1.setCaptionAsHtml(true);
  45. b2.setCaptionAsHtml(true);
  46. b3.setCaptionAsHtml(true);
  47. b4.setCaptionAsHtml(true);
  48. String design = "<vaadin-grid-layout><row>" //
  49. + "<column expand=1.0>" + writeChild(b1) + "</column>" //
  50. + "<column expand=3.3>" + writeChild(b2) + "</column>" //
  51. + "</row><row>" //
  52. + "<column>" + writeChild(b3) + "</column>" //
  53. + "<column>" + writeChild(b4) + "</column>" //
  54. + "</row></vaadin-grid-layout>";
  55. GridLayout gl = new GridLayout(2, 2);
  56. gl.addComponent(b1);
  57. gl.addComponent(b2);
  58. gl.addComponent(b3);
  59. gl.addComponent(b4);
  60. gl.setColumnExpandRatio(0, 1f);
  61. gl.setColumnExpandRatio(1, 3.3f);
  62. testWrite(design, gl);
  63. testRead(design, gl);
  64. }
  65. @Test
  66. public void testReadIntegerExpandRatioGridLayout() {
  67. // To make sure that it can read from old declarative which use
  68. // integer expand ratio
  69. Button b1 = new Button("Button 0,0");
  70. b1.setCaptionAsHtml(true);
  71. String design = "<vaadin-grid-layout><row expand=3>" //
  72. + "<column expand=1>" + writeChild(b1) + "</column>" //
  73. + "</row>"//
  74. + "</vaadin-grid-layout>";
  75. GridLayout gl = new GridLayout(1, 1);
  76. gl.addComponent(b1);
  77. gl.setColumnExpandRatio(0, 1.0f);
  78. gl.setRowExpandRatio(0, 3.0f);
  79. testRead(design, gl);
  80. }
  81. @Test
  82. public void testOneBigComponentGridLayout() {
  83. Button b1 = new Button("Button 0,0 -&gt; 1,1");
  84. b1.setCaptionAsHtml(true);
  85. String design = "<vaadin-grid-layout><row>" //
  86. + "<column colspan=2 rowspan=2>" + writeChild(b1) + "</column>" //
  87. + "</row><row expand=2>" //
  88. + "</row></vaadin-grid-layout>";
  89. GridLayout gl = new GridLayout(2, 2);
  90. gl.addComponent(b1, 0, 0, 1, 1);
  91. gl.setRowExpandRatio(1, 2);
  92. testWrite(design, gl);
  93. testRead(design, gl);
  94. }
  95. @Test
  96. public void testMultipleSpannedComponentsGridLayout() {
  97. GridLayout gl = new GridLayout(5, 5);
  98. Button b1 = new Button("Button 0,0 -&gt; 0,2");
  99. b1.setCaptionAsHtml(true);
  100. gl.addComponent(b1, 0, 0, 2, 0);
  101. Button b2 = new Button("Button 0,3 -&gt; 3,3");
  102. b2.setCaptionAsHtml(true);
  103. gl.addComponent(b2, 3, 0, 3, 3);
  104. Button b3 = new Button("Button 0,4 -&gt; 1,4");
  105. b3.setCaptionAsHtml(true);
  106. gl.addComponent(b3, 4, 0, 4, 1);
  107. Button b4 = new Button("Button 1,0 -&gt; 3,1");
  108. b4.setCaptionAsHtml(true);
  109. gl.addComponent(b4, 0, 1, 1, 3);
  110. Button b5 = new Button("Button 2,2");
  111. b5.setCaptionAsHtml(true);
  112. gl.addComponent(b5, 2, 2);
  113. Button b6 = new Button("Button 3,4 -&gt; 4,4");
  114. b6.setCaptionAsHtml(true);
  115. gl.addComponent(b6, 4, 3, 4, 4);
  116. Button b7 = new Button("Button 4,1 -&gt; 4,2");
  117. b7.setCaptionAsHtml(true);
  118. gl.addComponent(b7, 2, 4, 3, 4);
  119. /*
  120. * Buttons in the GridLayout
  121. */
  122. // 1 1 1 2 3
  123. // 4 4 - 2 3
  124. // 4 4 5 2 -
  125. // 4 4 - 2 6
  126. // - - 7 7 6
  127. String design = "<vaadin-grid-layout><row>" //
  128. + "<column colspan=3>" + writeChild(b1) + "</column>" //
  129. + "<column rowspan=4>" + writeChild(b2) + "</column>" //
  130. + "<column rowspan=2>" + writeChild(b3) + "</column>" //
  131. + "</row><row>" //
  132. + "<column rowspan=3 colspan=2>" + writeChild(b4) + "</column>" //
  133. + "</row><row>" //
  134. + "<column>" + writeChild(b5) + "</column>" //
  135. + "</row><row>" //
  136. + "<column />" // Empty placeholder
  137. + "<column rowspan=2>" + writeChild(b6) + "</column>" //
  138. + "</row><row>" //
  139. + "<column colspan=2 />" // Empty placeholder
  140. + "<column colspan=2>" + writeChild(b7) + "</column>" //
  141. + "</row></vaadin-grid-layout>";
  142. testWrite(design, gl);
  143. testRead(design, gl);
  144. }
  145. @Test
  146. public void testManyExtraGridLayoutSlots() {
  147. GridLayout gl = new GridLayout(5, 5);
  148. Button b1 = new Button("Button 0,4 -&gt; 4,4");
  149. b1.setCaptionAsHtml(true);
  150. gl.addComponent(b1, 4, 0, 4, 4);
  151. gl.setColumnExpandRatio(2, 2.0f);
  152. String design = "<vaadin-grid-layout><row>" //
  153. + "<column colspan=4 rowspan=5 expand='0.0,0.0,2.0,0.0' />" //
  154. + "<column rowspan=5>" + writeChild(b1) + "</column>" //
  155. + "</row><row>" //
  156. + "</row><row>" //
  157. + "</row><row>" //
  158. + "</row><row>" //
  159. + "</row></vaadin-grid-layout>";
  160. testWrite(design, gl);
  161. testRead(design, gl);
  162. }
  163. @Test
  164. public void testManyEmptyColumnsWithOneExpand() {
  165. GridLayout gl = new GridLayout(5, 5);
  166. Button b1 = new Button("Button 0,4 -&gt; 4,4");
  167. b1.setCaptionAsHtml(true);
  168. gl.addComponent(b1, 0, 0, 0, 4);
  169. gl.setColumnExpandRatio(4, 2.0f);
  170. String design = "<vaadin-grid-layout><row>" //
  171. + "<column rowspan=5>" + writeChild(b1) + "</column>" //
  172. + "<column colspan=4 rowspan=5 expand='0.0,0.0,0.0,2.0' />" //
  173. + "</row><row>" //
  174. + "</row><row>" //
  175. + "</row><row>" //
  176. + "</row><row>" //
  177. + "</row></vaadin-grid-layout>";
  178. testWrite(design, gl);
  179. testRead(design, gl);
  180. }
  181. @Test
  182. public void testEmptyGridLayout() {
  183. GridLayout gl = new GridLayout();
  184. String design = "<vaadin-grid-layout />";
  185. testWrite(design, gl);
  186. testRead(design, gl);
  187. }
  188. private String writeChild(Component childComponent) {
  189. return new DesignContext().createElement(childComponent).toString();
  190. }
  191. @Override
  192. public GridLayout testRead(String design, GridLayout expected) {
  193. expected.setCursorX(0);
  194. expected.setCursorY(expected.getRows());
  195. GridLayout result = super.testRead(design, expected);
  196. for (int row = 0; row < expected.getRows(); ++row) {
  197. Assert.assertEquals(expected.getRowExpandRatio(row),
  198. result.getRowExpandRatio(row), 0.00001);
  199. }
  200. for (int col = 0; col < expected.getColumns(); ++col) {
  201. Assert.assertEquals(expected.getColumnExpandRatio(col),
  202. result.getColumnExpandRatio(col), 0.00001);
  203. }
  204. for (int row = 0; row < expected.getRows(); ++row) {
  205. for (int col = 0; col < expected.getColumns(); ++col) {
  206. Component eC = expected.getComponent(col, row);
  207. Component rC = result.getComponent(col, row);
  208. assertEquals(eC, rC);
  209. if (eC == null) {
  210. continue;
  211. }
  212. Assert.assertEquals(expected.getComponentAlignment(eC),
  213. result.getComponentAlignment(rC));
  214. }
  215. }
  216. return result;
  217. }
  218. @Test
  219. public void testNestedGridLayouts() {
  220. String design = "<!DOCTYPE html>" + //
  221. "<html>" + //
  222. " <body> " + //
  223. " <vaadin-grid-layout> " + //
  224. " <row> " + //
  225. " <column> " + //
  226. " <vaadin-grid-layout> " + //
  227. " <row> " + //
  228. " <column> " + //
  229. " <vaadin-button>" + //
  230. " Button " + //
  231. " </vaadin-button> " + //
  232. " </column> " + //
  233. " </row> " + //
  234. " </vaadin-grid-layout> " + //
  235. " </column> " + //
  236. " </row> " + //
  237. " </vaadin-grid-layout> " + //
  238. " </body>" + //
  239. "</html>";
  240. GridLayout outer = new GridLayout();
  241. GridLayout inner = new GridLayout();
  242. Button b = new Button("Button");
  243. b.setCaptionAsHtml(true);
  244. inner.addComponent(b);
  245. outer.addComponent(inner);
  246. testRead(design, outer);
  247. testWrite(design, outer);
  248. }
  249. @Test
  250. public void testEmptyGridLayoutWithColsAndRowsSet() throws IOException {
  251. GridLayout layout = new GridLayout();
  252. layout.setRows(2);
  253. layout.setColumns(2);
  254. ByteArrayOutputStream out = new ByteArrayOutputStream();
  255. DesignContext context = new DesignContext();
  256. context.setRootComponent(layout);
  257. Design.write(context, out);
  258. ByteArrayInputStream input = new ByteArrayInputStream(
  259. out.toByteArray());
  260. Component component = Design.read(input);
  261. GridLayout readLayout = (GridLayout) component;
  262. Assert.assertEquals(layout.getRows(), readLayout.getRows());
  263. }
  264. @Test
  265. public void testGridLayoutAlignments() {
  266. String design = "<vaadin-grid-layout><row>" //
  267. + "<column><vaadin-label :middle>0</label></column>"//
  268. + "<column><vaadin-label :right>1</label>"//
  269. + "</row><row>" //
  270. + "<column><vaadin-label :bottom :center>2</label></column>"//
  271. + "<column><vaadin-label :middle :center>3</label>" //
  272. + "</row></vaadin-grid-layout>";
  273. GridLayout gl = new GridLayout(2, 2);
  274. Alignment[] alignments = { Alignment.MIDDLE_LEFT, Alignment.TOP_RIGHT,
  275. Alignment.BOTTOM_CENTER, Alignment.MIDDLE_CENTER };
  276. for (int i = 0; i < 4; i++) {
  277. Label child = new Label("" + i, ContentMode.HTML);
  278. gl.addComponent(child);
  279. gl.setComponentAlignment(child, alignments[i]);
  280. }
  281. testWrite(design, gl);
  282. testRead(design, gl);
  283. }
  284. @Test
  285. public void testGridLayoutMargins() throws IOException {
  286. String design = "<vaadin-grid-layout _id=\"marginComponent\"margin>"
  287. + "<row><column><vaadin-grid-layout _id=\"marginLeftComponent\" margin-left></vaadin-grid-layout></column></row>"
  288. + "<row><column><vaadin-grid-layout _id=\"marginRightComponent\" margin-right></vaadin-grid-layout></column></row>"
  289. + "<row><column><vaadin-grid-layout _id=\"marginTopComponent\" margin-top></vaadin-grid-layout></column></row>"
  290. + "<row><column><vaadin-grid-layout _id=\"marginBottomComponent\" margin-bottom></vaadin-grid-layout></column></row>"
  291. + "</vaadin-grid-layout>";
  292. DesignContext context = Design
  293. .read(new ByteArrayInputStream(design.getBytes("UTF-8")), null);
  294. Assert.assertEquals(null, context.getCustomAttributes(
  295. context.getComponentByLocalId("marginComponent")));
  296. Assert.assertEquals(null, context.getCustomAttributes(
  297. context.getComponentByLocalId("marginLeftComponent")));
  298. Assert.assertEquals(null, context.getCustomAttributes(
  299. context.getComponentByLocalId("marginRightComponent")));
  300. Assert.assertEquals(null, context.getCustomAttributes(
  301. context.getComponentByLocalId("marginTopComponent")));
  302. Assert.assertEquals(null, context.getCustomAttributes(
  303. context.getComponentByLocalId("marginBottomComponent")));
  304. }
  305. @Test
  306. public void designWithPreconfiguredGridLayout() throws Exception {
  307. String design = "<html>" //
  308. + "<head>" //
  309. + "<meta name='package-mapping' content='my:com.vaadin.tests.server.component.gridlayout'>"
  310. + "</meta>" + "</head>" + "<body>"
  311. + "<my-preconfigured-grid-layout></my-preconfigured-grid-layout>";
  312. PreconfiguredGridLayout myLayout = (PreconfiguredGridLayout) Design
  313. .read(new ByteArrayInputStream(design.getBytes("UTF-8")));
  314. Assert.assertEquals(2, myLayout.getRows());
  315. Assert.assertEquals(2, myLayout.getColumns());
  316. }
  317. }