Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1ecbb7d1 authored by Maurice Lam's avatar Maurice Lam Committed by Android (Google) Code Review
Browse files

Merge "Revert choose lock header file to non-setup theme" into oc-dev

parents d40ea0a9 f8fb4806
Loading
Loading
Loading
Loading
+3 −3
Original line number Original line Diff line number Diff line
@@ -18,9 +18,9 @@
    android:id="@+id/fingerprint_header_description"
    android:id="@+id/fingerprint_header_description"
    android:layout_width="match_parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="56dp"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:paddingTop="@dimen/suw_description_glif_margin_top"
    android:paddingBottom="@dimen/suw_description_glif_margin_bottom_lists"
    android:text="@string/lock_settings_picker_fingerprint_message"
    android:text="@string/lock_settings_picker_fingerprint_message"
    style="@style/SuwDescription.Glif" />
    style="@style/FingerprintHeaderStyle" />
+85 −100
Original line number Original line Diff line number Diff line
@@ -16,11 +16,20 @@


package com.android.settings;
package com.android.settings;


import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static android.support.test.InstrumentationRegistry.getTargetContext;

import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.assertTrue;

import android.app.Activity;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.AppTask;
import android.app.KeyguardManager;
import android.app.admin.DevicePolicyManager;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.content.Context;
import android.content.Intent;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.test.filters.MediumTest;
import android.support.test.filters.MediumTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.runner.AndroidJUnit4;
@@ -29,24 +38,17 @@ import android.support.test.runner.lifecycle.Stage;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiSelector;
import android.support.test.uiautomator.UiSelector;

import android.text.format.DateUtils;
import android.text.format.DateUtils;
import android.view.KeyEvent;


import com.android.settings.R;
import com.android.internal.widget.LockPatternUtils;

import java.util.Collection;


import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.RunWith;


import static android.support.test.InstrumentationRegistry.getInstrumentation;
import java.util.Collection;
import static com.google.common.truth.Truth.assertThat;
import java.util.List;
import static org.junit.Assert.assertTrue;


