1 /* 2 * Copyright (C) 2023 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 android.platform.tests; 18 19 import static junit.framework.Assert.assertTrue; 20 21 import android.platform.helpers.HelperAccessor; 22 import android.platform.helpers.IAutoDateTimeSettingsHelper; 23 import android.platform.helpers.IAutoSettingHelper; 24 import android.platform.helpers.SettingsConstants; 25 26 import androidx.test.runner.AndroidJUnit4; 27 28 import org.junit.After; 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 33 import java.time.LocalDate; 34 35 @RunWith(AndroidJUnit4.class) 36 public class DateTimeSettingTest { 37 private HelperAccessor<IAutoDateTimeSettingsHelper> mDateTimeSettingsHelper; 38 private HelperAccessor<IAutoSettingHelper> mSettingHelper; 39 40 private static final boolean IS_AM = true; 41 private static final int DEFAULT_WAIT_TIME = 5000; 42 private static final int HOUR = 9; 43 private static final int MINUTE = 23; 44 private static final int YEAR = 2020; 45 private static final int MONTH = 3; 46 private static final int DAY = 9; 47 private static final LocalDate DATE = LocalDate.of(YEAR, MONTH, DAY); 48 49 private static final String FULL_TIME_TWELVE_REGEX = "9:23[ \\x{202f}]AM"; 50 private static final String FULL_TIME_TWENTYFOUR = "09:23"; 51 private static final String TIME_ZONE = "Costa Rica"; 52 private static final String TIME_ZONE_FULL = "GMT-06:00 Central Standard Time"; 53 DateTimeSettingTest()54 public DateTimeSettingTest() throws Exception { 55 mDateTimeSettingsHelper = new HelperAccessor<>(IAutoDateTimeSettingsHelper.class); 56 mSettingHelper = new HelperAccessor<>(IAutoSettingHelper.class); 57 } 58 59 60 @Before openDateTimeFacet()61 public void openDateTimeFacet() { 62 mSettingHelper.get().openSetting(SettingsConstants.DATE_AND_TIME_SETTINGS); 63 } 64 65 @After goBackToSettingsScreen()66 public void goBackToSettingsScreen() { 67 mSettingHelper.get().goBackToSettingsScreen(); 68 } 69 70 @Test testSetDate()71 public void testSetDate() { 72 mDateTimeSettingsHelper.get().setDate(DATE); 73 assertTrue(mDateTimeSettingsHelper.get().getDate().equals(DATE)); 74 } 75 76 @Test testSetTimeTwelveHourFormat()77 public void testSetTimeTwelveHourFormat() { 78 mDateTimeSettingsHelper.get().setTimeInTwelveHourFormat(HOUR, MINUTE, IS_AM); 79 assertTrue(mDateTimeSettingsHelper.get().getTime().matches(FULL_TIME_TWELVE_REGEX)); 80 } 81 82 @Test testSetTimeTwentyFourHourFormat()83 public void testSetTimeTwentyFourHourFormat() { 84 mDateTimeSettingsHelper.get().setTimeInTwentyFourHourFormat(HOUR, MINUTE); 85 assertTrue(mDateTimeSettingsHelper.get().getTime().equals(FULL_TIME_TWENTYFOUR)); 86 } 87 88 @Test testSetTimeZone()89 public void testSetTimeZone() { 90 mDateTimeSettingsHelper.get().setTimeZone(TIME_ZONE); 91 assertTrue(mDateTimeSettingsHelper.get().getTimeZone().equals(TIME_ZONE_FULL)); 92 } 93 94 @Test testSetTwentyFourHourFormat()95 public void testSetTwentyFourHourFormat() { 96 if (!mDateTimeSettingsHelper.get().isTwentyFourHourFormatEnabled()) { 97 mDateTimeSettingsHelper.get().toggleTwentyFourHourFormatSwitch(); 98 } 99 String currentUiTime = mDateTimeSettingsHelper.get().getTime(); 100 assertTrue(currentUiTime.indexOf("AM") == -1 && currentUiTime.indexOf("PM") == -1); 101 } 102 103 @Test testSetTwelveHourFormat()104 public void testSetTwelveHourFormat() { 105 if (mDateTimeSettingsHelper.get().isTwentyFourHourFormatEnabled()) { 106 mDateTimeSettingsHelper.get().toggleTwentyFourHourFormatSwitch(); 107 } 108 String currentUiTime = mDateTimeSettingsHelper.get().getTime(); 109 assertTrue(currentUiTime.indexOf("AM") != -1 || currentUiTime.indexOf("PM") != -1); 110 } 111 } 112