Merge "Support following typing focus in window mode [3/n]."

This commit is contained in:
Daniel Hsieh
2022-01-15 15:18:46 +00:00
committed by Android (Google) Code Review
5 changed files with 250 additions and 20 deletions

View File

@@ -0,0 +1,97 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* 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.accessibility;
import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.content.Context;
import android.provider.Settings;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class MagnificationFollowTypingPreferenceControllerTest {
private static final String KEY_FOLLOW_TYPING =
Settings.Secure.ACCESSIBILITY_MAGNIFICATION_FOLLOW_TYPING_ENABLED;
private final Context mContext = ApplicationProvider.getApplicationContext();
private final SwitchPreference mSwitchPreference = spy(new SwitchPreference(mContext));
private final MagnificationFollowTypingPreferenceController mController =
new MagnificationFollowTypingPreferenceController(mContext,
MagnificationFollowTypingPreferenceController.PREF_KEY);
@Before
public void setUp() {
final PreferenceManager preferenceManager = new PreferenceManager(mContext);
final PreferenceScreen screen = preferenceManager.createPreferenceScreen(mContext);
mSwitchPreference.setKey(MagnificationFollowTypingPreferenceController.PREF_KEY);
screen.addPreference(mSwitchPreference);
mController.displayPreference(screen);
}
@Test
public void isChecked_defaultStateForFollowTyping_onResumeShouldReturnTrue() {
mController.onResume();
assertThat(mController.isChecked()).isTrue();
assertThat(mSwitchPreference.isChecked()).isTrue();
}
@Test
public void isChecked_enableFollowTyping_onResumeShouldReturnTrue() {
Settings.Secure.putInt(mContext.getContentResolver(), KEY_FOLLOW_TYPING, ON);
mController.onResume();
assertThat(mController.isChecked()).isTrue();
assertThat(mSwitchPreference.isChecked()).isTrue();
}
@Test
public void isChecked_disableFollowTyping_onResumeShouldReturnFalse() {
Settings.Secure.putInt(mContext.getContentResolver(), KEY_FOLLOW_TYPING, OFF);
mController.onResume();
assertThat(mController.isChecked()).isFalse();
assertThat(mSwitchPreference.isChecked()).isFalse();
}
@Test
public void performClick_switchDefaultStateForFollowTyping_shouldReturnFalse() {
mController.onResume();
mSwitchPreference.performClick();
verify(mSwitchPreference).setChecked(false);
assertThat(mController.isChecked()).isFalse();
assertThat(mSwitchPreference.isChecked()).isFalse();
}
}

View File

@@ -49,6 +49,7 @@ import androidx.fragment.app.FragmentActivity;
import androidx.preference.Preference;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.DialogCreatable;
@@ -59,9 +60,9 @@ import com.android.settings.testutils.shadow.ShadowSettingsPreferenceFragment;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
@@ -88,23 +89,57 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
private static final String MAGNIFICATION_CONTROLLER_NAME =
"com.android.server.accessibility.MagnificationController";
private static final String KEY_FOLLOW_TYPING =
Settings.Secure.ACCESSIBILITY_MAGNIFICATION_FOLLOW_TYPING_ENABLED;
private TestToggleScreenMagnificationPreferenceFragment mFragment;
private Context mContext;
private Resources mResources;
private FragmentActivity mActivity;
@Before
public void setUpTestFragment() {
MockitoAnnotations.initMocks(this);
mContext = spy(ApplicationProvider.getApplicationContext());
mActivity = Robolectric.setupActivity(FragmentActivity.class);
final FragmentActivity activity = Robolectric.setupActivity(FragmentActivity.class);
mFragment = spy(new TestToggleScreenMagnificationPreferenceFragment(mContext));
mResources = spy(mContext.getResources());
when(mContext.getResources()).thenReturn(mResources);
when(mFragment.getContext().getResources()).thenReturn(mResources);
doReturn(mActivity).when(mFragment).getActivity();
doReturn(activity).when(mFragment).getActivity();
}
@Ignore("Ignore it since a NPE is happened in ShadowWindowManagerGlobal. (Ref. b/214161063)")
@Test
@Config(shadows = ShadowFragment.class)
public void onResume_defaultStateForFollowingTyping_switchPreferenceShouldReturnTrue() {
mFragment.onCreate(new Bundle());
mFragment.onCreateView(LayoutInflater.from(mContext), mock(ViewGroup.class), Bundle.EMPTY);
mFragment.onAttach(mContext);
final SwitchPreference switchPreference =
mFragment.findPreference(MagnificationFollowTypingPreferenceController.PREF_KEY);
mFragment.onResume();
assertThat(switchPreference).isNotNull();
assertThat(switchPreference.isChecked()).isTrue();
}
@Ignore("Ignore it since a NPE is happened in ShadowWindowManagerGlobal. (Ref. b/214161063)")
@Test
@Config(shadows = ShadowFragment.class)
public void onResume_disableFollowingTyping_switchPreferenceShouldReturnFalse() {
Settings.Secure.putInt(mContext.getContentResolver(), KEY_FOLLOW_TYPING, OFF);
mFragment.onCreate(new Bundle());
mFragment.onCreateView(LayoutInflater.from(mContext), mock(ViewGroup.class), Bundle.EMPTY);
mFragment.onAttach(mContext);
SwitchPreference switchPreference =
mFragment.findPreference(MagnificationFollowTypingPreferenceController.PREF_KEY);
mFragment.onResume();
assertThat(switchPreference).isNotNull();
assertThat(switchPreference.isChecked()).isFalse();
}
@Test
@@ -267,6 +302,7 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
assertThat(expectedType).isEqualTo(UserShortcutType.HARDWARE | UserShortcutType.TRIPLETAP);
}
@Ignore("Ignore it since a NPE is happened in ShadowWindowManagerGlobal. (Ref. b/214161063)")
@Test
public void onCreateView_notSupportsMagnificationArea_settingsPreferenceIsNull() {
when(mResources.getBoolean(
@@ -278,13 +314,16 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
assertThat(mFragment.mSettingsPreference).isNull();
}
@Ignore("Ignore it since a NPE is happened in ShadowWindowManagerGlobal. (Ref. b/214161063)")
@Test
public void onCreateView_setDialogDelegateAndAddTheControllerToLifeCycleObserver() {
Lifecycle lifecycle = mock(Lifecycle.class);
when(mFragment.getSettingsLifecycle()).thenReturn(lifecycle);
mFragment.onCreateView(LayoutInflater.from(mContext), mock(ViewGroup.class), Bundle.EMPTY);
verify(mFragment).setDialogDelegate(any(MagnificationModePreferenceController.class));
verify(mFragment.mSpyLifeyCycle).addObserver(
any(MagnificationModePreferenceController.class));
verify(lifecycle).addObserver(any(MagnificationModePreferenceController.class));
}
@Test
@@ -331,8 +370,6 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
static class TestToggleScreenMagnificationPreferenceFragment
extends ToggleScreenMagnificationPreferenceFragment {
private final Lifecycle mSpyLifeyCycle = Mockito.mock(Lifecycle.class);
private final Context mContext;
private final PreferenceManager mPreferenceManager;
@@ -401,11 +438,6 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
// UI related function, do nothing in tests
}
@Override
public Lifecycle getSettingsLifecycle() {
return mSpyLifeyCycle;
}
@Override
public Context getContext() {
return mContext;