Java Code Examples for org.robolectric.shadows.ShadowActivity#receiveResult()

The following examples show how to use org.robolectric.shadows.ShadowActivity#receiveResult() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FormRecordProcessingTest.java    From commcare-android with Apache License 2.0 6 votes vote down vote up
private void fillOutFormWithCaseUpdate() {
    StandardHomeActivity homeActivity = buildHomeActivityForFormEntryLaunch();

    ShadowActivity shadowActivity = Shadows.shadowOf(homeActivity);
    Intent formEntryIntent = shadowActivity.getNextStartedActivity();

    // make sure the form entry activity should be launched
    String intentActivityName = formEntryIntent.getComponent().getClassName();
    assertTrue(intentActivityName.equals(FormEntryActivity.class.getName()));

    ShadowActivity shadowFormEntryActivity = navigateFormEntry(formEntryIntent);

    // trigger CommCareHomeActivity.onActivityResult for the completion of
    // FormEntryActivity
    shadowActivity.receiveResult(formEntryIntent,
            shadowFormEntryActivity.getResultCode(),
            shadowFormEntryActivity.getResultIntent());
    assertStoredFroms();
}
 
Example 2
Source File: FormRecordListActivityTest.java    From commcare-android with Apache License 2.0 6 votes vote down vote up
private static void launchFormEntryForSavedForm(ShadowActivity homeActivityShadow,
                                                Intent savedFormsIntent,
                                                FormRecordListActivity savedFormsActivity) {

    ShadowActivity formRecordShadow = Shadows.shadowOf(savedFormsActivity);
    homeActivityShadow.receiveResult(savedFormsIntent,
            formRecordShadow.getResultCode(),
            formRecordShadow.getResultIntent());
    ShadowActivity.IntentForResult formEntryIntent =
            homeActivityShadow.getNextStartedActivityForResult();
    Robolectric.buildActivity(FormEntryActivity.class, formEntryIntent.intent)
                    .create().start().resume().get();

    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();

    assertNotNull(FormEntryActivity.mFormController);
}
 
Example 3
Source File: MultimediaInflaterActivityTest.java    From commcare-android with Apache License 2.0 6 votes vote down vote up
/**
 * Ensure user sees invalid path toast when the file browser doesn't return a path uri
 */
@Test
public void emptyFileSelectionTest() {
    MultimediaInflaterActivity multimediaInflaterActivity =
            Robolectric.buildActivity(MultimediaInflaterActivity.class)
                    .create().start().resume().get();

    ImageButton selectFileButton =
            multimediaInflaterActivity.findViewById(
                    R.id.screen_multimedia_inflater_filefetch);
    selectFileButton.performClick();

    Intent fileSelectIntent = new Intent(Intent.ACTION_GET_CONTENT);
    fileSelectIntent.setType("application/zip");

    Intent emptyFileSelectResult = new Intent();

    ShadowActivity shadowActivity =
            Shadows.shadowOf(multimediaInflaterActivity);
    shadowActivity.receiveResult(fileSelectIntent,
            Activity.RESULT_OK,
            emptyFileSelectResult);

    Assert.assertEquals(Localization.get("file.invalid.path"),
            ShadowToast.getTextOfLatestToast());
}
 
Example 4
Source File: EndOfFormTest.java    From commcare-android with Apache License 2.0 6 votes vote down vote up
/**
 * Test filling out and saving a form ending in a hidden repeat group. This
 * type of form exercises uncommon end-of-form code paths
 */
@Test
public void testHiddenRepeatAtEndOfForm() {
    ShadowActivity shadowActivity =
            ActivityLaunchUtils.buildHomeActivityForFormEntryLaunch("m0-f0");

    Intent formEntryIntent = shadowActivity.getNextStartedActivity();

    // make sure the form entry activity should be launched
    String intentActivityName = formEntryIntent.getComponent().getClassName();
    assertTrue(intentActivityName.equals(FormEntryActivity.class.getName()));

    ShadowActivity shadowFormEntryActivity = navigateFormEntry(formEntryIntent);

    // trigger CommCareHomeActivity.onActivityResult for the completion of
    // FormEntryActivity
    shadowActivity.receiveResult(formEntryIntent,
            shadowFormEntryActivity.getResultCode(),
            shadowFormEntryActivity.getResultIntent());
    assertStoredForms();
}
 
