Search in sources :

Example 11 with ShadowActivity

use of org.robolectric.shadows.ShadowActivity in project android_packages_apps_Settings by LineageOS.

the class SetNewPasswordActivityTest method testChooseLockGeneric.

@Test
public void testChooseLockGeneric() {
    Settings.Global.putInt(RuntimeEnvironment.application.getContentResolver(), Settings.Global.DEVICE_PROVISIONED, 1);
    SetNewPasswordActivity activity = Robolectric.buildActivity(SetNewPasswordActivity.class).get();
    activity.launchChooseLock(new Bundle());
    ShadowActivity shadowActivity = shadowOf(activity);
    Intent intent = shadowActivity.getNextStartedActivityForResult().intent;
    assertThat(intent.getComponent()).isEqualTo(new ComponentName(activity, ChooseLockGeneric.class));
}
Also used : Bundle(android.os.Bundle) ShadowActivity(org.robolectric.shadows.ShadowActivity) Intent(android.content.Intent) ComponentName(android.content.ComponentName) Test(org.junit.Test)

Example 12 with ShadowActivity

use of org.robolectric.shadows.ShadowActivity in project android_packages_apps_Settings by omnirom.

the class ChooseLockSettingsHelperTest method testLaunchConfirmationActivityInternalAndChallenge.

@Test
public void testLaunchConfirmationActivityInternalAndChallenge() {
    final int userId = UserHandle.myUserId();
    final int request = 100;
    final long challenge = 10000L;
    final Activity activity = Robolectric.setupActivity(Activity.class);
    ChooseLockSettingsHelper helper = getChooseLockSettingsHelper(activity);
    helper.launchConfirmationActivityWithExternalAndChallenge(request, "title", "header", "description", // external
    false, challenge, userId);
    ShadowActivity shadowActivity = Shadows.shadowOf(activity);
    Intent startedIntent = shadowActivity.getNextStartedActivity();
    assertEquals(new ComponentName("com.android.settings", ConfirmLockPattern.InternalActivity.class.getName()), startedIntent.getComponent());
    assertFalse(startedIntent.getBooleanExtra(ChooseLockSettingsHelper.EXTRA_KEY_RETURN_CREDENTIALS, false));
    assertTrue(startedIntent.getBooleanExtra(ChooseLockSettingsHelper.EXTRA_KEY_HAS_CHALLENGE, false));
    assertEquals(challenge, startedIntent.getLongExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE, 0L));
    assertEquals(false, (startedIntent.getFlags() & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0);
    assertEquals(false, startedIntent.getBooleanExtra(ConfirmDeviceCredentialBaseFragment.ALLOW_FP_AUTHENTICATION, false));
    assertEquals(false, startedIntent.getBooleanExtra(ConfirmDeviceCredentialBaseFragment.DARK_THEME, false));
    assertEquals(false, startedIntent.getBooleanExtra(ConfirmDeviceCredentialBaseFragment.SHOW_CANCEL_BUTTON, false));
    assertEquals(false, startedIntent.getBooleanExtra(ConfirmDeviceCredentialBaseFragment.SHOW_WHEN_LOCKED, false));
}
Also used : ShadowActivity(org.robolectric.shadows.ShadowActivity) Activity(android.app.Activity) ShadowActivity(org.robolectric.shadows.ShadowActivity) Intent(android.content.Intent) ComponentName(android.content.ComponentName) Test(org.junit.Test)

Example 13 with ShadowActivity

use of org.robolectric.shadows.ShadowActivity in project robolectric by robolectric.

the class LocalActivityInvoker method getActivityResult.

@Override
public ActivityResult getActivityResult() {
    checkNotNull(controller);
    checkState(controller.get().isFinishing(), "You must finish your Activity first");
    ShadowActivity shadowActivity = Shadow.extract(controller.get());
    return new ActivityResult(shadowActivity.getResultCode(), shadowActivity.getResultIntent());
}
Also used : ShadowActivity(org.robolectric.shadows.ShadowActivity) ActivityResult(android.app.Instrumentation.ActivityResult)

