PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/sigmah/src/test/java/org/sigmah/client/mock/MockEventBus.java

http://sigma-h.googlecode.com/
Java | 77 lines | 49 code | 21 blank | 7 comment | 6 complexity | 436c7e750eae53575c255b8fae2ba358 MD5 | raw file
 1/*
 2 * All Sigmah code is released under the GNU General Public License v3
 3 * See COPYRIGHT.txt and LICENSE.txt.
 4 */
 5
 6package org.sigmah.client.mock;
 7
 8import com.extjs.gxt.ui.client.event.BaseEvent;
 9import com.extjs.gxt.ui.client.event.BaseObservable;
10import com.extjs.gxt.ui.client.event.EventType;
11import org.junit.Assert;
12import org.sigmah.client.EventBus;
13import org.sigmah.client.event.NavigationEvent;
14
15import java.util.ArrayList;
16import java.util.List;
17
18import static org.junit.Assert.assertTrue;
19
20/**
21 * @author Alex Bertram (akbertram@gmail.com)
22 */
23public class MockEventBus extends BaseObservable implements EventBus {
24
25    public List<BaseEvent> eventLog = new ArrayList<BaseEvent>();
26
27   
28    
29    @Override
30    public boolean fireEvent(EventType eventType, BaseEvent be) {
31        
32        eventLog.add(be);
33
34        return super.fireEvent(eventType, be);
35
36    }
37
38	@Override
39	public boolean fireEvent(BaseEvent event) {
40		return fireEvent(event.getType(), event);
41	}
42
43    public void assertEventFired(BaseEvent event) {
44        Assert.assertTrue(eventLog.contains(event));
45    }
46
47    public int getEventCount(EventType type) {
48        int count = 0;
49        for(BaseEvent event : eventLog) {
50
51            if(event.getType() == type) {
52                count++;
53            }
54       }
55        return count;
56    }
57
58
59
60
61    public <T> T getLastNavigationEvent(Class<T> placeClass) {
62        for(int i=eventLog.size()-1; i>=0;i--) {
63            BaseEvent event = eventLog.get(i);
64            if(event instanceof NavigationEvent) {
65                NavigationEvent nevent = (NavigationEvent)event;
66                if(placeClass.isAssignableFrom(nevent.getPlace().getClass())) {
67                    return (T)nevent.getPlace();
68                }
69            }
70        }
71        return null;
72    }
73
74    public void assertNotFired(EventType eventType) {
75        assertTrue("eventType" + eventType.toString() + " has not been fired", getEventCount(eventType)==0);
76    }
77}