Updated time picker in Settings to check if settings is using 24-hour
format.
If using 24 hour format, then the time picker should be updated so that
it's using 24 hour format, and vice versa.
Bug: 142438399
Test: Manual, robolectic
Merged-In: Ia223d14e8c346d75d1ce9b241f087c3755e72748
Change-Id: I1978862dd76def23bd61c2b7e40f6f75c30ee0e3
diff --git a/src/com/android/car/settings/datetime/TimePickerFragment.java b/src/com/android/car/settings/datetime/TimePickerFragment.java
index 2e0cdcd..897834d 100644
--- a/src/com/android/car/settings/datetime/TimePickerFragment.java
+++ b/src/com/android/car/settings/datetime/TimePickerFragment.java
@@ -20,6 +20,7 @@
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
+import android.text.format.DateFormat;
import android.widget.Button;
import android.widget.TimePicker;
@@ -62,6 +63,7 @@
super.onActivityCreated(savedInstanceState);
mTimePicker = (TimePicker) getView().findViewById(R.id.time_picker);
+ mTimePicker.setIs24HourView(is24Hour());
Button button = (Button) getActivity().findViewById(R.id.action_button1);
button.setText(android.R.string.ok);
@@ -80,4 +82,8 @@
getFragmentController().goBack();
});
}
+
+ private boolean is24Hour() {
+ return DateFormat.is24HourFormat(getContext());
+ }
}
diff --git a/tests/robotests/src/com/android/car/settings/datetime/TimePickerFragmentTest.java b/tests/robotests/src/com/android/car/settings/datetime/TimePickerFragmentTest.java
new file mode 100644
index 0000000..9e8d69f
--- /dev/null
+++ b/tests/robotests/src/com/android/car/settings/datetime/TimePickerFragmentTest.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2019 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.app.Activity;
+import android.widget.TimePicker;
+
+import com.android.car.settings.CarSettingsRobolectricTestRunner;
+import com.android.car.settings.R;
+import com.android.car.settings.testutils.FragmentController;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.shadows.ShadowSettings;
+
+/** Unit test for {@link TimePickerFragment}. */
+@RunWith(CarSettingsRobolectricTestRunner.class)
+public class TimePickerFragmentTest {
+
+ private TimePickerFragment mFragment;
+ private FragmentController<TimePickerFragment> mFragmentController;
+
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mFragment = new TimePickerFragment();
+ mFragmentController = FragmentController.of(mFragment);
+ }
+
+ @Test
+ public void onActivityCreated_isNot24HourFormat_timePickerShouldShow12HourTimeFormat() {
+ ShadowSettings.set24HourTimeFormat(false);
+ mFragmentController.create().start();
+ TimePicker timePicker = findTimePicker(mFragment.requireActivity());
+
+ assertThat(timePicker.is24HourView()).isFalse();
+ }
+
+ @Test
+ public void onActivityCreated_is24HourFormat_timePickerShouldShow24HourTimeFormat() {
+ ShadowSettings.set24HourTimeFormat(true);
+ mFragmentController.create().start();
+ TimePicker timePicker = findTimePicker(mFragment.requireActivity());
+
+ assertThat(timePicker.is24HourView()).isTrue();
+ }
+
+ private TimePicker findTimePicker(Activity activity) {
+ return activity.findViewById(R.id.time_picker);
+ }
+}