Example 14 with ShadowActivity

use of org.robolectric.shadows.ShadowActivity in project robolectric by robolectric.

the class RoboMonitoringInstrumentation method execStartActivity.

/**
 * {@inheritDoc}
 */
@Override
public ActivityResult execStartActivity(Context who, IBinder contextThread, IBinder token, Activity target, Intent intent, int requestCode, Bundle options) {
    intentMonitor.signalIntent(intent);
    ActivityResult ar = stubResultFor(intent);
    if (ar != null) {
        Log.i(TAG, String.format("Stubbing intent %s", intent));
    } else {
        ar = super.execStartActivity(who, contextThread, token, target, intent, requestCode, options);
    }
    if (ar != null && target != null) {
        ShadowActivity shadowActivity = extract(target);
        postDispatchActivityResult(shadowActivity, null, requestCode, ar);
    }
    return ar;
}
Also used : ShadowActivity(org.robolectric.shadows.ShadowActivity)

Example 15 with ShadowActivity

use of org.robolectric.shadows.ShadowActivity in project robolectric by robolectric.

the class ActivityController method configurationChange.

/**
 * Performs a configuration change on the Activity.
 *
 * <p>If the activity is configured to handle changes without being recreated, {@link
 * Activity#onConfigurationChanged(Configuration)} will be called. Otherwise, the activity is
 * recreated as described <a
 * href="https://developer.android.com/guide/topics/resources/runtime-changes.html">here</a>.
 *
 * @param newConfiguration The new configuration to be set.
 * @return ActivityController instance
 */
