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())); } }
/** * 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()); } } } }
/** * Called when the controller is created. Note that it could be called either when the * controller is instantiated for the first time or restored by views. * <p/> * <p>The model of the controller will be instantiated by model's default no-argument * constructor here whe {@link #modelType()} doesn't return null.</p> */ public void onCreated() { super.onCreated(); if (uiThreadRunner == null) { //Use mvc graph's default uiThreadRunner uiThreadRunner = Mvc.graph().uiThreadRunner; } eventBusC.register(this); }
@After public void tearDown() throws Exception { Mvc.graph().uiThreadRunner = originalUiThreadRunner; }
@Test public void should_be_able_to_schedule_runnable_on_new_thread() { Mvc.graph().inject(this); executorService.submit(new Runnable() { @Override public void run() { } }); } }
@Override @Before public void setUp() throws Exception { super.setUp(); originalUiThreadRunner = Mvc.graph().uiThreadRunner; uiThreadRunnerMock = mock(UiThreadRunner.class); when(uiThreadRunnerMock.isOnUiThread()).thenReturn(true); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Runnable runnable = (Runnable) invocation.getArguments()[0]; runnable.run(); return null; } }).when(uiThreadRunnerMock).post(any(Runnable.class)); Mvc.graph().uiThreadRunner = uiThreadRunnerMock; }
@Test(expected = MvcGraphException.class) public void should_throw_provider_missing_exception_when_locate_an_qualified_class() { class Shop { @Inject private Phone nexus6; } Mvc.graph().inject(new Shop()); }
@Test(expected = MvcGraphException.class) public void should_throw_provider_missing_exception_when_locate_an_unqualified_class() { class Shop { @Inject @Smart private Robot nexus6; } Mvc.graph().inject(new Shop()); }
@Test public void eventBusV_should_post_event_to_ui_thread_strait_away_from_ui_thread() throws Exception { Mvc.graph().inject(this); when(uiThreadRunnerMock.isOnUiThread()).thenReturn(true); eventBusV.post(""); verify(uiThreadRunnerMock, times(2)).isOnUiThread(); verify(uiThreadRunnerMock, times(0)).post(any(Runnable.class)); }
@Test public void eventBusV_should_post_event_to_ui_thread_from_non_ui_thread() throws Exception { Mvc.graph().inject(this); when(uiThreadRunnerMock.isOnUiThread()).thenReturn(false); eventBusV.post(""); verify(uiThreadRunnerMock, times(2)).isOnUiThread(); verify(uiThreadRunnerMock, times(1)).post(any(Runnable.class)); }
T instance; try { instance = Mvc.graph().reference(type, qualifier); } catch (PokeException e) { throw new MvcGraphException(e.getMessage(), e);
@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()); }
@After public void tearDown() throws Exception { Component rootComponent = Mvc.graph().getRootComponent(); rootComponent.detach(overridingComponent); } }
@Override public void setUp() throws Exception { super.setUp(); controller = new CounterMasterController(); Mvc.graph().inject(controller); controller.onCreated(); view = mock(CounterMasterController.View.class); TestUtil.assignControllerView(controller, view); }
@Test public void run_car_with_default_components() { Car car = new Car(); Mvc.graph().inject(car); testDrive(car); }
Mvc.graph().getRootComponent().attach(testComponent, overrideExistingProviders); Mvc.graph().inject(racingCar); Mvc.graph().getRootComponent().detach(testComponent); Car familyCar = new Car(); Mvc.graph().inject(familyCar); System.out.println(); System.out.println("-------------------------------");
@Before public void setUp() throws Exception { super.setUp(); executorService = mock(ExecutorService.class); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Runnable runnable = (Runnable) invocation.getArguments()[0]; runnable.run(); return null; } }).when(executorService).submit(any(Runnable.class)); Mvc.graph().getRootComponent().attach(new Component().register(new Object() { @Provides protected ExecutorService createExecutorService() { return executorService; } }), true); navigationManager = new NavigationManager(); graph.inject(navigationManager); navigationManager.onCreated(); }