1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.datetime; 18 19 20 import android.content.Context; 21 import android.content.Intent; 22 import android.provider.Settings; 23 import android.support.v14.preference.SwitchPreference; 24 import android.support.v7.preference.PreferenceScreen; 25 26 import com.android.settings.SettingsRobolectricTestRunner; 27 import com.android.settings.TestConfig; 28 29 import java.util.List; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.Mock; 35 import org.mockito.MockitoAnnotations; 36 import org.robolectric.annotation.Config; 37 import org.robolectric.shadows.ShadowApplication; 38 39 import static com.google.common.truth.Truth.assertThat; 40 import static org.mockito.Answers.RETURNS_DEEP_STUBS; 41 import static org.mockito.Mockito.verify; 42 43 @RunWith(SettingsRobolectricTestRunner.class) 44 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 45 public class TimeFormatPreferenceControllerTest { 46 47 @Mock(answer = RETURNS_DEEP_STUBS) 48 private PreferenceScreen mScreen; 49 @Mock 50 private UpdateTimeAndDateCallback mCallback; 51 52 private ShadowApplication mApplication; 53 private Context mContext; 54 private SwitchPreference mPreference; 55 private TimeFormatPreferenceController mController; 56 57 @Before 58 public void setUp() { 59 MockitoAnnotations.initMocks(this); 60 mApplication = ShadowApplication.getInstance(); 61 mContext = mApplication.getApplicationContext(); 62 } 63 64 @Test 65 public void isCalledFromSUW_NotAvailable() { 66 mController = new TimeFormatPreferenceController(mContext, mCallback, true); 67 68 assertThat(mController.isAvailable()).isFalse(); 69 } 70 71 @Test 72 public void notCalledFromSUW_shouldBeAvailable() { 73 mController = new TimeFormatPreferenceController(mContext, mCallback, false); 74 75 assertThat(mController.isAvailable()).isTrue(); 76 } 77 78 @Test 79 public void updateState_24HourSet_shouldCheckPreference() { 80 mController = new TimeFormatPreferenceController(mContext, mCallback, false); 81 mPreference = new SwitchPreference(mContext); 82 mPreference.setKey(mController.getPreferenceKey()); 83 Settings.System.putString(mContext.getContentResolver(), Settings.System.TIME_12_24, 84 TimeFormatPreferenceController.HOURS_24); 85 86 mController.updateState(mPreference); 87 88 assertThat(mPreference.isChecked()).isTrue(); 89 } 90 91 @Test 92 public void updateState_12HourSet_shouldNotCheckPreference() { 93 mController = new TimeFormatPreferenceController(mContext, mCallback, false); 94 mPreference = new SwitchPreference(mContext); 95 mPreference.setKey(mController.getPreferenceKey()); 96 Settings.System.putString(mContext.getContentResolver(), Settings.System.TIME_12_24, 97 TimeFormatPreferenceController.HOURS_12); 98 99 mController.updateState(mPreference); 100 101 assertThat(mPreference.isChecked()).isFalse(); 102 } 103 104 @Test 105 public void updatePreference_12HourSet_shouldSendIntent() { 106 mController = new TimeFormatPreferenceController(mContext, mCallback, false); 107 mPreference = new SwitchPreference(mContext); 108 mPreference.setKey(mController.getPreferenceKey()); 109 mPreference.setChecked(false); 110 111 boolean result = mController.handlePreferenceTreeClick(mPreference); 112 113 assertThat(result).isTrue(); 114 115 List<Intent> intentsFired = mApplication.getBroadcastIntents(); 116 assertThat(intentsFired.size()).isEqualTo(1); 117 Intent intentFired = intentsFired.get(0); 118 assertThat(intentFired.getAction()).isEqualTo(Intent.ACTION_TIME_CHANGED); 119 assertThat(intentFired.getIntExtra(Intent.EXTRA_TIME_PREF_24_HOUR_FORMAT, -1)) 120 .isEqualTo(Intent.EXTRA_TIME_PREF_VALUE_USE_12_HOUR); 121 } 122 123 @Test 124 public void updatePreference_24HourSet_shouldSendIntent() { 125 mController = new TimeFormatPreferenceController(mContext, mCallback, false); 126 mPreference = new SwitchPreference(mContext); 127 mPreference.setKey(mController.getPreferenceKey()); 128 mPreference.setChecked(true); 129 130 boolean result = mController.handlePreferenceTreeClick(mPreference); 131 132 assertThat(result).isTrue(); 133 134 List<Intent> intentsFired = mApplication.getBroadcastIntents(); 135 assertThat(intentsFired.size()).isEqualTo(1); 136 Intent intentFired = intentsFired.get(0); 137 assertThat(intentFired.getAction()).isEqualTo(Intent.ACTION_TIME_CHANGED); 138 assertThat(intentFired.getIntExtra(Intent.EXTRA_TIME_PREF_24_HOUR_FORMAT, -1)) 139 .isEqualTo(Intent.EXTRA_TIME_PREF_VALUE_USE_24_HOUR); 140 } 141 } 142