001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.form;
016    
017    import static org.easymock.EasyMock.checkOrder;
018    import static org.easymock.EasyMock.expect;
019    
020    import java.util.Locale;
021    
022    import org.apache.hivemind.ClassResolver;
023    import org.apache.hivemind.Resource;
024    import org.apache.hivemind.util.ClasspathResource;
025    import org.apache.tapestry.BaseComponentTestCase;
026    import org.apache.tapestry.IForm;
027    import org.apache.tapestry.IRequestCycle;
028    import org.apache.tapestry.PageRenderSupport;
029    import org.apache.tapestry.TapestryUtils;
030    import org.apache.tapestry.services.Infrastructure;
031    import org.testng.annotations.Test;
032    
033    /**
034     * Tests for {@link org.apache.tapestry.form.FormComponentContributorContextImpl}.
035     * 
036     * @author Howard Lewis Ship
037     * @since 4.0
038     */
039    @Test
040    public class TestFormComponentContributorContext extends BaseComponentTestCase
041    {
042        private IForm newForm(String name)
043        {
044            IForm form = newMock(IForm.class);
045            checkOrder(form, false);
046            
047            expect(form.getName()).andReturn(name);
048    
049            return form;
050        }
051    
052        private IFormComponent newField(IForm form)
053        {
054            IFormComponent field = newMock(IFormComponent.class);
055    
056            expect(field.getForm()).andReturn(form);
057    
058            return field;
059        }
060    
061        private ClassResolver newResolver()
062        {
063            return newMock(ClassResolver.class);
064        }
065    
066        private Infrastructure newInfrastructure(ClassResolver resolver)
067        {
068            Infrastructure inf = newMock(Infrastructure.class);
069    
070            expect(inf.getClassResolver()).andReturn(resolver);
071    
072            return inf;
073        }
074    
075        public void testIncludeClasspathScript()
076        {
077            IForm form = newForm("myform");
078            IFormComponent field = newField(form);
079            ClassResolver resolver = newResolver();
080    
081            IRequestCycle cycle = newCycle();
082    
083            Infrastructure inf = newInfrastructure(resolver);
084    
085            expect(cycle.getInfrastructure()).andReturn(inf);
086    
087            PageRenderSupport prs = newSupport();
088    
089            Resource expected = new ClasspathResource(resolver, "/foo.js");
090    
091            prs.addExternalScript(form, expected);
092    
093            trainGetAttribute(cycle, TapestryUtils.PAGE_RENDER_SUPPORT_ATTRIBUTE, prs);
094    
095            replay();
096    
097            FormComponentContributorContext context = new FormComponentContributorContextImpl(
098                    Locale.ENGLISH, cycle, field);
099    
100            context.includeClasspathScript("/foo.js");
101    
102            verify();
103        }
104    
105        public void testAddSubmitHandler()
106        {
107    
108            IForm form = newForm("myform");
109            IFormComponent field = newField(form);
110            ClassResolver resolver = newResolver();
111            
112            IRequestCycle cycle = newCycle();
113            Infrastructure inf = newInfrastructure(resolver);
114    
115            expect(cycle.getInfrastructure()).andReturn(inf);
116    
117            PageRenderSupport prs = newSupport();
118    
119            prs.addInitializationScript(form, "Tapestry.onsubmit('myform', foo);");
120    
121            trainGetAttribute(cycle, TapestryUtils.PAGE_RENDER_SUPPORT_ATTRIBUTE, prs);
122    
123            replay();
124    
125            FormComponentContributorContext context = new FormComponentContributorContextImpl(
126                    Locale.ENGLISH, cycle, field);
127    
128            context.addSubmitHandler("foo");
129    
130            verify();
131        }
132    
133        private PageRenderSupport newSupport()
134        {
135            return newMock(PageRenderSupport.class);
136        }
137    }