Merge changes I5409c1e8,I8ed80b44

* changes:
  Refactor CaptionAppearanceFragment to improve maintainability (4/n)
  Refactor CaptionAppearanceFragment to improve maintainability (3/n)
This commit is contained in:
Menghan Li
2022-07-25 08:42:40 +00:00
committed by Android (Google) Code Review
25 changed files with 2173 additions and 228 deletions

View File

@@ -0,0 +1,159 @@
/*
* 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.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.settings.SettingsEnums;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Bundle;
import android.provider.Settings;
import android.view.accessibility.CaptioningManager.CaptionStyle;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.testutils.XmlTestUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.util.ReflectionHelpers;
import java.util.List;
/** Tests for {@link CaptionAppearanceFragment}. */
@RunWith(RobolectricTestRunner.class)
public class CaptionAppearanceFragmentTest {
@Rule
public MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock
private SettingsActivity mActivity;
@Mock
private PreferenceScreen mScreen;
@Mock
private PreferenceManager mPreferenceManager;
@Mock
private ContentResolver mContentResolver;
@Mock
private PreferenceCategory mCustomPref;
@Spy
private Context mContext = ApplicationProvider.getApplicationContext();
private TestCaptionAppearanceFragment mFragment;
@Before
public void setUp() {
mFragment = spy(new TestCaptionAppearanceFragment());
doReturn(mActivity).when(mFragment).getActivity();
doReturn(mContext).when(mFragment).getContext();
doReturn(mCustomPref).when(mFragment).findPreference(mFragment.PREF_CUSTOM);
when(mPreferenceManager.getPreferenceScreen()).thenReturn(mScreen);
ReflectionHelpers.setField(mFragment, "mPreferenceManager", mPreferenceManager);
}
@Test
public void onCreatePreferences_shouldPreferenceIsInvisible() {
mFragment.onAttach(mContext);
mFragment.onCreatePreferences(Bundle.EMPTY, /* rootKey */ null);
verify(mCustomPref).setVisible(false);
}
@Test
public void onCreatePreferences_customValue_shouldPreferenceIsVisible() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, CaptionStyle.PRESET_CUSTOM);
mFragment.onAttach(mContext);
mFragment.onCreatePreferences(Bundle.EMPTY, /* rootKey */ null);
verify(mCustomPref).setVisible(true);
}
@Test
public void onStart_registerSpecificContentObserverForSpecificKeys() {
when(mContext.getContentResolver()).thenReturn(mContentResolver);
mFragment.onAttach(mContext);
mFragment.onCreatePreferences(Bundle.EMPTY, /* rootKey */ null);
mFragment.onStart();
for (String key : mFragment.CAPTIONING_FEATURE_KEYS) {
verify(mContentResolver).registerContentObserver(Settings.Secure.getUriFor(key),
/* notifyForDescendants= */ false, mFragment.mSettingsContentObserver);
}
}
@Test
public void onStop_unregisterContentObserver() {
when(mContext.getContentResolver()).thenReturn(mContentResolver);
mFragment.onAttach(mContext);
mFragment.onCreatePreferences(Bundle.EMPTY, /* rootKey */ null);
mFragment.onStart();
mFragment.onStop();
verify(mContentResolver).unregisterContentObserver(mFragment.mSettingsContentObserver);
}
@Test
public void getMetricsCategory_returnsCorrectCategory() {
assertThat(mFragment.getMetricsCategory()).isEqualTo(
SettingsEnums.ACCESSIBILITY_CAPTION_APPEARANCE);
}
@Test
public void getLogTag_returnsCorrectTag() {
assertThat(mFragment.getLogTag()).isEqualTo("CaptionAppearanceFragment");
}
@Test
public void getNonIndexableKeys_existInXmlLayout() {
final List<String> niks = CaptionAppearanceFragment.SEARCH_INDEX_DATA_PROVIDER
.getNonIndexableKeys(mContext);
final List<String> keys = XmlTestUtils.getKeysFromPreferenceXml(mContext,
R.xml.captioning_appearance);
assertThat(keys).containsAtLeastElementsIn(niks);
}
private static class TestCaptionAppearanceFragment extends CaptionAppearanceFragment {
@Override
public int getPreferenceScreenResId() {
return R.xml.placeholder_prefs;
}
}
}

