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

Commit 28c4a576 authored by Ajay Kumar Nadathur Sreenivasan's avatar Ajay Kumar Nadathur Sreenivasan Committed by android-build-merger
Browse files

Merge "Add missing 'pin' screen lock option" into oc-dr1-dev am: f5b77d65

am: c14e0660

Change-Id: Ia9d5cf1fc594d9db756c1325cbb4a71a901a3934
parents d92d83e4 c14e0660
Loading
Loading
Loading
Loading
+1 −7
Original line number Diff line number Diff line
@@ -46,15 +46,13 @@ public class ChooseLockTypeDialogFragment extends InstrumentedDialogFragment
        implements OnClickListener {

    private static final String ARG_USER_ID = "userId";
    private static final String ARG_EXCLUDE_LOCK = "excludeLock";

    private ScreenLockAdapter mAdapter;
    private ChooseLockGenericController mController;

    public static ChooseLockTypeDialogFragment newInstance(int userId, String excludeLock) {
    public static ChooseLockTypeDialogFragment newInstance(int userId) {
        Bundle args = new Bundle();
        args.putInt(ARG_USER_ID, userId);
        args.putString(ARG_EXCLUDE_LOCK, excludeLock);
        ChooseLockTypeDialogFragment fragment = new ChooseLockTypeDialogFragment();
        fragment.setArguments(args);
        return fragment;
@@ -96,10 +94,6 @@ public class ChooseLockTypeDialogFragment extends InstrumentedDialogFragment
                mController.getVisibleScreenLockTypes(
                        DevicePolicyManager.PASSWORD_QUALITY_SOMETHING,
                        false /* includeDisabled */);
        String excludeLockName = getArguments().getString(ARG_EXCLUDE_LOCK);
        if (excludeLockName != null) {
            locks.remove(ScreenLockType.valueOf(excludeLockName));
        }
        mAdapter = new ScreenLockAdapter(context, locks, mController);
        builder.setAdapter(mAdapter, this);
        builder.setTitle(R.string.setup_lock_settings_options_dialog_title);
+7 −3
Original line number Diff line number Diff line
@@ -114,9 +114,7 @@ public class SetupChooseLockPassword extends ChooseLockPassword {
        }

        private void launchChooseLockGeneric() {
            ScreenLockType currentLock = mIsAlphaMode
                    ? ScreenLockType.PASSWORD : ScreenLockType.PIN;
            ChooseLockTypeDialogFragment.newInstance(mUserId, currentLock.toString())
            ChooseLockTypeDialogFragment.newInstance(mUserId)
                    .show(getChildFragmentManager(), null);
        }

@@ -130,6 +128,12 @@ public class SetupChooseLockPassword extends ChooseLockPassword {

        @Override
        public void onLockTypeSelected(ScreenLockType lock) {
            ScreenLockType currentLockType = mIsAlphaMode ?
                    ScreenLockType.PASSWORD : ScreenLockType.PIN;
            if (currentLockType.equals(lock)) {
                // ignore same lock type.
                return;
            }
            Intent activityIntent = getActivity().getIntent();
            Intent intent = new Intent(getContext(), SetupChooseLockGeneric.class);

+115 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.password;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Context;

import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.password.ChooseLockTypeDialogFragment.OnLockTypeSelectedListener;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowEventLogWriter;
import com.android.settings.testutils.shadow.ShadowUserManager;
import com.android.settings.testutils.shadow.ShadowUtils;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowAlertDialog;
import org.robolectric.shadows.ShadowDialog;
import org.robolectric.util.FragmentTestUtil;

@RunWith(SettingsRobolectricTestRunner.class)
@Config(
        manifest = TestConfig.MANIFEST_PATH,
        sdk = TestConfig.SDK_VERSION,
        shadows = {
                ShadowEventLogWriter.class,
                ShadowUserManager.class,
                ShadowUtils.class
        })
public class ChooseLockTypeDialogFragmentTest {
    private Context mContext;
    private TestFragment mFragment;

    @Before
    public void setUp() {
        mContext = RuntimeEnvironment.application;
        mFragment = new TestFragment();
        FragmentTestUtil.startFragment(mFragment);
    }

    @Test
    public void testThatDialog_IsShown() {
        AlertDialog latestDialog = startLockFragment();
        assertNotNull(latestDialog);
        ShadowDialog shadowDialog = Shadows.shadowOf(latestDialog);
        // verify that we are looking at the expected dialog.
        assertEquals(shadowDialog.getTitle(),
                mContext.getString(R.string.setup_lock_settings_options_dialog_title));
    }

    @Test
    public void testThat_OnClickListener_IsCalled() {
        mFragment.mDelegate = mock(OnLockTypeSelectedListener.class);
        AlertDialog lockDialog = startLockFragment();
        ShadowAlertDialog shadowAlertDialog = Shadows.shadowOf(lockDialog);
        shadowAlertDialog.clickOnItem(0);
        verify(mFragment.mDelegate, times(1)).onLockTypeSelected(any(ScreenLockType.class));
    }

    @Test
    public void testThat_OnClickListener_IsNotCalledWhenCancelled() {
        mFragment.mDelegate = mock(OnLockTypeSelectedListener.class);
        AlertDialog lockDialog = startLockFragment();
        lockDialog.dismiss();
        verify(mFragment.mDelegate, never()).onLockTypeSelected(any(ScreenLockType.class));
    }


    private AlertDialog startLockFragment() {
        ChooseLockTypeDialogFragment chooseLockTypeDialogFragment =
                ChooseLockTypeDialogFragment.newInstance(1234);
        chooseLockTypeDialogFragment.show(mFragment.getChildFragmentManager(), null);
        return ShadowAlertDialog.getLatestAlertDialog();
    }


    public static class TestFragment extends Fragment
            implements OnLockTypeSelectedListener{
        OnLockTypeSelectedListener mDelegate;
        @Override
        public void onLockTypeSelected(ScreenLockType lock) {
            if (mDelegate != null) {
                mDelegate.onLockTypeSelected(lock);
            }
        }
    }
}
+22 −11
Original line number Diff line number Diff line
@@ -21,16 +21,17 @@ import static com.google.common.truth.Truth.assertThat;
import static org.robolectric.RuntimeEnvironment.application;
import static org.robolectric.Shadows.shadowOf;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;

import com.android.settings.R;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.password.ChooseLockGeneric.ChooseLockGenericFragment;
import com.android.settings.password.ChooseLockPassword.IntentBuilder;
import com.android.settings.password.SetupChooseLockPassword.SetupChooseLockPasswordFragment;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.SettingsShadowResources;
import com.android.settings.testutils.shadow.ShadowDynamicIndexableContentMonitor;
import com.android.settings.testutils.shadow.ShadowEventLogWriter;
@@ -39,9 +40,11 @@ import com.android.settings.testutils.shadow.ShadowUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowActivity;
import org.robolectric.shadows.ShadowActivity.IntentForResult;
import org.robolectric.shadows.ShadowAlertDialog;
import org.robolectric.shadows.ShadowDialog;

@RunWith(SettingsRobolectricTestRunner.class)
@@ -69,22 +72,22 @@ public class SetupChooseLockPasswordTest {

    @Test
    public void createActivity_withShowOptionsButtonExtra_shouldShowButton() {
        Intent intent = SetupChooseLockPassword.modifyIntentForSetup(
                application,
                new IntentBuilder(application).build());
        intent.putExtra(ChooseLockGenericFragment.EXTRA_SHOW_OPTIONS_BUTTON, true);
        SetupChooseLockPassword activity =
                Robolectric.buildActivity(SetupChooseLockPassword.class, intent).setup().get();

        SetupChooseLockPassword activity = createSetupChooseLockPassword();
        Button optionsButton = activity.findViewById(R.id.screen_lock_options);
        assertThat(optionsButton).isNotNull();

        ShadowActivity shadowActivity = shadowOf(activity);
        optionsButton.performClick();

        assertThat(ShadowDialog.getLatestDialog()).isNotNull();
    }

    @Test
    public void allSecurityOptions_shouldBeShown_When_OptionsButtonIsClicked() {
        SetupChooseLockPassword activity = createSetupChooseLockPassword();
        activity.findViewById(R.id.screen_lock_options).performClick();
        AlertDialog latestAlertDialog = ShadowAlertDialog.getLatestAlertDialog();
        int count = Shadows.shadowOf(latestAlertDialog).getAdapter().getCount();
        assertThat(count).named("List items shown").isEqualTo(3);
    }

    @Test
    public void createActivity_clickDifferentOption_extrasShouldBePropagated() {
        Bundle bundle = new Bundle();
@@ -111,4 +114,12 @@ public class SetupChooseLockPasswordTest {
        assertThat(chooseLockIntent.intent.getStringExtra("foo")).named("Foo extra")
                .isEqualTo("bar");
    }

    private SetupChooseLockPassword createSetupChooseLockPassword() {
        Intent intent = SetupChooseLockPassword.modifyIntentForSetup(
                application,
                new IntentBuilder(application).build());
        intent.putExtra(ChooseLockGenericFragment.EXTRA_SHOW_OPTIONS_BUTTON, true);
        return Robolectric.buildActivity(SetupChooseLockPassword.class, intent).setup().get();
    }
}