001    /*
002     * Copyright 2006-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package edu.samplu.travel.krad.test;
017    
018    import edu.samplu.common.ITUtil;
019    import edu.samplu.common.UpgradedSeleniumITBase;
020    import org.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    import org.junit.Test;
023    import org.kuali.rice.krad.uif.UifConstants;
024    
025    import java.util.HashMap;
026    import java.util.Map;
027    
028    import static org.junit.Assert.*;
029    
030    /**
031     *  Tests that the data attributes are rendered as expected for all controls
032     * 
033     * @author Kuali Rice Team (rice.collab@kuali.org)
034     */
035    public class UifDataAttributesIT extends UpgradedSeleniumITBase {
036        @Override
037        public String getTestUrl() {
038            return ITUtil.PORTAL;
039        }
040    
041        private  Log log = LogFactory.getLog(getClass());
042    
043        /**
044         * verify that a tag has simple data attributes
045         *
046         * @param tag - html tag e.g. img or a
047         * @param tagId - derived from the bean id set in the view
048         * @param tagIdSuffix - where applicable, a suffix that is appended to the control by krad e.g. _control
049         */
050        private void verifySimpleAttributes(String tag, String tagId, String tagIdSuffix) {
051            // test the attributes that are set via the data attributes list
052            tagId = tagId + tagIdSuffix;
053            String simpleAttributesXpath="//" + tag + "[(@id='" + tagId + "') and (@data-iconTemplateName='cool-icon-%s.png') and (@data-transitions='3')]";
054            assertTrue(tagId + " does not have simple data attributes (via list) present", isElementPresent(simpleAttributesXpath));
055            verifyStaticDataAttributes(tag, tagId);
056    
057        }
058    
059        /**
060         * test the attributes that are set via the data*Attribute properties
061         *
062         * @param tag - html tag e.g. img or a
063         * @param tagId - the html tag id - a combination of bean id and any suffix
064         */
065        private void verifyStaticDataAttributes(String tag, String tagId) {
066            final String simpleAttributesXpath;
067            simpleAttributesXpath="//" + tag + "[(@id='" + tagId + "')"
068                    + " and (@data-role='role') and (@data-type='type') and (@data-meta='meta')]";
069            assertTrue(tagId + " does not have simple data attributes (via data*Attribute) properties present",
070                    isElementPresent(simpleAttributesXpath));
071        }
072    
073        /**
074         * check that complex attributes exist in the script
075         *
076         * @param tagId - the expected tag id
077         * @param suffix - the expected suffix e.g. _button
078         */
079        private void verifyComplexAttributes(String tagId, String suffix) {
080            tagId = tagId + suffix;
081            String complexAttributesXpath="//input[(@type='hidden') and (@data-role='dataScript') and (@data-for='"+ tagId +  "')]";
082            assertTrue(tagId + ": complex data attributes script not found", isElementPresent(complexAttributesXpath));
083    
084            // the message field does not support complex attributes
085            //if (!tagId.equalsIgnoreCase("messageField")) {
086                String scriptValue = getAttribute(complexAttributesXpath + "@value");
087                assertNotNull("script value is null",scriptValue);
088            boolean ok = scriptValue.contains(
089                    "jQuery('#" + tagId + "').data('capitals', {kenya:'nairobi', uganda:'kampala', tanzania:'dar'});")
090                    && scriptValue.contains("jQuery('#" + tagId + "').data('intervals', {short:2, medium:5, long:13});");
091            if (!ok) {
092                log.info("scriptValue for " + tagId + " is " + scriptValue);
093            }
094            // check for complex attributes
095            assertTrue(tagId + ": complex attributes script does not contain expected code", ok);
096            //}
097        }
098    
099        /**
100         * check that all attributes exist in the script
101         *
102         * @param tagId - the expected tag id
103         * @param suffix - the expected suffix e.g. _control
104         * @return true if all attributes were found in script, false otherwise
105         */
106        private boolean verifyAllAttributesInScript(String tagId, String suffix) {
107            tagId = tagId + suffix;
108            String complexAttributesXpath="//input[(@type='hidden') and (@data-for='"+ tagId +  "')]";
109            assertTrue(tagId + ": complex data attributes script not found see https://jira.kuali.org/browse/KULRICE-7752", isElementPresent(complexAttributesXpath));
110    
111            // the message field does not support complex attributes
112            String scriptValue = getAttribute(complexAttributesXpath + "@value");
113            assertNotNull("script value is null",scriptValue);
114            // log.info("scriptValue for " + tagId + " is " + scriptValue);
115            return scriptValue.contains("jQuery('#" + tagId + "').data('transitions', 3);") &&
116                    scriptValue.contains("jQuery('#" + tagId + "').data('iconTemplateName', 'cool-icon-%s.png');") &&
117                    scriptValue.contains("jQuery('#" + tagId + "').data('capitals', {kenya:'nairobi', uganda:'kampala', tanzania:'dar'});") &&
118                    scriptValue.contains("jQuery('#" + tagId + "').data('intervals', {short:2, medium:5, long:13});");
119        }
120    
121    
122        /**
123         * Tests that the data attributes are rendered as expected for all controls
124         */
125        @Test
126        public void testDataAttributesPresentInControls () {
127            assertEquals("Kuali Portal Index", getTitle());
128            open( "/kr-dev/kr-krad/data-attributes-test-uif-controller?viewId=dataAttributesView_selenium&methodToCall=start");
129            waitForPageToLoad(); // if this times out make a special one that 50000
130    
131            // custom suffix to mark  test bean ids
132            String testIdSuffix = "_attrs";
133            // input fields, whose controls are implemented as spring form tags, will have both simple and complex attributes set via a script
134            // KULRICE-7752 : UifDataAttributesIT testDataAttributesPresentInControls textInputField_attrs_control: complex data attributes script not found
135            String[] inputControls = {"textInputField", "textAreaInputField", "dropDown", "datePicker", "fileUpload", "userControl",
136                    "spinnerControl", "hiddenControl", "checkBox"};//, "radioButton",
137            for (int i=0; i<inputControls.length; i++) {
138                assertTrue(inputControls[i] + ": script does not contain expected code",
139                        verifyAllAttributesInScript(inputControls[i], testIdSuffix + UifConstants.IdSuffixes.CONTROL));
140                String tag = "input";
141                if (inputControls[i].equalsIgnoreCase("textAreaInputField")) {
142                    tag = "textarea";
143                } else if (inputControls[i].equalsIgnoreCase("dropDown")) {
144                    tag = "select";
145                }
146                try {
147                    verifyStaticDataAttributes(tag, inputControls[i] + testIdSuffix + UifConstants.IdSuffixes.CONTROL);
148                } catch (AssertionError ae) {
149                    assertTrue("KULRICE-7752 : UifDataAttributesIT testDataAttributesPresentInControls textInputField_attrs_control: complex data attributes script not found", false);
150                }
151            }
152            // these controls allow for simple attributes on the tag and complex attributes via js
153            Map<String, String[]> otherControlsMap = new HashMap<String, String[]>();
154            // controls whose simple attributes are set in an img tag
155            String[] imgControls = {"imageField_image"};
156            // fields whose simple attributes are set in an anchor tag
157            String[] anchorFields = {"navigationLink", "actionLink-noImage", "actionLink-imageRight", "actionLink-imageLeft",
158                    "linkField", "linkElement"};
159            // fields whose simple attributes are set in a span tag
160            String[] spanFields = {"messageField", "spaceField"};
161            // fields whose simple attributes are set in an input tag
162            String[] inputFields = {"imageAction"};
163            // fields whose simple attributes are set in button tag
164            String[] buttonElements = {"buttonTextOnly", "buttonImageBottom", "buttonImageLeft", "buttonImageTop", "buttonImageRight"};
165            // iframe field
166            String[] iframeField = {"iframe"};
167            
168            otherControlsMap.put("img", imgControls);
169            otherControlsMap.put("a", anchorFields);
170            otherControlsMap.put("span", spanFields);
171            otherControlsMap.put("input", inputFields);
172            otherControlsMap.put("button", buttonElements);
173            otherControlsMap.put("iframe", iframeField);
174    
175            // a map to hold the tags where the simple attributes are affixed
176            // if a tag is not here, a empty string will be used for the suffix
177            Map<String, String> simpleTagIdSuffix = new HashMap<String, String>();
178            simpleTagIdSuffix.put("span", "_span");
179            
180    
181            for (String tag: otherControlsMap.keySet()) {
182                String[] controlIds = otherControlsMap.get(tag);
183                for (int i=0; i<controlIds.length; i++) {
184                    String tagId = controlIds[i];
185    
186                    // check for complex attributes
187                    verifyComplexAttributes(tagId, testIdSuffix);
188    
189                    // determine whether we are using a tag id suffix for the simple attributes
190                    String tagIdSuffix = testIdSuffix;
191                    if (simpleTagIdSuffix.containsKey(tag)) {
192                        tagIdSuffix = tagIdSuffix + simpleTagIdSuffix.get(tag);
193                    }
194    
195                    // check for simple attributes
196                    verifySimpleAttributes(tag, tagId, tagIdSuffix);
197                }
198                
199                // test label field - which uses the tagId suffix for both the simple attributes and complex
200                String tagId = "textInputField";
201                String tagIdSuffix = testIdSuffix + "_label";
202                // check for complex attributes
203                verifyComplexAttributes(tagId, tagIdSuffix);
204                // check for simple attributes
205                verifySimpleAttributes("label", tagId, tagIdSuffix);
206    
207                //test that the radio buttons have the 3 data attributes that can appear in the tag
208                tagId = "radioButton" + testIdSuffix + UifConstants.IdSuffixes.CONTROL;
209                String[] radioButtonIds = {tagId + "1", tagId + "2"};
210                for (String id: radioButtonIds) {
211                    verifyStaticDataAttributes("input", id);
212                }
213                //test that all complex and simple attributes set via the list are in a script
214                verifyAllAttributesInScript(tagId, "");
215            }
216        }
217    }