View File

@@ -0,0 +1,114 @@
/*
* 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.when;
import android.content.Context;
import android.provider.Settings;
import android.util.AttributeSet;
import android.view.accessibility.CaptioningManager;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowCaptioningManager;
/** Tests for {@link CaptionBackgroundColorController}. */
@RunWith(RobolectricTestRunner.class)
public class CaptionBackgroundColorControllerTest {
@Rule
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock
private PreferenceScreen mScreen;
private final Context mContext = ApplicationProvider.getApplicationContext();
private CaptionBackgroundColorController mController;
private ColorPreference mPreference;
private ShadowCaptioningManager mShadowCaptioningManager;
@Before
public void setUp() {
mController = new CaptionBackgroundColorController(mContext, "captioning_background_color");
final AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
mPreference = new ColorPreference(mContext, attributeSet);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
mShadowCaptioningManager = Shadow.extract(captioningManager);
}
@Test
public void getAvailabilityStatus_shouldReturnAvailable() {
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
}
@Test
public void getSummary_defaultValue_shouldReturnBlack() {
mController.displayPreference(mScreen);
assertThat(mPreference.getSummary().toString()).isEqualTo("Black");
}
@Test
public void getSummary_redValue_shouldReturnRed() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_BACKGROUND_COLOR, 0xFFFF0000);
mController.displayPreference(mScreen);
assertThat(mPreference.getSummary().toString()).isEqualTo("Red");
}
@Test
public void setRedValue_shouldReturnRed() {
mController.displayPreference(mScreen);
mPreference.setValue(0xFFFF0000);
assertThat(mPreference.getSummary().toString()).isEqualTo("Red");
}
@Test
public void onValueChanged_shouldSetCaptionEnabled() {
mShadowCaptioningManager.setEnabled(false);
mController.displayPreference(mScreen);
mController.onValueChanged(mPreference, 0xFFFF0000);
final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
assertThat(isCaptionEnabled).isTrue();
}
}

View File

@@ -0,0 +1,115 @@
/*
* 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.when;
import android.content.Context;
import android.provider.Settings;
import android.util.AttributeSet;
import android.view.accessibility.CaptioningManager;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowCaptioningManager;
/** Tests for {@link CaptionBackgroundOpacityController}. */
@RunWith(RobolectricTestRunner.class)
public class CaptionBackgroundOpacityControllerTest {
@Rule
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock
private PreferenceScreen mScreen;
private final Context mContext = ApplicationProvider.getApplicationContext();
private CaptionBackgroundOpacityController mController;
private ColorPreference mPreference;
private ShadowCaptioningManager mShadowCaptioningManager;
@Before
public void setUp() {
mController =
new CaptionBackgroundOpacityController(mContext, "captioning_background_opacity");
final AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
mPreference = new ColorPreference(mContext, attributeSet);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
mShadowCaptioningManager = Shadow.extract(captioningManager);
}
@Test
public void getAvailabilityStatus_shouldReturnAvailable() {
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
}
@Test
public void getSummary_defaultValue_shouldReturnNonTransparent() {
mController.displayPreference(mScreen);
assertThat(mPreference.getSummary().toString()).isEqualTo("100%");
}
@Test
public void getSummary_halfTransparentValue_shouldReturnHalfTransparent() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_BACKGROUND_COLOR, 0x80FFFFFF);
mController.displayPreference(mScreen);
assertThat(mPreference.getSummary().toString()).isEqualTo("50%");
}
@Test
public void setHalfTransparentValue_shouldReturnHalfTransparent() {
mController.displayPreference(mScreen);
mPreference.setValue(0x80FFFFFF);
assertThat(mPreference.getSummary().toString()).isEqualTo("50%");
}
@Test
public void onValueChanged_shouldSetCaptionEnabled() {
mShadowCaptioningManager.setEnabled(false);
mController.displayPreference(mScreen);
mController.onValueChanged(mPreference, 0x80FFFFFF);
final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
assertThat(isCaptionEnabled).isTrue();
}
}

