1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.custom.inputAjax;
20  
21  import javax.faces.application.FacesMessage;
22  
23  import junit.framework.Test;
24  import junit.framework.TestSuite;
25  
26  import org.apache.myfaces.component.html.ext.HtmlInputText;
27  import org.apache.myfaces.custom.form.HtmlForm;
28  import org.apache.myfaces.test.AbstractTomahawkViewControllerTestCase;
29  import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
30  import org.apache.myfaces.test.utils.HtmlRenderedAttr;
31  import org.apache.shale.test.mock.MockResponseWriter;
32  
33  public class HtmlMessageRendererTest extends AbstractTomahawkViewControllerTestCase
34  {
35      private static final String ERROR_CLASS = "errorClass";
36      private static final String WARN_CLASS = "warnClass";
37      private static final String INFO_CLASS = "infoClass";
38      
39      private HtmlMessage message;
40      private HtmlForm form;
41      private HtmlInputText inputText;
42  
43      public HtmlMessageRendererTest(String name)
44      {
45          super(name);
46      }
47      
48      public static Test suite() {
49          return new TestSuite(HtmlMessageRendererTest.class);
50      }
51  
52      public void setUp() throws Exception
53      {
54          super.setUp();
55  
56          form = new HtmlForm();
57          inputText = new HtmlInputText();
58          inputText.setParent(form);
59          inputText.setId("myInputId");
60          
61          message = new HtmlMessage();
62          message.setErrorClass(ERROR_CLASS);
63          message.setWarnClass(WARN_CLASS);
64          message.setInfoClass(INFO_CLASS);
65          message.setParent(form);
66          
67          form.getChildren().add(inputText);
68          form.getChildren().add(message);
69          
70          facesContext.addMessage(inputText.getClientId(facesContext), 
71                  new FacesMessage("Validation message here."));
72      }
73  
74      public void tearDown() throws Exception
75      {
76          super.tearDown();
77      }    
78      
79      public void testHtmlPropertyPassTru() throws Exception
80      {
81          HtmlRenderedAttr[] attrs = {
82              //_UniversalProperties
83              new HtmlRenderedAttr("dir"), 
84              new HtmlRenderedAttr("lang"), 
85              new HtmlRenderedAttr("title"),
86              //_EventProperties
87              new HtmlRenderedAttr("onclick"), 
88              new HtmlRenderedAttr("ondblclick"), 
89              new HtmlRenderedAttr("onkeydown"), 
90              new HtmlRenderedAttr("onkeypress"),
91              new HtmlRenderedAttr("onkeyup"), 
92              new HtmlRenderedAttr("onmousedown"), 
93              new HtmlRenderedAttr("onmousemove"), 
94              new HtmlRenderedAttr("onmouseout"),
95              new HtmlRenderedAttr("onmouseover"), 
96              new HtmlRenderedAttr("onmouseup"),
97              //_StyleProperties
98              new HtmlRenderedAttr("style"), 
99              new HtmlRenderedAttr("styleClass", "styleClass", "class=\"infoClass\""),
100         };
101         
102         facesContext.addMessage("test1", new FacesMessage(FacesMessage.SEVERITY_WARN, "warnSumary", "detailWarnSummary"));
103         message.setStyle("left: 48px; top: 432px; position: absolute");
104         message.setFor("myInputId");
105         
106         MockResponseWriter writer = (MockResponseWriter)facesContext.getResponseWriter();
107         HtmlCheckAttributesUtil.checkRenderedAttributes(
108                 message, facesContext, writer, attrs);
109         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
110             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
111         }
112     }
113     
114     public void testHtmlPropertyPassTruNotRendered() throws Exception
115     {
116         HtmlRenderedAttr[] attrs = HtmlCheckAttributesUtil.generateAttrsNotRenderedForReadOnly();
117         
118         facesContext.addMessage("test1", new FacesMessage(FacesMessage.SEVERITY_WARN, "warnSumary", "detailWarnSummary"));
119         message.setStyle("left: 48px; top: 432px; position: absolute");
120         message.setFor("myInputId");
121         
122         MockResponseWriter writer = (MockResponseWriter)facesContext.getResponseWriter();
123         HtmlCheckAttributesUtil.checkRenderedAttributes(
124                 message, facesContext, writer, attrs);
125         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
126             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
127         }
128     }
129 }