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