View Javadoc

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.view.facelets.updateheadres;
20  
21  import javax.faces.application.StateManager;
22  import javax.faces.component.UICommand;
23  import javax.faces.component.UIComponent;
24  
25  import org.apache.myfaces.mc.test.core.AbstractMyFacesRequestTestCase;
26  import org.apache.myfaces.shared.config.MyfacesConfig;
27  import org.junit.Assert;
28  import org.junit.Test;
29  
30  public class UpdateHeadDynamicViewTestCase extends AbstractMyFacesRequestTestCase
31  {
32  
33      @Override
34      protected boolean isScanAnnotations()
35      {
36          return true;
37      }
38  
39      @Override
40      protected void setUpWebConfigParams() throws Exception
41      {
42          super.setUpWebConfigParams();
43          servletContext.addInitParameter("org.apache.myfaces.annotation.SCAN_PACKAGES","org.apache.myfaces.view.facelets.updateheadres.managed");
44          servletContext.addInitParameter(StateManager.STATE_SAVING_METHOD_PARAM_NAME, StateManager.STATE_SAVING_METHOD_CLIENT);
45          servletContext.addInitParameter("javax.faces.PARTIAL_STATE_SAVING", "true");
46          servletContext.addInitParameter(MyfacesConfig.INIT_PARAM_REFRESH_TRANSIENT_BUILD_ON_PSS, "auto");
47          servletContext.addInitParameter("org.apache.myfaces.STRICT_JSF_2_REFRESH_TARGET_AJAX", "true");
48      }
49      
50      @Test
51      public void testNoUpdateScript1Head() throws Exception
52      {
53          setupRequest("/ajaxContent.xhtml");
54          processLifecycleExecuteAndRender();
55          
56          UIComponent content = facesContext.getViewRoot().findComponent("content");
57          UIComponent page1Button = facesContext.getViewRoot().findComponent("mainForm:page1");
58          
59          client.ajax((UICommand)page1Button, "action", page1Button.getClientId(facesContext), content.getClientId(facesContext), true);
60          
61          processLifecycleExecuteAndRender();
62          String text = getRenderedContent(facesContext);
63          // the inclusion should trigger update head
64          Assert.assertFalse(text.contains("update id=\"javax.faces.ViewHead\""));
65          //System.out.println(text);
66          tearDownRequest();
67      }
68      
69      @Test
70      public void testUpdateScript2Head() throws Exception
71      {
72          setupRequest("/ajaxContent.xhtml");
73          processLifecycleExecuteAndRender();
74          
75          UIComponent content = facesContext.getViewRoot().findComponent("content");
76          UIComponent page2Button = facesContext.getViewRoot().findComponent("mainForm:page2");
77          
78          client.ajax((UICommand)page2Button, "action", page2Button.getClientId(facesContext), content.getClientId(facesContext), true);
79          
80          processLifecycleExecuteAndRender();
81          
82          String text = getRenderedContent(facesContext);
83          // the inclusion should trigger update head
84          Assert.assertTrue(text.contains("update id=\"javax.faces.ViewHead\""));
85          Assert.assertTrue(text.contains("alert(\"script2\");"));
86          //System.out.println(text);
87          tearDownRequest();
88      }
89      
90      @Test
91      public void testUpdateScript3Head() throws Exception
92      {
93          setupRequest("/ajaxContent.xhtml");
94          processLifecycleExecuteAndRender();
95          
96          UIComponent content = facesContext.getViewRoot().findComponent("content");
97          UIComponent page3Button = facesContext.getViewRoot().findComponent("mainForm:page3");
98          
99          client.ajax((UICommand)page3Button, "action", page3Button.getClientId(facesContext), content.getClientId(facesContext), true);
100         
101         processLifecycleExecuteAndRender();
102         
103         String text = getRenderedContent(facesContext);
104         // the inclusion should trigger update head
105         Assert.assertTrue(text.contains("update id=\"javax.faces.ViewHead\""));
106         Assert.assertTrue(text.contains("alert(\"script3\");"));
107         Assert.assertTrue(text.contains("link rel=\"stylesheet\" media=\"screen\" type=\"text/css\" href=\"/test/faces/javax.faces.resource/style3.css\""));
108         //System.out.println(text);
109         tearDownRequest();
110     }
111    
112 }