Home | History | Annotate | Download | only in timezone
      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 android.content.Context;
     20 
     21 import com.android.settings.datetime.timezone.BaseTimeZoneAdapter.AdapterItem;
     22 import com.android.settings.datetime.timezone.model.TimeZoneData;
     23 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     24 
     25 import libcore.util.CountryZonesFinder;
     26 
     27 import org.junit.Before;
     28 import org.junit.Test;
     29 import org.junit.runner.RunWith;
     30 import org.robolectric.RuntimeEnvironment;
     31 
     32 import java.util.Collections;
     33 import java.util.List;
     34 import java.util.Locale;
     35 import java.util.stream.Collectors;
     36 
     37 import static com.google.common.truth.Truth.assertThat;
     38 import static org.mockito.Mockito.mock;
     39 import static org.mockito.Mockito.when;
     40 
     41 @RunWith(SettingsRobolectricTestRunner.class)
     42 public class FixedOffsetPickerTest {
     43 
     44     private CountryZonesFinder mFinder;
     45 
     46     @Before
     47     public void setUp() {
     48         List regionList = Collections.emptyList();
     49         mFinder = mock(CountryZonesFinder.class);
     50         when(mFinder.lookupAllCountryIsoCodes()).thenReturn(regionList);
     51     }
     52 
     53     @Test
     54     public void getAllTimeZoneInfos_containsUtcAndGmtZones() {
     55         TestFixedOffsetPicker picker = new TestFixedOffsetPicker();
     56         List<TimeZoneInfo> infos = picker.getAllTimeZoneInfos(new TimeZoneData(mFinder));
     57         List<String> tzIds = infos.stream().map(info -> info.getId()).collect(Collectors.toList());
     58         assertThat(tzIds).contains("Etc/UTC");
     59         assertThat(tzIds).contains("Etc/GMT-14"); // Etc/GMT-14 means GMT+14:00
     60         assertThat(tzIds).contains("Etc/GMT+12"); // Etc/GMT+14 means GMT-12:00
     61     }
     62 
     63     @Test
     64     public void createAdapter_verifyTitleAndOffset() {
     65         TestFixedOffsetPicker picker = new TestFixedOffsetPicker();
     66         BaseTimeZoneAdapter adapter = picker.createAdapter(new TimeZoneData(mFinder));
     67         assertThat(adapter.getItemCount()).isEqualTo(12 + 1 + 14); // 27 GMT offsets from -12 to +14
     68         AdapterItem utc = adapter.getDataItem(0);
     69         assertThat(utc.getTitle().toString()).isEqualTo("Coordinated Universal Time");
     70         assertThat(utc.getSummary().toString()).isEqualTo("GMT+00:00");
     71         AdapterItem gmtMinus12 = adapter.getDataItem(1);
     72         assertThat(gmtMinus12.getTitle().toString()).isEqualTo("GMT-12:00");
     73         assertThat(gmtMinus12.getSummary().toString()).isEmpty();
     74     }
     75 
     76     public static class TestFixedOffsetPicker extends FixedOffsetPicker {
     77         // Make the method public
     78         @Override
     79         public BaseTimeZoneAdapter createAdapter(TimeZoneData timeZoneData) {
     80             return super.createAdapter(timeZoneData);
     81         }
     82 
     83         @Override
     84         protected Locale getLocale() {
     85             return Locale.US;
     86         }
     87 
     88         @Override
     89         public Context getContext() {
     90             return RuntimeEnvironment.application;
     91         }
     92     }
     93 }
     94