Search in sources :

Example 31 with Environment

use of com.kickstarter.libs.Environment in project android-oss by kickstarter.

the class DiscoveryViewModelTest method testRootCategoriesEmitWithPosition.

@Test
public void testRootCategoriesEmitWithPosition() {
    final DiscoveryViewModel vm = new DiscoveryViewModel(environment());
    final TestSubscriber<List<Category>> rootCategories = new TestSubscriber<>();
    final TestSubscriber<Integer> position = new TestSubscriber<>();
    vm.outputs.rootCategoriesAndPosition().map(cp -> cp.first).subscribe(rootCategories);
    vm.outputs.rootCategoriesAndPosition().map(cp -> cp.second).subscribe(position);
    // Start initial activity.
    vm.intent(new Intent(Intent.ACTION_MAIN));
    // Initial HOME page selected.
    vm.inputs.discoveryPagerAdapterSetPrimaryPage(null, 0);
    // Root categories should emit for the initial HOME sort position.
    rootCategories.assertValueCount(1);
    position.assertValues(0);
    // Select POPULAR sort position.
    vm.inputs.discoveryPagerAdapterSetPrimaryPage(null, 1);
    // Root categories should emit for the POPULAR sort position.
    rootCategories.assertValueCount(2);
    position.assertValues(0, 1);
    // Select ART category from the drawer.
    vm.inputs.childFilterViewHolderRowClick(null, NavigationDrawerData.Section.Row.builder().params(DiscoveryParams.builder().category(CategoryFactory.artCategory()).build()).build());
    // Root categories should not emit again for the same position.
    rootCategories.assertValueCount(2);
    position.assertValues(0, 1);
}
Also used : Transformers(com.kickstarter.libs.rx.transformers.Transformers) Category(com.kickstarter.models.Category) Arrays(java.util.Arrays) MockCurrentUser(com.kickstarter.libs.MockCurrentUser) CategoryFactory(com.kickstarter.factories.CategoryFactory) DiscoveryParams(com.kickstarter.services.DiscoveryParams) Intent(android.content.Intent) Test(org.junit.Test) UserFactory(com.kickstarter.factories.UserFactory) DiscoveryUtils(com.kickstarter.libs.utils.DiscoveryUtils) KSRobolectricTestCase(com.kickstarter.KSRobolectricTestCase) InternalBuildEnvelope(com.kickstarter.services.apiresponses.InternalBuildEnvelope) List(java.util.List) TestSubscriber(rx.observers.TestSubscriber) Environment(com.kickstarter.libs.Environment) NavigationDrawerData(com.kickstarter.ui.adapters.data.NavigationDrawerData) InternalBuildEnvelopeFactory(com.kickstarter.factories.InternalBuildEnvelopeFactory) TestSubscriber(rx.observers.TestSubscriber) List(java.util.List) Intent(android.content.Intent) Test(org.junit.Test)

Example 32 with Environment

use of com.kickstarter.libs.Environment in project android-oss by kickstarter.

the class DiscoveryViewModelTest method testInteractionBetweenParamsAndPageAdapter.