Example 5
Source File: XFormUpdateInfoTest.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
@Test
public void loginShouldLaunchUpdateInfoForm() {
    TestAppInstaller.installAppAndLogin(
            "jr://resource/commcare-apps/update_info_form/profile.ccpr",
            "test", "123");

    // Verify that Update Info Form is set
    AndroidCommCarePlatform platform = CommCareApplication.instance().getCommCarePlatform();
    String updateInfoFormXmlns = platform.getUpdateInfoFormXmlns();
    assertEquals(updateInfoFormXmlns, UPDATE_FORM_INFO_XMLNS);

    HiddenPreferences.setShowXformUpdateInfo(true);
    // Start HomeActivity from Login
    Intent i = new Intent();
    i.putExtra(DispatchActivity.START_FROM_LOGIN, true);
    ShadowActivity shadowActivity = ActivityLaunchUtils.buildHomeActivity(false, i);

    // make sure that form entry activity is going to be launched
    Intent formEntryIntent = shadowActivity.getNextStartedActivity();
    String intentActivityName = formEntryIntent.getComponent().getClassName();
    assertTrue(intentActivityName.equals(FormEntryActivity.class.getName()));

    // make sure that update info form id is set in Intent Extras
    assertEquals(formEntryIntent.getIntExtra(FormEntryActivity.KEY_FORM_DEF_ID, -1), platform.getFormDefId(UPDATE_FORM_INFO_XMLNS));

    ShadowActivity shadowFormEntryActivity = navigateFormEntry(formEntryIntent);

    // trigger CommCareHomeActivity.onActivityResult for the completion of
    // FormEntryActivity
    shadowActivity.receiveResult(formEntryIntent,
            shadowFormEntryActivity.getResultCode(),
            shadowFormEntryActivity.getResultIntent());

    // Check if our preference has been reset
    assertFalse(HiddenPreferences.shouldShowXformUpdateInfo());
}
 
Example 6
Source File: CommCareSetupActivityTest.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
/**
 * Test that trying to install an app with an invalid suite file results in
 * the appropriate error message and a pinned notification with more
 * details
 */
@Test
public void invalidAppInstall() {
    String invalidUpdateReference = "jr://resource/commcare-apps/update_tests/invalid_suite_update/profile.ccpr";


    // start the setup activity
    Intent setupIntent =
            new Intent(RuntimeEnvironment.application, CommCareSetupActivity.class);

    CommCareSetupActivity setupActivity =
            Robolectric.buildActivity(CommCareSetupActivity.class, setupIntent)
                    .setup().get();

    // click the 'offline install' menu item
    ShadowActivity shadowActivity = Shadows.shadowOf(setupActivity);
    shadowActivity.clickMenuItem(CommCareSetupActivity.MENU_ARCHIVE);

    // make sure there are no pinned notifications
    NotificationManager notificationManager =
            (NotificationManager)RuntimeEnvironment.application.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = Shadows.shadowOf(notificationManager).getNotification(CommCareNoficationManager.MESSAGE_NOTIFICATION);
    assertNull(notification);

    // mock receiving the offline app reference and start the install
    Intent referenceIntent = new Intent();
    referenceIntent.putExtra(InstallArchiveActivity.ARCHIVE_JR_REFERENCE, invalidUpdateReference);
    shadowActivity.receiveResult(shadowActivity.getNextStartedActivity(), Activity.RESULT_OK, referenceIntent);

    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();

    // assert that we get the right error message
    assertTrue(setupActivity.getErrorMessageToDisplay().contains(Localization.get("notification.install.invalid.title")));

    // check that a pinned notification was created for invalid update
    // NOTE: it is way more work to assert the notification body is correct, so skip over that
    notification = Shadows.shadowOf(notificationManager).getNotification(CommCareNoficationManager.MESSAGE_NOTIFICATION);
    assertNotNull(notification);
}
 
Example 7
Source File: EntityListCalloutDataTest.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
private void performFingerprintCallout() {
    // make entity list callout to 'fingerprint identification'
    entitySelectActivity.barcodeScanOnClickListener.onClick(null);

    // receive the (faked) callout result
    Callout identificationScanCallout = getEntitySelectCallout();
    Intent calloutIntent = EntitySelectCalloutSetup.buildCalloutIntent(
            identificationScanCallout,
            entitySelectActivity.evalContext());
    Intent responseIntent = buildIdentificationResultIntent();
    ShadowActivity shadowEntitySelect = Shadows.shadowOf(entitySelectActivity);
    shadowEntitySelect.receiveResult(calloutIntent, Activity.RESULT_OK, responseIntent);
}
 
