001    // Copyright Oct 7, 2006 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    package org.apache.tapestry.dojo;
015    
016    import org.apache.hivemind.Resource;
017    import org.apache.tapestry.*;
018    import org.apache.tapestry.engine.IEngineService;
019    import org.apache.tapestry.engine.ILink;
020    import static org.easymock.EasyMock.checkOrder;
021    import static org.easymock.EasyMock.expect;
022    import org.testng.annotations.Test;
023    
024    import java.util.Locale;
025    
026    
027    /**
028     * Tests basic functionality of {@link AjaxShellDelegate}.
029     * 
030     * @author jkuhnert
031     */
032    @Test(sequential=true)
033    @SuppressWarnings("all")
034    public class AjaxShellDelegateTest extends BaseComponentTestCase
035    {
036        private static final String SYSTEM_NEWLINE= (String)java.security.AccessController.doPrivileged(
037                new sun.security.action.GetPropertyAction("line.separator"));
038    
039        void trainStaticPath(IEngineService engine, IAsset asset, String path)
040        {
041            Resource res = newMock(Resource.class);
042            
043            expect(asset.getResourceLocation()).andReturn(res);
044            expect(res.getPath()).andReturn(path);
045            
046            ILink link = newLink();
047            
048            expect(engine.getLink(Boolean.TRUE, path)).andReturn(link);
049            expect(link.getURL()).andReturn("http://" + path);
050        }
051        
052        void trainPageLocale(IRequestCycle cycle, Locale locale)
053        {
054            IPage page = newMock(IPage.class);        
055            
056            expect(cycle.getPage()).andReturn(page);
057            expect(page.getLocale()).andReturn(locale);
058        }
059        
060        public void test_Default_Render()
061        {
062            IAsset dojoSource = newAsset();
063            IAsset dojoPath = newAsset();
064            IAsset tSource = newAsset();
065            IAsset tPath = newAsset();
066            IPage page = newMock(IPage.class);
067            checkOrder(page, false);
068            IEngine engine = newMock(IEngine.class);
069            
070            IRequestCycle cycle = newCycle();
071            IMarkupWriter writer = newBufferWriter();
072    
073            expect(cycle.getEngine()).andReturn(engine);
074            expect(engine.getOutputEncoding()).andReturn("utf-foo");
075            expect(cycle.getPage()).andReturn(page);
076            expect(dojoPath.buildURL()).andReturn("http:///dojo/path");
077    
078            expect(page.hasFormComponents()).andReturn(false).anyTimes();
079            expect(page.hasWidgets()).andReturn(false).anyTimes();
080    
081            trainPageLocale(cycle, Locale.US);
082            
083            expect(dojoSource.buildURL()).andReturn("http:///dojo/path/dojo.js");
084            expect(tPath.buildURL()).andReturn("/tapestry");
085            expect(tSource.buildURL()).andReturn("/tapestry/tapestry.js");
086            
087            AjaxShellDelegate d = new AjaxShellDelegate();
088            d.setDojoPath(dojoPath);
089            d.setDojoSource(dojoSource);
090            d.setTapestrySource(tSource);
091            d.setTapestryPath(tPath);
092            
093            replay();
094            
095            d.render(writer, cycle);
096            
097            verify();
098            
099            assertBuffer("<script type=\"text/javascript\">djConfig = {"
100                    + "\"baseRelativePath\":\"http:///dojo/path\","
101                    +"\"parseWidgets\":false,\"locale\":\"en-us\"} </script>" + SYSTEM_NEWLINE +
102                    SYSTEM_NEWLINE + 
103                    "<script type=\"text/javascript\" src=\"http:///dojo/path/dojo.js\"></script>" + SYSTEM_NEWLINE
104                    + "<script type=\"text/javascript\">" + SYSTEM_NEWLINE + 
105                    "dojo.registerModulePath(\"tapestry\", \"/tapestry\");" + SYSTEM_NEWLINE +
106                    "</script>" + SYSTEM_NEWLINE +
107                    "<script type=\"text/javascript\" src=\"/tapestry/tapestry.js\"></script>" + SYSTEM_NEWLINE +
108                    "<script type=\"text/javascript\">" + SYSTEM_NEWLINE +
109                    "dojo.require(\"tapestry.namespace\");" + SYSTEM_NEWLINE +
110                    "tapestry.requestEncoding='utf-foo';" + SYSTEM_NEWLINE + 
111                    "</script>" + SYSTEM_NEWLINE);
112        }
113        
114        public void test_Debug_Render()
115        {
116            IAsset dojoSource = newAsset();
117            IAsset dojoPath = newAsset();
118            IAsset tSource = newAsset();
119            IAsset tPath = newAsset();
120            IPage page = newMock(IPage.class);
121            checkOrder(page, false);
122            IEngine engine = newMock(IEngine.class);
123            
124            IRequestCycle cycle = newCycle();
125            IMarkupWriter writer = newBufferWriter();
126    
127            expect(cycle.getEngine()).andReturn(engine);
128            expect(engine.getOutputEncoding()).andReturn("utf-foo");
129            expect(cycle.getPage()).andReturn(page);
130            expect(dojoPath.buildURL()).andReturn("http:///dojo/path");
131    
132            expect(page.hasFormComponents()).andReturn(false).anyTimes();
133            expect(page.hasWidgets()).andReturn(false).anyTimes();
134    
135            trainPageLocale(cycle, Locale.UK);
136            
137            expect(dojoSource.buildURL()).andReturn("http:///dojo/path/dojo.js");
138            expect(tPath.buildURL()).andReturn("/tapestry");
139            expect(tSource.buildURL()).andReturn("/tapestry/tapestry.js");
140            
141            AjaxShellDelegate d = new AjaxShellDelegate();
142            d.setDojoPath(dojoPath);
143            d.setDojoSource(dojoSource);
144            d.setTapestrySource(tSource);
145            d.setTapestryPath(tPath);
146            d.setDebug(true);
147            d.setLogLevel(AjaxShellDelegate.BROWSER_LOG_DEBUG);
148            d.setConsoleEnabled(true);
149            d.setSearchIds("['treeId']");
150            
151            replay();
152            
153            d.render(writer, cycle);
154            
155            verify();
156            
157            assertBuffer("<script type=\"text/javascript\">djConfig = {\"isDebug\":true,\"baseRelativePath\":\"http:///dojo/path\"," +
158                         "\"parseWidgets\":false,\"searchIds\":['treeId'],\"locale\":\"en-gb\"} </script>" + SYSTEM_NEWLINE +
159                         SYSTEM_NEWLINE +
160                         "<script type=\"text/javascript\" src=\"http:///dojo/path/dojo.js\"></script>" + SYSTEM_NEWLINE +
161                         "<script type=\"text/javascript\">" + SYSTEM_NEWLINE +
162                         "dojo.require(\"dojo.debug.console\");" + SYSTEM_NEWLINE +
163                         "dojo.log.setLevel(dojo.log.getLevel(\"DEBUG\"));" + SYSTEM_NEWLINE +
164                         "</script>" + SYSTEM_NEWLINE +
165                         "<script type=\"text/javascript\">" + SYSTEM_NEWLINE +
166                         "dojo.registerModulePath(\"tapestry\", \"/tapestry\");" + SYSTEM_NEWLINE +
167                         "</script>" + SYSTEM_NEWLINE +
168                         "<script type=\"text/javascript\" src=\"/tapestry/tapestry.js\"></script>" + SYSTEM_NEWLINE +
169                         "<script type=\"text/javascript\">" + SYSTEM_NEWLINE +
170                         "dojo.require(\"tapestry.namespace\");" + SYSTEM_NEWLINE +
171                         "tapestry.requestEncoding='utf-foo';" + SYSTEM_NEWLINE + 
172                         "</script>" + SYSTEM_NEWLINE);
173        }
174    }