]> source.dussan.org Git - vaadin-framework.git/blob
87159d0024c53052791e022747dd07c597e4cb97
[vaadin-framework.git] /
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.v7.tests.server.component.grid.declarative;
17
18 import org.jsoup.nodes.Element;
19 import org.jsoup.parser.Tag;
20 import org.junit.Assert;
21 import org.junit.Test;
22
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;
30
31 public class GridHeaderFooterDeclarativeTest extends GridDeclarativeTestBase {
32
33     @Test
34     public void testSingleDefaultHeader() {
35         //@formatter:off
36         String design = "<vaadin7-grid><table>"
37                 + "<colgroup>"
38                 + "   <col sortable property-id='Column1'>"
39                 + "   <col sortable property-id='Column2'>"
40                 + "   <col sortable property-id='Column3'>"
41                 + "</colgroup>"
42                 + "<thead>"
43                 + "   <tr default><th plain-text>Column1<th plain-text>Column2<th plain-text>Column3</tr>"
44                 + "</thead>"
45                 + "</table></vaadin7-grid>";
46         //@formatter:on
47         Grid grid = new Grid();
48         grid.addColumn("Column1", String.class);
49         grid.addColumn("Column2", String.class);
50         grid.addColumn("Column3", String.class);
51
52         testWrite(design, grid);
53         testRead(design, grid, true);
54     }
55
56     @Test
57     public void testSingleDefaultHTMLHeader() {
58         //@formatter:off
59         String design = "<vaadin7-grid><table>"
60                 + "<colgroup>"
61                 + "   <col sortable property-id='Column1'>"
62                 + "   <col sortable property-id='Column2'>"
63                 + "   <col sortable property-id='Column3'>" + "</colgroup>"
64                 + "<thead>"
65                 + "   <tr default><th>Column1<th>Column2<th>Column3</tr>"
66                 + "</thead>"
67                 + "</table></vaadin7-grid>";
68         //@formatter:on
69         Grid grid = new Grid();
70         grid.addColumn("Column1", String.class);
71         grid.addColumn("Column2", String.class);
72         grid.addColumn("Column3", String.class);
73
74         HeaderRow row = grid.getDefaultHeaderRow();
75         for (Column c : grid.getColumns()) {
76             row.getCell(c.getPropertyId()).setHtml(c.getHeaderCaption());
77         }
78
79         testWrite(design, grid);
80         testRead(design, grid, true);
81     }
82
83     @Test
84     public void testNoHeaderRows() {
85         //@formatter:off
86         String design = "<vaadin7-grid><table>"
87                 + "<colgroup>"
88                 + "   <col sortable property-id='Column1'>"
89                 + "</colgroup>"
90                 + "<thead />"
91                 + "</table></vaadin7-grid>";
92         //@formatter:on
93         Grid grid = new Grid();
94         grid.addColumn("Column1", String.class);
95         grid.removeHeaderRow(grid.getDefaultHeaderRow());
96
97         testWrite(design, grid);
98         testRead(design, grid, true);
99     }
100
101     @Test
102     public void testMultipleHeadersWithColSpans() {
103         //@formatter:off
104         String design = "<vaadin7-grid><table>"
105                 + "<colgroup>"
106                 + "   <col sortable property-id='Column1'>"
107                 + "   <col sortable property-id='Column2'>"
108                 + "   <col sortable property-id='Column3'>"
109                 + "</colgroup>"
110                 + "<thead>"
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>"
114                 + "</thead>"
115                 + "</table></vaadin7-grid>";
116         //@formatter:on
117         Grid grid = new Grid();
118         grid.addColumn("Column1", String.class);
119         grid.addColumn("Column2", String.class);
120         grid.addColumn("Column3", String.class);
121
122         HeaderRow row = grid.getDefaultHeaderRow();
123         for (Column c : grid.getColumns()) {
124             row.getCell(c.getPropertyId()).setHtml(c.getHeaderCaption());
125         }
126
127         grid.prependHeaderRow().join("Column1", "Column2", "Column3")
128                 .setHtml("Baz");
129         row = grid.appendHeaderRow();
130         row.getCell("Column1").setHtml("Foo");
131         row.join("Column2", "Column3").setHtml("Bar");
132
133         testWrite(design, grid);
134         testRead(design, grid, true);
135     }
136
137     @Test
138     public void testSingleDefaultFooter() {
139         //@formatter:off
140         String design = "<vaadin7-grid><table>"
141                 + "<colgroup>"
142                 + "   <col sortable property-id='Column1'>"
143                 + "   <col sortable property-id='Column2'>"
144                 + "   <col sortable property-id='Column3'>"
145                 + "</colgroup>"
146                 + "<thead />" // No headers read or written
147                 + "<tfoot>"
148                 + "   <tr><td plain-text>Column1<td plain-text>Column2<td plain-text>Column3</tr>"
149                 + "</tfoot>"
150                 + "</table></vaadin7-grid>";
151         //@formatter:on
152         Grid grid = new Grid();
153         grid.addColumn("Column1", String.class);
154         grid.addColumn("Column2", String.class);
155         grid.addColumn("Column3", String.class);
156
157         FooterRow row = grid.appendFooterRow();
158         for (Column c : grid.getColumns()) {
159             row.getCell(c.getPropertyId()).setText(c.getHeaderCaption());
160         }
161
162         grid.removeHeaderRow(grid.getDefaultHeaderRow());
163
164         testWrite(design, grid);
165         testRead(design, grid, true);
166     }
167
168     @Test
169     public void testSingleDefaultHTMLFooter() {
170         //@formatter:off
171         String design = "<vaadin7-grid><table>"
172                 + "<colgroup>"
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
177                 + "<tfoot>"
178                 + "   <tr><td>Column1<td>Column2<td>Column3</tr>"
179                 + "</tfoot>"
180                 + "</table></vaadin7-grid>";
181         //@formatter:on
182
183         Grid grid = new Grid();
184         grid.addColumn("Column1", String.class);
185         grid.addColumn("Column2", String.class);
186         grid.addColumn("Column3", String.class);
187
188         FooterRow row = grid.appendFooterRow();
189         for (Column c : grid.getColumns()) {
190             row.getCell(c.getPropertyId()).setHtml(c.getHeaderCaption());
191         }
192
193         grid.removeHeaderRow(grid.getDefaultHeaderRow());
194
195         testWrite(design, grid);
196         testRead(design, grid, true);
197     }
198
199     @Test
200     public void testMultipleFootersWithColSpans() {
201         //@formatter:off
202         String design = "<vaadin7-grid><table>"
203                 + "<colgroup>"
204                 + "   <col sortable property-id='Column1'>"
205                 + "   <col sortable property-id='Column2'>"
206                 + "   <col sortable property-id='Column3'>"
207                 + "</colgroup>"
208                 + "<thead />" // No headers read or written.
209                 + "<tfoot>"
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>"
213                 + "</tfoot>"
214                 + "</table></vaadin7-grid>";
215         //@formatter:on
216
217         Grid grid = new Grid();
218         grid.addColumn("Column1", String.class);
219         grid.addColumn("Column2", String.class);
220         grid.addColumn("Column3", String.class);
221
222         FooterRow row = grid.appendFooterRow();
223         for (Column c : grid.getColumns()) {
224             row.getCell(c.getPropertyId()).setHtml(c.getHeaderCaption());
225         }
226
227         grid.prependFooterRow().join("Column1", "Column2", "Column3")
228                 .setHtml("Baz");
229         row = grid.appendFooterRow();
230         row.getCell("Column1").setHtml("Foo");
231         row.join("Column2", "Column3").setHtml("Bar");
232
233         grid.removeHeaderRow(grid.getDefaultHeaderRow());
234
235         testWrite(design, grid);
236         testRead(design, grid, true);
237     }
238
239     @Test
240     public void testComponentInGridHeader() {
241         //@formatter:off
242         String design = "<vaadin7-grid><table>"
243                 + "<colgroup>"
244                 + "   <col sortable property-id='Column1'>"
245                 + "</colgroup>"
246                 + "<thead>"
247                 + "<tr default><th><vaadin-label><b>Foo</b></vaadin-label></tr>"
248                 + "</thead>"
249                 + "</table></vaadin7-grid>";
250         //@formatter:on
251         Label component = new Label("<b>Foo</b>");
252         component.setContentMode(ContentMode.HTML);
253
254         Grid grid = new Grid();
255         grid.addColumn("Column1", String.class);
256         grid.getDefaultHeaderRow().getCell("Column1").setComponent(component);
257
258         testRead(design, grid, true);
259         testWrite(design, grid);
260     }
261
262     @Test
263     public void testComponentInGridFooter() {
264         //@formatter:off
265         String design = "<vaadin7-grid><table>"
266                 + "<colgroup>"
267                 + "   <col sortable property-id='Column1'>"
268                 + "</colgroup>"
269                 + "<thead />" // No headers read or written
270                 + "<tfoot>"
271                 + "<tr><td><vaadin-label><b>Foo</b></vaadin-label></tr>"
272                 + "</tfoot>"
273                 + "</table></vaadin7-grid>";
274         //@formatter:on
275
276         Label component = new Label("<b>Foo</b>");
277         component.setContentMode(ContentMode.HTML);
278
279         Grid grid = new Grid();
280         grid.addColumn("Column1", String.class);
281         grid.prependFooterRow().getCell("Column1").setComponent(component);
282         grid.removeHeaderRow(grid.getDefaultHeaderRow());
283
284         testRead(design, grid, true);
285         testWrite(design, grid);
286     }
287
288     @Test
289     public void testHtmlEntitiesinGridHeaderFooter() {
290         //@formatter:off
291         String design = "<vaadin7-grid><table>"
292                 + "<colgroup>"
293                 + "  <col sortable=\"true\" property-id=\"> test\">"
294                 + "</colgroup>"
295                 + "<thead>"
296                 + "  <tr><th plain-text=\"true\">&gt; Test</th></tr>"
297                 + "</thead>"
298                 + "<tfoot>"
299                 + "  <tr><td plain-text=\"true\">&gt; Test</td></tr>"
300                 + "</tfoot>"
301                 + "<tbody />"
302                 + "</table></vaadin7-grid>";
303         //@formatter:on
304
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";
309
310         assertEquals(expected, actualHeader);
311         assertEquals(expected, actualFooter);
312
313         design = design.replace("plain-text=\"true\"", "");
314         grid = read(design);
315         actualHeader = grid.getHeaderRow(0).getCell("> test").getHtml();
316         actualFooter = grid.getFooterRow(0).getCell("> test").getHtml();
317         expected = "&gt; Test";
318
319         assertEquals(expected, actualHeader);
320         assertEquals(expected, actualFooter);
321
322         grid = new Grid();
323         grid.setColumns("test");
324         HeaderRow header = grid.addHeaderRowAt(0);
325         FooterRow footer = grid.addFooterRowAt(0);
326         grid.removeHeaderRow(grid.getDefaultHeaderRow());
327
328         // entities should be encoded when writing back, not interpreted as HTML
329         header.getCell("test").setText("&amp; Test");
330         footer.getCell("test").setText("&amp; Test");
331
332         Element root = new Element(Tag.valueOf("vaadin7-grid"), "");
333         grid.writeDesign(root, new DesignContext());
334
335         assertEquals("&amp;amp; Test",
336                 root.getElementsByTag("th").get(0).html());
337         assertEquals("&amp;amp; Test",
338                 root.getElementsByTag("td").get(0).html());
339
340         header = grid.addHeaderRowAt(0);
341         footer = grid.addFooterRowAt(0);
342
343         // entities should not be encoded, this is already given as HTML
344         header.getCell("test").setHtml("&amp; Test");
345         footer.getCell("test").setHtml("&amp; Test");
346
347         root = new Element(Tag.valueOf("vaadin7-grid"), "");
348         grid.writeDesign(root, new DesignContext());
349
350         assertEquals("&amp; Test",
351                 root.getElementsByTag("th").get(0).html());
352         assertEquals("&amp; Test",
353                 root.getElementsByTag("td").get(0).html());
354
355     }
356 }