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 com.google.common.truth.Truth.assertThat; 20 21 import android.icu.text.TimeZoneFormat; 22 import android.text.Spannable; 23 import android.text.SpannableStringBuilder; 24 25 import com.android.settings.R; 26 import com.android.settingslib.datetime.ZoneGetter; 27 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.robolectric.RobolectricTestRunner; 31 import org.robolectric.RuntimeEnvironment; 32 33 import java.util.Date; 34 import java.util.Locale; 35 import java.util.TimeZone; 36 37 @RunWith(RobolectricTestRunner.class) 38 public class SpannableUtilTest { 39 40 @Test testGetResourceText()41 public void testGetResourceText() { 42 CharSequence gmtString = getGmtString("GMT+00:00"); 43 44 Spannable spannable = SpannableUtil.getResourcesText( 45 RuntimeEnvironment.application.getResources(), R.string.zone_info_offset_and_name, 46 gmtString, "UTC"); 47 assertThat(spannable.toString()).isEqualTo("UTC (GMT+00:00)"); 48 49 // Verify that the spans are kept. 50 Object[] spans = ((Spannable) gmtString).getSpans(0, gmtString.length(), Object.class); 51 Object[] newSpans = spannable.getSpans(0, spannable.length(), Object.class); 52 assertThat(newSpans.length).isEqualTo(spans.length); 53 assertThat(newSpans).isEqualTo(spans); 54 } 55 getGmtString(String tzId)56 private static CharSequence getGmtString(String tzId) { 57 Locale locale = Locale.US; 58 TimeZoneFormat timeZoneFormat = TimeZoneFormat.getInstance(locale); 59 TimeZone gmtZone = TimeZone.getTimeZone(tzId); 60 Date date = new Date(0); 61 return ZoneGetter.getGmtOffsetText(timeZoneFormat, locale, gmtZone, date); 62 } 63 /** 64 * Verify the assumption on the GMT string used in {@link #testGetResourceText()} 65 */ 66 @Test testGetGmtString()67 public void testGetGmtString() { 68 // Create a GMT string and verify the assumptions 69 CharSequence gmtString = getGmtString("GMT+00:00"); 70 assertThat(gmtString.toString()).isEqualTo("GMT+00:00"); 71 assertThat(gmtString).isInstanceOf(Spannable.class); 72 Object[] spans = ((Spannable) gmtString).getSpans(0, gmtString.length(), Object.class); 73 assertThat(spans).isNotEmpty(); 74 75 assertThat(getGmtString("GMT-08:00").toString()).isEqualTo("GMT-08:00"); 76 } 77 78 @Test testTitleCaseSentences_enUS()79 public void testTitleCaseSentences_enUS() { 80 Locale locale = Locale.US; 81 CharSequence titleCasedFirstSentence = SpannableUtil.titleCaseSentences(locale, 82 "pacific Daylight Time starts on Mar 11 2018."); 83 assertThat(titleCasedFirstSentence.toString()) 84 .isEqualTo("Pacific Daylight Time starts on Mar 11 2018."); 85 86 SpannableStringBuilder sb = new SpannableStringBuilder() 87 .append("uses ") 88 .append("Pacific Time (") 89 .append(getGmtString("GMT-08:00")) 90 .append("). ") 91 .append(titleCasedFirstSentence); 92 93 assertThat(sb.toString()).isEqualTo( 94 "uses Pacific Time (GMT-08:00). Pacific Daylight Time starts on Mar 11 2018."); 95 96 Object[] spans = sb.getSpans(0, sb.length(), Object.class); 97 assertThat(spans).isNotEmpty(); 98 99 CharSequence titledOutput = SpannableUtil.titleCaseSentences(Locale.US, sb); 100 assertThat(titledOutput.toString()).isEqualTo( 101 "Uses Pacific Time (GMT-08:00). Pacific Daylight Time starts on Mar 11 2018."); 102 assertThat(titledOutput).isInstanceOf(Spannable.class); 103 Object[] newSpans = ((Spannable) titledOutput).getSpans(0, titledOutput.length(), 104 Object.class); 105 assertThat(newSpans.length).isEqualTo(spans.length); 106 assertThat(newSpans).isEqualTo(spans); 107 } 108 } 109