public ActivityController<T> configurationChange(final Configuration newConfiguration) {
    final Configuration currentConfig = component.getResources().getConfiguration();
    final int changedBits = currentConfig.diff(newConfiguration);
    currentConfig.setTo(newConfiguration);
    // Can the activity handle itself ALL configuration changes?
    if ((getActivityInfo(component.getApplication()).configChanges & changedBits) == changedBits) {
        shadowMainLooper.runPaused(() -> {
            component.onConfigurationChanged(newConfiguration);
            ViewRootImpl root = getViewRoot();
            if (root != null) {
                if (RuntimeEnvironment.getApiLevel() <= N_MR1) {
                    ReflectionHelpers.callInstanceMethod(root, "updateConfiguration", ClassParameter.from(Configuration.class, newConfiguration), ClassParameter.from(boolean.class, false));
                } else {
                    root.updateConfiguration(Display.INVALID_DISPLAY);
                }
            }
        });
        return this;
    } else {
        @SuppressWarnings("unchecked") final T recreatedActivity = (T) ReflectionHelpers.callConstructor(component.getClass());
        final _Activity_ _recreatedActivity_ = reflector(_Activity_.class, recreatedActivity);
        shadowMainLooper.runPaused(() -> {
            // Set flags
            _component_.setChangingConfigurations(true);
            _component_.setConfigChangeFlags(changedBits);
            // Perform activity destruction
            final Bundle outState = new Bundle();
            // The order of onPause/onStop/onSaveInstanceState is undefined, but is usually:
            // onPause -> onSaveInstanceState -> onStop before API P, and onPause -> onStop ->
            // onSaveInstanceState from API P.
            // See
            // https://developer.android.com/reference/android/app/Activity#onSaveInstanceState(android.os.Bundle) for documentation explained.
            // And see ActivityThread#callActivityOnStop for related code.
            _component_.performPause();
            if (RuntimeEnvironment.getApiLevel() < P) {
                _component_.performSaveInstanceState(outState);
                if (RuntimeEnvironment.getApiLevel() <= M) {
                    _component_.performStop();
                } else {
                    // API from N to O_MR1(both including)
                    _component_.performStop(true);
                }
            } else {
                _component_.performStop(true, "configurationChange");
                _component_.performSaveInstanceState(outState);
            }
            // This is the true and complete retained state, including loaders and retained
            // fragments.
            final Object nonConfigInstance = _component_.retainNonConfigurationInstances();
            // This is the activity's "user" state
            final Object activityConfigInstance = nonConfigInstance == null ? // No framework or user state.
            null : reflector(_NonConfigurationInstances_.class, nonConfigInstance).getActivity();
            _component_.performDestroy();
            makeActivityEligibleForGc();
            // Restore theme in case it was set in the test manually.
            // This is not technically what happens but is purely to make this easier to use in
            // Robolectric.
            ShadowContextThemeWrapper shadowContextThemeWrapper = Shadow.extract(component);
            int theme = shadowContextThemeWrapper.callGetThemeResId();
            // Setup controller for the new activity
            attached = false;
            component = recreatedActivity;
            _component_ = _recreatedActivity_;
            // TODO: Pass nonConfigurationInstance here instead of setting
            // mLastNonConfigurationInstances directly below. This field must be set before
            // attach. Since current implementation sets it after attach(), initialization is not
            // done correctly. For instance, fragment marked as retained is not retained.
            attach(/* activityOptions= */
            null, /* lastNonConfigurationInstances= */
            null);
            if (theme != 0) {
                recreatedActivity.setTheme(theme);
            }
            // Set saved non config instance
            _recreatedActivity_.setLastNonConfigurationInstances(nonConfigInstance);
            ShadowActivity shadowActivity = Shadow.extract(recreatedActivity);
            shadowActivity.setLastNonConfigurationInstance(activityConfigInstance);
            // Create lifecycle
            _recreatedActivity_.performCreate(outState);
            if (RuntimeEnvironment.getApiLevel() <= O_MR1) {
                _recreatedActivity_.performStart();
            } else {
                _recreatedActivity_.performStart("configurationChange");
            }
            _recreatedActivity_.performRestoreInstanceState(outState);
            _recreatedActivity_.onPostCreate(outState);
            if (RuntimeEnvironment.getApiLevel() <= O_MR1) {
                _recreatedActivity_.performResume();
            } else {
                _recreatedActivity_.performResume(true, "configurationChange");
            }
            _recreatedActivity_.onPostResume();
        // TODO: Call visible() too.
        });
    }
    return this;
}
Also used : ShadowViewRootImpl(org.robolectric.shadows.ShadowViewRootImpl) ViewRootImpl(android.view.ViewRootImpl) Configuration(android.content.res.Configuration) org.robolectric.shadows._Activity_(org.robolectric.shadows._Activity_) ShadowContextThemeWrapper(org.robolectric.shadows.ShadowContextThemeWrapper) Bundle(android.os.Bundle) ShadowActivity(org.robolectric.shadows.ShadowActivity)

Aggregations

ShadowActivity (org.robolectric.shadows.ShadowActivity)343 Test (org.junit.Test)330 Intent (android.content.Intent)163 ComponentName (android.content.ComponentName)100 Button (android.widget.Button)67 Config (org.robolectric.annotation.Config)53 IntentForResult (org.robolectric.shadows.ShadowActivity.IntentForResult)53 Activity (android.app.Activity)41 EnrollmentCallback (android.hardware.fingerprint.FingerprintManager.EnrollmentCallback)33 Bundle (android.os.Bundle)32 PartnerCustomizationLayout (com.google.android.setupcompat.PartnerCustomizationLayout)30 FooterBarMixin (com.google.android.setupcompat.template.FooterBarMixin)20 FragmentActivity (androidx.fragment.app.FragmentActivity)11 Tile (com.android.settingslib.drawer.Tile)11 Settings (com.android.settings.Settings)10 ContextualCardsFragment (com.android.settings.homepage.contextualcards.ContextualCardsFragment)10 IntentBuilder (com.android.settings.password.ChooseLockPassword.IntentBuilder)7 SetupChooseLockPasswordFragment (com.android.settings.password.SetupChooseLockPassword.SetupChooseLockPasswordFragment)7 ShadowIntent (org.robolectric.shadows.ShadowIntent)7 Preference (android.support.v7.preference.Preference)6