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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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.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.label.ContentMode;
  23. import com.vaadin.tests.server.component.DeclarativeMarginTestBase;
  24. import com.vaadin.ui.Alignment;
  25. import com.vaadin.ui.Button;
  26. import com.vaadin.ui.Component;
  27. import com.vaadin.ui.GridLayout;
  28. import com.vaadin.ui.Label;
  29. import com.vaadin.ui.declarative.Design;
  30. import com.vaadin.ui.declarative.DesignContext;
  31. public class GridLayoutDeclarativeTest extends
  32. DeclarativeMarginTestBase<GridLayout> {
  33. @Test
  34. public void testMargins() {
  35. testMargins("vaadin-grid-layout");
  36. }
  37. @Test
  38. public void testSimpleGridLayout() {
  39. Button b1 = new Button("Button 0,0");
  40. Button b2 = new Button("Button 0,1");
  41. Button b3 = new Button("Button 1,0");
  42. Button b4 = new Button("Button 1,1");
  43. b1.setCaptionAsHtml(true);
  44. b2.setCaptionAsHtml(true);
  45. b3.setCaptionAsHtml(true);
  46. b4.setCaptionAsHtml(true);
  47. String design = "<vaadin-grid-layout><row>" //
  48. + "<column expand=1>" + writeChild(b1) + "</column>" //
  49. + "<column expand=3>" + writeChild(b2) + "</column>" //
  50. + "</row><row>" //
  51. + "<column>" + writeChild(b3) + "</column>" //
  52. + "<column>" + writeChild(b4) + "</column>" //
  53. + "</row></vaadin-grid-layout>";
  54. GridLayout gl = new GridLayout(2, 2);
  55. gl.addComponent(b1);
  56. gl.addComponent(b2);
  57. gl.addComponent(b3);
  58. gl.addComponent(b4);
  59. gl.setColumnExpandRatio(0, 1.0f);
  60. gl.setColumnExpandRatio(1, 3.0f);
  61. testWrite(design, gl);
  62. testRead(design, gl);
  63. }
  64. @Test
  65. public void testOneBigComponentGridLayout() {
  66. Button b1 = new Button("Button 0,0 -> 1,1");
  67. b1.setCaptionAsHtml(true);
  68. String design = "<vaadin-grid-layout><row>" //
  69. + "<column colspan=2 rowspan=2>" + writeChild(b1) + "</column>" //
  70. + "</row><row expand=2>" //
  71. + "</row></vaadin-grid-layout>";
  72. GridLayout gl = new GridLayout(2, 2);
  73. gl.addComponent(b1, 0, 0, 1, 1);
  74. gl.setRowExpandRatio(1, 2);
  75. testWrite(design, gl);
  76. testRead(design, gl);
  77. }
  78. @Test
  79. public void testMultipleSpannedComponentsGridLayout() {
  80. GridLayout gl = new GridLayout(5, 5);
  81. Button b1 = new Button("Button 0,0 -> 0,2");
  82. b1.setCaptionAsHtml(true);
  83. gl.addComponent(b1, 0, 0, 2, 0);
  84. Button b2 = new Button("Button 0,3 -> 3,3");
  85. b2.setCaptionAsHtml(true);
  86. gl.addComponent(b2, 3, 0, 3, 3);
  87. Button b3 = new Button("Button 0,4 -> 1,4");
  88. b3.setCaptionAsHtml(true);
  89. gl.addComponent(b3, 4, 0, 4, 1);
  90. Button b4 = new Button("Button 1,0 -> 3,1");
  91. b4.setCaptionAsHtml(true);
  92. gl.addComponent(b4, 0, 1, 1, 3);
  93. Button b5 = new Button("Button 2,2");
  94. b5.setCaptionAsHtml(true);
  95. gl.addComponent(b5, 2, 2);
  96. Button b6 = new Button("Button 3,4 -> 4,4");
  97. b6.setCaptionAsHtml(true);
  98. gl.addComponent(b6, 4, 3, 4, 4);
  99. Button b7 = new Button("Button 4,1 -> 4,2");
  100. b7.setCaptionAsHtml(true);
  101. gl.addComponent(b7, 2, 4, 3, 4);
  102. /*
  103. * Buttons in the GridLayout
  104. */
  105. // 1 1 1 2 3
  106. // 4 4 - 2 3
  107. // 4 4 5 2 -
  108. // 4 4 - 2 6
  109. // - - 7 7 6
  110. String design = "<vaadin-grid-layout><row>" //
  111. + "<column colspan=3>" + writeChild(b1) + "</column>" //
  112. + "<column rowspan=4>" + writeChild(b2) + "</column>" //
  113. + "<column rowspan=2>" + writeChild(b3) + "</column>" //
  114. + "</row><row>" //
  115. + "<column rowspan=3 colspan=2>" + writeChild(b4) + "</column>" //
  116. + "</row><row>" //
  117. + "<column>" + writeChild(b5) + "</column>" //
  118. + "</row><row>" //
  119. + "<column />" // Empty placeholder
  120. + "<column rowspan=2>" + writeChild(b6) + "</column>" //
  121. + "</row><row>" //
  122. + "<column colspan=2 />" // Empty placeholder
  123. + "<column colspan=2>" + writeChild(b7) + "</column>" //
  124. + "</row></vaadin-grid-layout>";
  125. testWrite(design, gl);
  126. testRead(design, gl);
  127. }
  128. @Test
  129. public void testManyExtraGridLayoutSlots() {
  130. GridLayout gl = new GridLayout(5, 5);
  131. Button b1 = new Button("Button 0,4 -> 4,4");
  132. b1.setCaptionAsHtml(true);
  133. gl.addComponent(b1, 4, 0, 4, 4);
  134. gl.setColumnExpandRatio(2, 2.0f);
  135. String design = "<vaadin-grid-layout><row>" //
  136. + "<column colspan=4 rowspan=5 expand='0,0,2,0' />" //
  137. + "<column rowspan=5>" + writeChild(b1) + "</column>" //
  138. + "</row><row>" //
  139. + "</row><row>" //
  140. + "</row><row>" //
  141. + "</row><row>" //
  142. + "</row></vaadin-grid-layout>";
  143. testWrite(design, gl);
  144. testRead(design, gl);
  145. }
  146. @Test
  147. public void testManyEmptyColumnsWithOneExpand() {
  148. GridLayout gl = new GridLayout(5, 5);
  149. Button b1 = new Button("Button 0,4 -> 4,4");
  150. b1.setCaptionAsHtml(true);
  151. gl.addComponent(b1, 0, 0, 0, 4);
  152. gl.setColumnExpandRatio(4, 2.0f);
  153. String design = "<vaadin-grid-layout><row>" //
  154. + "<column rowspan=5>" + writeChild(b1) + "</column>" //
  155. + "<column colspan=4 rowspan=5 expand='0,0,0,2' />" //
  156. + "</row><row>" //
  157. + "</row><row>" //
  158. + "</row><row>" //
  159. + "</row><row>" //
  160. + "</row></vaadin-grid-layout>";
  161. testWrite(design, gl);
  162. testRead(design, gl);
  163. }
  164. @Test
  165. public void testEmptyGridLayout() {
  166. GridLayout gl = new GridLayout();
  167. String design = "<vaadin-grid-layout />";
  168. testWrite(design, gl);
  169. testRead(design, gl);
  170. }
  171. private String writeChild(Component childComponent) {
  172. return new DesignContext().createElement(childComponent).toString();
  173. }
  174. @Override
  175. public GridLayout testRead(String design, GridLayout expected) {
  176. expected.setCursorX(0);
  177. expected.setCursorY(expected.getRows());
  178. GridLayout result = super.testRead(design, expected);
  179. for (int row = 0; row < expected.getRows(); ++row) {
  180. Assert.assertTrue(Math.abs(expected.getRowExpandRatio(row)
  181. - result.getRowExpandRatio(row)) < 0.00001);
  182. }
  183. for (int col = 0; col < expected.getColumns(); ++col) {
  184. Assert.assertTrue(Math.abs(expected.getColumnExpandRatio(col)
  185. - result.getColumnExpandRatio(col)) < 0.00001);
  186. }
  187. return result;
  188. }
  189. @Test
  190. public void testNestedGridLayouts() {
  191. String design = "<!DOCTYPE html>" + //
  192. "<html>" + //
  193. " <body> " + //
  194. " <vaadin-grid-layout> " + //
  195. " <row> " + //
  196. " <column> " + //
  197. " <vaadin-grid-layout> " + //
  198. " <row> " + //
  199. " <column> " + //
  200. " <vaadin-button>" + //
  201. " Button " + //
  202. " </vaadin-button> " + //
  203. " </column> " + //
  204. " </row> " + //
  205. " </vaadin-grid-layout> " + //
  206. " </column> " + //
  207. " </row> " + //
  208. " </vaadin-grid-layout> " + //
  209. " </body>" + //
  210. "</html>";
  211. GridLayout outer = new GridLayout();
  212. GridLayout inner = new GridLayout();
  213. Button b = new Button("Button");
  214. b.setCaptionAsHtml(true);
  215. inner.addComponent(b);
  216. outer.addComponent(inner);
  217. testRead(design, outer);
  218. testWrite(design, outer);
  219. }
  220. @Test
  221. public void testEmptyGridLayoutWithColsAndRowsSet() throws IOException {
  222. GridLayout layout = new GridLayout();
  223. layout.setRows(2);
  224. layout.setColumns(2);
  225. ByteArrayOutputStream out = new ByteArrayOutputStream();
  226. DesignContext context = new DesignContext();
  227. context.setRootComponent(layout);
  228. Design.write(context, out);
  229. ByteArrayInputStream input = new ByteArrayInputStream(out.toByteArray());
  230. Component component = Design.read(input);
  231. GridLayout readLayout = (GridLayout) component;
  232. Assert.assertEquals(layout.getRows(), readLayout.getRows());
  233. }
  234. @Test
  235. public void testGridLayoutAlignments() {
  236. String design = "<vaadin-grid-layout><row>" //
  237. + "<column><vaadin-label :middle>0</label></column>"//
  238. + "<column><vaadin-label :right>1</label>"//
  239. + "</row><row>" //
  240. + "<column><vaadin-label :bottom :center>2</label></column>"//
  241. + "<column><vaadin-label :middle :center>3</label>" //
  242. + "</row></vaadin-grid-layout>";
  243. GridLayout gl = new GridLayout(2, 2);
  244. Alignment[] alignments = { Alignment.MIDDLE_LEFT, Alignment.TOP_RIGHT,
  245. Alignment.BOTTOM_CENTER, Alignment.MIDDLE_CENTER };
  246. for (int i = 0; i < 4; i++) {
  247. Label child = new Label("" + i, ContentMode.HTML);
  248. gl.addComponent(child);
  249. gl.setComponentAlignment(child, alignments[i]);
  250. }
  251. testWrite(design, gl);
  252. testRead(design, gl);
  253. }
  254. }