View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau.bean.html5;
18  
19  import static org.apache.juneau.bean.html5.HtmlBuilder.*;
20  import static org.apache.juneau.commons.utils.CollectionUtils.*;
21  import static org.apache.juneau.junit.bct.BctAssertions.*;
22  import static org.junit.jupiter.api.Assertions.*;
23  
24  import org.apache.juneau.*;
25  import org.junit.jupiter.api.*;
26  
27  class HtmlElementContainer_Test extends TestBase {
28  
29  	@Test void a01_getChild() {
30  		var x = new Div();
31  		assertNull(new Div().getChild(0));
32  		assertNull(x.getChild(String.class, 0));
33  
34  		x = div("child1");
35  		assertNull(x.getChild(5));
36  		assertNull(x.getChild(-1));
37  		assertNull(x.getChild(String.class, 5));
38  		assertNull(x.getChild(String.class, -1));
39  
40  		x = div("child1", "child2");
41  		assertString("child1", x.getChild(0));
42  		assertString("child2", x.getChild(1));
43  		assertString("child1", x.getChild(String.class, 0));
44  		assertString("child2", x.getChild(String.class, 1));
45  
46  		x = new Div();
47  		assertNull(x.getChildren());
48  		x.children();
49  		assertNull(x.getChildren());
50  
51  		x = div("child1", "child2");
52  		assertString("[child1,child2]", x.getChildren());
53  		x.children("child1", "child2");
54  		assertString("[child1,child2,child1,child2]", x.getChildren());
55  
56  		x = new Div();
57  		x.child("child1");
58  		assertString("[child1]", x.getChildren());
59  
60  		x = div("child1");
61  		assertNull(x.getChild(ints()));
62  
63  		x = div("child1", "child2");
64  		assertString("child1", x.getChild(ints(0)));
65  
66  		x = div(
67  			div(
68  				div("text1", "text2"),
69  				div("text3")
70  			),
71  			div("text4")
72  		);
73  
74  		// Navigate to nested elements
75  		assertString("text1", x.getChild(0, 0, 0));
76  		assertString("text2", x.getChild(0, 0, 1));
77  		assertString("text3", x.getChild(0, 1, 0));
78  		assertString("text4", x.getChild(1, 0));
79  
80  		x = div("text");
81  		assertNull(x.getChild(0, 0));// Try to get child of a text node (which is not a container)
82  	}
83  
84  	@Test void a02_getChild_varargs_mixedElement() {
85  		// Test navigation through HtmlElementMixed
86  		P x = p(
87  			span("text1"),
88  			span("text2")
89  		);
90  
91  		assertString("text1", x.getChild(0, 0));
92  		assertString("text2", x.getChild(1, 0));
93  	}
94  
95  	@Test void a03_setChildren() {
96  		var x = new Div();
97  		java.util.List<Object> children = l("child1", "child2");
98  		x.setChildren(children);
99  		assertString("[child1,child2]", x.getChildren());
100 	}
101 }