2 * Copyright 2000-2016 Vaadin Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
16 package com.vaadin.v7.tests.server.component.grid.declarative;
18 import org.jsoup.nodes.Element;
19 import org.jsoup.parser.Tag;
20 import org.junit.Assert;
21 import org.junit.Test;
23 import com.vaadin.shared.ui.ContentMode;
24 import com.vaadin.ui.Label;
25 import com.vaadin.ui.declarative.DesignContext;
26 import com.vaadin.v7.ui.Grid;
27 import com.vaadin.v7.ui.Grid.Column;
28 import com.vaadin.v7.ui.Grid.FooterRow;
29 import com.vaadin.v7.ui.Grid.HeaderRow;
31 public class GridHeaderFooterDeclarativeTest extends GridDeclarativeTestBase {
34 public void testSingleDefaultHeader() {
36 String design = "<vaadin7-grid><table>"
38 + " <col sortable property-id='Column1'>"
39 + " <col sortable property-id='Column2'>"
40 + " <col sortable property-id='Column3'>"
43 + " <tr default><th plain-text>Column1<th plain-text>Column2<th plain-text>Column3</tr>"
45 + "</table></vaadin7-grid>";
47 Grid grid = new Grid();
48 grid.addColumn("Column1", String.class);
49 grid.addColumn("Column2", String.class);
50 grid.addColumn("Column3", String.class);
52 testWrite(design, grid);
53 testRead(design, grid, true);
57 public void testSingleDefaultHTMLHeader() {
59 String design = "<vaadin7-grid><table>"
61 + " <col sortable property-id='Column1'>"
62 + " <col sortable property-id='Column2'>"
63 + " <col sortable property-id='Column3'>" + "</colgroup>"
65 + " <tr default><th>Column1<th>Column2<th>Column3</tr>"
67 + "</table></vaadin7-grid>";
69 Grid grid = new Grid();
70 grid.addColumn("Column1", String.class);
71 grid.addColumn("Column2", String.class);
72 grid.addColumn("Column3", String.class);
74 HeaderRow row = grid.getDefaultHeaderRow();
75 for (Column c : grid.getColumns()) {
76 row.getCell(c.getPropertyId()).setHtml(c.getHeaderCaption());
79 testWrite(design, grid);
80 testRead(design, grid, true);
84 public void testNoHeaderRows() {
86 String design = "<vaadin7-grid><table>"
88 + " <col sortable property-id='Column1'>"
91 + "</table></vaadin7-grid>";
93 Grid grid = new Grid();
94 grid.addColumn("Column1", String.class);
95 grid.removeHeaderRow(grid.getDefaultHeaderRow());
97 testWrite(design, grid);
98 testRead(design, grid, true);
102 public void testMultipleHeadersWithColSpans() {
104 String design = "<vaadin7-grid><table>"
106 + " <col sortable property-id='Column1'>"
107 + " <col sortable property-id='Column2'>"
108 + " <col sortable property-id='Column3'>"
111 + " <tr><th colspan=3>Baz</tr>"
112 + " <tr default><th>Column1<th>Column2<th>Column3</tr>"
113 + " <tr><th>Foo<th colspan=2>Bar</tr>"
115 + "</table></vaadin7-grid>";
117 Grid grid = new Grid();
118 grid.addColumn("Column1", String.class);
119 grid.addColumn("Column2", String.class);
120 grid.addColumn("Column3", String.class);
122 HeaderRow row = grid.getDefaultHeaderRow();
123 for (Column c : grid.getColumns()) {
124 row.getCell(c.getPropertyId()).setHtml(c.getHeaderCaption());
127 grid.prependHeaderRow().join("Column1", "Column2", "Column3")
129 row = grid.appendHeaderRow();
130 row.getCell("Column1").setHtml("Foo");
131 row.join("Column2", "Column3").setHtml("Bar");
133 testWrite(design, grid);
134 testRead(design, grid, true);
138 public void testSingleDefaultFooter() {
140 String design = "<vaadin7-grid><table>"
142 + " <col sortable property-id='Column1'>"
143 + " <col sortable property-id='Column2'>"
144 + " <col sortable property-id='Column3'>"
146 + "<thead />" // No headers read or written
148 + " <tr><td plain-text>Column1<td plain-text>Column2<td plain-text>Column3</tr>"
150 + "</table></vaadin7-grid>";
152 Grid grid = new Grid();
153 grid.addColumn("Column1", String.class);
154 grid.addColumn("Column2", String.class);
155 grid.addColumn("Column3", String.class);
157 FooterRow row = grid.appendFooterRow();
158 for (Column c : grid.getColumns()) {
159 row.getCell(c.getPropertyId()).setText(c.getHeaderCaption());
162 grid.removeHeaderRow(grid.getDefaultHeaderRow());
164 testWrite(design, grid);
165 testRead(design, grid, true);
169 public void testSingleDefaultHTMLFooter() {
171 String design = "<vaadin7-grid><table>"
173 + " <col sortable property-id='Column1'>"
174 + " <col sortable property-id='Column2'>"
175 + " <col sortable property-id='Column3'>" + "</colgroup>"
176 + "<thead />" // No headers read or written
178 + " <tr><td>Column1<td>Column2<td>Column3</tr>"
180 + "</table></vaadin7-grid>";
183 Grid grid = new Grid();
184 grid.addColumn("Column1", String.class);
185 grid.addColumn("Column2", String.class);
186 grid.addColumn("Column3", String.class);
188 FooterRow row = grid.appendFooterRow();
189 for (Column c : grid.getColumns()) {
190 row.getCell(c.getPropertyId()).setHtml(c.getHeaderCaption());
193 grid.removeHeaderRow(grid.getDefaultHeaderRow());
195 testWrite(design, grid);
196 testRead(design, grid, true);
200 public void testMultipleFootersWithColSpans() {
202 String design = "<vaadin7-grid><table>"
204 + " <col sortable property-id='Column1'>"
205 + " <col sortable property-id='Column2'>"
206 + " <col sortable property-id='Column3'>"
208 + "<thead />" // No headers read or written.
210 + " <tr><td colspan=3>Baz</tr>"
211 + " <tr><td>Column1<td>Column2<td>Column3</tr>"
212 + " <tr><td>Foo<td colspan=2>Bar</tr>"
214 + "</table></vaadin7-grid>";
217 Grid grid = new Grid();
218 grid.addColumn("Column1", String.class);
219 grid.addColumn("Column2", String.class);
220 grid.addColumn("Column3", String.class);
222 FooterRow row = grid.appendFooterRow();
223 for (Column c : grid.getColumns()) {
224 row.getCell(c.getPropertyId()).setHtml(c.getHeaderCaption());
227 grid.prependFooterRow().join("Column1", "Column2", "Column3")
229 row = grid.appendFooterRow();
230 row.getCell("Column1").setHtml("Foo");
231 row.join("Column2", "Column3").setHtml("Bar");
233 grid.removeHeaderRow(grid.getDefaultHeaderRow());
235 testWrite(design, grid);
236 testRead(design, grid, true);
240 public void testComponentInGridHeader() {
242 String design = "<vaadin7-grid><table>"
244 + " <col sortable property-id='Column1'>"
247 + "<tr default><th><vaadin-label><b>Foo</b></vaadin-label></tr>"
249 + "</table></vaadin7-grid>";
251 Label component = new Label("<b>Foo</b>");
252 component.setContentMode(ContentMode.HTML);
254 Grid grid = new Grid();
255 grid.addColumn("Column1", String.class);
256 grid.getDefaultHeaderRow().getCell("Column1").setComponent(component);
258 testRead(design, grid, true);
259 testWrite(design, grid);
263 public void testComponentInGridFooter() {
265 String design = "<vaadin7-grid><table>"
267 + " <col sortable property-id='Column1'>"
269 + "<thead />" // No headers read or written
271 + "<tr><td><vaadin-label><b>Foo</b></vaadin-label></tr>"
273 + "</table></vaadin7-grid>";
276 Label component = new Label("<b>Foo</b>");
277 component.setContentMode(ContentMode.HTML);
279 Grid grid = new Grid();
280 grid.addColumn("Column1", String.class);
281 grid.prependFooterRow().getCell("Column1").setComponent(component);
282 grid.removeHeaderRow(grid.getDefaultHeaderRow());
284 testRead(design, grid, true);
285 testWrite(design, grid);
289 public void testHtmlEntitiesinGridHeaderFooter() {
291 String design = "<vaadin7-grid><table>"
293 + " <col sortable=\"true\" property-id=\"> test\">"
296 + " <tr><th plain-text=\"true\">> Test</th></tr>"
299 + " <tr><td plain-text=\"true\">> Test</td></tr>"
302 + "</table></vaadin7-grid>";
305 Grid grid = read(design);
306 String actualHeader = grid.getHeaderRow(0).getCell("> test").getText();
307 String actualFooter = grid.getFooterRow(0).getCell("> test").getText();
308 String expected = "> Test";
310 assertEquals(expected, actualHeader);
311 assertEquals(expected, actualFooter);
313 design = design.replace("plain-text=\"true\"", "");
315 actualHeader = grid.getHeaderRow(0).getCell("> test").getHtml();
316 actualFooter = grid.getFooterRow(0).getCell("> test").getHtml();
317 expected = "> Test";
319 assertEquals(expected, actualHeader);
320 assertEquals(expected, actualFooter);
323 grid.setColumns("test");
324 HeaderRow header = grid.addHeaderRowAt(0);
325 FooterRow footer = grid.addFooterRowAt(0);
326 grid.removeHeaderRow(grid.getDefaultHeaderRow());
328 // entities should be encoded when writing back, not interpreted as HTML
329 header.getCell("test").setText("& Test");
330 footer.getCell("test").setText("& Test");
332 Element root = new Element(Tag.valueOf("vaadin7-grid"), "");
333 grid.writeDesign(root, new DesignContext());
335 assertEquals("&amp; Test",
336 root.getElementsByTag("th").get(0).html());
337 assertEquals("&amp; Test",
338 root.getElementsByTag("td").get(0).html());
340 header = grid.addHeaderRowAt(0);
341 footer = grid.addFooterRowAt(0);
343 // entities should not be encoded, this is already given as HTML
344 header.getCell("test").setHtml("& Test");
345 footer.getCell("test").setHtml("& Test");
347 root = new Element(Tag.valueOf("vaadin7-grid"), "");
348 grid.writeDesign(root, new DesignContext());
350 assertEquals("& Test",
351 root.getElementsByTag("th").get(0).html());
352 assertEquals("& Test",
353 root.getElementsByTag("td").get(0).html());