package roboguice.event;
import java.lang.reflect.Method;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import roboguice.RoboGuice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import android.app.Activity;
import android.content.Context;
/**
* @author John Ericksen
*/
@RunWith(RobolectricTestRunner.class)
public class ObservesTypeListenerTest {
protected EventManager eventManager;
protected Injector injector;
protected List<Method> eventOneMethods;
protected List<Method> eventTwoMethods;
protected Context context = new Activity();
@Before
public void setup() throws NoSuchMethodException {
injector = RoboGuice.getInjector(Robolectric.application);
eventManager = injector.getInstance(EventManager.class);
eventOneMethods = ContextObserverTesterImpl.getMethods(EventOne.class);
eventTwoMethods = ContextObserverTesterImpl.getMethods(EventTwo.class);
}
@Test
public void simulateInjection() {
final InjectedTestClass testClass = new InjectedTestClass();
injector.injectMembers(testClass);
eventManager.fire(new EventOne());
testClass.tester.verifyCallCount(eventOneMethods, EventOne.class, 1);
testClass.tester.verifyCallCount(eventTwoMethods, EventTwo.class, 0);
}
@Test(expected = RuntimeException.class)
public void invalidObservesMethodSignature(){
injector.getInstance(MalformedObserves.class);
}
public static class InjectedTestClass{
//CHECKSTYLE:OFF
@Inject protected ContextObserverTesterImpl tester;
//CHECKSTYLE:ON
}
public class MalformedObserves{
public void malformedObserves(int val, @Observes EventOne event){}
}
}