View File

@@ -0,0 +1,113 @@
/*
* 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.when;
import android.content.Context;
import android.provider.Settings;
import android.util.AttributeSet;
import android.view.accessibility.CaptioningManager;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowCaptioningManager;
/** Tests for {@link CaptionEdgeColorController}. */
@RunWith(RobolectricTestRunner.class)
public class CaptionEdgeColorControllerTest {
@Rule
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock
private PreferenceScreen mScreen;
private final Context mContext = ApplicationProvider.getApplicationContext();
private CaptionEdgeColorController mController;
private ColorPreference mPreference;
private ShadowCaptioningManager mShadowCaptioningManager;
@Before
public void setUp() {
mController = new CaptionEdgeColorController(mContext, "captioning_edge_color");
final AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
mPreference = new ColorPreference(mContext, attributeSet);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
mShadowCaptioningManager = Shadow.extract(captioningManager);
}
@Test
public void getAvailabilityStatus_shouldReturnAvailable() {
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
}
@Test
public void getSummary_defaultValue_shouldReturnBlack() {
mController.displayPreference(mScreen);
assertThat(mPreference.getSummary().toString()).isEqualTo("Black");
}
@Test
public void getSummary_redValue_shouldReturnRed() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_COLOR, 0xFFFF0000);
mController.displayPreference(mScreen);
assertThat(mPreference.getSummary().toString()).isEqualTo("Red");
}
@Test
public void setRedValue_shouldReturnRed() {
mController.displayPreference(mScreen);
mPreference.setValue(0xFFFF0000);
assertThat(mPreference.getSummary().toString()).isEqualTo("Red");
}
@Test
public void onValueChanged_shouldSetCaptionEnabled() {
mShadowCaptioningManager.setEnabled(false);
mController.displayPreference(mScreen);
mController.onValueChanged(mPreference, 0xFFFF0000);
final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
assertThat(isCaptionEnabled).isTrue();
}
}

View File

@@ -0,0 +1,115 @@
/*
* 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.when;
import android.content.Context;
import android.provider.Settings;
import android.util.AttributeSet;
import android.view.accessibility.CaptioningManager;
import android.view.accessibility.CaptioningManager.CaptionStyle;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowCaptioningManager;
/** Tests for {@link CaptionEdgeTypeController}. */
@RunWith(RobolectricTestRunner.class)
public class CaptionEdgeTypeControllerTest {
@Rule
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock
private PreferenceScreen mScreen;
private final Context mContext = ApplicationProvider.getApplicationContext();
private CaptionEdgeTypeController mController;
private EdgeTypePreference mPreference;
private ShadowCaptioningManager mShadowCaptioningManager;
@Before
public void setUp() {
mController = new CaptionEdgeTypeController(mContext, "captioning_edge_type");
final AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
mPreference = new EdgeTypePreference(mContext, attributeSet);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
mShadowCaptioningManager = Shadow.extract(captioningManager);
}
@Test
public void getAvailabilityStatus_shouldReturnAvailable() {
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
}
@Test
public void getSummary_defaultValue_shouldReturnNone() {
mController.displayPreference(mScreen);
assertThat(mPreference.getSummary().toString()).isEqualTo("None");
}
@Test
public void getSummary_outlineValue_shouldReturnOutline() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_TYPE, CaptionStyle.EDGE_TYPE_OUTLINE);
mController.displayPreference(mScreen);
assertThat(mPreference.getSummary().toString()).isEqualTo("Outline");
}
@Test
public void setOutlineValue_shouldReturnOutline() {
mController.displayPreference(mScreen);
mPreference.setValue(CaptionStyle.EDGE_TYPE_OUTLINE);
assertThat(mPreference.getSummary().toString()).isEqualTo("Outline");
}
@Test
public void onValueChanged_shouldSetCaptionEnabled() {
mShadowCaptioningManager.setEnabled(false);
mController.displayPreference(mScreen);
mController.onValueChanged(mPreference, CaptionStyle.EDGE_TYPE_OUTLINE);
final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
assertThat(isCaptionEnabled).isTrue();
}
}

