<commit_before>package org.openlmis.core.view.activity;

import android.content.Intent;
import android.view.View;

import com.google.inject.AbstractModule;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openlmis.core.LMISTestApp;
import org.openlmis.core.LMISTestRunner;
import org.openlmis.core.R;
import org.openlmis.core.exceptions.LMISException;
import org.openlmis.core.manager.SharedPreferenceMgr;
import org.openlmis.core.model.Product;
import org.openlmis.core.model.builder.ProductBuilder;
import org.openlmis.core.presenter.InitialInventoryPresenter;
import org.openlmis.core.utils.Constants;
import org.openlmis.core.view.adapter.InventoryListAdapter;
import org.openlmis.core.view.viewmodel.InventoryViewModel;
import org.openlmis.core.view.widget.SingleClickButtonListener;
import org.robolectric.Robolectric;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowToast;

import java.util.List;

import roboguice.RoboGuice;
import rx.Observable;
import rx.functions.Action1;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.util.Lists.newArrayList;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;

@RunWith(LMISTestRunner.class)
public class InitialInventoryActivityTest {
    private InitialInventoryActivity initialInventoryActivity;
    private InitialInventoryPresenter mockedPresenter;

    private List<InventoryViewModel> data;

    @Before
    public void setUp() throws LMISException {
        mockedPresenter = mock(InitialInventoryPresenter.class);
        when(mockedPresenter.loadInventory()).thenReturn(Observable.<List<InventoryViewModel>>empty());

        RoboGuice.overrideApplicationInjector(RuntimeEnvironment.application, new AbstractModule() {
            @Override
            protected void configure() {
                bind(InitialInventoryPresenter.class).toInstance(mockedPresenter);
            }
        });

        initialInventoryActivity = Robolectric.buildActivity(InitialInventoryActivity.class).create().get();

        InventoryListAdapter mockedAdapter = mock(InventoryListAdapter.class);
        Product product = new ProductBuilder().setCode("Product code").setPrimaryName("Primary name").setStrength("10mg").build();
        data = newArrayList(new InventoryViewModel(product), new InventoryViewModel(product));
        when(mockedAdapter.getData()).thenReturn(data);

        initialInventoryActivity.mAdapter = mockedAdapter;
    }

    @After
    public void teardown() {
        RoboGuice.Util.reset();
    }

    @Test
    public void shouldGoToHomePageAfterInitInventoryOrDoPhysicalInventory(){
        initialInventoryActivity.goToNextPage();

        Intent startIntent = shadowOf(initialInventoryActivity).getNextStartedActivity();
        assertEquals(startIntent.getComponent().getClassName(), HomeActivity.class.getName());
    }

    @Test
    public void shouldGoToStockCardPageAfterAddedNewProduct(){
        Intent intentToStockCard = new Intent();
        intentToStockCard.putExtra(Constants.PARAM_IS_ADD_NEW_DRUG, true);

        initialInventoryActivity = Robolectric.buildActivity(InitialInventoryActivity.class).withIntent(intentToStockCard).create().get();

        initialInventoryActivity.goToNextPage();

        Intent startIntent = shadowOf(initialInventoryActivity).getNextStartedActivity();
        assertEquals(startIntent.getComponent().getClassName(), StockCardListActivity.class.getName());
        assertEquals(startIntent.getFlags(), Intent.FLAG_ACTIVITY_CLEAR_TOP);
    }

    @Test
    public void shouldShowMessageAndNeverBackWhenPressBackInInitInventory() {
        initialInventoryActivity.onBackPressed();

        assertEquals(ShadowToast.getTextOfLatestToast(), initialInventoryActivity.getString(R.string.msg_save_before_exit));

        initialInventoryActivity.onBackPressed();

        assertFalse(initialInventoryActivity.isFinishing());
    }

    @Test
    public void shouldGetAddNewDrugActivity() {
        Intent intent = InitialInventoryActivity.getIntentToMe(RuntimeEnvironment.application, true);

        assertNotNull(intent);
        assertEquals(intent.getComponent().getClassName(), InitialInventoryActivity.class.getName());
        assertTrue(intent.getBooleanExtra(Constants.PARAM_IS_ADD_NEW_DRUG, false));
    }

    @Test
    public void shouldInitUIWhenInitialInventory() {
        assertThat(initialInventoryActivity.btnSave.getVisibility()).isEqualTo(View.GONE);
        assertTrue(initialInventoryActivity.loadingDialog.isShowing());

        verify(mockedPresenter).loadInventory();
    }

    @Test
    public void shouldDoInitialInventoryWhenBtnDoneClicked() {
        LMISTestApp.getInstance().setCurrentTimeMillis(100000);
        SingleClickButtonListener.isViewClicked = false;

        when(initialInventoryActivity.mAdapter.validateAll()).thenReturn(-1);
        initialInventoryActivity.onNextMainPageAction = new Action1<Object>() {
            @Override
            public void call(Object o) {
                return;
            }
        };

        when(mockedPresenter.initStockCardObservable()).thenReturn(Observable.empty());
        initialInventoryActivity.btnDone.performClick();
        verify(mockedPresenter).initStockCardObservable();
    }

