1 /* 2 * Copyright (C) 2018 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.timezone; 18 19 import static org.mockito.Mockito.mock; 20 21 import android.content.Context; 22 import android.icu.util.TimeZone; 23 24 import com.android.settings.datetime.timezone.model.TimeZoneData; 25 26 import com.google.common.truth.Truth; 27 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.robolectric.RobolectricTestRunner; 31 import org.robolectric.RuntimeEnvironment; 32 import org.robolectric.annotation.Config; 33 import org.robolectric.annotation.Implementation; 34 import org.robolectric.annotation.Implements; 35 36 import java.util.Arrays; 37 import java.util.List; 38 import java.util.Locale; 39 40 @RunWith(RobolectricTestRunner.class) 41 @Config(shadows = { BaseTimeZoneInfoPickerTest.ShadowDataFormat.class }) 42 public class BaseTimeZoneInfoPickerTest { 43 @Implements(android.text.format.DateFormat.class) 44 public static class ShadowDataFormat { 45 46 private static String sTimeFormatString = ""; 47 48 @Implementation getTimeFormatString(Context context)49 protected static String getTimeFormatString(Context context) { 50 return sTimeFormatString; 51 } 52 } 53 54 /** 55 * Verify the summary, title, and time label in a time zone item are formatted properly. 56 */ 57 @Test createAdapter_matchTimeZoneInfoAndOrder()58 public void createAdapter_matchTimeZoneInfoAndOrder() { 59 ShadowDataFormat.sTimeFormatString = "HH:MM"; 60 BaseTimeZoneInfoPicker picker = new TestBaseTimeZoneInfoPicker(); 61 BaseTimeZoneAdapter adapter = picker.createAdapter(mock(TimeZoneData.class)); 62 Truth.assertThat(adapter.getItemCount()).isEqualTo(2); 63 64 BaseTimeZoneAdapter.AdapterItem item1 = adapter.getDataItem(0); 65 Truth.assertThat(item1.getTitle().toString()).isEqualTo("Los Angeles"); 66 Truth.assertThat(item1.getSummary().toString()).isEqualTo("Pacific Time (GMT-08:00)"); 67 Truth.assertThat(item1.getCurrentTime()) 68 .hasLength(ShadowDataFormat.sTimeFormatString.length()); 69 70 BaseTimeZoneAdapter.AdapterItem item2 = adapter.getDataItem(1); 71 Truth.assertThat(item2.getTitle().toString()).isEqualTo("New York"); 72 Truth.assertThat(item2.getSummary().toString()).isEqualTo("Eastern Time (GMT-05:00)"); 73 Truth.assertThat(item2.getCurrentTime()) 74 .hasLength(ShadowDataFormat.sTimeFormatString.length()); 75 } 76 77 public static class TestBaseTimeZoneInfoPicker extends BaseTimeZoneInfoPicker { 78 TestBaseTimeZoneInfoPicker()79 private TestBaseTimeZoneInfoPicker() { 80 super(0, 0, false, false); 81 } 82 83 @Override getAllTimeZoneInfos(TimeZoneData timeZoneData)84 public List<TimeZoneInfo> getAllTimeZoneInfos(TimeZoneData timeZoneData) { 85 TimeZoneInfo zone1 = new TimeZoneInfo.Builder( 86 TimeZone.getFrozenTimeZone("America/Los_Angeles")) 87 .setGenericName("Pacific Time") 88 .setStandardName("Pacific Standard Time") 89 .setDaylightName("Pacific Daylight Time") 90 .setExemplarLocation("Los Angeles") 91 .setGmtOffset("GMT-08:00") 92 .build(); 93 TimeZoneInfo zone2 = new TimeZoneInfo.Builder( 94 TimeZone.getFrozenTimeZone("America/New_York")) 95 .setGenericName("Eastern Time") 96 .setStandardName("Eastern Standard Time") 97 .setDaylightName("Eastern Daylight Time") 98 .setExemplarLocation("New York") 99 .setGmtOffset("GMT-05:00") 100 .build(); 101 102 return Arrays.asList(zone1, zone2); 103 } 104 105 // Make the method public 106 @Override createAdapter(TimeZoneData timeZoneData)107 public BaseTimeZoneAdapter createAdapter(TimeZoneData timeZoneData) { 108 return super.createAdapter(timeZoneData); 109 } 110 111 @Override getLocale()112 protected Locale getLocale() { 113 return Locale.US; 114 } 115 116 @Override getContext()117 public Context getContext() { 118 return RuntimeEnvironment.application; 119 } 120 121 @Override getMetricsCategory()122 public int getMetricsCategory() { 123 // the metric id doesn't matter in test 124 return 1; 125 } 126 } 127 } 128