| /* |
| * Copyright (C) 2018 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.car.settings.datetime; |
| |
| import static com.google.common.truth.Truth.assertThat; |
| |
| import android.content.Context; |
| import android.content.Intent; |
| import android.provider.Settings; |
| |
| import androidx.lifecycle.Lifecycle; |
| import androidx.preference.SwitchPreference; |
| import androidx.preference.TwoStatePreference; |
| |
| import com.android.car.settings.CarSettingsRobolectricTestRunner; |
| import com.android.car.settings.common.PreferenceControllerTestHelper; |
| |
| import org.junit.Before; |
| import org.junit.Test; |
| import org.junit.runner.RunWith; |
| import org.robolectric.RuntimeEnvironment; |
| import org.robolectric.shadows.ShadowApplication; |
| |
| import java.util.List; |
| |
| @RunWith(CarSettingsRobolectricTestRunner.class) |
| public class TimeFormatTogglePreferenceControllerTest { |
| |
| private Context mContext; |
| private TwoStatePreference mPreference; |
| private PreferenceControllerTestHelper<TimeFormatTogglePreferenceController> |
| mPreferenceControllerHelper; |
| private TimeFormatTogglePreferenceController mController; |
| |
| @Before |
| public void setUp() { |
| mContext = RuntimeEnvironment.application; |
| mPreference = new SwitchPreference(mContext); |
| mPreferenceControllerHelper = new PreferenceControllerTestHelper<>(mContext, |
| TimeFormatTogglePreferenceController.class, mPreference); |
| mController = mPreferenceControllerHelper.getController(); |
| mPreferenceControllerHelper.markState(Lifecycle.State.CREATED); |
| } |
| |
| @Test |
| public void testRefreshUi_24HourSet_shouldCheckPreference() { |
| Settings.System.putString(mContext.getContentResolver(), Settings.System.TIME_12_24, |
| TimeFormatTogglePreferenceController.HOURS_24); |
| mController.refreshUi(); |
| assertThat(mPreference.isChecked()).isTrue(); |
| } |
| |
| @Test |
| public void testRefreshUi_12HourSet_shouldUncheckPreference() { |
| Settings.System.putString(mContext.getContentResolver(), Settings.System.TIME_12_24, |
| TimeFormatTogglePreferenceController.HOURS_12); |
| mController.refreshUi(); |
| assertThat(mPreference.isChecked()).isFalse(); |
| } |
| |
| @Test |
| public void testOnPreferenceChange_24HourSet_shouldSendIntent() { |
| mPreference.setChecked(true); |
| mPreference.callChangeListener(true); |
| |
| List<Intent> intentsFired = ShadowApplication.getInstance().getBroadcastIntents(); |
| assertThat(intentsFired.size()).isEqualTo(1); |
| Intent intentFired = intentsFired.get(0); |
| assertThat(intentFired.getAction()).isEqualTo(Intent.ACTION_TIME_CHANGED); |
| assertThat(intentFired.getIntExtra(Intent.EXTRA_TIME_PREF_24_HOUR_FORMAT, -1)) |
| .isEqualTo(Intent.EXTRA_TIME_PREF_VALUE_USE_24_HOUR); |
| } |
| |
| @Test |
| public void testOnPreferenceChange_12HourSet_shouldSendIntent() { |
| mPreference.setChecked(false); |
| mPreference.callChangeListener(false); |
| |
| List<Intent> intentsFired = ShadowApplication.getInstance().getBroadcastIntents(); |
| assertThat(intentsFired.size()).isEqualTo(1); |
| Intent intentFired = intentsFired.get(0); |
| assertThat(intentFired.getAction()).isEqualTo(Intent.ACTION_TIME_CHANGED); |
| assertThat(intentFired.getIntExtra(Intent.EXTRA_TIME_PREF_24_HOUR_FORMAT, -1)) |
| .isEqualTo(Intent.EXTRA_TIME_PREF_VALUE_USE_12_HOUR); |
| } |
| } |