    @Test
    public void shouldGoToMainPageWhenOnNextCalled() {
        initialInventoryActivity.onNextMainPageAction.call(null);

        assertNull(initialInventoryActivity.loadingDialog);
        assertFalse(SharedPreferenceMgr.getInstance().isNeedsInventory());<commit_msg>US00 - MR - removing unneeded assert<commit_after>package org.openlmis.core.view.activity;

import android.content.Intent;
import android.view.View;

import com.google.inject.AbstractModule;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openlmis.core.LMISTestApp;
import org.openlmis.core.LMISTestRunner;
import org.openlmis.core.R;
import org.openlmis.core.exceptions.LMISException;
import org.openlmis.core.manager.SharedPreferenceMgr;
import org.openlmis.core.model.Product;
import org.openlmis.core.model.builder.ProductBuilder;
import org.openlmis.core.presenter.InitialInventoryPresenter;
import org.openlmis.core.utils.Constants;
import org.openlmis.core.view.adapter.InventoryListAdapter;
import org.openlmis.core.view.viewmodel.InventoryViewModel;
import org.openlmis.core.view.widget.SingleClickButtonListener;
import org.robolectric.Robolectric;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowToast;

import java.util.List;

import roboguice.RoboGuice;
import rx.Observable;
import rx.functions.Action1;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.util.Lists.newArrayList;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;

@RunWith(LMISTestRunner.class)
public class InitialInventoryActivityTest {
    private InitialInventoryActivity initialInventoryActivity;
    private InitialInventoryPresenter mockedPresenter;

    private List<InventoryViewModel> data;

    @Before
    public void setUp() throws LMISException {
        mockedPresenter = mock(InitialInventoryPresenter.class);
        when(mockedPresenter.loadInventory()).thenReturn(Observable.<List<InventoryViewModel>>empty());

        RoboGuice.overrideApplicationInjector(RuntimeEnvironment.application, new AbstractModule() {
            @Override
            protected void configure() {
                bind(InitialInventoryPresenter.class).toInstance(mockedPresenter);
            }
        });

        initialInventoryActivity = Robolectric.buildActivity(InitialInventoryActivity.class).create().get();

        InventoryListAdapter mockedAdapter = mock(InventoryListAdapter.class);
        Product product = new ProductBuilder().setCode("Product code").setPrimaryName("Primary name").setStrength("10mg").build();
        data = newArrayList(new InventoryViewModel(product), new InventoryViewModel(product));
        when(mockedAdapter.getData()).thenReturn(data);

        initialInventoryActivity.mAdapter = mockedAdapter;
    }

    @After
    public void teardown() {
        RoboGuice.Util.reset();
    }

    @Test
    public void shouldGoToHomePageAfterInitInventoryOrDoPhysicalInventory(){
        initialInventoryActivity.goToNextPage();

        Intent startIntent = shadowOf(initialInventoryActivity).getNextStartedActivity();
        assertEquals(startIntent.getComponent().getClassName(), HomeActivity.class.getName());
    }

    @Test
    public void shouldGoToStockCardPageAfterAddedNewProduct(){
        Intent intentToStockCard = new Intent();
        intentToStockCard.putExtra(Constants.PARAM_IS_ADD_NEW_DRUG, true);

        initialInventoryActivity = Robolectric.buildActivity(InitialInventoryActivity.class).withIntent(intentToStockCard).create().get();

        initialInventoryActivity.goToNextPage();

        Intent startIntent = shadowOf(initialInventoryActivity).getNextStartedActivity();
        assertEquals(startIntent.getComponent().getClassName(), StockCardListActivity.class.getName());
        assertEquals(startIntent.getFlags(), Intent.FLAG_ACTIVITY_CLEAR_TOP);
    }

    @Test
    public void shouldShowMessageAndNeverBackWhenPressBackInInitInventory() {
        initialInventoryActivity.onBackPressed();

        assertEquals(ShadowToast.getTextOfLatestToast(), initialInventoryActivity.getString(R.string.msg_save_before_exit));

        initialInventoryActivity.onBackPressed();

        assertFalse(initialInventoryActivity.isFinishing());
    }

    @Test
    public void shouldGetAddNewDrugActivity() {
        Intent intent = InitialInventoryActivity.getIntentToMe(RuntimeEnvironment.application, true);

        assertNotNull(intent);
        assertEquals(intent.getComponent().getClassName(), InitialInventoryActivity.class.getName());
        assertTrue(intent.getBooleanExtra(Constants.PARAM_IS_ADD_NEW_DRUG, false));
    }

    @Test
    public void shouldInitUIWhenInitialInventory() {
        assertTrue(initialInventoryActivity.loadingDialog.isShowing());
        verify(mockedPresenter).loadInventory();
    }

    @Test
    public void shouldDoInitialInventoryWhenBtnDoneClicked() {
        LMISTestApp.getInstance().setCurrentTimeMillis(100000);
        SingleClickButtonListener.isViewClicked = false;

        when(initialInventoryActivity.mAdapter.validateAll()).thenReturn(-1);
        initialInventoryActivity.onNextMainPageAction = new Action1<Object>() {
            @Override
            public void call(Object o) {
                return;
            }
        };

        when(mockedPresenter.initStockCardObservable()).thenReturn(Observable.empty());
        initialInventoryActivity.btnDone.performClick();
        verify(mockedPresenter).initStockCardObservable();
    }

    @Test
    public void shouldGoToMainPageWhenOnNextCalled() {
        initialInventoryActivity.onNextMainPageAction.call(null);

        assertNull(initialInventoryActivity.loadingDialog);
        assertFalse(SharedPreferenceMgr.getInstance().isNeedsInventory());
{'fraction_non_alphanumeric': 0.09407313997477931, 'fraction_numerical': 0.00201765447667087, 'mean_word_length': 4.603391427225624, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2657454', 'n_tokens_mistral': 3287, 'n_tokens_neox': 3189, 'n_words': 456}