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.

GridDeclarativeTest.java 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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.grid;
  17. import java.lang.reflect.InvocationTargetException;
  18. import java.lang.reflect.Method;
  19. import java.util.List;
  20. import java.util.Locale;
  21. import org.jsoup.nodes.Element;
  22. import org.jsoup.parser.Tag;
  23. import org.junit.Assert;
  24. import org.junit.Test;
  25. import com.vaadin.data.SelectionModel.Multi;
  26. import com.vaadin.data.SelectionModel.Single;
  27. import com.vaadin.data.provider.DataProvider;
  28. import com.vaadin.data.provider.Query;
  29. import com.vaadin.shared.ui.ContentMode;
  30. import com.vaadin.shared.ui.grid.HeightMode;
  31. import com.vaadin.tests.data.bean.Person;
  32. import com.vaadin.tests.server.component.abstractlisting.AbstractListingDeclarativeTest;
  33. import com.vaadin.ui.Grid;
  34. import com.vaadin.ui.Grid.Column;
  35. import com.vaadin.ui.Grid.SelectionMode;
  36. import com.vaadin.ui.Label;
  37. import com.vaadin.ui.components.grid.FooterCell;
  38. import com.vaadin.ui.components.grid.FooterRow;
  39. import com.vaadin.ui.components.grid.HeaderCell;
  40. import com.vaadin.ui.components.grid.HeaderRow;
  41. import com.vaadin.ui.declarative.DesignContext;
  42. import com.vaadin.ui.declarative.DesignException;
  43. /**
  44. * @author Vaadin Ltd
  45. *
  46. */
  47. public class GridDeclarativeTest extends AbstractListingDeclarativeTest<Grid> {
  48. @Test
  49. public void gridAttributes() {
  50. Grid<Person> grid = new Grid<>();
  51. int frozenColumns = 1;
  52. HeightMode heightMode = HeightMode.ROW;
  53. double heightByRows = 13.7d;
  54. grid.addColumn(Person::getFirstName).setCaption("First Name");
  55. grid.addColumn(Person::getLastName).setId("id").setCaption("Id");
  56. grid.setFrozenColumnCount(frozenColumns);
  57. grid.setSelectionMode(SelectionMode.MULTI);
  58. grid.setHeightMode(heightMode);
  59. grid.setHeightByRows(heightByRows);
  60. String design = String.format(
  61. "<%s height-mode='%s' frozen-columns='%d' rows='%s' selection-mode='%s'><table><colgroup>"
  62. + "<col column-id='column0' sortable>"
  63. + "<col column-id='id' sortable>" + "</colgroup><thead>"
  64. + "<tr default><th plain-text column-ids='column0'>First Name</th>"
  65. + "<th plain-text column-ids='id'>Id</th></tr>"
  66. + "</thead></table></%s>",
  67. getComponentTag(),
  68. heightMode.toString().toLowerCase(Locale.ENGLISH),
  69. frozenColumns, heightByRows,
  70. SelectionMode.MULTI.toString().toLowerCase(Locale.ENGLISH),
  71. getComponentTag());
  72. testRead(design, grid);
  73. testWrite(design, grid);
  74. }
  75. @Test
  76. public void mergedHeaderCells() {
  77. Grid<Person> grid = new Grid<>();
  78. Column<Person, String> column1 = grid.addColumn(Person::getFirstName)
  79. .setCaption("First Name");
  80. Column<Person, String> column2 = grid.addColumn(Person::getLastName)
  81. .setId("id").setCaption("Id");
  82. Column<Person, String> column3 = grid.addColumn(Person::getEmail)
  83. .setId("mail").setCaption("Mail");
  84. HeaderRow header = grid.addHeaderRowAt(1);
  85. String headerRowText1 = "foo";
  86. header.getCell(column1).setText(headerRowText1);
  87. HeaderCell cell2 = header.getCell(column2);
  88. HeaderCell join = header.join(cell2, header.getCell(column3));
  89. String headerRowText3 = "foobar";
  90. join.setText(headerRowText3);
  91. String design = String.format(
  92. "<%s><table><colgroup>" + "<col column-id='column0' sortable>"
  93. + "<col column-id='id' sortable>"
  94. + "<col column-id='mail' sortable>"
  95. + "</colgroup><thead>"
  96. + "<tr default><th plain-text column-ids='column0'>First Name</th>"
  97. + "<th plain-text column-ids='id'>Id</th>"
  98. + "<th plain-text column-ids='mail'>Mail</th></tr>"
  99. + "<tr><th plain-text column-ids='column0'>%s</th>"
  100. + "<th colspan='2' plain-text column-ids='id,mail'>foobar</th></tr>"
  101. + "</thead></table></%s>",
  102. getComponentTag(), headerRowText1, headerRowText3,
  103. getComponentTag());
  104. testRead(design, grid);
  105. testWrite(design, grid);
  106. }
  107. @Test
  108. public void mergedFooterCells() {
  109. Grid<Person> grid = new Grid<>();
  110. Column<Person, String> column1 = grid.addColumn(Person::getFirstName)
  111. .setCaption("First Name");
  112. Column<Person, String> column2 = grid.addColumn(Person::getLastName)
  113. .setId("id").setCaption("Id");
  114. Column<Person, String> column3 = grid.addColumn(Person::getEmail)
  115. .setId("mail").setCaption("Mail");
  116. FooterRow footer = grid.addFooterRowAt(0);
  117. FooterCell cell1 = footer.getCell(column1);
  118. String footerRowText1 = "foo";
  119. cell1.setText(footerRowText1);
  120. FooterCell cell2 = footer.getCell(column2);
  121. FooterCell cell3 = footer.getCell(column3);
  122. String footerRowText2 = "foobar";
  123. footer.join(cell2, cell3).setHtml(footerRowText2);
  124. String design = String.format(
  125. "<%s><table><colgroup>" + "<col column-id='column0' sortable>"
  126. + "<col column-id='id' sortable>"
  127. + "<col column-id='mail' sortable>"
  128. + "</colgroup><thead>"
  129. + "<tr default><th plain-text column-ids='column0'>First Name</th>"
  130. + "<th plain-text column-ids='id'>Id</th>"
  131. + "<th plain-text column-ids='mail'>Mail</th></tr></thead>"
  132. + "<tfoot><tr><td plain-text column-ids='column0'>%s</td>"
  133. + "<td colspan='2' column-ids='id,mail'>%s</td></tr></tfoot>"
  134. + "</table></%s>",
  135. getComponentTag(), footerRowText1, footerRowText2,
  136. getComponentTag());
  137. testRead(design, grid);
  138. testWrite(design, grid);
  139. }
  140. @Test
  141. public void columnAttributes() {
  142. Grid<Person> grid = new Grid<>();
  143. String secondColumnId = "id";
  144. Column<Person, String> column1 = grid.addColumn(Person::getFirstName)
  145. .setCaption("First Name");
  146. Column<Person, String> column2 = grid.addColumn(Person::getLastName)
  147. .setId(secondColumnId).setCaption("Id");
  148. String caption = "test-caption";
  149. column1.setCaption(caption);
  150. boolean sortable = false;
  151. column1.setSortable(sortable);
  152. boolean editable = true;
  153. column1.setEditorComponentGenerator(component -> null);
  154. column1.setEditable(editable);
  155. boolean resizable = false;
  156. column1.setResizable(resizable);
  157. boolean hidable = true;
  158. column1.setHidable(hidable);
  159. boolean hidden = true;
  160. column1.setHidden(hidden);
  161. String hidingToggleCaption = "toggle-caption";
  162. column2.setHidingToggleCaption(hidingToggleCaption);
  163. double width = 17.3;
  164. column2.setWidth(width);
  165. double minWidth = 37.3;
  166. column2.setMinimumWidth(minWidth);
  167. double maxWidth = 63.4;
  168. column2.setMaximumWidth(maxWidth);
  169. int expandRatio = 83;
  170. column2.setExpandRatio(expandRatio);
  171. String design = String.format(
  172. "<%s><table><colgroup>"
  173. + "<col column-id='column0' sortable='%s' editable resizable='%s' hidable hidden>"
  174. + "<col column-id='id' sortable hiding-toggle-caption='%s' width='%s' min-width='%s' max-width='%s' expand='%s'>"
  175. + "</colgroup><thead>"
  176. + "<tr default><th plain-text column-ids='column0'>%s</th>"
  177. + "<th plain-text column-ids='id'>%s</th>"
  178. + "</tr></thead>" + "</table></%s>",
  179. getComponentTag(), sortable, resizable, hidingToggleCaption,
  180. width, minWidth, maxWidth, expandRatio, caption, "Id",
  181. getComponentTag());
  182. testRead(design, grid, true);
  183. testWrite(design, grid);
  184. }
  185. @Test
  186. public void headerFooterSerialization() {
  187. Grid<Person> grid = new Grid<>();
  188. Column<Person, String> column1 = grid.addColumn(Person::getFirstName)
  189. .setCaption("First Name");
  190. Column<Person, String> column2 = grid.addColumn(Person::getLastName)
  191. .setId("id").setCaption("Id");
  192. FooterRow footerRow = grid.addFooterRowAt(0);
  193. footerRow.getCell(column1).setText("x");
  194. footerRow.getCell(column2).setHtml("y");
  195. String design = String.format(
  196. "<%s><table><colgroup>" + "<col column-id='column0' sortable>"
  197. + "<col column-id='id' sortable></colgroup><thead>"
  198. + "<tr default><th plain-text column-ids='column0'>First Name</th>"
  199. + "<th plain-text column-ids='id'>Id</th></tr>"
  200. + "</thead><tbody></tbody>"
  201. + "<tfoot><tr><td plain-text column-ids='column0'>x</td>"
  202. + "<td column-ids='id'>y</td></tr></tfoot>"
  203. + "</table></%s>",
  204. getComponentTag(), getComponentTag());
  205. testRead(design, grid);
  206. testWrite(design, grid, true);
  207. }
  208. @Override
  209. public void dataSerialization() throws InstantiationException,
  210. IllegalAccessException, InvocationTargetException {
  211. Grid<Person> grid = new Grid<>();
  212. Person person1 = createPerson("foo", "bar");
  213. Person person2 = createPerson("name", "last-name");
  214. grid.setItems(person1, person2);
  215. grid.addColumn(Person::getFirstName).setCaption("First Name");
  216. grid.addColumn(Person::getLastName).setId("id").setCaption("Id");
  217. String design = String.format(
  218. "<%s><table><colgroup>" + "<col column-id='column0' sortable>"
  219. + "<col column-id='id' sortable></colgroup><thead>"
  220. + "<tr default><th plain-text column-ids='column0'>First Name</th>"
  221. + "<th plain-text column-ids='id'>Id</th></tr>"
  222. + "</thead><tbody>"
  223. + "<tr item='%s'><td>%s</td><td>%s</td></tr>"
  224. + "<tr item='%s'><td>%s</td><td>%s</td></tr>"
  225. + "</tbody></table></%s>",
  226. getComponentTag(), person1.toString(), person1.getFirstName(),
  227. person1.getLastName(), person2.toString(),
  228. person2.getFirstName(), person2.getLastName(),
  229. getComponentTag());
  230. Grid<?> readGrid = testRead(design, grid, true, true);
  231. Assert.assertEquals(2, readGrid.getDataProvider().size(new Query<>()));
  232. testWrite(design, grid, true);
  233. }
  234. /**
  235. * Value for single select
  236. */
  237. @Override
  238. @Test
  239. public void valueSerialization() throws InstantiationException,
  240. IllegalAccessException, InvocationTargetException {
  241. valueSingleSelectSerialization();
  242. }
  243. @SuppressWarnings("unchecked")
  244. @Test
  245. public void valueMultiSelectSerialization() throws InstantiationException,
  246. IllegalAccessException, InvocationTargetException {
  247. Grid<Person> grid = new Grid<>();
  248. Person person1 = createPerson("foo", "bar");
  249. Person person2 = createPerson("name", "last-name");
  250. Person person3 = createPerson("foo", "last-name");
  251. grid.setItems(person1, person2, person3);
  252. grid.addColumn(Person::getFirstName).setCaption("First Name");
  253. grid.addColumn(Person::getLastName).setId("id").setCaption("Id");
  254. Multi<Person> model = (Multi<Person>) grid
  255. .setSelectionMode(SelectionMode.MULTI);
  256. model.selectItems(person1, person3);
  257. String design = String.format(
  258. "<%s selection-mode='multi'><table><colgroup>"
  259. + "<col column-id='column0' sortable>"
  260. + "<col column-id='id' sortable></colgroup><thead>"
  261. + "<tr default><th plain-text column-ids='column0'>First Name</th>"
  262. + "<th plain-text column-ids='id'>Id</th></tr>"
  263. + "</thead><tbody>"
  264. + "<tr item='%s' selected><td>%s</td><td>%s</td></tr>"
  265. + "<tr item='%s'><td>%s</td><td>%s</td></tr>"
  266. + "<tr item='%s' selected><td>%s</td><td>%s</td></tr>"
  267. + "</tbody></table></%s>",
  268. getComponentTag(), person1.toString(), person1.getFirstName(),
  269. person1.getLastName(), person2.toString(),
  270. person2.getFirstName(), person2.getLastName(),
  271. person3.toString(), person3.getFirstName(),
  272. person3.getLastName(), getComponentTag());
  273. Grid<?> readGrid = testRead(design, grid, true, true);
  274. Assert.assertEquals(3, readGrid.getDataProvider().size(new Query<>()));
  275. testWrite(design, grid, true);
  276. }
  277. @SuppressWarnings("unchecked")
  278. private void valueSingleSelectSerialization() throws InstantiationException,
  279. IllegalAccessException, InvocationTargetException {
  280. Grid<Person> grid = new Grid<>();
  281. Person person1 = createPerson("foo", "bar");
  282. Person person2 = createPerson("name", "last-name");
  283. grid.setItems(person1, person2);
  284. grid.addColumn(Person::getFirstName).setCaption("First Name");
  285. grid.addColumn(Person::getLastName).setId("id").setCaption("Id");
  286. Single<Person> model = (Single<Person>) grid
  287. .setSelectionMode(SelectionMode.SINGLE);
  288. model.select(person2);
  289. String design = String.format(
  290. "<%s><table><colgroup>" + "<col column-id='column0' sortable>"
  291. + "<col column-id='id' sortable></colgroup><thead>"
  292. + "<tr default><th plain-text column-ids='column0'>First Name</th>"
  293. + "<th plain-text column-ids='id'>Id</th></tr>"
  294. + "</thead><tbody>"
  295. + "<tr item='%s'><td>%s</td><td>%s</td></tr>"
  296. + "<tr item='%s' selected><td>%s</td><td>%s</td></tr>"
  297. + "</tbody></table></%s>",
  298. getComponentTag(), person1.toString(), person1.getFirstName(),
  299. person1.getLastName(), person2.toString(),
  300. person2.getFirstName(), person2.getLastName(),
  301. getComponentTag());
  302. Grid<?> readGrid = testRead(design, grid, true, true);
  303. Assert.assertEquals(2, readGrid.getDataProvider().size(new Query<>()));
  304. testWrite(design, grid, true);
  305. }
  306. @Override
  307. public void readOnlySelection() throws InstantiationException,
  308. IllegalAccessException, InvocationTargetException {
  309. Grid<Person> grid = new Grid<>();
  310. Person person1 = createPerson("foo", "bar");
  311. Person person2 = createPerson("name", "last-name");
  312. grid.setItems(person1, person2);
  313. grid.addColumn(Person::getFirstName).setCaption("First Name");
  314. grid.addColumn(Person::getLastName).setId("id").setCaption("Id");
  315. grid.setSelectionMode(SelectionMode.MULTI);
  316. grid.asMultiSelect().setReadOnly(true);
  317. String formatString = "<%s %s selection-allowed><table><colgroup>"
  318. + "<col column-id='column0' sortable>"
  319. + "<col column-id='id' sortable>" + "</colgroup><thead>"
  320. + "<tr default><th plain-text column-ids='column0'>First Name</th>"
  321. + "<th plain-text column-ids='id'>Id</th></tr>"
  322. + "</thead><tbody>"
  323. + "<tr item='%s'><td>%s</td><td>%s</td></tr>"
  324. + "<tr item='%s'><td>%s</td><td>%s</td></tr>"
  325. + "</tbody></table></%s>";
  326. String design = String.format(formatString, getComponentTag(),
  327. "selection-mode='multi'", person1.toString(),
  328. person1.getFirstName(), person1.getLastName(),
  329. person2.toString(), person2.getFirstName(),
  330. person2.getLastName(), getComponentTag());
  331. Grid<?> readGrid = testRead(design, grid, true, true);
  332. Assert.assertEquals(2, readGrid.getDataProvider().size(new Query<>()));
  333. testWrite(design, grid, true);
  334. grid.setSelectionMode(SelectionMode.SINGLE);
  335. grid.asSingleSelect().setReadOnly(true);
  336. design = String.format(formatString, getComponentTag(), "",
  337. person1.toString(), person1.getFirstName(),
  338. person1.getLastName(), person2.toString(),
  339. person2.getFirstName(), person2.getLastName(),
  340. getComponentTag());
  341. readGrid = testRead(design, grid, true, true);
  342. Assert.assertEquals(2, readGrid.getDataProvider().size(new Query<>()));
  343. testWrite(design, grid, true);
  344. }
  345. @Test
  346. public void testComponentInGridHeader() {
  347. Grid<Person> grid = new Grid<>();
  348. Column<Person, String> column = grid.addColumn(Person::getFirstName)
  349. .setCaption("First Name");
  350. String html = "<b>Foo</b>";
  351. Label component = new Label(html);
  352. component.setContentMode(ContentMode.HTML);
  353. //@formatter:off
  354. String design = String.format( "<%s><table>"
  355. + "<colgroup>"
  356. + " <col sortable column-id='column0'>"
  357. + "</colgroup>"
  358. + "<thead>"
  359. + "<tr default><th column-ids='column0'><vaadin-label>%s</vaadin-label></th></tr>"
  360. + "</thead>"
  361. + "</table></%s>", getComponentTag(), html, getComponentTag());
  362. //@formatter:on
  363. grid.getDefaultHeaderRow().getCell(column).setComponent(component);
  364. testRead(design, grid, true);
  365. testWrite(design, grid);
  366. }
  367. @Test
  368. public void testComponentInGridFooter() {
  369. Grid<Person> grid = new Grid<>();
  370. Column<Person, String> column = grid.addColumn(Person::getFirstName)
  371. .setCaption("First Name");
  372. String html = "<b>Foo</b>";
  373. Label component = new Label(html);
  374. component.setContentMode(ContentMode.HTML);
  375. grid.prependFooterRow().getCell(column).setComponent(component);
  376. grid.removeHeaderRow(grid.getDefaultHeaderRow());
  377. //@formatter:off
  378. String design = String.format( "<%s><table>"
  379. + "<colgroup>"
  380. + " <col sortable column-id='column0'>"
  381. + "</colgroup>"
  382. + "<thead>"
  383. +"<tfoot>"
  384. + "<tr><td column-ids='column0'><vaadin-label>%s</vaadin-label></td></tr>"
  385. + "</tfoot>"
  386. + "</table>"
  387. + "</%s>", getComponentTag(), html, getComponentTag());
  388. //@formatter:on
  389. testRead(design, grid, true);
  390. testWrite(design, grid);
  391. }
  392. @Test
  393. public void testNoHeaderRows() {
  394. //@formatter:off
  395. String design = "<vaadin-grid><table>"
  396. + "<colgroup>"
  397. + " <col sortable column-id='column0'>"
  398. + "</colgroup>"
  399. + "<thead />"
  400. + "</table>"
  401. + "</vaadin-grid>";
  402. //@formatter:on
  403. Grid<Person> grid = new Grid<>();
  404. grid.addColumn(Person::getFirstName).setCaption("First Name");
  405. grid.removeHeaderRow(grid.getDefaultHeaderRow());
  406. testWrite(design, grid);
  407. testRead(design, grid, true);
  408. }
  409. @Test
  410. public void testReadEmptyGrid() {
  411. String design = "<vaadin-grid />";
  412. testRead(design, new Grid<String>(), false);
  413. }
  414. @Test
  415. public void testEmptyGrid() {
  416. String design = "<vaadin-grid></vaadin-grid>";
  417. Grid<String> expected = new Grid<>();
  418. testWrite(design, expected);
  419. testRead(design, expected, true);
  420. }
  421. @Test(expected = DesignException.class)
  422. public void testMalformedGrid() {
  423. String design = "<vaadin-grid><vaadin-label /></vaadin-grid>";
  424. testRead(design, new Grid<String>());
  425. }
  426. @Test(expected = DesignException.class)
  427. public void testGridWithNoColGroup() {
  428. String design = "<vaadin-grid><table><thead><tr><th>Foo</tr></thead></table></vaadin-grid>";
  429. testRead(design, new Grid<String>());
  430. }
  431. @Test
  432. @SuppressWarnings("unchecked")
  433. public void testHtmlEntitiesinGridHeaderFooter() {
  434. String id = "> id";
  435. String plainText = "plain-text";
  436. //@formatter:off
  437. String design = String.format( "<%s><table>"
  438. + "<colgroup>"
  439. + " <col sortable column-id='%s'>"
  440. + "</colgroup>"
  441. + "<thead>"
  442. +" <tr default><th %s column-ids='%s'>&gt; Test</th>"
  443. + "</thead>"
  444. + "<tfoot>"
  445. + "<tr><td %s column-ids='%s'>&gt; Test</td></tr>"
  446. + "</tfoot>"
  447. + "<tbody />"
  448. + "</table></%s>",
  449. getComponentTag() , id, plainText, id, plainText, id, getComponentTag());
  450. //@formatter:on
  451. Grid<Person> grid = read(design);
  452. String actualHeader = grid.getHeaderRow(0).getCell(id).getText();
  453. String actualFooter = grid.getFooterRow(0).getCell(id).getText();
  454. String expected = "> Test";
  455. Assert.assertEquals(expected, actualHeader);
  456. Assert.assertEquals(expected, actualFooter);
  457. design = design.replace(plainText, "");
  458. grid = read(design);
  459. actualHeader = grid.getHeaderRow(0).getCell(id).getHtml();
  460. actualFooter = grid.getFooterRow(0).getCell(id).getHtml();
  461. expected = "&gt; Test";
  462. Assert.assertEquals(expected, actualHeader);
  463. Assert.assertEquals(expected, actualFooter);
  464. grid = new Grid<>();
  465. Column<Person, String> column = grid.addColumn(Person::getFirstName)
  466. .setId(id);
  467. HeaderRow header = grid.addHeaderRowAt(0);
  468. FooterRow footer = grid.addFooterRowAt(0);
  469. grid.removeHeaderRow(grid.getDefaultHeaderRow());
  470. // entities should be encoded when writing back, not interpreted as HTML
  471. header.getCell(column).setText("&amp; Test");
  472. footer.getCell(column).setText("&amp; Test");
  473. Element root = new Element(Tag.valueOf(getComponentTag()), "");
  474. grid.writeDesign(root, new DesignContext());
  475. Assert.assertEquals("&amp;amp; Test",
  476. root.getElementsByTag("th").get(0).html());
  477. Assert.assertEquals("&amp;amp; Test",
  478. root.getElementsByTag("td").get(0).html());
  479. header = grid.addHeaderRowAt(0);
  480. footer = grid.addFooterRowAt(0);
  481. // entities should not be encoded, this is already given as HTML
  482. header.getCell(id).setHtml("&amp; Test");
  483. footer.getCell(id).setHtml("&amp; Test");
  484. root = new Element(Tag.valueOf(getComponentTag()), "");
  485. grid.writeDesign(root, new DesignContext());
  486. Assert.assertEquals("&amp; Test",
  487. root.getElementsByTag("th").get(0).html());
  488. Assert.assertEquals("&amp; Test",
  489. root.getElementsByTag("td").get(0).html());
  490. }
  491. @SuppressWarnings("rawtypes")
  492. @Override
  493. public Grid<?> testRead(String design, Grid expected) {
  494. return testRead(design, expected, false);
  495. }
  496. @Override
  497. @SuppressWarnings("rawtypes")
  498. public Grid<?> testRead(String design, Grid expected, boolean retestWrite) {
  499. return testRead(design, expected, retestWrite, false);
  500. }
  501. @SuppressWarnings("rawtypes")
  502. public Grid<?> testRead(String design, Grid expected, boolean retestWrite,
  503. boolean writeData) {
  504. Grid<?> actual = super.testRead(design, expected);
  505. compareGridColumns(expected, actual);
  506. compareHeaders(expected, actual);
  507. compareFooters(expected, actual);
  508. if (retestWrite) {
  509. testWrite(design, actual, writeData);
  510. }
  511. return actual;
  512. }
  513. private void compareHeaders(Grid<?> expected, Grid<?> actual) {
  514. Assert.assertEquals("Different header row count",
  515. expected.getHeaderRowCount(), actual.getHeaderRowCount());
  516. for (int i = 0; i < expected.getHeaderRowCount(); ++i) {
  517. HeaderRow expectedRow = expected.getHeaderRow(i);
  518. HeaderRow actualRow = actual.getHeaderRow(i);
  519. if (expectedRow.equals(expected.getDefaultHeaderRow())) {
  520. Assert.assertEquals("Different index for default header row",
  521. actual.getDefaultHeaderRow(), actualRow);
  522. }
  523. for (Column<?, ?> column : expected.getColumns()) {
  524. String baseError = "Difference when comparing cell for "
  525. + column.toString() + " on header row " + i + ": ";
  526. HeaderCell expectedCell = expectedRow.getCell(column);
  527. HeaderCell actualCell = actualRow.getCell(column);
  528. switch (expectedCell.getCellType()) {
  529. case TEXT:
  530. Assert.assertEquals(baseError + "Text content",
  531. expectedCell.getText(), actualCell.getText());
  532. break;
  533. case HTML:
  534. Assert.assertEquals(baseError + "HTML content",
  535. expectedCell.getHtml(), actualCell.getHtml());
  536. break;
  537. case WIDGET:
  538. assertEquals(baseError + "Component content",
  539. expectedCell.getComponent(),
  540. actualCell.getComponent());
  541. break;
  542. }
  543. }
  544. }
  545. }
  546. private void compareFooters(Grid<?> expected, Grid<?> actual) {
  547. Assert.assertEquals("Different footer row count",
  548. expected.getFooterRowCount(), actual.getFooterRowCount());
  549. for (int i = 0; i < expected.getFooterRowCount(); ++i) {
  550. FooterRow expectedRow = expected.getFooterRow(i);
  551. FooterRow actualRow = actual.getFooterRow(i);
  552. for (Column<?, ?> column : expected.getColumns()) {
  553. String baseError = "Difference when comparing cell for "
  554. + column.toString() + " on footer row " + i + ": ";
  555. FooterCell expectedCell = expectedRow.getCell(column);
  556. FooterCell actualCell = actualRow.getCell(column);
  557. switch (expectedCell.getCellType()) {
  558. case TEXT:
  559. Assert.assertEquals(baseError + "Text content",
  560. expectedCell.getText(), actualCell.getText());
  561. break;
  562. case HTML:
  563. Assert.assertEquals(baseError + "HTML content",
  564. expectedCell.getHtml(), actualCell.getHtml());
  565. break;
  566. case WIDGET:
  567. assertEquals(baseError + "Component content",
  568. expectedCell.getComponent(),
  569. actualCell.getComponent());
  570. break;
  571. }
  572. }
  573. }
  574. }
  575. private void compareGridColumns(Grid<?> expected, Grid<?> actual) {
  576. List<?> columns = expected.getColumns();
  577. List<?> actualColumns = actual.getColumns();
  578. Assert.assertEquals("Different amount of columns", columns.size(),
  579. actualColumns.size());
  580. for (int i = 0; i < columns.size(); ++i) {
  581. Column<?, ?> col1 = (Column<?, ?>) columns.get(i);
  582. Column<?, ?> col2 = (Column<?, ?>) actualColumns.get(i);
  583. String baseError = "Error when comparing columns for property "
  584. + col1.getId() + ": ";
  585. assertEquals(baseError + "Width", col1.getWidth(), col2.getWidth());
  586. assertEquals(baseError + "Maximum width", col1.getMaximumWidth(),
  587. col2.getMaximumWidth());
  588. assertEquals(baseError + "Minimum width", col1.getMinimumWidth(),
  589. col2.getMinimumWidth());
  590. assertEquals(baseError + "Expand ratio", col1.getExpandRatio(),
  591. col2.getExpandRatio());
  592. assertEquals(baseError + "Sortable", col1.isSortable(),
  593. col2.isSortable());
  594. assertEquals(baseError + "Editable", col1.isEditable(),
  595. col2.isEditable());
  596. assertEquals(baseError + "Hidable", col1.isHidable(),
  597. col2.isHidable());
  598. assertEquals(baseError + "Hidden", col1.isHidden(),
  599. col2.isHidden());
  600. assertEquals(baseError + "HidingToggleCaption",
  601. col1.getHidingToggleCaption(),
  602. col2.getHidingToggleCaption());
  603. }
  604. }
  605. @Override
  606. protected String getComponentTag() {
  607. return "vaadin-grid";
  608. }
  609. @Override
  610. protected Class<? extends Grid> getComponentClass() {
  611. return Grid.class;
  612. }
  613. @Override
  614. protected boolean acceptProperty(Class<?> clazz, Method readMethod,
  615. Method writeMethod) {
  616. if (readMethod != null) {
  617. Class<?> returnType = readMethod.getReturnType();
  618. if (HeaderRow.class.equals(returnType)
  619. || DataProvider.class.equals(returnType)) {
  620. return false;
  621. }
  622. }
  623. return super.acceptProperty(clazz, readMethod, writeMethod);
  624. }
  625. private Person createPerson(String name, String lastName) {
  626. Person person = new Person() {
  627. @Override
  628. public String toString() {
  629. return getFirstName() + " " + getLastName();
  630. }
  631. };
  632. person.setFirstName(name);
  633. person.setLastName(lastName);
  634. return person;
  635. }
  636. }