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