Search in sources :

Example 6 with DependencyInjection

use of com.hannesdorfmann.mosby3.sample.mvi.dependencyinjection.DependencyInjection in project mosby by sockeqwe.

the class HomePresenterTest method loadingFirstPageAndMoreOfCategoryFails.

@Test
public void loadingFirstPageAndMoreOfCategoryFails() throws IOException {
    // 
    // Prepare mock server to deliver mock response on incoming http request
    // 
    List<Product> mockProducts = Arrays.asList(new Product(1, "image", "name", "category1", "description", 21.9), new Product(2, "image", "name", "category1", "description", 21.9), new Product(3, "image", "name", "category1", "description", 21.9), new Product(4, "image", "name", "category1", "description", 21.9), new Product(5, "image", "name", "category1", "description", 21.9));
    // first page response
    mockWebServer.enqueue(new MockResponse().setBody(adapter.toJson(mockProducts)));
    // more of category responses
    mockWebServer.enqueue(new MockResponse().setBody(adapter.toJson(mockProducts)));
    mockWebServer.enqueue(new MockResponse().setBody(adapter.toJson(Collections.emptyList())));
    mockWebServer.enqueue(new MockResponse().setBody(adapter.toJson(Collections.emptyList())));
    mockWebServer.enqueue(new MockResponse().setBody(adapter.toJson(Collections.emptyList())));
    // 
    // init the robot to drive to View which triggers intents on the presenter
    // 
    HomePresenter presenter = // In a real app you could use dagger or instantiate the Presenter manually like new HomePresenter(...)
    new DependencyInjection().newHomePresenter();
    HomeViewRobot robot = new HomeViewRobot(presenter);
    // 
    // We are ready, so let's start: fire an intent
    // 
    robot.fireLoadFirstPageIntent();
    // 
    // we expect that 2 view.render() events happened with the following HomeViewState:
    // 1. show loading indicator
    // 2. show the items with the first page
    // 
    List<FeedItem> expectedData = Arrays.asList(new SectionHeader("category1"), mockProducts.get(0), mockProducts.get(1), mockProducts.get(2), new AdditionalItemsLoadable(2, "category1", false, null));
    HomeViewState loadingFirstPage = new HomeViewState.Builder().firstPageLoading(true).build();
    HomeViewState firstPage = new HomeViewState.Builder().data(expectedData).build();
    // Check if as expected
    robot.assertViewStateRendered(loadingFirstPage, firstPage);
    // 
    // Load more of category
    // 
    mockWebServer.shutdown();
    robot.fireLoadAllProductsFromCategory("category1");
    // 
    // we expect that 4 view.render() events happened with the following HomeViewState:
    // 1. show loading indicator
    // 2. show the items with the first page
    // 3. show indicator load more of category
    // 4. show loading all items of the category failed
    // 
    List<FeedItem> expectedDataWhileLoadingMoreOfCategory = Arrays.asList(new SectionHeader("category1"), mockProducts.get(0), mockProducts.get(1), mockProducts.get(2), new AdditionalItemsLoadable(2, "category1", true, null));
    List<FeedItem> expectedDataAfterLoadingMoreOfCategoryError = Arrays.asList(new SectionHeader("category1"), mockProducts.get(0), mockProducts.get(1), mockProducts.get(2), new AdditionalItemsLoadable(2, "category1", false, new ConnectException()));
    HomeViewState loadingMoreOfCategory = new HomeViewState.Builder().data(expectedDataWhileLoadingMoreOfCategory).build();
    HomeViewState moreOfCategoryError = new HomeViewState.Builder().data(expectedDataAfterLoadingMoreOfCategoryError).build();
    robot.assertViewStateRendered(loadingFirstPage, firstPage, loadingMoreOfCategory, moreOfCategoryError);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Product(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.Product) DependencyInjection(com.hannesdorfmann.mosby3.sample.mvi.dependencyinjection.DependencyInjection) FeedItem(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.FeedItem) SectionHeader(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.SectionHeader) AdditionalItemsLoadable(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.AdditionalItemsLoadable) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 7 with DependencyInjection

use of com.hannesdorfmann.mosby3.sample.mvi.dependencyinjection.DependencyInjection in project mosby by sockeqwe.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    unbinder = ButterKnife.bind(this);
    toolbar.setTitle("Mosby MVI");
    toolbar.inflateMenu(R.menu.activity_main_toolbar);
    toolbar.setOnMenuItemClickListener(item -> {
        getSupportFragmentManager().beginTransaction().setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out, android.R.anim.fade_in, android.R.anim.fade_out).add(R.id.drawerLayout, new SearchFragment()).addToBackStack("Search").commit();
        return true;
    });
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();
    if (savedInstanceState == null) {
        showCategoryItems(MainMenuItem.HOME);
    } else {
        title = savedInstanceState.getString(KEY_TOOLBAR_TITLE);
        toolbar.setTitle(title);
    }
    // TODO Create a Presenter & ViewState for this Activity
    DependencyInjection dependencyInjection = SampleApplication.getDependencyInjection(this);
    disposable = dependencyInjection.getMainMenuPresenter().getViewStateObservable().filter(state -> state instanceof MenuViewState.DataState).cast(MenuViewState.DataState.class).map(this::findSelectedMenuItem).subscribe(this::showCategoryItems);
    clearSelectionRelay = dependencyInjection.getClearSelectionRelay();
}
Also used : MenuViewState(com.hannesdorfmann.mosby3.sample.mvi.view.menu.MenuViewState) ActionBarDrawerToggle(android.support.v7.app.ActionBarDrawerToggle) SearchFragment(com.hannesdorfmann.mosby3.sample.mvi.view.search.SearchFragment) DependencyInjection(com.hannesdorfmann.mosby3.sample.mvi.dependencyinjection.DependencyInjection)