/**
/**
 * Tests for {@link ChooseLockGenericTest}
 * Tests for {@link ChooseLockGenericTest}
@@ -68,7 +70,7 @@ public class ChooseLockGenericTest {
    private UiDevice mDevice;
    private UiDevice mDevice;
    private Context mTargetContext;
    private Context mTargetContext;
    private String mSettingPackage;
    private String mSettingPackage;
    private PackageManager mPackageManager;

    @Rule
    @Rule
    public ActivityTestRule<ChooseLockGeneric> mChooseLockGenericActivityRule =
    public ActivityTestRule<ChooseLockGeneric> mChooseLockGenericActivityRule =
            new ActivityTestRule<>(
            new ActivityTestRule<>(
@@ -81,108 +83,105 @@ public class ChooseLockGenericTest {
        mDevice = UiDevice.getInstance(getInstrumentation());
        mDevice = UiDevice.getInstance(getInstrumentation());
        mTargetContext = getInstrumentation().getTargetContext();
        mTargetContext = getInstrumentation().getTargetContext();
        mSettingPackage = mTargetContext.getPackageName();
        mSettingPackage = mTargetContext.getPackageName();
        mPackageManager = mTargetContext.getPackageManager();

        setPassword();
    }

    @After
    public void tearDown() throws Exception {
        clearPassword();
    }
    }


    @Test
    @Test
    public void testConfirmLockPasswordShown_deviceWithPassword() throws Exception, Throwable {
    public void testConfirmLockPasswordShown_deviceWithPassword() throws Throwable {
        setPassword();
        try {
            // GIVEN a PIN password is set on this device at set up.
            // GIVEN a PIN password is set on this device at set up.
            // WHEN ChooseLockGeneric is launched with no extras.
            // WHEN ChooseLockGeneric is launched with no extras.
            mChooseLockGenericActivityRule.launchActivity(null /* No extras */);
            mChooseLockGenericActivityRule.launchActivity(null /* No extras */);
            // THEN ConfirmLockPassword.InternalActivity is shown.
            // THEN ConfirmLockPassword.InternalActivity is shown.
        assertThat(getCurrentActivity()).isInstanceOf(ConfirmLockPassword.InternalActivity.class);
            assertThat(getCurrentActivity())
                    .isInstanceOf(ConfirmLockPassword.InternalActivity.class);
        } finally {
            finishAllAppTasks();
            mDevice.waitForIdle();
            clearPassword();
        }
    }
    }


    @Test
    @Test
    public void testConfirmLockPasswordShown_deviceWithPassword_phishingAttack()
    public void testConfirmLockPasswordShown_deviceWithPassword_phishingAttack() throws Throwable {
            throws Exception, Throwable {
        setPassword();
        try {
            // GIVEN a PIN password is set on this device at set up.
            // GIVEN a PIN password is set on this device at set up.
            // WHEN ChooseLockGeneric is launched with extras to by-pass lock password confirmation.
            // WHEN ChooseLockGeneric is launched with extras to by-pass lock password confirmation.
            mChooseLockGenericActivityRule.launchActivity(PHISHING_ATTACK_INTENT);
            mChooseLockGenericActivityRule.launchActivity(PHISHING_ATTACK_INTENT);
            // THEN ConfirmLockPassword.InternalActivity is still shown.
            // THEN ConfirmLockPassword.InternalActivity is still shown.
        assertThat(getCurrentActivity()).isInstanceOf(ConfirmLockPassword.InternalActivity.class);
            assertThat(getCurrentActivity())
                    .isInstanceOf(ConfirmLockPassword.InternalActivity.class);
        } finally {
            finishAllAppTasks();
            mDevice.waitForIdle();
            clearPassword();
        }
    }

    @Test
    public void testForFingerprint_inflateLayout() {
        mChooseLockGenericActivityRule.launchActivity(new Intent()
                .putExtra(ChooseLockSettingsHelper.EXTRA_KEY_FOR_FINGERPRINT, true));

        assertThat(mChooseLockGenericActivityRule.getActivity().isResumed()).isTrue();
    }
    }


    private Activity getCurrentActivity() throws Throwable {
    private Activity getCurrentActivity() throws Throwable {
        getInstrumentation().waitForIdleSync();
        getInstrumentation().waitForIdleSync();
        final Activity[] activity = new Activity[1];
        final Activity[] activity = new Activity[1];
        getInstrumentation().runOnMainSync(new Runnable() {
        getInstrumentation().runOnMainSync(() -> {
            @Override
            public void run() {
            Collection<Activity> activities = ActivityLifecycleMonitorRegistry.getInstance()
            Collection<Activity> activities = ActivityLifecycleMonitorRegistry.getInstance()
                    .getActivitiesInStage(Stage.RESUMED);
                    .getActivitiesInStage(Stage.RESUMED);
            activity[0] = activities.iterator().next();
            activity[0] = activities.iterator().next();
            }
        });
        });
        return activity[0];
        return activity[0];
    }
    }


    private void launchNewPassword() throws Exception {
        Intent newPasswordIntent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD)
                .setPackage(mSettingPackage)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getInstrumentation().getContext().startActivity(newPasswordIntent);
        mDevice.waitForIdle();
    }

    /** Sets a PIN password, 12345, for testing. */
    /** Sets a PIN password, 12345, for testing. */
    private void setPassword() throws Exception {
    private void setPassword() throws Exception {
        launchNewPassword();
        Intent newPasswordIntent = new Intent(getTargetContext(), ChooseLockGeneric.class)

                .putExtra(LockPatternUtils.PASSWORD_TYPE_KEY,
        if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) {
                        DevicePolicyManager.PASSWORD_QUALITY_NUMERIC)
            // Set "lock_none", but it actually means we don't want to enroll a fingerprint.
                .putExtra(ChooseLockSettingsHelper.EXTRA_KEY_PASSWORD,
            UiObject view = new UiObject(
                        "12345")
                    new UiSelector().resourceId(mSettingPackage + ":id/lock_none"));
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            assertTrue("lock_none", view.waitForExists(TIMEOUT));
        getInstrumentation().getContext().startActivity(newPasswordIntent);
            view.click();
        mDevice.waitForIdle();
        mDevice.waitForIdle();
        }


        // Pick PIN from the option list
        UiObject view = new UiObject(new UiSelector()
                .resourceId(mSettingPackage + ":id/lock_pin"));
        assertTrue("lock_pin", view.waitForExists(TIMEOUT));
        view.click();
        mDevice.waitForIdle();


        // Ignore any interstitial options
        // Ignore any interstitial options
        view = new UiObject(new UiSelector()
        UiObject view = new UiObject(new UiSelector()
                .resourceId(mSettingPackage + ":id/encrypt_dont_require_password"));
                .resourceId(mSettingPackage + ":id/encrypt_dont_require_password"));
        if (view.waitForExists(TIMEOUT)) {
        if (view.waitForExists(TIMEOUT)) {
            view.click();
            view.click();
            mDevice.waitForIdle();
            mDevice.waitForIdle();
        }
        }


        // Yes, we really want to
        view = new UiObject(new UiSelector()
                .resourceId(mSettingPackage + ":id/next_button"));
        if (view.waitForExists(TIMEOUT)) {
            view.click();
            mDevice.waitForIdle();
        }

        // Set our PIN
        // Set our PIN
        view = new UiObject(new UiSelector()
        view = new UiObject(new UiSelector()
                .resourceId(mSettingPackage + ":id/password_entry"));
                .resourceId(mSettingPackage + ":id/password_entry"));
        assertTrue("password_entry", view.waitForExists(TIMEOUT));
        assertTrue("password_entry", view.waitForExists(TIMEOUT));


        // Enter it twice to confirm
        // Enter it twice to confirm
        enterTestPin();
        enterTestPin(view);
        enterTestPin();
        enterTestPin(view);


        mDevice.pressBack();
        mDevice.pressBack();

        assertThat(getTargetContext().getSystemService(KeyguardManager.class).isDeviceSecure())
                .isTrue();
    }
    }


    /** Clears the previous set PIN password. */
    /** Clears the previous set PIN password. */
    private void clearPassword() throws Exception {
    private void clearPassword() throws Exception {
        launchNewPassword();
        Intent newPasswordIntent = new Intent(getTargetContext(), ChooseLockGeneric.class)
                .putExtra(LockPatternUtils.PASSWORD_TYPE_KEY,
                        DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        getInstrumentation().getContext().startActivity(newPasswordIntent);
        mDevice.waitForIdle();


        // Enter current PIN
        // Enter current PIN
        UiObject view = new UiObject(
        UiObject view = new UiObject(
@@ -195,40 +194,26 @@ public class ChooseLockGenericTest {
            assertTrue("password_entry", view.waitForExists(TIMEOUT));
            assertTrue("password_entry", view.waitForExists(TIMEOUT));
        }
        }


        enterTestPin();
        enterTestPin(view);


        // Set back to "none"
        mDevice.pressBack();
        view = new UiObject(new UiSelector().resourceId(mSettingPackage + ":id/lock_none"));
        assertTrue("lock_none", view.waitForExists(TIMEOUT));
        view.click();
        mDevice.waitForIdle();


        // Yes, we really want "none" if prompted again
        assertThat(getTargetContext().getSystemService(KeyguardManager.class).isDeviceSecure())
        view = new UiObject(new UiSelector().resourceId(mSettingPackage + ":id/lock_none"));
                .isFalse();
        if (view.waitForExists(TIMEOUT)) {
            view.click();
            mDevice.waitForIdle();
    }
    }


        // Yes, we really want to
    private void finishAllAppTasks() {
        view = new UiObject(new UiSelector()
        final ActivityManager activityManager =
                .resourceId("android:id/button1"));
                getTargetContext().getSystemService(ActivityManager.class);
        if (view.waitForExists(TIMEOUT)) {
        final List<AppTask> appTasks = activityManager.getAppTasks();
            view.click();
        for (ActivityManager.AppTask task : appTasks) {
            mDevice.waitForIdle();
            task.finishAndRemoveTask();
        }
        }

        mDevice.pressBack();
    }
    }


    private void enterTestPin() throws Exception {
    private void enterTestPin(UiObject view) throws Exception {
        mDevice.waitForIdle();
        mDevice.pressKeyCode(KeyEvent.KEYCODE_1);
        mDevice.pressKeyCode(KeyEvent.KEYCODE_2);
        mDevice.pressKeyCode(KeyEvent.KEYCODE_3);
        mDevice.pressKeyCode(KeyEvent.KEYCODE_4);
        mDevice.pressKeyCode(KeyEvent.KEYCODE_5);
        mDevice.waitForIdle();
        mDevice.waitForIdle();
        view.setText("12345");
        mDevice.pressEnter();
        mDevice.pressEnter();
        mDevice.waitForIdle();
        mDevice.waitForIdle();
    }
    }