View File

@@ -0,0 +1,114 @@
/*
* 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.when;
import android.content.Context;
import android.provider.Settings;
import android.util.AttributeSet;
import android.view.accessibility.CaptioningManager;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowCaptioningManager;
/** Tests for {@link CaptionForegroundColorController}. */
@RunWith(RobolectricTestRunner.class)
public class CaptionForegroundColorControllerTest {
@Rule
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock
private PreferenceScreen mScreen;
private final Context mContext = ApplicationProvider.getApplicationContext();
private CaptionForegroundColorController mController;
private ColorPreference mPreference;
private ShadowCaptioningManager mShadowCaptioningManager;
@Before
public void setUp() {
mController = new CaptionForegroundColorController(mContext, "captioning_foreground_color");
final AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
mPreference = new ColorPreference(mContext, attributeSet);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
mShadowCaptioningManager = Shadow.extract(captioningManager);
}
@Test
public void getAvailabilityStatus_shouldReturnAvailable() {
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
}
@Test
public void getSummary_defaultValue_shouldReturnWhite() {
mController.displayPreference(mScreen);
assertThat(mPreference.getSummary().toString()).isEqualTo("White");
}
@Test
public void getSummary_redValue_shouldReturnRed() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_FOREGROUND_COLOR, 0xFFFF0000);
mController.displayPreference(mScreen);
assertThat(mPreference.getSummary().toString()).isEqualTo("Red");
}
@Test
public void setRedValue_shouldReturnRed() {
mController.displayPreference(mScreen);
mPreference.setValue(0xFFFF0000);
assertThat(mPreference.getSummary().toString()).isEqualTo("Red");
}
@Test
public void onValueChanged_shouldSetCaptionEnabled() {
mShadowCaptioningManager.setEnabled(false);
mController.displayPreference(mScreen);
mController.onValueChanged(mPreference, 0xFFFF0000);
final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
assertThat(isCaptionEnabled).isTrue();
}
}

View File

@@ -0,0 +1,115 @@
/*
* 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.when;
import android.content.Context;
import android.provider.Settings;
import android.util.AttributeSet;
import android.view.accessibility.CaptioningManager;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowCaptioningManager;
/** Tests for {@link CaptionForegroundOpacityController}. */
@RunWith(RobolectricTestRunner.class)
public class CaptionForegroundOpacityControllerTest {
@Rule
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock
private PreferenceScreen mScreen;
private final Context mContext = ApplicationProvider.getApplicationContext();
private CaptionForegroundOpacityController mController;
private ColorPreference mPreference;
private ShadowCaptioningManager mShadowCaptioningManager;
@Before
public void setUp() {
mController =
new CaptionForegroundOpacityController(mContext, "captioning_foreground_opacity");
final AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
mPreference = new ColorPreference(mContext, attributeSet);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
mShadowCaptioningManager = Shadow.extract(captioningManager);
}
@Test
public void getAvailabilityStatus_shouldReturnAvailable() {
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
}
@Test
public void getSummary_defaultValue_shouldReturnNonTransparent() {
mController.displayPreference(mScreen);
assertThat(mPreference.getSummary().toString()).isEqualTo("100%");
}
@Test
public void getSummary_halfTransparentValue_shouldReturnHalfTransparent() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_FOREGROUND_COLOR, 0x80FFFFFF);
mController.displayPreference(mScreen);
assertThat(mPreference.getSummary().toString()).isEqualTo("50%");
}
@Test
public void setHalfTransparentValue_shouldReturnHalfTransparent() {
mController.displayPreference(mScreen);
mPreference.setValue(0x80FFFFFF);
assertThat(mPreference.getSummary().toString()).isEqualTo("50%");
}
@Test
public void onValueChanged_shouldSetCaptionEnabled() {
mShadowCaptioningManager.setEnabled(false);
mController.displayPreference(mScreen);
mController.onValueChanged(mPreference, 0x80FFFFFF);
final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
assertThat(isCaptionEnabled).isTrue();
}
}