@Test
public void testInteractionBetweenParamsAndPageAdapter() {
    final DiscoveryViewModel vm = new DiscoveryViewModel(environment());
    final TestSubscriber<DiscoveryParams> updateParams = new TestSubscriber<>();
    vm.outputs.updateParamsForPage().subscribe(updateParams);
    final TestSubscriber<Integer> updatePage = new TestSubscriber<>();
    vm.outputs.updateParamsForPage().map(params -> DiscoveryUtils.positionFromSort(params.sort())).subscribe(updatePage);
    // Start initial activity.
    final Intent intent = new Intent(Intent.ACTION_MAIN);
    vm.intent(intent);
    // Initial HOME page selected.
    vm.inputs.discoveryPagerAdapterSetPrimaryPage(null, 0);
    // Initial params should emit. Page should not be updated yet.
    updateParams.assertValues(DiscoveryParams.builder().sort(DiscoveryParams.Sort.HOME).build());
    updatePage.assertValues(0);
    // Select POPULAR sort position.
    vm.inputs.discoveryPagerAdapterSetPrimaryPage(null, 1);
    // Params and page should update with new POPULAR sort values.
    updateParams.assertValues(DiscoveryParams.builder().sort(DiscoveryParams.Sort.HOME).build(), DiscoveryParams.builder().sort(DiscoveryParams.Sort.POPULAR).build());
    updatePage.assertValues(0, 1);
    // Select ART category from the drawer.
    vm.inputs.childFilterViewHolderRowClick(null, NavigationDrawerData.Section.Row.builder().params(DiscoveryParams.builder().category(CategoryFactory.artCategory()).build()).build());
    // Params should update with new category; page should remain the same.
    updateParams.assertValues(DiscoveryParams.builder().sort(DiscoveryParams.Sort.HOME).build(), DiscoveryParams.builder().sort(DiscoveryParams.Sort.POPULAR).build(), DiscoveryParams.builder().sort(DiscoveryParams.Sort.POPULAR).category(CategoryFactory.artCategory()).build());
    updatePage.assertValues(0, 1, 1);
    koalaTest.assertValues("Discover Modal Selected Filter");
    // Select HOME sort position.
    vm.inputs.discoveryPagerAdapterSetPrimaryPage(null, 0);
    // Params and page should update with new HOME sort value.
    updateParams.assertValues(DiscoveryParams.builder().sort(DiscoveryParams.Sort.HOME).build(), DiscoveryParams.builder().sort(DiscoveryParams.Sort.POPULAR).build(), DiscoveryParams.builder().sort(DiscoveryParams.Sort.POPULAR).category(CategoryFactory.artCategory()).build(), DiscoveryParams.builder().sort(DiscoveryParams.Sort.HOME).category(CategoryFactory.artCategory()).build());
    updatePage.assertValues(0, 1, 1, 0);
    // Simulate rotating the device and hitting initial inputs again.
    final TestSubscriber<DiscoveryParams> rotatedUpdateParams = new TestSubscriber<>();
    vm.outputs.updateParamsForPage().subscribe(rotatedUpdateParams);
    final TestSubscriber<Integer> rotatedUpdatePage = new TestSubscriber<>();
    vm.outputs.updateParamsForPage().map(params -> DiscoveryUtils.positionFromSort(params.sort())).subscribe(rotatedUpdatePage);
    // Should emit again with same params.
    rotatedUpdateParams.assertValues(DiscoveryParams.builder().sort(DiscoveryParams.Sort.HOME).category(CategoryFactory.artCategory()).build());
    rotatedUpdatePage.assertValues(0);
}
Also used : Transformers(com.kickstarter.libs.rx.transformers.Transformers) Category(com.kickstarter.models.Category) Arrays(java.util.Arrays) MockCurrentUser(com.kickstarter.libs.MockCurrentUser) CategoryFactory(com.kickstarter.factories.CategoryFactory) DiscoveryParams(com.kickstarter.services.DiscoveryParams) Intent(android.content.Intent) Test(org.junit.Test) UserFactory(com.kickstarter.factories.UserFactory) DiscoveryUtils(com.kickstarter.libs.utils.DiscoveryUtils) KSRobolectricTestCase(com.kickstarter.KSRobolectricTestCase) InternalBuildEnvelope(com.kickstarter.services.apiresponses.InternalBuildEnvelope) List(java.util.List) TestSubscriber(rx.observers.TestSubscriber) Environment(com.kickstarter.libs.Environment) NavigationDrawerData(com.kickstarter.ui.adapters.data.NavigationDrawerData) InternalBuildEnvelopeFactory(com.kickstarter.factories.InternalBuildEnvelopeFactory) TestSubscriber(rx.observers.TestSubscriber) Intent(android.content.Intent) DiscoveryParams(com.kickstarter.services.DiscoveryParams) Test(org.junit.Test)

Example 33 with Environment

use of com.kickstarter.libs.Environment in project android-oss by kickstarter.

the class LoginViewModelTest method testLoginTfaChallenge.

@Test
public void testLoginTfaChallenge() {
    final ApiClientType apiClient = new MockApiClient() {

        @Override
        @NonNull
        public Observable<AccessTokenEnvelope> login(@NonNull final String email, @NonNull final String password) {
            return Observable.error(ApiExceptionFactory.tfaRequired());
        }
    };
    final Environment environment = environment().toBuilder().apiClient(apiClient).build();
    final LoginViewModel vm = new LoginViewModel(environment);
    final TestSubscriber<Void> tfaChallenge = new TestSubscriber<>();
    vm.errors.tfaChallenge().subscribe(tfaChallenge);
    final TestSubscriber<Void> loginSuccess = new TestSubscriber<>();
    vm.outputs.loginSuccess().subscribe(loginSuccess);
    vm.inputs.email("hello@kickstarter.com");
    vm.inputs.password("danisawesome");
    vm.inputs.loginClick();
    loginSuccess.assertNoValues();
    tfaChallenge.assertValueCount(1);
}
Also used : AccessTokenEnvelope(com.kickstarter.services.apiresponses.AccessTokenEnvelope) MockApiClient(com.kickstarter.services.MockApiClient) NonNull(android.support.annotation.NonNull) Environment(com.kickstarter.libs.Environment) TestSubscriber(rx.observers.TestSubscriber) ApiClientType(com.kickstarter.services.ApiClientType) Test(org.junit.Test)

Example 34 with Environment

use of com.kickstarter.libs.Environment in project android-oss by kickstarter.

the class ProfileViewModelTest method testProfileViewModel_EmitsUserNameAndAvatar.