Example 8
Source File: RecoveryMeasuresTest.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
@Test
public void executeRecoveryMeasuresActivity_ShouldGetLaunched_WhenRecoveryMeasuresArePending() {
    requestRecoveryMeasures(REINSTALL_AND_UPDATE_VALID_FOR_CURRENT_APP_VERSION);

    // Set resources as valid so that dispatch doesn't fire VerificationActivity
    CommCareApplication.instance().getCurrentApp().getAppRecord().setResourcesStatus(true);
    ActivityController<DispatchActivity> dispatchActivityController
            = Robolectric.buildActivity(DispatchActivity.class).create().resume().start();

    DispatchActivity dispatchActivity = dispatchActivityController.get();
    ShadowActivity shadowDispatchActivity = Shadows.shadowOf(dispatchActivity);

    // Verify that Dispatch fires HomeActivity
    Intent homeActivityIntent = shadowDispatchActivity.getNextStartedActivity();
    String intentActivityName = homeActivityIntent.getComponent().getClassName();
    assertTrue(intentActivityName.contentEquals(StandardHomeActivity.class.getName()));

    // launch home activty
    StandardHomeActivity homeActivity =
            Robolectric.buildActivity(StandardHomeActivity.class, homeActivityIntent)
                    .create().start().resume().get();
    ShadowActivity shadowHomeActivity = Shadows.shadowOf(homeActivity);

    // Verify HomeActivity finishes since recovery measures are pending
    assertTrue(shadowHomeActivity.isFinishing());

    shadowDispatchActivity.receiveResult(homeActivityIntent,
            shadowHomeActivity.getResultCode(),
            shadowHomeActivity.getResultIntent());

    // Verify that Dispatch fires up the ExecuteRecoveryMeasuresActivity this time
    dispatchActivityController.resume();
    Intent executeRecoveryMeasuresActivityIntent = shadowDispatchActivity.getNextStartedActivity();
    intentActivityName  = executeRecoveryMeasuresActivityIntent.getComponent().getClassName();
    assertTrue(intentActivityName.contentEquals(ExecuteRecoveryMeasuresActivity.class.getName()));
}
 
Example 9
Source File: UpdateActivityTest.java    From commcare-android with Apache License 2.0 4 votes vote down vote up
/**
 * Use the update activity to update to a ccz with an invalid suite file.
 * Assert that an error is shown and pinned notification is created w/ more details
 */
@Test
public void invalidUpdateTest() {
    String invalidUpdateReference =
            "jr://resource/commcare-apps/update_tests/invalid_suite_update/profile.ccpr";

    // start the update activity
    Intent updateActivityIntent =
            new Intent(RuntimeEnvironment.application, UpdateActivity.class);

    UpdateActivity updateActivity =
            Robolectric.buildActivity(UpdateActivity.class, updateActivityIntent)
                    .setup().get();

    // click the 'offline install' menu item
    ShadowActivity shadowActivity = Shadows.shadowOf(updateActivity);
    shadowActivity.clickMenuItem(UpdateActivity.MENU_UPDATE_FROM_CCZ);

    // Make sure there are no pinned notifications before we start
    NotificationManager notificationManager =
            (NotificationManager)RuntimeEnvironment.application
                    .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(CommCareNoficationManager.MESSAGE_NOTIFICATION);
    Notification notification = Shadows.shadowOf(notificationManager)
            .getNotification(CommCareNoficationManager.MESSAGE_NOTIFICATION);
    assertNull(notification);

    // mock receiving the offline app reference and start the update
    Intent referenceIntent = new Intent();
    referenceIntent.putExtra(InstallArchiveActivity.ARCHIVE_JR_REFERENCE, invalidUpdateReference);
    shadowActivity.receiveResult(shadowActivity.getNextStartedActivity(), Activity.RESULT_OK,
            referenceIntent);

    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();

    // assert that we get the right error message
    String errorMessage = (String)((TextView)updateActivity
            .findViewById(R.id.update_progress_text)).getText();
    assertEquals(Localization.get("updates.check.failed"), errorMessage);

    // check that a pinned notification was created for invalid update
    // NOTE: it is way more work to assert the notification body is correct, so skip over that
    notification = Shadows.shadowOf(notificationManager)
            .getNotification(CommCareNoficationManager.MESSAGE_NOTIFICATION);
    assertNotNull(notification);
}