View File

@@ -24,10 +24,12 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import android.view.View;
import android.view.accessibility.CaptioningManager;
import android.view.accessibility.CaptioningManager.CaptionStyle;
import androidx.test.core.app.ApplicationProvider;
@@ -60,12 +62,14 @@ public class CaptionHelperTest {
private View mPreviewWindow;
@Spy
private final Context mContext = ApplicationProvider.getApplicationContext();
private ContentResolver mContentResolver;
private CaptionHelper mCaptionHelper;
@Before
public void setUp() {
when(mContext.getSystemService(CaptioningManager.class)).thenReturn(mCaptioningManager);
mCaptionHelper = new CaptionHelper(mContext);
mContentResolver = mContext.getContentResolver();
}
@Test
@@ -129,4 +133,53 @@ public class CaptionHelperTest {
Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
assertThat(isCaptionEnabled).isFalse();
}
@Test
public void setBackgroundColor_shouldReturnSpecificColor() {
mCaptionHelper.setBackgroundColor(0xFFFF0000);
final int backgroundColor = mCaptionHelper.getBackgroundColor();
assertThat(backgroundColor).isEqualTo(0xFFFF0000);
}
@Test
public void setForegroundColor_shouldReturnSpecificColor() {
mCaptionHelper.setForegroundColor(0xFFFF0000);
final int foregroundColor = mCaptionHelper.getForegroundColor();
assertThat(foregroundColor).isEqualTo(0xFFFF0000);
}
@Test
public void setWindowColor_shouldReturnSpecificColor() {
mCaptionHelper.setWindowColor(0xFFFF0000);
final int windowColor = mCaptionHelper.getWindowColor();
assertThat(windowColor).isEqualTo(0xFFFF0000);
}
@Test
public void setEdgeColor_shouldReturnSpecificColor() {
mCaptionHelper.setEdgeColor(0xFFFF0000);
final int edgeColor = mCaptionHelper.getEdgeColor();
assertThat(edgeColor).isEqualTo(0xFFFF0000);
}
@Test
public void setEdgeType_shouldReturnSpecificType() {
mCaptionHelper.setEdgeType(CaptionStyle.EDGE_TYPE_OUTLINE);
final int edgeType = mCaptionHelper.getEdgeType();
assertThat(edgeType).isEqualTo(CaptionStyle.EDGE_TYPE_OUTLINE);
}
@Test
public void setRawUserStyle_shouldReturnSpecificStyle() {
mCaptionHelper.setRawUserStyle(CaptionStyle.PRESET_CUSTOM);
final int style = Settings.Secure.getInt(mContentResolver,
Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, 0);
assertThat(style).isEqualTo(CaptionStyle.PRESET_CUSTOM);
}
}

View File

