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(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 should_be_able_to_schedule_runnable_on_new_thread() { Mvc.graph().inject(this); executorService.submit(new Runnable() { @Override public void run() { } }); } }
@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)); }
@Test public void dependenciesOfBaseControllerImplShouldBeInjected() throws Exception{ TestBadView testView = new TestBadView(); graph.inject(testView); } }
@Override public void consume(Os instance) { //OsReferenceCount = 1 graph.inject(device); //Injection will reuse the cached instance and increment the reference count //OsReferenceCount = 2 //Since the cached instance is cleared, the new instance is a newly created one. Assert.assertTrue(device.os == instance); Assert.assertTrue(instance instanceof iOS); } });
@Override public void consume(Os instance) { //OsReferenceCount = 1 graph.inject(device); //Injection will reuse the cached instance and increment the reference count //OsReferenceCount = 2 //Since the cached instance is cleared, the new instance is a newly created one. Assert.assertTrue(device.android == instance); Assert.assertTrue(instance instanceof Android); } });
@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 injectedControllerShouldBeSingleton() throws Exception { TestView testView = new TestView(); graph.inject(testView); TestView testView1 = new TestView(); graph.inject(testView1); Assert.assertTrue(testView != testView1); Assert.assertTrue(testView.printController == testView1.printController); }
@Before public void setUp() throws Exception { super.setUp(); executorService = Executors.newSingleThreadExecutor(); controller = new MyController(); graph.inject(controller); controller.onCreated(); view = mock(MyController.View.class); eventBusV.register(view); controller.view = view; }
@Test public void testInjectionOfRealController() throws Exception { TestView testView = new TestView(); graph.inject(testView); testView.present(); }
@Test (expected = RuntimeException.class) public void should_throw_out_runtime_exception_when_unable_to_create_model_instance() throws Exception { BadController presenter = new BadController(); graph.inject(presenter); presenter.onCreated(); } }
@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 should_be_able_to_inject_concrete_class() throws Exception { Road road = new Road(); Assert.assertNull(road.car); graph.inject(road); Assert.assertNotNull(road.car); }
@Test public void run_car_with_default_components() { Car car = new Car(); Mvc.graph().inject(car); testDrive(car); }
@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); }
@Test public void controllerShouldBeOverrideAfterExplicitlyRegistration() throws ProvideException, ProviderConflictException, CircularDependenciesException, ProviderMissingException { graph.getRootComponent().register(new PrinterModule(new MockPrinter())); TestView testView = new TestView(); graph.inject(testView); Assert.assertEquals(testView.printController.getClass(), MockPrinter.class); Assert.assertEquals(testView.printController.print(), ((MockPrinter)testView.printController).printContent); }