@Test
public void testProfileViewModel_EmitsUserNameAndAvatar() {
    final User user = UserFactory.user();
    final ApiClientType apiClient = new MockApiClient() {

        @Override
        @NonNull
        public Observable<User> fetchCurrentUser() {
            return Observable.just(user);
        }
    };
    final Environment env = environment().toBuilder().apiClient(apiClient).build();
    final ProfileViewModel.ViewModel vm = new ProfileViewModel.ViewModel(env);
    final TestSubscriber<String> avatarImageViewUrl = new TestSubscriber<>();
    vm.outputs.avatarImageViewUrl().subscribe(avatarImageViewUrl);
    final TestSubscriber<String> userNameTextViewText = new TestSubscriber<>();
    vm.outputs.userNameTextViewText().subscribe(userNameTextViewText);
    avatarImageViewUrl.assertValues(user.avatar().medium());
    userNameTextViewText.assertValues(user.name());
}
Also used : User(com.kickstarter.models.User) MockApiClient(com.kickstarter.services.MockApiClient) Environment(com.kickstarter.libs.Environment) TestSubscriber(rx.observers.TestSubscriber) ApiClientType(com.kickstarter.services.ApiClientType) Test(org.junit.Test)

Example 35 with Environment

use of com.kickstarter.libs.Environment in project android-oss by kickstarter.

the class ProfileViewModelTest method testProfileViewModel_EmitsCreatedProjectsData.

@Test
public void testProfileViewModel_EmitsCreatedProjectsData() {
    final User user = UserFactory.user().toBuilder().backedProjectsCount(0).createdProjectsCount(2).build();
    final ApiClientType apiClient = new MockApiClient() {

        @Override
        @NonNull
        public Observable<User> fetchCurrentUser() {
            return Observable.just(user);
        }
    };
    final Environment env = environment().toBuilder().apiClient(apiClient).build();
    final ProfileViewModel.ViewModel vm = new ProfileViewModel.ViewModel(env);
    final TestSubscriber<Boolean> backedCountTextViewHidden = new TestSubscriber<>();
    vm.outputs.backedCountTextViewHidden().subscribe(backedCountTextViewHidden);
    final TestSubscriber<String> backedCountTextViewText = new TestSubscriber<>();
    vm.outputs.backedCountTextViewText().subscribe(backedCountTextViewText);
    final TestSubscriber<Boolean> backedTextViewHidden = new TestSubscriber<>();
    vm.outputs.backedTextViewHidden().subscribe(backedTextViewHidden);
    final TestSubscriber<Boolean> createdCountTextViewHidden = new TestSubscriber<>();
    vm.outputs.createdCountTextViewHidden().subscribe(createdCountTextViewHidden);
    final TestSubscriber<String> createdCountTextViewText = new TestSubscriber<>();
    vm.outputs.createdCountTextViewText().subscribe(createdCountTextViewText);
    final TestSubscriber<Boolean> createdTextViewHidden = new TestSubscriber<>();
    vm.outputs.createdTextViewHidden().subscribe(createdTextViewHidden);
    final TestSubscriber<Boolean> dividerViewHidden = new TestSubscriber<>();
    vm.outputs.dividerViewHidden().subscribe(dividerViewHidden);
    // Backed text views are hidden.
    backedCountTextViewHidden.assertValues(true);
    backedCountTextViewText.assertNoValues();
    backedTextViewHidden.assertValues(true);
    // Created text views are displayed.
    createdCountTextViewHidden.assertValues(false);
    createdCountTextViewText.assertValues(NumberUtils.format(user.createdProjectsCount()));
    createdTextViewHidden.assertValues(false);
    // Divider view is hidden.
    dividerViewHidden.assertValues(true);
}
Also used : User(com.kickstarter.models.User) MockApiClient(com.kickstarter.services.MockApiClient) Environment(com.kickstarter.libs.Environment) TestSubscriber(rx.observers.TestSubscriber) ApiClientType(com.kickstarter.services.ApiClientType) Test(org.junit.Test)

Aggregations

Environment (com.kickstarter.libs.Environment)50 Test (org.junit.Test)49 TestSubscriber (rx.observers.TestSubscriber)43 Intent (android.content.Intent)23 MockCurrentUser (com.kickstarter.libs.MockCurrentUser)22 MockApiClient (com.kickstarter.services.MockApiClient)21 ApiClientType (com.kickstarter.services.ApiClientType)20 CurrentUserType (com.kickstarter.libs.CurrentUserType)19 Project (com.kickstarter.models.Project)19 User (com.kickstarter.models.User)16 NonNull (android.support.annotation.NonNull)14 KSRobolectricTestCase (com.kickstarter.KSRobolectricTestCase)9 MockBooleanPreference (com.kickstarter.libs.preferences.MockBooleanPreference)8 UserFactory (com.kickstarter.factories.UserFactory)7 AccessTokenEnvelope (com.kickstarter.services.apiresponses.AccessTokenEnvelope)7 ProjectFactory (com.kickstarter.factories.ProjectFactory)6 IntentKey (com.kickstarter.ui.IntentKey)6 List (java.util.List)6 KoalaEvent (com.kickstarter.libs.KoalaEvent)5 ConfigFactory (com.kickstarter.factories.ConfigFactory)4