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 android.app.Instrumentation;
20 import android.platform.spectatio.utils.SpectatioUiUtil;
21 import android.util.Log;
22 
23 import androidx.test.uiautomator.UiObject2;
24 
25 /** Helper class for general UI actions */
26 public class UIHelperImpl extends AbstractStandardAppHelper implements IAutoGeneralUIHelper {
27 
28     private static final String LOG_TAG = UIHelperImpl.class.getSimpleName();
29 
30     private SpectatioUiUtil mSpectatioUiUtil;
31 
UIHelperImpl(Instrumentation instr)32     public UIHelperImpl(Instrumentation instr) {
33         super(instr);
34         mSpectatioUiUtil = getSpectatioUiUtil();
35     }
36 
37     /** {@inheritDoc} */
38     @Override
getPackage()39     public String getPackage() {
40         return getPackageFromConfig(AutomotiveConfigConstants.GENERAL_UI_PACKAGE);
41     }
42 
43     /** {@inheritDoc} */
44     @Override
getLauncherName()45     public String getLauncherName() {
46         throw new UnsupportedOperationException("Operation not supported.");
47     }
48 
49     /** {@inheritDoc} */
50     @Override
dismissInitialDialogs()51     public void dismissInitialDialogs() {
52         // Nothing to dismiss
53     }
54 
55     /** {@inheritDoc} */
56     @Override
clickElementWithText(String text)57     public void clickElementWithText(String text) {
58         // TODO: How can me make button-pressing an asynchronous task?
59         try {
60             UiObject2 toClick = mSpectatioUiUtil.findUiObject(text);
61             mSpectatioUiUtil.clickAndWait(toClick);
62         } catch (Exception e) {
63             Log.e(
64                     LOG_TAG,
65                     String.format(
66                             "Error when clicking element with text %s : %s", text, e.toString()));
67         }
68     }
69 
70     /** {@inheritDoc} */
71     @Override
hasElementWithText(String text)72     public boolean hasElementWithText(String text) {
73         return mSpectatioUiUtil.hasUiElement(text);
74     }
75 }
76