001    // Copyright 2004, 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    package org.apache.tapestry.event;
015    
016    import org.apache.hivemind.ApplicationRuntimeException;
017    import org.apache.tapestry.BaseComponentTestCase;
018    import org.apache.tapestry.IRequestCycle;
019    import static org.easymock.EasyMock.expect;
020    import org.testng.annotations.Test;
021    
022    /**
023     * Tests how {@link BrowserEvent} extracts itself from the {@link IRequestCycle}.
024     */
025    @Test
026    public class BrowserEventTest extends BaseComponentTestCase
027    {
028        public void test_Construct_And_Read_Method_Arguments()
029        {
030            IRequestCycle cycle = newCycle();
031            trainCycleForStandardBrowserEvent(cycle);
032            
033            expect(cycle.getParameter(BrowserEvent.METHOD_ARGUMENTS)).andReturn("[1,2]");
034    
035            replay();
036    
037            BrowserEvent event = new BrowserEvent(cycle);
038    
039            verify();
040    
041            assertEquals(event.getMethodArguments().getInt(0), 1);
042            assertEquals(event.getMethodArguments().getInt(1), 2);
043        }
044    
045        @Test(expectedExceptions = ApplicationRuntimeException.class)
046        public void test_Unparseable_JSON_Method_Arguments()
047        {
048            IRequestCycle cycle = newCycle();
049            
050            trainCycleForStandardBrowserEvent(cycle);
051            
052            expect(cycle.getParameter(BrowserEvent.METHOD_ARGUMENTS)).andReturn("*/utterRubb???sh");
053    
054            replay();
055    
056            BrowserEvent event = new BrowserEvent(cycle);
057    
058            verify();
059    
060            event.getMethodArguments();
061        }
062    
063        private void trainCycleForStandardBrowserEvent(IRequestCycle cycle)
064        {
065            expect(cycle.getParameter(BrowserEvent.NAME)).andReturn("onClick").anyTimes();
066    
067            expect(cycle.getParameter(BrowserEvent.TYPE)).andReturn("click");
068            expect(cycle.getParameters(BrowserEvent.KEYS)).andReturn(null);
069            expect(cycle.getParameter(BrowserEvent.CHAR_CODE)).andReturn(null);
070            expect(cycle.getParameter(BrowserEvent.PAGE_X)).andReturn("123");
071            expect(cycle.getParameter(BrowserEvent.PAGE_Y)).andReturn("1243");
072            expect(cycle.getParameter(BrowserEvent.LAYER_X)).andReturn(null);
073            expect(cycle.getParameter(BrowserEvent.LAYER_Y)).andReturn(null);
074            expect(cycle.getParameter(BrowserEvent.COMPONENT_ID)).andReturn(null);
075            expect(cycle.getParameter(BrowserEvent.COMPONENT_ID_PATH)).andReturn(null);
076            
077            expect(cycle.getParameter(BrowserEvent.TARGET + "." + BrowserEvent.TARGET_ATTR_ID)).andReturn("element1");
078        }
079    }