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.helpers;
18 
19 import static junit.framework.Assert.assertTrue;
20 
21 import android.app.Instrumentation;
22 import android.app.time.Capabilities;
23 import android.app.time.TimeManager;
24 import android.platform.helpers.ScrollUtility.ScrollActions;
25 import android.platform.helpers.ScrollUtility.ScrollDirection;
26 import android.text.format.DateFormat;
27 
28 import androidx.test.uiautomator.By;
29 import androidx.test.uiautomator.BySelector;
30 import androidx.test.uiautomator.UiObject;
31 import androidx.test.uiautomator.UiObject2;
32 import androidx.test.uiautomator.UiScrollable;
33 import androidx.test.uiautomator.UiSelector;
34 
35 import com.android.car.settings.Flags;
36 
37 import java.time.LocalDate;
38 import java.time.Month;
39 import java.time.format.TextStyle;
40 import java.util.List;
41 import java.util.Locale;
42 
43 /** Helper file for Date&Time test */
44 public class SettingsDateTimeHelperImpl extends AbstractStandardAppHelper
45         implements IAutoDateTimeSettingsHelper {
46     private static final Locale LOCALE = Locale.ENGLISH;
47     private static final TextStyle TEXT_STYLE = TextStyle.SHORT;
48     private static final String LOG_TAG = SettingsDateTimeHelperImpl.class.getSimpleName();
49     private ScrollUtility mScrollUtility;
50     private ScrollActions mScrollAction;
51     private BySelector mBackwardButtonSelector;
52     private BySelector mForwardButtonSelector;
53     private BySelector mScrollableElementSelector;
54     private BySelector mSummarySelector;
55     private ScrollDirection mScrollDirection;
56 
SettingsDateTimeHelperImpl(Instrumentation instr)57     public SettingsDateTimeHelperImpl(Instrumentation instr) {
58         super(instr);
59         mScrollUtility = ScrollUtility.getInstance(getSpectatioUiUtil());
60         mScrollAction =
61                 ScrollActions.valueOf(
62                         getActionFromConfig(
63                                 AutomotiveConfigConstants.DATE_TIME_SETTINGS_SCROLL_ACTION));
64         mBackwardButtonSelector =
65                 getUiElementFromConfig(
66                         AutomotiveConfigConstants.DATE_TIME_SETTINGS_SCROLL_BACKWARD_BUTTON);
67         mForwardButtonSelector =
68                 getUiElementFromConfig(
69                         AutomotiveConfigConstants.DATE_TIME_SETTINGS_SCROLL_FORWARD_BUTTON);
70         mScrollableElementSelector =
71                 getUiElementFromConfig(AutomotiveConfigConstants.DATE_TIME_SETTINGS_SCROLL_ELEMENT);
72         mSummarySelector = getUiElementFromConfig(AutomotiveConfigConstants.SETTINGS_SUMMARY);
73         mScrollDirection =
74                 ScrollDirection.valueOf(
75                         getActionFromConfig(
76                                 AutomotiveConfigConstants.DATE_TIME_SETTINGS_SCROLL_DIRECTION));
77         mScrollUtility.setScrollValues(
78                 Integer.valueOf(
79                         getActionFromConfig(
80                                 AutomotiveConfigConstants.DATE_TIME_SETTINGS_SCROLL_MARGIN)),
81                 Integer.valueOf(
82                         getActionFromConfig(
83                                 AutomotiveConfigConstants.DATE_TIME_SETTINGS_SCROLL_WAIT_TIME)));
84     }
85 
86     /** {@inheritDoc} */
87     @Override
getPackage()88     public String getPackage() {
89         return getPackageFromConfig(AutomotiveConfigConstants.SETTINGS_PACKAGE);
90     }
91 
92     /** {@inheritDoc} */
93     @Override
getLauncherName()94     public String getLauncherName() {
95         throw new UnsupportedOperationException("Operation not supported.");
96     }
97 
98     /** {@inheritDoc} */
99     @Override
dismissInitialDialogs()100     public void dismissInitialDialogs() {
101         // Nothing to dismiss
102     }
103 
104     /** {@inheritDoc} */
105     @Override
setDate(LocalDate date)106     public void setDate(LocalDate date) {
107         UiObject2 autoDateTimeSwitchWidget = getAutoDateTimeSwitchWidget();
108         UiObject2 autoDateTimeMenu =
109                 getMenu(
110                         getUiElementFromConfig(
111                                 AutomotiveConfigConstants
112                                         .DATE_TIME_SETTINGS_SET_TIME_AUTOMATICALLY));
113         if (autoDateTimeSwitchWidget.isChecked()) {
114             getSpectatioUiUtil().clickAndWait(autoDateTimeMenu);
115             getSpectatioUiUtil().wait1Second();
116         }
117         UiObject2 setDateMenu = getSetDateMenu();
118         assertTrue("set date menu is not clickable", setDateMenu.isEnabled()); // from UI
119         getSpectatioUiUtil().clickAndWait(setDateMenu);
120         getSpectatioUiUtil().wait1Second();
121         assertTrue("automatic date & time is not switched off", !isAutomaticOn()); // from API
122         int year = date.getYear();
123         Month month = date.getMonth();
124         int day = date.getDayOfMonth();
125         String month_string = month.getDisplayName(TEXT_STYLE, LOCALE);
126         String day_string = "";
127         if (day < 10) {
128             day_string = "0" + String.valueOf(day);
129         } else {
130             day_string = "" + String.valueOf(day);
131         }
132         String year_string = "" + year;
133         setCalendar(1, day_string);
134         setCalendar(0, month_string);
135         setCalendar(2, year_string);
136         pressSettingsBackNavIcon();
137     }
138 
pressSettingsBackNavIcon()139     private void pressSettingsBackNavIcon() {
140         UiObject2 navIcon =
141                 getSpectatioUiUtil()
142                         .findUiObject(
143                                 getUiElementFromConfig(
144                                         AutomotiveConfigConstants.SETTINGS_BACK_NAV_ICON));
145         getSpectatioUiUtil()
146                 .validateUiObject(
147                         navIcon, "Could not press back; unable to find settings back nav icon");
148         getSpectatioUiUtil().clickAndWait(navIcon);
149     }
150 
setCalendar(int index, String s)151     private void setCalendar(int index, String s) {
152         UiSelector selector =
153                 new UiSelector()
154                         .className(
155                                 getPackageFromConfig(
156                                         AutomotiveConfigConstants.NUMBER_PICKER_WIDGET_CLASS))
157                         .index(index);
158         boolean scrollForwards = true;
159         if (index == 2) {
160             UiSelector yearSelector =
161                     selector.childSelector(
162                             new UiSelector()
163                                     .className(
164                                             getPackageFromConfig(
165                                                     AutomotiveConfigConstants
166                                                             .EDIT_TEXT_WIDGET_CLASS)));
167             String curYear = "";
168             try {
169                 curYear = new UiObject(yearSelector).getText();
170             } catch (Exception e) {
171                 throw new RuntimeException(e);
172             }
173             if (Integer.valueOf(curYear) > Integer.valueOf(s)) scrollForwards = false;
174         }
175         scrollToObjectInPicker(index, s, scrollForwards);
176     }
177 
178     /** {@inheritDoc} */
179     @Override
getDate()180     public LocalDate getDate() {
181         UiObject2 obj = getSetDateMenu();
182         if (obj == null) {
183             throw new RuntimeException("Unable to find set date menu.");
184         }
185         String uiDate = getMenuSummaryText(obj);
186         String[] arr = uiDate.split(" ");
187         if (arr.length != 3) {
188             throw new RuntimeException("Cannot find date from UI");
189         }
190         int year = Integer.valueOf(arr[2]);
191         int month = Month.valueOf(arr[0].toUpperCase()).getValue();
192         int day = Integer.valueOf(arr[1].substring(0, arr[1].length() - 1));
193         return LocalDate.of(year, month, day);
194     }
195 
196     /** {@inheritDoc} */
197     @Override
setTimeInTwelveHourFormat(int hour, int minute, boolean am)198     public void setTimeInTwelveHourFormat(int hour, int minute, boolean am) {
199         // Get current time
200         String currentTime = getTime();
201 
202         // check Automatic date & time switch is turned off
203         UiObject2 autoDateTimeSwitchWidget = getAutoDateTimeSwitchWidget();
204         UiObject2 autoDateTimeMenu =
205                 getMenu(
206                         getUiElementFromConfig(
207                                 AutomotiveConfigConstants
208                                         .DATE_TIME_SETTINGS_SET_TIME_AUTOMATICALLY));
209         if (autoDateTimeSwitchWidget.isChecked()) {
210             getSpectatioUiUtil().clickAndWait(autoDateTimeMenu);
211             getSpectatioUiUtil().wait1Second();
212         }
213         // check 24-hour format switch is turned off
214         if (isTwentyFourHourFormatEnabled()) {
215             toggleTwentyFourHourFormatSwitch();
216         }
217 
218         UiObject2 setTimeMenu = getSetTimeMenu();
219         assertTrue("set time menu is not clickable", setTimeMenu.isEnabled()); // from UI
220         getSpectatioUiUtil().clickAndWait(setTimeMenu);
221         assertTrue("automatic date & time is not switched off", !isAutomaticOn()); // from API
222         String minute_string = "" + minute;
223         String am_pm = "";
224         am_pm = am ? "AM" : "PM";
225         if (minute < 10) {
226             minute_string = "0" + minute;
227         }
228         setTime(2, minute_string, currentTime);
229         setTime(0, String.valueOf(hour), currentTime);
230         setTime(1, am_pm, currentTime);
231         pressSettingsBackNavIcon();
232     }
233 
234     /** {@inheritDoc} */
235     @Override
setTimeInTwentyFourHourFormat(int hour, int minute)236     public void setTimeInTwentyFourHourFormat(int hour, int minute) {
237         // Get current time
238         String currentTime = getTime();
239 
240         // check Automatic date & time switch is turned off
241         UiObject2 autoDateTimeSwitchWidget = getAutoDateTimeSwitchWidget();
242         UiObject2 autoDateTimeMenu =
243                 getMenu(
244                         getUiElementFromConfig(
245                                 AutomotiveConfigConstants
246                                         .DATE_TIME_SETTINGS_SET_TIME_AUTOMATICALLY));
247         if (autoDateTimeSwitchWidget.isChecked()) {
248             getSpectatioUiUtil().clickAndWait(autoDateTimeMenu);
249         }
250         // check 24-hour format switch is turned on
251         if (!isTwentyFourHourFormatEnabled()) {
252             toggleTwentyFourHourFormatSwitch();
253         }
254 
255         UiObject2 setTimeMenu = getSetTimeMenu();
256         assertTrue("set time menu is not clickable", setTimeMenu.isEnabled()); // from UI
257         getSpectatioUiUtil().clickAndWait(setTimeMenu);
258         assertTrue("automatic date & time is not switched off", !isAutomaticOn()); // from API
259         String minute_string = "" + minute;
260         if (minute < 10) {
261             minute_string = "0" + minute;
262         }
263         String hour_string = "" + hour;
264         if (hour < 10) {
265             hour_string = "0" + hour;
266         }
267         setTime(2, minute_string, currentTime);
268         setTime(0, hour_string, currentTime);
269         pressSettingsBackNavIcon();
270     }
271 
setTime(int index, String s, String currentTime)272     private void setTime(int index, String s, String currentTime) {
273         UiSelector selector =
274                 new UiSelector()
275                         .className(
276                                 getPackageFromConfig(
277                                         AutomotiveConfigConstants.NUMBER_PICKER_WIDGET_CLASS))
278                         .index(index);
279         boolean scrollForwards = true;
280         String curAM_PM;
281         if (index == 1) {
282             UiSelector am_pm_Selector =
283                     selector.childSelector(
284                             new UiSelector()
285                                     .className(
286                                             getPackageFromConfig(
287                                                     AutomotiveConfigConstants
288                                                             .EDIT_TEXT_WIDGET_CLASS)));
289             try {
290                 curAM_PM = new UiObject(am_pm_Selector).getText();
291             } catch (Exception e) {
292                 throw new RuntimeException(e);
293             }
294             if (curAM_PM.equals("PM")) scrollForwards = false;
295         } else if (index == 2) {
296             int currentMinute = Integer.parseInt(currentTime.split(":")[1].split("\\s+")[0]);
297             int setMinute = Integer.parseInt(s);
298 
299             /* Set scrollForwards such that the minute is scrolled a max of 30 times */
300             if (currentMinute > setMinute) {
301                 if (currentMinute - setMinute <= 30) scrollForwards = false;
302             } else if (setMinute > currentMinute) {
303                 if (setMinute - currentMinute > 30) scrollForwards = false;
304             }
305         } else {
306             int currentHour = Integer.parseInt(currentTime.split(":")[0]);
307             int setHour = Integer.parseInt(s);
308 
309             /* Set max scrolls based on whether we're in 12 or 24 hour format */
310             int maxScrolls =
311                     (currentTime.trim().endsWith("AM") || currentTime.trim().endsWith("PM"))
312                             ? 6
313                             : 12;
314 
315             /* Calculate forward or backward like we did for minutes */
316             if (currentHour > setHour) {
317                 if (currentHour - setHour <= maxScrolls) scrollForwards = false;
318             } else if (setHour > currentHour) {
319                 if (setHour - currentHour > maxScrolls) scrollForwards = false;
320             }
321         }
322         scrollToObjectInPicker(index, s, scrollForwards);
323     }
324 
scrollToObjectInPicker(int index, String s, boolean scrollForwards)325     private void scrollToObjectInPicker(int index, String s, boolean scrollForwards) {
326         UiSelector selector =
327                 new UiSelector()
328                         .className(
329                                 getPackageFromConfig(
330                                         AutomotiveConfigConstants.NUMBER_PICKER_WIDGET_CLASS))
331                         .index(index);
332         UiScrollable scrollable = new UiScrollable(selector);
333         scrollable.setAsVerticalList();
334         UiObject2 obj =
335                 getSpectatioUiUtil()
336                         .findUiObject(
337                                 By.text(s)
338                                         .clazz(
339                                                 getPackageFromConfig(
340                                                         AutomotiveConfigConstants
341                                                                 .EDIT_TEXT_WIDGET_CLASS)));
342 
343         /* For hour and minute, search by child object instead of text */
344         if (index == 0 || index == 2) {
345             UiSelector dayOrMonthSelector =
346                     selector.childSelector(
347                             new UiSelector()
348                                     .className(
349                                             getPackageFromConfig(
350                                                     AutomotiveConfigConstants
351                                                             .EDIT_TEXT_WIDGET_CLASS)));
352 
353             /* Once we have the child selector, search for text within that selector */
354             String currentValue = "";
355             try {
356                 currentValue = new UiObject(dayOrMonthSelector).getText().trim();
357             } catch (Exception e) {
358                 throw new RuntimeException(e);
359             }
360 
361             while (!currentValue.equals(s.trim())) {
362                 try {
363                     if (scrollForwards) {
364                         scrollable.scrollForward();
365                     } else {
366                         scrollable.scrollBackward();
367                     }
368                 } catch (Exception e) {
369                     throw new RuntimeException(e);
370                 }
371 
372                 dayOrMonthSelector =
373                         selector.childSelector(
374                                 new UiSelector()
375                                         .className(
376                                                 getPackageFromConfig(
377                                                         AutomotiveConfigConstants
378                                                                 .EDIT_TEXT_WIDGET_CLASS)));
379 
380                 try {
381                     currentValue = new UiObject(dayOrMonthSelector).getText().trim();
382                 } catch (Exception e) {
383                     throw new RuntimeException(e);
384                 }
385             }
386         } else {
387             while (obj == null) {
388                 try {
389                     if (scrollForwards) {
390                         scrollable.scrollForward();
391                     } else {
392                         scrollable.scrollBackward();
393                     }
394                 } catch (Exception e) {
395                     throw new RuntimeException(e);
396                 }
397                 obj =
398                         getSpectatioUiUtil()
399                                 .findUiObject(
400                                         By.text(s)
401                                                 .clazz(
402                                                         getPackageFromConfig(
403                                                                 AutomotiveConfigConstants
404                                                                         .EDIT_TEXT_WIDGET_CLASS)));
405             }
406 
407             if (obj == null) throw new RuntimeException("cannot find value in the picker");
408         }
409     }
410 
411     /** {@inheritDoc} */
412     @Override
getTime()413     public String getTime() {
414         UiObject2 obj = getSetTimeMenu();
415         if (obj == null) {
416             throw new RuntimeException("Unable to find time menu.");
417         }
418         String uiTime = getMenuSummaryText(obj);
419         return uiTime;
420     }
421 
422     /** {@inheritDoc} */
423     @Override
setTimeZone(String timezone)424     public void setTimeZone(String timezone) {
425         UiObject2 autoTimeZoneMenu =
426                 getMenu(
427                         getUiElementFromConfig(
428                                 Flags.updateDateAndTimePage()
429                                         ? AutomotiveConfigConstants
430                                                 .DATE_TIME_SETTINGS_SET_TIME_AUTOMATICALLY
431                                         : AutomotiveConfigConstants
432                                                 .DATE_TIME_SETTINGS_SET_TIME_ZONE_AUTOMATICALLY));
433         if (Flags.updateDateAndTimePage()
434                 ? getAutoDateTimeSwitchWidget().isChecked()
435                 : getAutoTimeZoneSwitchWidget().isChecked()) {
436             getSpectatioUiUtil().clickAndWait(autoTimeZoneMenu);
437         }
438         UiObject2 selectTimeZoneMenu = getSelectTimeZoneMenu();
439         assertTrue(
440                 "select time zone menu is not clickable",
441                 selectTimeZoneMenu.isEnabled()); // from UI
442         assertTrue("automatic time zone is not switched off", !isAutomaticOn()); // from API
443         getSpectatioUiUtil().clickAndWait(selectTimeZoneMenu);
444         BySelector selector = By.clickable(true).hasDescendant(By.text(timezone));
445         ScrollActions scrollAction =
446                 ScrollActions.valueOf(
447                         getActionFromConfig(
448                                 AutomotiveConfigConstants.DATE_TIME_SETTINGS_SCROLL_ACTION));
449         BySelector backwardButtonSelector =
450                 getUiElementFromConfig(
451                         AutomotiveConfigConstants.DATE_TIME_SETTINGS_SCROLL_BACKWARD_BUTTON);
452         BySelector forwardButtonSelector =
453                 getUiElementFromConfig(
454                         AutomotiveConfigConstants.DATE_TIME_SETTINGS_SCROLL_FORWARD_BUTTON);
455         BySelector scrollElementSelector =
456                 getUiElementFromConfig(AutomotiveConfigConstants.DATE_TIME_SETTINGS_SCROLL_ELEMENT);
457         ScrollDirection scrollDirection =
458                 ScrollDirection.valueOf(
459                         getActionFromConfig(
460                                 AutomotiveConfigConstants.DATE_TIME_SETTINGS_SCROLL_DIRECTION));
461         UiObject2 object =
462                 mScrollUtility.scrollAndFindUiObject(
463                         scrollAction,
464                         scrollDirection,
465                         forwardButtonSelector,
466                         backwardButtonSelector,
467                         scrollElementSelector,
468                         selector,
469                         String.format("Scroll on date & time to find %s", timezone));
470         getSpectatioUiUtil()
471                 .validateUiObject(object, String.format("Unable to find timezone %s", timezone));
472         getSpectatioUiUtil().clickAndWait(object);
473     }
474 
475     /** {@inheritDoc} */
476     @Override
toggleTwentyFourHourFormatSwitch()477     public boolean toggleTwentyFourHourFormatSwitch() {
478         UiObject2 twentyFourHourFormatSwitch = getUseTwentyFourHourFormatSwitchWidget();
479         if (twentyFourHourFormatSwitch.isChecked()) {
480             assertTrue(
481                     "System time format is different from UI format",
482                     DateFormat.is24HourFormat(mInstrumentation.getContext()));
483         } else {
484             assertTrue(
485                     "System time format is different from UI format",
486                     !DateFormat.is24HourFormat(mInstrumentation.getContext()));
487         }
488         getSpectatioUiUtil().clickAndWait(twentyFourHourFormatSwitch);
489         return true;
490     }
491 
492     /** {@inheritDoc} */
493     @Override
getTimeZone()494     public String getTimeZone() {
495         UiObject2 obj = getSelectTimeZoneMenu();
496         if (obj == null) {
497             throw new RuntimeException("Unable to find timezone menu.");
498         }
499         String timeZone = getMenuSummaryText(obj);
500         return timeZone;
501     }
502 
503     /** {@inheritDoc} */
504     @Override
isTwentyFourHourFormatEnabled()505     public boolean isTwentyFourHourFormatEnabled() {
506         UiObject2 twentyFourHourFormatSwitchWidget = getUseTwentyFourHourFormatSwitchWidget();
507         return twentyFourHourFormatSwitchWidget.isChecked();
508     }
509 
getSetDateMenu()510     private UiObject2 getSetDateMenu() {
511         return getMenu(
512                 getUiElementFromConfig(AutomotiveConfigConstants.DATE_TIME_SETTINGS_SET_DATE));
513     }
514 
getSetTimeMenu()515     private UiObject2 getSetTimeMenu() {
516         return getMenu(
517                 getUiElementFromConfig(AutomotiveConfigConstants.DATE_TIME_SETTINGS_SET_TIME));
518     }
519 
getTwentyFourFormatMenu()520     private UiObject2 getTwentyFourFormatMenu() {
521         return getMenu(
522                 getUiElementFromConfig(
523                         AutomotiveConfigConstants.DATE_TIME_SETTINGS_USE_24_HOUR_FORMAT));
524     }
525 
getSelectTimeZoneMenu()526     private UiObject2 getSelectTimeZoneMenu() {
527         return getMenu(
528                 getUiElementFromConfig(
529                         AutomotiveConfigConstants.DATE_TIME_SETTINGS_SELECT_TIME_ZONE));
530     }
531 
getMenu(BySelector bySelector)532     private UiObject2 getMenu(BySelector bySelector) {
533         BySelector selector = By.clickable(true).hasDescendant(bySelector);
534         ScrollActions scrollAction =
535                 ScrollActions.valueOf(
536                         getActionFromConfig(
537                                 AutomotiveConfigConstants.DATE_TIME_SETTINGS_SCROLL_ACTION));
538         BySelector backwardButtonSelector =
539                 getUiElementFromConfig(
540                         AutomotiveConfigConstants.DATE_TIME_SETTINGS_SCROLL_BACKWARD_BUTTON);
541         BySelector forwardButtonSelector =
542                 getUiElementFromConfig(
543                         AutomotiveConfigConstants.DATE_TIME_SETTINGS_SCROLL_FORWARD_BUTTON);
544         BySelector scrollElementSelector =
545                 getUiElementFromConfig(AutomotiveConfigConstants.DATE_TIME_SETTINGS_SCROLL_ELEMENT);
546         ScrollDirection scrollDirection =
547                 ScrollDirection.valueOf(
548                         getActionFromConfig(
549                                 AutomotiveConfigConstants.DATE_TIME_SETTINGS_SCROLL_DIRECTION));
550         UiObject2 object =
551                 mScrollUtility.scrollAndFindUiObject(
552                         scrollAction,
553                         scrollDirection,
554                         forwardButtonSelector,
555                         backwardButtonSelector,
556                         scrollElementSelector,
557                         selector,
558                         String.format("Scroll on date & time to find %s", selector));
559         return object;
560     }
561 
getAutoDateTimeSwitchWidget()562     private UiObject2 getAutoDateTimeSwitchWidget() {
563         return getSwitchWidget(
564                 getUiElementFromConfig(
565                         AutomotiveConfigConstants.DATE_TIME_SETTINGS_SET_TIME_AUTOMATICALLY));
566     }
567 
getAutoTimeZoneSwitchWidget()568     private UiObject2 getAutoTimeZoneSwitchWidget() {
569         return getSwitchWidget(
570                 getUiElementFromConfig(
571                         Flags.updateDateAndTimePage()
572                                 ? AutomotiveConfigConstants
573                                         .DATE_TIME_SETTINGS_SET_TIME_AUTOMATICALLY
574                                 : AutomotiveConfigConstants
575                                         .DATE_TIME_SETTINGS_SET_TIME_ZONE_AUTOMATICALLY));
576     }
577 
getUseTwentyFourHourFormatSwitchWidget()578     private UiObject2 getUseTwentyFourHourFormatSwitchWidget() {
579         return getSwitchWidget(
580                 getUiElementFromConfig(
581                         AutomotiveConfigConstants.DATE_TIME_SETTINGS_USE_24_HOUR_FORMAT));
582     }
583 
getSwitchWidget(BySelector bySelector)584     private UiObject2 getSwitchWidget(BySelector bySelector) {
585         BySelector selector = By.hasDescendant(bySelector);
586         UiObject2 object =
587                 mScrollUtility.scrollAndFindUiObject(
588                         mScrollAction,
589                         mScrollDirection,
590                         mForwardButtonSelector,
591                         mBackwardButtonSelector,
592                         mScrollableElementSelector,
593                         selector,
594                         String.format("Scroll on date & time to find %s", selector));
595         List<UiObject2> list = object.getParent().getChildren();
596         UiObject2 switchWidget = list.get(1).getChildren().get(0).getChildren().get(0);
597         return switchWidget;
598     }
599 
getMenuSummaryText(UiObject2 obj)600     private String getMenuSummaryText(UiObject2 obj) {
601         return obj.findObject(mSummarySelector).getText();
602     }
603 
isAutomaticOn()604     private boolean isAutomaticOn() {
605         TimeManager timeManager = mInstrumentation.getContext().getSystemService(TimeManager.class);
606         boolean status;
607         try {
608             status = timeManager
609                             .getTimeCapabilitiesAndConfig()
610                             .getCapabilities()
611                             .getConfigureAutoDetectionEnabledCapability()
612                                     == Capabilities.CAPABILITY_POSSESSED
613                     && timeManager
614                             .getTimeCapabilitiesAndConfig()
615                             .getConfiguration()
616                             .isAutoDetectionEnabled()
617                     && timeManager
618                             .getTimeZoneCapabilitiesAndConfig()
619                             .getCapabilities()
620                             .getConfigureAutoDetectionEnabledCapability()
621                                     == Capabilities.CAPABILITY_POSSESSED
622                     && timeManager
623                             .getTimeZoneCapabilitiesAndConfig()
624                             .getConfiguration()
625                             .isAutoDetectionEnabled();
626         } catch (Exception e) {
627             throw new RuntimeException(e);
628         }
629         return status;
630     }
631 
632 }
633