@Test public void testInjectionOfMockController() throws Exception { final PrintController mockPrintController = mock(PrintController.class); graph.getRootComponent().register(new Provider<PrintController>(PrintController.class) { @Override protected PrintController createInstance() throws ProvideException { return mockPrintController; } }); TestView testView = new TestView(); graph.inject(testView); testView.present(); verify(mockPrintController, times(1)).print(); }
@Test (expected = IllegalStateException.class) public void should_throw_out_exceptions_when_registering_component() throws ProvideException, ProviderConflictException, Graph.IllegalRootComponentException { // Arrange MvcComponent badComponent = mock(MvcComponent.class); MvcGraph mvcGraph = new MvcGraph(); mvcGraph.setRootComponent(badComponent); Object obj = new Object(); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { throw new IllegalStateException(); } }).when(badComponent).register(any(Object.class)); // Act mvcGraph.getRootComponent().register(obj); }
/** * Internal use. Don't do it in your app. */ void destroy() { if (onSettled != null) { onSettled.run(); } if (pendingReleaseInstances != null) { for (PendingReleaseInstance i : pendingReleaseInstances) { try { Mvc.graph().dereference(i.instance, i.type, i.qualifier); } catch (ProviderMissingException e) { //should not happen //in case this happens just logs it navigationManager.logger.warn("Failed to auto release {} after navigation settled", i.type.getName()); } } } }
graph = new MvcGraph(); graph.getRootComponent().register(new Object() { @Provides @EventBusC
graph.use(Os.class, iosQualifier, new Consumer<Os>() { @Override public void consume(Os instance) { graph.inject(device); //OsReferenceCount = 1 graph.use(Os.class, iosQualifier, new Consumer<Os>() { @Override public void consume(Os instance) { graph.release(device); //OsReferenceCount = 0 graph.use(Os.class, iosQualifier, new Consumer<Os>() { @Override public void consume(Os instance) { graph.use(Os.class, iosQualifier, new Consumer<Os>() { @Override public void consume(Os instance) { graph.use(Os.class, iosQualifier, new Consumer<Os>() { @Override public void consume(Os instance) { graph.release(device); //OsReferenceCount = 0
static void runCarWithDefaultComponent() { Car car = new Car(); Mvc.graph().inject(car); System.out.println("New car built"); System.out.println(String.format("Speed: %f", car.getSpeed())); car.accelerate(); System.out.println("Accelerate"); System.out.println(String.format("Speed: %f", car.getSpeed())); car.decelerate(); System.out.println("Decelerate"); System.out.println(String.format("Speed: %f", car.getSpeed())); } }
@Test public void use_method_should_retain_and_release_instance_without_qualifier_correctly() throws ProvideException, ProviderConflictException { graph.getRootComponent().register(new DeviceModule()); graph.use(Os.class, new Consumer<Os>() { @Override public void consume(Os instance) { graph.inject(device); //OsReferenceCount = 1 graph.use(Os.class, new Consumer<Os>() { @Override public void consume(Os instance) { graph.release(device); //OsReferenceCount = 0 graph.use(Os.class, new Consumer<Os>() { @Override public void consume(Os instance) { graph.use(Os.class, new Consumer<Os>() { @Override public void consume(Os instance) { graph.use(Os.class, new Consumer<Os>() { @Override public void consume(Os instance) { graph.release(device); //OsReferenceCount = 0
@Test public void should_be_able_to_reinject_new_instance_without_default_constructor() throws ProvideException, ProviderConflictException, ProviderMissingException, CircularDependenciesException { AndroidModule component = new AndroidModule(); graph.getRootComponent().register(component); AndroidView androidView = new AndroidView(); Assert.assertNull(androidView.androidPart); graph.inject(androidView); Assert.assertNotNull(androidView.androidPart); graph.release(androidView); graph.inject(androidView); Assert.assertNotNull(androidView.androidPart); }
private int getGraphSize() { Map<String, Object> cache = graph.getRootComponent().getCache(); return cache == null ? 0 :cache.size(); }
graph.registerMonitor(monitor); graph.unregisterMonitor(monitor); graph.clearMonitors(); Provider.DereferenceListener providerFreedListener = mock(Provider.DereferenceListener.class); graph.registerDereferencedListener(providerFreedListener); graph.unregisterDereferencedListener(providerFreedListener); graph.clearDereferencedListeners();
@Test(expected = MvcGraphException.class) public void should_catch_invocation_exception_when_NPE_detected_on_injection() throws Exception { Object com = new Object() { @Provides @Singleton @Slower2 TimerController timerSlowerX2() { return new TimerController() { { onCreated(); } @Override public void setInitialValue(long value) { super.setInitialValue(value * 2); } }; } }; graph.getRootComponent().register(com); Annotation slower2Qualifier = SlowXHolder.class.getAnnotation(Slower2.class); graph.use(TimerController.class, slower2Qualifier, new Consumer<TimerController>() { @Override public void consume(TimerController instance) { //Controller should have now been released Assert.assertEquals(0, instance.getInitialValue()); } }); graph.getRootComponent().unregister(com); }
graph.setRootComponent((MvcComponent) new MvcComponent("Root").register(new Object() { @Provides @EventBusC graph.inject(myManager);
@Before public void setUp() throws Exception { graph = new MvcGraph(); graph.setRootComponent((MvcComponent) new MvcComponent("Root").register(new Object() { @Provides @EventBusC public EventBus eventBusC() { return new EventBusImpl(); } @Provides @EventBusV public EventBus eventBusV() { return new EventBusImpl(); } @Provides public ExecutorService provideExe() { return mock(ExecutorService.class); } })); }
@Test(expected = MvcGraphException.class) public void should_throw_exception_when_mvc_graph_reference_on_non_main_thread() throws ProvideException, CircularDependenciesException, ProviderMissingException { graph.reference(String.class, null); when(graph.uiThreadRunner.isOnUiThread()).thenReturn(false); graph.reference(String.class, null); }
@Test public void should_throw_exception_when_mvc_graph_clear_monitors_on_non_main_thread() throws Graph.IllegalRootComponentException { when(graph.uiThreadRunner.isOnUiThread()).thenReturn(false); graph.clearMonitors(); when(graph.uiThreadRunner.isOnUiThread()).thenReturn(true); graph.clearMonitors(); }
@Test public void should_throw_exception_when_mvc_graph_clear_deferenceListeners_on_non_main_thread() throws Graph.IllegalRootComponentException { when(graph.uiThreadRunner.isOnUiThread()).thenReturn(false); graph.clearDereferencedListeners(); when(graph.uiThreadRunner.isOnUiThread()).thenReturn(true); graph.clearDereferencedListeners(); }
@Test(expected = MvcGraphException.class) public void should_raise_mvc_graph_exception_when_inject_on_poke_exception() { class View { @Inject UnimplementedInterface unimplementedInterface; } graph.inject(new View()); }
@Test public void testOnDisposeShouldBeCalledWhenControllerReleased() throws Exception { final LifeCycleTestController.Proxy lifeCycleProxy = mock(LifeCycleTestController.Proxy.class); graph.getRootComponent().register(new LifeCycleTestControllerModule(lifeCycleProxy)); TestLifCycleView testView = new TestLifCycleView(); verify(lifeCycleProxy, times(0)).onCreated(); graph.inject(testView); verify(lifeCycleProxy, times(1)).onCreated(); verify(lifeCycleProxy, times(0)).onDestroy(); TestLifCycleView testView1 = new TestLifCycleView(); graph.inject(testView1); //Should be 1 still since a cached instance will be reused. verify(lifeCycleProxy, times(1)).onCreated(); verify(lifeCycleProxy, times(0)).onDestroy(); graph.release(testView1); verify(lifeCycleProxy, times(0)).onDestroy(); graph.release(testView); verify(lifeCycleProxy, times(1)).onDestroy(); }
@Test public void should_throw_MvcGraphException_when_mvcGraph_destroy_method_encounters_PokeException() throws ProvideException, ProviderConflictException, ProviderMissingException { navigationManager.logger = mock(Logger.class); Navigator navigator = navigationManager.navigate(this); Mvc.graph().getRootComponent().register(new Object(){ @Provides public X x() { return new X_1(); } }); navigator.with(X.class); Mvc.graph().getRootComponent().unregister(X.class, null); navigator.destroy(); verify(navigationManager.logger).warn(anyString(), anyString()); }