@@ -0,0 +1,115 @@
/*
* 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.when;
import android.content.Context;
import android.provider.Settings;
import android.util.AttributeSet;
import android.view.accessibility.CaptioningManager;
import android.view.accessibility.CaptioningManager.CaptionStyle;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowCaptioningManager;
/** Tests for {@link CaptionPresetController}. */
@RunWith(RobolectricTestRunner.class)
public class CaptionPresetControllerTest {
@Rule
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock
private PreferenceScreen mScreen;
private final Context mContext = ApplicationProvider.getApplicationContext();
private CaptionPresetController mController;
private PresetPreference mPreference;
private ShadowCaptioningManager mShadowCaptioningManager;
@Before
public void setUp() {
mController = new CaptionPresetController(mContext, "captioning_preset");
final AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
mPreference = new PresetPreference(mContext, attributeSet);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
mShadowCaptioningManager = Shadow.extract(captioningManager);
}
@Test
public void getAvailabilityStatus_shouldReturnAvailable() {
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
}
@Test
public void getSummary_defaultValue_shouldReturnWhiteOnBlack() {
mController.displayPreference(mScreen);
assertThat(mPreference.getSummary().toString()).isEqualTo("White on black");
}
@Test
public void getSummary_customValue_shouldReturnCustom() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, CaptionStyle.PRESET_CUSTOM);
mController.displayPreference(mScreen);
assertThat(mPreference.getSummary().toString()).isEqualTo("Custom");
}
@Test
public void setCustomValue_shouldReturnCustom() {
mController.displayPreference(mScreen);
mPreference.setValue(CaptionStyle.PRESET_CUSTOM);
assertThat(mPreference.getSummary().toString()).isEqualTo("Custom");
}
@Test
public void onValueChanged_shouldSetCaptionEnabled() {
mShadowCaptioningManager.setEnabled(false);
mController.displayPreference(mScreen);
mController.onValueChanged(mPreference, CaptionStyle.PRESET_CUSTOM);
final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
assertThat(isCaptionEnabled).isTrue();
}
}

View File

@@ -0,0 +1,80 @@
/*
* 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.google.common.truth.Truth.assertThat;
import android.graphics.Color;
import android.view.accessibility.CaptioningManager;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
/** Tests for {@link CaptionUtils}. */
@RunWith(RobolectricTestRunner.class)
public class CaptionUtilsTest {
@Test
public void parseColor_defaultPackedColor_shouldReturnUnspecified() {
final int color = CaptionUtils.parseColor(0xFFFF00);
assertThat(color).isEqualTo(CaptioningManager.CaptionStyle.COLOR_UNSPECIFIED);
}
@Test
public void parseColor_unrecognizedColor_shouldReturnTransparent() {
final int color = CaptionUtils.parseColor(0x00);
assertThat(color).isEqualTo(Color.TRANSPARENT);
}
@Test
public void parseColor_redColor_shouldReturnRed() {
final int color = CaptionUtils.parseColor(0xFFFF0000);
assertThat(color).isEqualTo(Color.RED);
}
@Test
public void parseOpacity_defaultPackedColor_shouldReturnUnspecified() {
final int color = CaptionUtils.parseOpacity(0xFFFF00);
assertThat(color).isEqualTo(CaptioningManager.CaptionStyle.COLOR_UNSPECIFIED);
}
@Test
public void parseOpacity_unrecognizedColor_shouldReturnTransparent() {
final int color = CaptionUtils.parseOpacity(0x00);
assertThat(color).isEqualTo(0xFFFFFF);
}
@Test
public void parseOpacity_halfTransparentValue_shouldReturnHalfTransparent() {
final int color = CaptionUtils.parseOpacity(0x80FFFFFF);
assertThat(color).isEqualTo(0x80FFFFFF);
}
@Test
public void mergeColorOpacity_halfTransparentRedValue_shouldReturnMergeColorOpacityValue() {
final int color = CaptionUtils.mergeColorOpacity(0xFFFF0000, 0x80FFFFFF);
assertThat(color).isEqualTo(0x80FF0000);
}
}

View File

