fix: 引入Settings的Module

This commit is contained in:
2024-12-10 14:57:24 +08:00
parent ad8fc8731d
commit df105485bd
6934 changed files with 896168 additions and 2 deletions

View File

@@ -0,0 +1,71 @@
/*
* Copyright (C) 2019 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.language;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import android.view.inputmethod.InputMethodInfo;
import com.android.settings.testutils.shadow.ShadowInputMethodManagerWithMethodList;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import java.util.ArrayList;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowInputMethodManagerWithMethodList.class)
public class LanguageAndInputPreferenceControllerTest {
private Context mContext;
@Before
public void setUp() {
mContext = spy(RuntimeEnvironment.application);
}
@Test
public void getSummary_shouldSetToCurrentImeName() {
final ComponentName componentName = new ComponentName("name1", "cls");
final ContentResolver cr = mContext.getContentResolver();
Settings.Secure.putString(cr, Settings.Secure.DEFAULT_INPUT_METHOD,
componentName.flattenToString());
final List<InputMethodInfo> imis = new ArrayList<>();
imis.add(mock(InputMethodInfo.class));
when(imis.get(0).getPackageName()).thenReturn("name1");
when(imis.get(0).loadLabel(any())).thenReturn("label");
ShadowInputMethodManagerWithMethodList.getShadow().setInputMethodList(imis);
final LanguageAndInputPreferenceController controller =
new LanguageAndInputPreferenceController(mContext, "key");
assertThat(controller.getSummary().toString()).contains("label");
}
}

View File

@@ -0,0 +1,185 @@
/*
* Copyright (C) 2016 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.language;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.content.res.Resources;
import android.hardware.input.InputManager;
import android.os.UserManager;
import android.util.FeatureFlagUtils;
import android.view.autofill.AutofillManager;
import android.view.inputmethod.InputMethodManager;
import android.view.textservice.TextServicesManager;
import androidx.lifecycle.LifecycleObserver;
import com.android.settings.R;
import com.android.settings.testutils.XmlTestUtils;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.ArrayList;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class LanguageAndInputSettingsTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Activity mActivity;
@Mock
private InputManager mIm;
@Mock
private InputMethodManager mImm;
@Mock
private DevicePolicyManager mDpm;
@Mock
private AutofillManager mAutofillManager;
private TestFragment mFragment;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mock(UserManager.class));
when(mActivity.getSystemService(Context.INPUT_SERVICE))
.thenReturn(mock(InputManager.class));
when(mActivity.getSystemService(Context.INPUT_SERVICE)).thenReturn(mIm);
when(mActivity.getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE))
.thenReturn(mock(TextServicesManager.class));
when(mActivity.getSystemService(Context.DEVICE_POLICY_SERVICE)).thenReturn(mDpm);
when(mActivity.getSystemService(Context.INPUT_METHOD_SERVICE)).thenReturn(mImm);
when((Object) mActivity.getSystemService(AutofillManager.class))
.thenReturn(mAutofillManager);
mFragment = new TestFragment(mActivity);
}
@Test
public void testGetPreferenceScreenResId() {
assertThat(mFragment.getPreferenceScreenResId()).isEqualTo(R.xml.language_and_input);
}
@Test
public void testGetPreferenceControllers_shouldRegisterLifecycleObservers() {
final List<AbstractPreferenceController> controllers =
mFragment.createPreferenceControllers(mActivity);
int lifecycleObserverCount = 0;
for (AbstractPreferenceController controller : controllers) {
if (controller instanceof LifecycleObserver) {
lifecycleObserverCount++;
}
}
verify(mFragment.getSettingsLifecycle(), times(lifecycleObserverCount))
.addObserver(any(LifecycleObserver.class));
}
@Test
public void testGetPreferenceControllers_shouldAllBeCreated() {
final List<AbstractPreferenceController> controllers =
mFragment.createPreferenceControllers(mActivity);
assertThat(controllers.isEmpty()).isFalse();
}
@Test
public void testNonIndexableKeys_existInXmlLayout() {
final Context context = spy(RuntimeEnvironment.application);
final Resources res = spy(RuntimeEnvironment.application.getResources());
final InputManager inputManager = mock(InputManager.class);
final TextServicesManager textServicesManager = mock(TextServicesManager.class);
FeatureFlagUtils.setEnabled(context, FeatureFlagUtils.SETTINGS_NEW_KEYBOARD_UI, false);
when(inputManager.getInputDeviceIds()).thenReturn(new int[0]);
doReturn(inputManager).when(context).getSystemService(Context.INPUT_SERVICE);
doReturn(textServicesManager).when(context)
.getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE);
doReturn(res).when(context).getResources();
doReturn(false).when(res)
.getBoolean(com.android.internal.R.bool.config_supportSystemNavigationKeys);
final List<String> niks =
LanguageAndInputSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(context);
LanguageAndInputSettings settings = new LanguageAndInputSettings();
final int xmlId = settings.getPreferenceScreenResId();
final List<String> keys = XmlTestUtils.getKeysFromPreferenceXml(context, xmlId);
assertThat(keys).containsAtLeastElementsIn(niks);
}
@Test
public void testPreferenceControllers_getPreferenceKeys_existInPreferenceScreen() {
final Context context = spy(RuntimeEnvironment.application);
final TextServicesManager textServicesManager = mock(TextServicesManager.class);
doReturn(textServicesManager).when(context)
.getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE);
final LanguageAndInputSettings fragment = new LanguageAndInputSettings();
final List<String> preferenceScreenKeys =
XmlTestUtils.getKeysFromPreferenceXml(context, fragment.getPreferenceScreenResId());
final List<String> preferenceKeys = new ArrayList<>();
for (AbstractPreferenceController controller : fragment.createPreferenceControllers(context)) {
preferenceKeys.add(controller.getPreferenceKey());
}
assertThat(preferenceScreenKeys).containsAtLeastElementsIn(preferenceKeys);
}
/**
* Test fragment to expose lifecycle and context so we can verify behavior for observables.
*/
public static class TestFragment extends LanguageAndInputSettings {
private Lifecycle mLifecycle;
private Context mContext;
public TestFragment(Context context) {
mContext = context;
mLifecycle = mock(Lifecycle.class);
}
@Override
public Context getContext() {
return mContext;
}
@Override
public Lifecycle getSettingsLifecycle() {
if (mLifecycle == null) {
return super.getSettingsLifecycle();
}
return mLifecycle;
}
}
}

