1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.internal.component;
21
22 import org.apache.myfaces.tobago.component.Attributes;
23 import org.apache.myfaces.tobago.component.RendererTypes;
24 import org.apache.myfaces.tobago.component.Tags;
25 import org.apache.myfaces.tobago.internal.config.AbstractTobagoTestBase;
26 import org.apache.myfaces.tobago.util.ComponentUtils;
27 import org.junit.jupiter.api.Assertions;
28 import org.junit.jupiter.api.Test;
29
30 import javax.faces.component.UIComponent;
31 import java.util.Arrays;
32
33 public class AbstractUIGridLayoutUnitTest extends AbstractTobagoTestBase {
34
35 private static final UIComponent X = AbstractUIGridLayout.SPAN;
36 private static final UIComponent N = null;
37
38 @Test
39 public void test1() {
40 final AbstractUIGridLayout grid = (AbstractUIGridLayout) ComponentUtils.createComponent(
41 facesContext, Tags.gridLayout.componentType(), RendererTypes.GridLayout, null);
42
43 final UIComponent a = createComponent("a");
44 final UIComponent b = createComponent("b");
45 final UIComponent c = createComponent("c");
46 c.getAttributes().put(Attributes.columnSpan.getName(), 2);
47 final UIComponent d = createComponent("d");
48 final UIComponent e = createComponent("e");
49 e.getAttributes().put(Attributes.gridColumn.getName(), 1);
50 e.getAttributes().put(Attributes.gridRow.getName(), 3);
51
52 final UIComponent[][] cells = grid.layout(2, 3, Arrays.asList(a, b, c, d, e));
53
54 Assertions.assertEquals("┏━┳━┓\n"
55 + "┃a┃b┃\n"
56 + "┣━╋━┫\n"
57 + "┃c┃█┃\n"
58 + "┣━╋━┫\n"
59 + "┃e┃d┃\n"
60 + "┗━┻━┛\n", toString(cells));
61
62 Assertions.assertEquals(a, cells[0][0]);
63 Assertions.assertEquals(b, cells[0][1]);
64 Assertions.assertEquals(c, cells[1][0]);
65 Assertions.assertEquals(X, cells[1][1]);
66 Assertions.assertEquals(e, cells[2][0]);
67 Assertions.assertEquals(d, cells[2][1]);
68 }
69
70
71 @Test
72 public void test2() {
73 final AbstractUIGridLayout grid = (AbstractUIGridLayout) ComponentUtils.createComponent(
74 facesContext, Tags.gridLayout.componentType(), RendererTypes.GridLayout, null);
75
76 final UIComponent a = createComponent("a");
77 final UIComponent b = createComponent("b");
78 final UIComponent c = createComponent("c");
79 c.getAttributes().put(Attributes.rowSpan.getName(), 2);
80 final UIComponent d = createComponent("d");
81 final UIComponent e = createComponent("e");
82 e.getAttributes().put(Attributes.gridColumn.getName(), 1);
83 e.getAttributes().put(Attributes.gridRow.getName(), 1);
84
85 final UIComponent[][] cells = grid.layout(2, 3, Arrays.asList(a, b, c, d, e));
86
87 Assertions.assertEquals("┏━┳━┓\n"
88 + "┃e┃a┃\n"
89 + "┣━╋━┫\n"
90 + "┃b┃c┃\n"
91 + "┣━╋━┫\n"
92 + "┃d┃█┃\n"
93 + "┗━┻━┛\n", toString(cells));
94
95 Assertions.assertEquals(e, cells[0][0]);
96 Assertions.assertEquals(a, cells[0][1]);
97 Assertions.assertEquals(b, cells[1][0]);
98 Assertions.assertEquals(c, cells[1][1]);
99 Assertions.assertEquals(d, cells[2][0]);
100 Assertions.assertEquals(X, cells[2][1]);
101 }
102
103 @Test
104 public void test3() {
105 final AbstractUIGridLayout grid = (AbstractUIGridLayout) ComponentUtils.createComponent(
106 facesContext, Tags.gridLayout.componentType(), RendererTypes.GridLayout, null);
107
108 final UIComponent a = createComponent("a");
109 a.getAttributes().put(Attributes.rowSpan.getName(), 7);
110 final UIComponent b = createComponent("b");
111 final UIComponent c = createComponent("c");
112 final UIComponent d = createComponent("d");
113 final UIComponent e = createComponent("e");
114
115 final UIComponent[][] cells = grid.layout(2, 3, Arrays.asList(a, b, c, d, e));
116
117 Assertions.assertEquals("┏━┳━┓\n"
118 + "┃a┃b┃\n"
119 + "┣━╋━┫\n"
120 + "┃█┃c┃\n"
121 + "┣━╋━┫\n"
122 + "┃█┃d┃\n"
123 + "┣━╋━┫\n"
124 + "┃█┃e┃\n"
125 + "┣━╋━┫\n"
126 + "┃█┃◌┃\n"
127 + "┣━╋━┫\n"
128 + "┃█┃◌┃\n"
129 + "┣━╋━┫\n"
130 + "┃█┃◌┃\n"
131 + "┣━╋━┫\n"
132 + "┃◌┃◌┃\n"
133 + "┗━┻━┛\n", toString(cells));
134
135 Assertions.assertEquals(a, cells[0][0]);
136 Assertions.assertEquals(b, cells[0][1]);
137 Assertions.assertEquals(X, cells[1][0]);
138 Assertions.assertEquals(c, cells[1][1]);
139 Assertions.assertEquals(X, cells[2][0]);
140 Assertions.assertEquals(d, cells[2][1]);
141 Assertions.assertEquals(X, cells[3][0]);
142 Assertions.assertEquals(e, cells[3][1]);
143 Assertions.assertEquals(X, cells[4][0]);
144 Assertions.assertEquals(N, cells[4][1]);
145 Assertions.assertEquals(X, cells[5][0]);
146 Assertions.assertEquals(N, cells[5][1]);
147 Assertions.assertEquals(X, cells[6][0]);
148 Assertions.assertEquals(N, cells[6][1]);
149 Assertions.assertEquals(N, cells[7][0]);
150 Assertions.assertEquals(N, cells[7][1]);
151 }
152
153 @Test
154 public void test4() {
155 final AbstractUIGridLayout grid = (AbstractUIGridLayout) ComponentUtils.createComponent(
156 facesContext, Tags.gridLayout.componentType(), RendererTypes.GridLayout, null);
157
158 final UIComponent a = createComponent("a");
159 final UIComponent b = createComponent("b");
160 b.getAttributes().put(Attributes.columnSpan.getName(), 2);
161
162 final UIComponent[][] cells = grid.layout(2, 1, Arrays.asList(a, b));
163
164 Assertions.assertEquals("┏━┳━┓\n"
165 + "┃a┃b┃\n"
166 + "┗━┻━┛\n", toString(cells));
167
168 Assertions.assertEquals(a, cells[0][0]);
169 Assertions.assertEquals(b, cells[0][1]);
170
171
172 }
173
174 @Test
175 public void test5() {
176 final AbstractUIGridLayout grid = (AbstractUIGridLayout) ComponentUtils.createComponent(
177 facesContext, Tags.gridLayout.componentType(), RendererTypes.GridLayout, null);
178
179 final UIComponent a = createComponent("a");
180 final UIComponent b = createComponent("b");
181
182 final UIComponent c = createComponent("c");
183 final UIComponent d = createComponent("d");
184
185 final UIComponent e = createComponent("e");
186 e.getAttributes().put(Attributes.columnSpan.getName(), 2);
187
188 final UIComponent f = createComponent("f");
189 final UIComponent g = createComponent("g");
190
191 final UIComponent h = createComponent("h");
192 final UIComponent i = createComponent("i");
193
194 final UIComponent j = createComponent("j");
195 j.getAttributes().put(Attributes.columnSpan.getName(), 2);
196
197 final UIComponent[][] cells = grid.layout(2, 5, Arrays.asList(a, b, c, d, e, f, g, h, i, j));
198
199 Assertions.assertEquals("┏━┳━┓\n"
200 + "┃a┃b┃\n"
201 + "┣━╋━┫\n"
202 + "┃c┃d┃\n"
203 + "┣━╋━┫\n"
204 + "┃e┃█┃\n"
205 + "┣━╋━┫\n"
206 + "┃f┃g┃\n"
207 + "┣━╋━┫\n"
208 + "┃h┃i┃\n"
209 + "┣━╋━┫\n"
210 + "┃j┃█┃\n"
211 + "┣━╋━┫\n"
212 + "┃◌┃◌┃\n"
213 + "┣━╋━┫\n"
214 + "┃◌┃◌┃\n"
215 + "┣━╋━┫\n"
216 + "┃◌┃◌┃\n"
217 + "┣━╋━┫\n"
218 + "┃◌┃◌┃\n"
219 + "┗━┻━┛\n", toString(cells));
220 }
221
222 @Test
223 public void testExpand() {
224 final AbstractUIGridLayout grid = (AbstractUIGridLayout) ComponentUtils.createComponent(
225 facesContext, Tags.gridLayout.componentType(), RendererTypes.GridLayout, null);
226
227 final UIComponent a = createComponent("a");
228 final UIComponent b = createComponent("b");
229
230 final UIComponent[][] array = new UIComponent[3][5];
231 array[0][0] = a;
232 array[2][4] = b;
233
234 Assertions.assertEquals(a, array[0][0]);
235 Assertions.assertEquals(b, array[2][4]);
236 Assertions.assertEquals(3, array.length);
237 Assertions.assertEquals(5, array[0].length);
238
239 final UIComponent[][] expand = grid.expand(array, 7);
240
241 Assertions.assertEquals(array[0][0], expand[0][0]);
242 Assertions.assertEquals(array[2][4], expand[2][4]);
243 Assertions.assertEquals(7, expand.length);
244 Assertions.assertEquals(5, expand[0].length);
245
246 Assertions.assertNull(expand[1][1]);
247 Assertions.assertNull(expand[6][1]);
248 }
249
250 private UIComponent createComponent(final String id) {
251 return ComponentUtils.createComponent(facesContext, Tags.panel.componentType(), RendererTypes.Panel, id);
252 }
253
254 private static String toString(final UIComponent[][] cells) {
255
256 final StringBuilder builder = new StringBuilder();
257
258
259 for (int i = 0; i < cells[0].length; i++) {
260 if (i == 0) {
261 builder.append("┏");
262 } else {
263 builder.append("┳");
264 }
265 builder.append("━");
266 }
267 builder.append("┓");
268 builder.append("\n");
269
270 for (int j = 0; j < cells.length; j++) {
271
272
273 if (j != 0) {
274 for (int i = 0; i < cells[0].length; i++) {
275 if (i == 0) {
276 builder.append("┣");
277 } else {
278 builder.append("╋");
279 }
280 builder.append("━");
281 }
282 builder.append("┫");
283 builder.append("\n");
284 }
285
286
287 for (int i = 0; i < cells[0].length; i++) {
288 builder.append("┃");
289 if (cells[j][i] == X) {
290 builder.append("█");
291 } else if (cells[j][i] != null) {
292 builder.append(cells[j][i].getClientId());
293 } else {
294 builder.append("◌");
295 }
296 }
297 builder.append("┃");
298 builder.append("\n");
299 }
300
301
302 for (int i = 0; i < cells[0].length; i++) {
303 if (i == 0) {
304 builder.append("┗");
305 } else {
306 builder.append("┻");
307 }
308 builder.append("━");
309 }
310 builder.append("┛");
311 builder.append("\n");
312
313 return builder.toString();
314 }
315
316 }