@@ -0,0 +1,116 @@
/*
* 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.when;
import android.content.Context;
import android.provider.Settings;
import android.util.AttributeSet;
import android.view.accessibility.CaptioningManager;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowCaptioningManager;
/** Tests for {@link CaptionWindowColorController}. */
@RunWith(RobolectricTestRunner.class)
public class CaptionWindowColorControllerTest {
@Rule
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock
private PreferenceScreen mScreen;
private final Context mContext = ApplicationProvider.getApplicationContext();
private CaptionWindowColorController mController;
private ColorPreference mPreference;
private ShadowCaptioningManager mShadowCaptioningManager;
@Before
public void setUp() {
mController = new CaptionWindowColorController(mContext, "captioning_window_color");
final AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
mPreference = new ColorPreference(mContext, attributeSet);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
mShadowCaptioningManager = Shadow.extract(captioningManager);
}
@Test
public void getAvailabilityStatus_shouldReturnAvailable() {
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
}
@Test
public void getSummary_defaultValue_shouldReturnNone() {
mController.displayPreference(mScreen);
assertThat(mPreference.getSummary().toString()).isEqualTo(
mContext.getString(R.string.color_none));
}
@Test
public void getSummary_redValue_shouldReturnRed() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_WINDOW_COLOR, 0xFFFF0000);
mController.displayPreference(mScreen);
assertThat(mPreference.getSummary().toString()).isEqualTo("Red");
}
@Test
public void setRedValue_shouldReturnRed() {
mController.displayPreference(mScreen);
mPreference.setValue(0xFFFF0000);
assertThat(mPreference.getSummary().toString()).isEqualTo("Red");
}
@Test
public void onValueChanged_shouldSetCaptionEnabled() {
mShadowCaptioningManager.setEnabled(false);
mController.displayPreference(mScreen);
mController.onValueChanged(mPreference, 0xFFFF0000);
final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
assertThat(isCaptionEnabled).isTrue();
}
}

View File

@@ -0,0 +1,114 @@
/*
* 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.when;
import android.content.Context;
import android.provider.Settings;
import android.util.AttributeSet;
import android.view.accessibility.CaptioningManager;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowCaptioningManager;
/** Tests for {@link CaptionWindowOpacityController}. */
@RunWith(RobolectricTestRunner.class)
public class CaptionWindowOpacityControllerTest {
@Rule
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock
private PreferenceScreen mScreen;
private final Context mContext = ApplicationProvider.getApplicationContext();
private CaptionWindowOpacityController mController;
private ColorPreference mPreference;
private ShadowCaptioningManager mShadowCaptioningManager;
@Before
public void setUp() {
mController = new CaptionWindowOpacityController(mContext, "captioning_window_opacity");
final AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
mPreference = new ColorPreference(mContext, attributeSet);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
mShadowCaptioningManager = Shadow.extract(captioningManager);
}
@Test
public void getAvailabilityStatus_shouldReturnAvailable() {
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
}
@Test
public void getSummary_defaultValue_shouldReturnNonTransparent() {
mController.displayPreference(mScreen);
assertThat(mPreference.getSummary().toString()).isEqualTo("100%");
}
@Test
public void getSummary_halfTransparentValue_shouldReturnHalfTransparent() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_WINDOW_COLOR, 0x80FFFFFF);
mController.displayPreference(mScreen);
assertThat(mPreference.getSummary().toString()).isEqualTo("50%");
}
@Test
public void setHalfTransparentValue_shouldReturnHalfTransparent() {
mController.displayPreference(mScreen);
mPreference.setValue(0x80FFFFFF);
assertThat(mPreference.getSummary().toString()).isEqualTo("50%");
}
@Test
public void onValueChanged_shouldSetCaptionEnabled() {
mShadowCaptioningManager.setEnabled(false);
mController.displayPreference(mScreen);
mController.onValueChanged(mPreference, 0x80FFFFFF);
final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
assertThat(isCaptionEnabled).isTrue();
}
}