OSDN Git Service

Use 24-hour settings for time formatting
authorVictor Chang <vichang@google.com>
Thu, 1 Feb 2018 17:54:16 +0000 (17:54 +0000)
committerVictor Chang <vichang@google.com>
Fri, 9 Feb 2018 18:35:14 +0000 (18:35 +0000)
Test: m RunSettingsRoboTests
Bug: 72311296
Change-Id: I7c1458dbc2e4e15765f703c35a0113341b912a2b

src/com/android/settings/datetime/timezone/TimeZoneAdapter.java
tests/robotests/src/com/android/settings/datetime/timezone/TimeZoneAdapterTest.java

index 79075ca..62fc8c9 100644 (file)
@@ -35,6 +35,7 @@ import com.android.settings.R;
 import java.util.Collections;
 import java.util.Date;
 import java.util.List;
+import java.util.Locale;
 
 /**
  * Adapter for showing {@link TimeZoneInfo} objects in a recycler view.
@@ -55,7 +56,11 @@ class TimeZoneAdapter extends RecyclerView.Adapter {
     TimeZoneAdapter(View.OnClickListener onClickListener, Context context) {
         mOnClickListener = onClickListener;
         mContext = context;
-        mTimeFormat = DateFormat.getTimeInstance(SimpleDateFormat.SHORT);
+        // Use android.text.format.DateFormat to observe 24-hour settings and find the best pattern
+        // using ICU with skeleton.
+        mTimeFormat = new SimpleDateFormat(
+                android.text.format.DateFormat.getTimeFormatString(context),
+                Locale.getDefault());
         mDateFormat = DateFormat.getDateInstance(SimpleDateFormat.MEDIUM);
         mDateFormat.setContext(DisplayContext.CAPITALIZATION_NONE);
         mCurrentTimeZone = TimeZone.getDefault().getID();
index 5f29a0b..1377f1d 100644 (file)
  */
 package com.android.settings.datetime.timezone;
 
+import android.content.Context;
 import android.icu.util.TimeZone;
+import android.provider.Settings;
 import android.text.TextUtils;
 import android.view.View;
 import android.widget.FrameLayout;
+
 import com.android.settings.TestConfig;
 import com.android.settings.testutils.SettingsRobolectricTestRunner;
 import com.android.settings.testutils.shadow.SettingsShadowResources;
+
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -29,8 +34,11 @@ import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 import org.robolectric.RuntimeEnvironment;
 import org.robolectric.annotation.Config;
+import org.robolectric.annotation.Implementation;
+import org.robolectric.annotation.Implements;
 
 import java.util.Collections;
+import java.util.Locale;
 
 import static com.google.common.truth.Truth.assertThat;
 
@@ -38,17 +46,39 @@ import static com.google.common.truth.Truth.assertThat;
 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
         shadows = {
                 SettingsShadowResources.class,
-                SettingsShadowResources.SettingsShadowTheme.class})
+                SettingsShadowResources.SettingsShadowTheme.class,
+                TimeZoneAdapterTest.ShadowDataFormat.class})
 public class TimeZoneAdapterTest {
     @Mock
     private View.OnClickListener mOnClickListener;
 
     private TimeZoneAdapter mTimeZoneAdapter;
 
+    private Context mContext;
+    private Locale mDefaultLocale;
+
     @Before
-    public void setUp() {
+    public void setUp() throws Exception {
         MockitoAnnotations.initMocks(this);
-        mTimeZoneAdapter = new TimeZoneAdapter(mOnClickListener, RuntimeEnvironment.application);
+        mContext = RuntimeEnvironment.application;
+        mTimeZoneAdapter = new TimeZoneAdapter(mOnClickListener, mContext);
+        mDefaultLocale = Locale.getDefault();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        Locale.setDefault(mDefaultLocale);
+    }
+
+    @Implements(android.text.format.DateFormat.class)
+    public static class ShadowDataFormat {
+
+        public static String mTimeFormatString = "";
+
+        @Implementation
+        public static String getTimeFormatString(Context context) {
+            return mTimeFormatString;
+        }
     }
 
     @Test
@@ -89,6 +119,38 @@ public class TimeZoneAdapterTest {
         assertThat(viewHolder.mDstView.getVisibility()).isEqualTo(View.GONE);
     }
 
+    @Test
+    public void bindViewHolder_on24Hour() {
+        Locale.setDefault(Locale.US);
+        ShadowDataFormat.mTimeFormatString = "HH:mm";
+        mTimeZoneAdapter = new TimeZoneAdapter(mOnClickListener, mContext);
+
+        final TimeZoneInfo tzi = dummyTimeZoneInfo(TimeZone.getTimeZone("Etc/UTC"));
+        mTimeZoneAdapter.setTimeZoneInfos(Collections.singletonList(tzi));
+
+        final FrameLayout parent = new FrameLayout(RuntimeEnvironment.application);
+
+        final ViewHolder viewHolder = (ViewHolder) mTimeZoneAdapter.createViewHolder(parent, TimeZoneAdapter.VIEW_TYPE_NORMAL);
+        mTimeZoneAdapter.bindViewHolder(viewHolder, 0);
+        assertThat(viewHolder.mTimeView.getText().toString()).hasLength(5);
+    }
+
+    @Test
+    public void bindViewHolder_on12Hour() {
+        Locale.setDefault(Locale.US);
+        ShadowDataFormat.mTimeFormatString = "hh:mm a";
+        mTimeZoneAdapter = new TimeZoneAdapter(mOnClickListener, mContext);
+
+        final TimeZoneInfo tzi = dummyTimeZoneInfo(TimeZone.getTimeZone("Etc/UTC"));
+        mTimeZoneAdapter.setTimeZoneInfos(Collections.singletonList(tzi));
+
+        final FrameLayout parent = new FrameLayout(RuntimeEnvironment.application);
+
+        final ViewHolder viewHolder = (ViewHolder) mTimeZoneAdapter.createViewHolder(parent, TimeZoneAdapter.VIEW_TYPE_NORMAL);
+        mTimeZoneAdapter.bindViewHolder(viewHolder, 0);
+        assertThat(viewHolder.mTimeView.getText().toString()).hasLength(8);
+    }
+
     // Pick an arbitrary time zone that's not the current default.
     private static TimeZone getNonDefaultTimeZone() {
         final String[] availableIDs = TimeZone.getAvailableIDs();