1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.component;
17
18
19
20
21
22
23
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.kuali.rice.krad.uif.control.FileControl;
27 import org.kuali.rice.krad.uif.control.TextAreaControl;
28 import org.kuali.rice.krad.uif.control.TextControl;
29 import org.kuali.rice.krad.uif.control.UserControl;
30 import org.kuali.rice.krad.uif.element.Action;
31 import org.kuali.rice.krad.uif.element.Image;
32 import org.kuali.rice.krad.uif.element.Message;
33 import org.kuali.rice.krad.uif.field.LinkField;
34
35 import java.util.TreeMap;
36
37 import static junit.framework.Assert.assertFalse;
38 import static junit.framework.Assert.assertTrue;
39 import static org.junit.Assert.assertEquals;
40 import static org.junit.Assert.assertNotNull;
41
42
43
44
45 public class ComponentBaseTest {
46 private Component component;
47 private TreeMap<String, String> dataAttributes;
48
49 @Before
50 public void setUp() throws Exception {
51
52 component = new Action();
53 component.setId("action1");
54
55 dataAttributes = new TreeMap<String, String>();
56
57 dataAttributes.put("iconTemplateName", "cool-icon-%s.png");
58 dataAttributes.put("transitions", "3");
59 dataAttributes.put("growing-seasons", "{summer:'hot', winter:'cold'}");
60 dataAttributes.put("intervals", "{short:2, medium:5, long:13}");
61 component.setDataAttributes(dataAttributes);
62 }
63
64 @Test
65
66
67
68 public void testGetComplexDataAttributesJs() throws Exception {
69 assertNotNull(component.getComplexDataAttributesJs());
70 String expected = "jQuery('#action1').data('growing-seasons', {summer:'hot', winter:'cold'});"
71 + "jQuery('#action1').data('intervals', {short:2, medium:5, long:13});";
72 assertEquals("complex attributes JS string did not match", expected, component.getComplexDataAttributesJs());
73 }
74
75 @Test
76
77
78
79 public void testGetSimpleDataAttributes() throws Exception {
80 assertNotNull(component.getSimpleDataAttributes());
81 String expected = " data-iconTemplateName=\"cool-icon-%s.png\" data-transitions=\"3\"";
82 assertEquals("simple attributes did not match", expected, component.getSimpleDataAttributes());
83 }
84
85 @Test
86
87
88
89 public void testGetAllDataAttributesJs() throws Exception {
90 assertNotNull(component.getAllDataAttributesJs());
91 String expected = "jQuery('#action1').data('growing-seasons', {summer:'hot', winter:'cold'});"
92 + "jQuery('#action1').data('iconTemplateName', 'cool-icon-%s.png');"
93 + "jQuery('#action1').data('intervals', {short:2, medium:5, long:13});"
94 + "jQuery('#action1').data('transitions', 3);";
95 assertEquals("all attributes JS string did not match", expected, component.getAllDataAttributesJs());
96 }
97
98 @Test
99
100
101
102 public void testGetComplexAttributesJSWhenNull () throws Exception{
103 component.setDataAttributes(null);
104 assertEquals("complex attributes JS string did not match", "", component.getComplexDataAttributesJs());
105 }
106
107 @Test
108
109
110
111 public void testGetSimpleDataAttributesWhenNull() throws Exception {
112 component.setDataAttributes(null);
113 assertEquals("simple attributes did not match", "", component.getSimpleDataAttributes());
114 }
115
116 @Test
117
118
119
120 public void testGetAllDataAttributesJsWhenNull() throws Exception {
121 component.setDataAttributes(null);
122 assertEquals("simple attributes did not match", "", component.getAllDataAttributesJs());
123 }
124
125
126
127
128 @Test
129 public void testGetComplexAttributesOverridingControls() {
130 Component[] overridingControls = {new TextControl(), new TextAreaControl(), new FileControl(), new UserControl()};
131 for (int i=0; i<overridingControls.length; i++) {
132 overridingControls[i].setDataAttributes(dataAttributes);
133 assertTrue(overridingControls[i].getClass() + " does not override getComplexAttributes",
134 overridingControls[i].getAllDataAttributesJs().equalsIgnoreCase(
135 overridingControls[i].getComplexDataAttributesJs()));
136 }
137
138 Component[] nonOverridingControls = {new Image(), new Action(), new LinkField(), new Message()};
139 for (Component component: nonOverridingControls) {
140 component.setDataAttributes(dataAttributes);
141 assertFalse(component.getClass() + " should not override getComplexAttributes",
142 component.getAllDataAttributesJs().equalsIgnoreCase(
143 component.getComplexDataAttributesJs()));
144 }
145 }
146 }