View File

@@ -0,0 +1,118 @@
/*
* Copyright (C) 2016 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.language;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.res.AssetManager;
import androidx.preference.Preference;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.testutils.FakeFeatureFactory;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import java.util.ArrayList;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class PhoneLanguagePreferenceControllerTest {
@Mock
private Preference mPreference;
@Mock
private AssetManager mAssets;
private Context mContext;
private FakeFeatureFactory mFeatureFactory;
private PhoneLanguagePreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
when(mContext.getAssets()).thenReturn(mAssets);
mFeatureFactory = FakeFeatureFactory.setupForTest();
mController = new PhoneLanguagePreferenceController(mContext, "key");
}
@Test
public void testIsAvailable_hasMultipleLocales_shouldReturnTrue() {
when(mAssets.getLocales()).thenReturn(new String[] {"en", "de"});
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void testIsAvailable_hasSingleLocales_shouldReturnFalse() {
when(mAssets.getLocales()).thenReturn(new String[] {"en"});
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void testGetAvailabilityStatus_hasMultipleLocales_returnAvailable() {
when(mAssets.getLocales()).thenReturn(new String[] {"en", "de"});
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
}
@Test
public void testGetAvailabilityStatus_hasSingleLocales_returnConditionallyUnavailable() {
when(mAssets.getLocales()).thenReturn(new String[] {"en"});
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
}
@Test
@Config(qualifiers = "mcc999")
public void testIsAvailable_ifDisabled_shouldReturnFalse() {
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void testUpdateState_shouldUpdateSummary() {
final String testSummary = "test";
when(mFeatureFactory.localeFeatureProvider.getLocaleNames()).thenReturn(testSummary);
mController.updateState(mPreference);
verify(mPreference).setSummary(testSummary);
}
@Test
public void testUpdateNonIndexable_shouldAddKey() {
final List<String> niks = new ArrayList<>();
mController.updateNonIndexableKeys(niks);
assertThat(niks).containsExactly(mController.getPreferenceKey());
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright (C) 2017 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.language;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import android.content.Context;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(RobolectricTestRunner.class)
public class PointerSpeedControllerTest {
private Context mContext;
private PointerSpeedController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application.getApplicationContext());
mController = new PointerSpeedController(mContext);
}
@Test
public void testDeviceAdministrators_byDefault_shouldBeShown() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
@Config(qualifiers = "mcc999")
public void testDeviceAdministrators_ifDisabled_shouldNotBeShown() {
assertThat(mController.isAvailable()).isFalse();
}
}

View File

@@ -0,0 +1,98 @@
/*
* Copyright (C) 2016 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.language;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TtsEngines;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import java.util.ArrayList;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class TtsPreferenceControllerTest {
@Mock
private TtsEngines mTtsEngines;
@Mock
private PreferenceScreen mScreen;
private Context mContext;
private TtsPreferenceController mController;
private Preference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mController = new TtsPreferenceController(mContext, "test_key");
mController.mTtsEngines = mTtsEngines;
mPreference = new Preference(RuntimeEnvironment.application);
mPreference.setKey(mController.getPreferenceKey());
when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference);
}
@Test
public void testIsAvailable_ttsEngineEmpty_shouldReturnFalse() {
// Not available when there is no engine.
when(mTtsEngines.getEngines()).thenReturn(new ArrayList<>());
assertThat(mController.isAvailable()).isFalse();
mController.displayPreference(mScreen);
assertThat(mPreference.isVisible()).isFalse();
}
@Test
public void testIsAvailable_ttsEngineInstalled_shouldReturnTrue() {
final List<TextToSpeech.EngineInfo> infolist = new ArrayList<>();
infolist.add(mock(TextToSpeech.EngineInfo.class));
when(mTtsEngines.getEngines()).thenReturn(infolist);
assertThat(mController.isAvailable()).isTrue();
mController.displayPreference(mScreen);
assertThat(mPreference.isVisible()).isTrue();
}
@Test
@Config(qualifiers = "mcc999")
public void testIsAvailable_ifDisabled_shouldReturnFalse() {
assertThat(mController.isAvailable()).isFalse();
}
}

View File

@@ -0,0 +1,112 @@
/*
* Copyright (C) 2016 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.language;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import androidx.preference.Preference;
import com.android.settings.inputmethod.UserDictionaryList;
import com.android.settings.inputmethod.UserDictionarySettings;
import com.android.settings.testutils.FakeFeatureFactory;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.TreeSet;
@RunWith(RobolectricTestRunner.class)
public class UserDictionaryPreferenceControllerTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
private Preference mPreference;
private TestController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
doReturn(mock(DevicePolicyManager.class)).when(mContext)
.getSystemService(Context.DEVICE_POLICY_SERVICE);
FakeFeatureFactory.setupForTest();
mController = new TestController(mContext);
mPreference = new Preference(RuntimeEnvironment.application);
}
@Test
public void testIsAvailable_shouldReturnTrue() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void updateState_noLocale_setUserDictionarySettingsAsFragment() {
mController.updateState(mPreference);
assertThat(mPreference.getFragment())
.isEqualTo(UserDictionarySettings.class.getCanonicalName());
}
@Test
public void updateState_singleLocale_setUserDictionarySettingsAsFragment_setLocaleInExtra() {
mController.mLocales.add("en");
mController.updateState(mPreference);
final String fragmentName = UserDictionarySettings.class.getCanonicalName();
assertThat(mPreference.getFragment()).isEqualTo(fragmentName);
assertThat(mPreference.getExtras().getString("locale")).isEqualTo("en");
}
@Test
public void updateState_multiLocale_setUserDictionaryListAsFragment() {
mController.mLocales.add("en");
mController.mLocales.add("de");
mController.updateState(mPreference);
assertThat(mPreference.getFragment())
.isEqualTo(UserDictionaryList.class.getCanonicalName());
}
/**
* Fake Controller that overrides getDictionaryLocales to make testing the rest of stuff easier.
*/
private class TestController extends UserDictionaryPreferenceController {
private TreeSet<String> mLocales = new TreeSet<>();
@Override
protected TreeSet<String> getDictionaryLocales() {
return mLocales;
}
private TestController(Context context) {
super(context, "test_key");
}
}
}