Introduce PictureColorModePreferenceController

- Create new PictureColorModePreferenceController
 - Create controller inside the DashboardFragment
 - Refactor ColorModePreference so isAvailable() no longer depends on
 ColorModePreference
 - Port logic from DevelopmentSettings into the controller

Bug: 34203528
Test: make RunSettingsRoboTests -j40
Change-Id: I8dff5b0d5ad1d7f043fc7ead540b2c0c960933e3
This commit is contained in:
jeffreyhuang
2017-09-18 14:45:49 -07:00
parent 31b04f526f
commit 6162fa5ef8
5 changed files with 273 additions and 18 deletions

View File

@@ -112,6 +112,9 @@ public class DevelopmentSettingsDashboardFragmentTest {
}
@Test
@Config(shadows = {
ShadowPictureColorModePreferenceController.class
})
public void searchIndex_pageEnabled_shouldNotAddKeysToNonIndexable() {
final Context appContext = RuntimeEnvironment.application;
DevelopmentSettingsEnabler.setDevelopmentSettingsEnabled(appContext, true);
@@ -198,4 +201,13 @@ public class DevelopmentSettingsDashboardFragmentTest {
mShown = true;
}
}
@Implements(PictureColorModePreferenceController.class)
public static class ShadowPictureColorModePreferenceController {
@Implementation
public boolean isAvailable() {
return true;
}
}
}

View File

@@ -0,0 +1,137 @@
/*
* 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.development;
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.content.Context;
import android.content.res.Resources;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class PictureColorModePreferenceControllerTest {
@Mock
private ColorModePreference mPreference;
@Mock
private Context mContext;
@Mock
private PreferenceScreen mPreferenceScreen;
@Mock
private Resources mResources;
private Lifecycle mLifecycle;
private PictureColorModePreferenceController mController;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mLifecycle = new Lifecycle();
mController = new PictureColorModePreferenceController(mContext, mLifecycle);
when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
mPreference);
when(mContext.getResources()).thenReturn(mResources);
when(mResources.getIntArray(R.array.color_mode_ids)).thenReturn(new int[0]);
mController.displayPreference(mPreferenceScreen);
}
@Test
public void isAvailable_shouldReturnFalseWhenWideColorGambit() {
mController = spy(mController);
doReturn(2).when(mController).getColorModeDescriptionsSize();
doReturn(true).when(mController).isWideColorGamut();
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_shouldReturnTrueWhenNotWideColorGambit() {
mController = spy(mController);
doReturn(2).when(mController).getColorModeDescriptionsSize();
doReturn(false).when(mController).isWideColorGamut();
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void isAvailable_shouldReturnFalseWhenColorCountIsOne() {
mController = spy(mController);
doReturn(1).when(mController).getColorModeDescriptionsSize();
doReturn(true).when(mController).isWideColorGamut();
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_shouldReturnTrueWhenColorCountIsTwo() {
mController = spy(mController);
doReturn(2).when(mController).getColorModeDescriptionsSize();
doReturn(false).when(mController).isWideColorGamut();
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void onDeveloperOptionEnabled_shouldEnablePreference() {
mController = spy(mController);
doReturn(true).when(mController).isAvailable();
mController.onDeveloperOptionsEnabled();
verify(mPreference).setEnabled(true);
}
@Test
public void onDeveloperOptionDisabled_shouldDisablePreference() {
mController = spy(mController);
doReturn(true).when(mController).isAvailable();
mController.onDeveloperOptionsDisabled();
verify(mPreference).setEnabled(false);
}
@Test
public void onResume_shouldStartListening() {
mLifecycle.onResume();
verify(mPreference).startListening();
}
@Test
public void onPause_shouldStopListening() {
mLifecycle.onPause();
verify(mPreference).stopListening();
}
}