Example 8 with DependencyInjection

use of com.hannesdorfmann.mosby3.sample.mvi.dependencyinjection.DependencyInjection in project mosby by sockeqwe.

the class HomePresenterTest method loadingFirstPageAndFailLoadingNextPage.

@Test
public void loadingFirstPageAndFailLoadingNextPage() throws Exception {
    // 
    // Prepare mock server to deliver mock response on incoming http request
    // 
    List<Product> mockProductsFirstPage = Arrays.asList(new Product(1, "image", "name", "category1", "description", 21.9), new Product(2, "image", "name", "category1", "description", 21.9), new Product(3, "image", "name", "category1", "description", 21.9), new Product(4, "image", "name", "category1", "description", 21.9), new Product(5, "image", "name", "category1", "description", 21.9));
    mockWebServer.enqueue(new MockResponse().setBody(adapter.toJson(mockProductsFirstPage)));
    // 
    // init the robot to drive to View which triggers intents on the presenter
    // 
    HomePresenter presenter = // In a real app you could use dagger or instantiate the Presenter manually like new HomePresenter(...)
    new DependencyInjection().newHomePresenter();
    HomeViewRobot robot = new HomeViewRobot(presenter);
    // 
    // We are ready, so let's start: fire intents
    // 
    robot.fireLoadFirstPageIntent();
    // 
    // we expect that 2 view.render() events happened with the following HomeViewState:
    // 1. show loading indicator
    // 2. show the items with the first page
    // 
    List<FeedItem> expectedDataAfterFristPage = Arrays.asList(new SectionHeader("category1"), mockProductsFirstPage.get(0), mockProductsFirstPage.get(1), mockProductsFirstPage.get(2), new AdditionalItemsLoadable(2, "category1", false, null));
    HomeViewState loadingFirstPage = new HomeViewState.Builder().firstPageLoading(true).build();
    HomeViewState firstPage = new HomeViewState.Builder().data(expectedDataAfterFristPage).build();
    // Check if as expected
    robot.assertViewStateRendered(loadingFirstPage, firstPage);
    // 
    // Fire second intent
    // 
    // causes loading next page error
    mockWebServer.shutdown();
    robot.fireLoadNextPageIntent();
    // 
    // we expect that 4 view.render() events happened with the following HomeViewState:
    // 1. show loading indicator (caused by loadFirstPageIntent)
    // 2. show the items with the first page (caused by loadFirstPageIntent)
    // 3. show loading next page indicator
    // 4. show next page error (plus original first page content)
    // 
    HomeViewState nextPageLoading = new HomeViewState.Builder().data(expectedDataAfterFristPage).nextPageLoading(true).build();
    HomeViewState nextPage = new HomeViewState.Builder().data(expectedDataAfterFristPage).nextPageError(new ConnectException()).build();
    // Check if as expected
    robot.assertViewStateRendered(loadingFirstPage, firstPage, nextPageLoading, nextPage);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Product(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.Product) DependencyInjection(com.hannesdorfmann.mosby3.sample.mvi.dependencyinjection.DependencyInjection) FeedItem(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.FeedItem) SectionHeader(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.SectionHeader) AdditionalItemsLoadable(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.AdditionalItemsLoadable) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 9 with DependencyInjection

use of com.hannesdorfmann.mosby3.sample.mvi.dependencyinjection.DependencyInjection in project mosby by sockeqwe.

the class HomePresenterTest method loadingFirstPageAndNextPage.

@Test
public void loadingFirstPageAndNextPage() {
    // 
    // Prepare mock server to deliver mock response on incoming http request
    // 
    List<Product> mockProductsFirstPage = Arrays.asList(new Product(1, "image", "name", "category1", "description", 21.9), new Product(2, "image", "name", "category1", "description", 21.9), new Product(3, "image", "name", "category1", "description", 21.9), new Product(4, "image", "name", "category1", "description", 21.9), new Product(5, "image", "name", "category1", "description", 21.9));
    List<Product> mockProductsNextPage = Arrays.asList(new Product(6, "image", "name", "category2", "description", 21.9), new Product(7, "image", "name", "category2", "description", 21.9), new Product(8, "image", "name", "category2", "description", 21.9), new Product(9, "image", "name", "category2", "description", 21.9));
    mockWebServer.enqueue(new MockResponse().setBody(adapter.toJson(mockProductsFirstPage)));
    mockWebServer.enqueue(new MockResponse().setBody(adapter.toJson(mockProductsNextPage)));
    // 
    // init the robot to drive to View which triggers intents on the presenter
    // 
    // In a real app you could use dagger or instantiate the Presenter manually like new HomePresenter(...)
    HomePresenter presenter = new DependencyInjection().newHomePresenter();
    HomeViewRobot robot = new HomeViewRobot(presenter);
    // 
    // We are ready, so let's start: fire intents
    // 
    robot.fireLoadFirstPageIntent();
    // 
    // we expect that 2 view.render() events happened with the following HomeViewState:
    // 1. show loading indicator
    // 2. show the items with the first page
    // 
    List<FeedItem> expectedDataAfterFristPage = Arrays.asList(new SectionHeader("category1"), mockProductsFirstPage.get(0), mockProductsFirstPage.get(1), mockProductsFirstPage.get(2), new AdditionalItemsLoadable(2, "category1", false, null));
    HomeViewState loadingFirstPage = new HomeViewState.Builder().firstPageLoading(true).build();
    HomeViewState firstPage = new HomeViewState.Builder().data(expectedDataAfterFristPage).build();
    // Check if as expected
    robot.assertViewStateRendered(loadingFirstPage, firstPage);
    // 
    // Fire second intent
    // 
    robot.fireLoadNextPageIntent();
    // 
    // we expect that 4 view.render() events happened with the following HomeViewState:
    // 1. show loading indicator (caused by loadFirstPageIntent)
    // 2. show the items with the first page (caused by loadFirstPageIntent)
    // 3. show loading next page indicator
    // 4. show next page content (plus original first page content)
    // 
    List<FeedItem> expectedDataAfterNextPage = Arrays.asList(new SectionHeader("category1"), mockProductsFirstPage.get(0), mockProductsFirstPage.get(1), mockProductsFirstPage.get(2), new AdditionalItemsLoadable(2, "category1", false, null), new SectionHeader("category2"), mockProductsNextPage.get(0), mockProductsNextPage.get(1), mockProductsNextPage.get(2), new AdditionalItemsLoadable(1, "category2", false, null));
    HomeViewState nextPageLoading = new HomeViewState.Builder().data(expectedDataAfterFristPage).nextPageLoading(true).build();
    HomeViewState nextPage = new HomeViewState.Builder().data(expectedDataAfterNextPage).build();
    // Check if as expected
    robot.assertViewStateRendered(loadingFirstPage, firstPage, nextPageLoading, nextPage);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Product(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.Product) DependencyInjection(com.hannesdorfmann.mosby3.sample.mvi.dependencyinjection.DependencyInjection) FeedItem(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.FeedItem) SectionHeader(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.SectionHeader) AdditionalItemsLoadable(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.AdditionalItemsLoadable) Test(org.junit.Test)

Aggregations

DependencyInjection (com.hannesdorfmann.mosby3.sample.mvi.dependencyinjection.DependencyInjection)9 Test (org.junit.Test)8 AdditionalItemsLoadable (com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.AdditionalItemsLoadable)7 FeedItem (com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.FeedItem)7 Product (com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.Product)7 SectionHeader (com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.SectionHeader)7 MockResponse (okhttp3.mockwebserver.MockResponse)7 ConnectException (java.net.ConnectException)4 ActionBarDrawerToggle (android.support.v7.app.ActionBarDrawerToggle)1 MenuViewState (com.hannesdorfmann.mosby3.sample.mvi.view.menu.MenuViewState)1 SearchFragment (com.hannesdorfmann.mosby3.sample.mvi.view.search.SearchFragment)1