Merge "Refactor time format preference to be more modular/testable"

This commit is contained in:
TreeHugger Robot
2016-11-17 23:14:18 +00:00
committed by Android (Google) Code Review
5 changed files with 275 additions and 77 deletions

View File

@@ -0,0 +1,97 @@
/*
* 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.datetime;
import android.content.Context;
import android.provider.Settings;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
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;
import org.robolectric.shadows.ShadowApplication;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class TimeFormatPreferenceControllerTest {
@Mock(answer = RETURNS_DEEP_STUBS)
private PreferenceScreen mScreen;
@Mock
private UpdateTimeAndDateCallback mCallback;
private Context mContext;
private SwitchPreference mPreference;
private TimeFormatPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = ShadowApplication.getInstance().getApplicationContext();
}
@Test
public void isCalledFromSUW_NotAvailable() {
mController = new TimeFormatPreferenceController(mContext, mCallback, true);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void notCalledFromSUW_shouldBeAvailable() {
mController = new TimeFormatPreferenceController(mContext, mCallback, false);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void updateState_24HourSet_shouldCheckPreference() {
mController = new TimeFormatPreferenceController(mContext, mCallback, false);
mPreference = new SwitchPreference(mContext);
mPreference.setKey(mController.getPreferenceKey());
Settings.System.putString(mContext.getContentResolver(), Settings.System.TIME_12_24,
TimeFormatPreferenceController.HOURS_24);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
}
@Test
public void updateState_12HourSet_shouldNotCheckPreference() {
mController = new TimeFormatPreferenceController(mContext, mCallback, false);
mPreference = new SwitchPreference(mContext);
mPreference.setKey(mController.getPreferenceKey());
Settings.System.putString(mContext.getContentResolver(), Settings.System.TIME_12_24,
TimeFormatPreferenceController.HOURS_12);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isFalse();
}
}

View File

@@ -26,6 +26,7 @@ import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.DevelopmentSettings;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import org.junit.Before;
@@ -34,7 +35,6 @@ import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
@@ -46,7 +46,7 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(RobolectricTestRunner.class)
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class BuildNumberPreferenceControllerTest {