1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package com.android.launcher3.ui;
17 
18 import static androidx.test.InstrumentationRegistry.getInstrumentation;
19 import static androidx.test.InstrumentationRegistry.getTargetContext;
20 
21 import android.content.ComponentName;
22 import android.os.Process;
23 import android.util.Log;
24 import android.view.View;
25 import android.view.ViewGroup;
26 
27 import com.android.launcher3.testcomponent.AppWidgetNoConfig;
28 import com.android.launcher3.testcomponent.AppWidgetWithConfig;
29 import com.android.launcher3.widget.LauncherAppWidgetProviderInfo;
30 import com.android.launcher3.widget.WidgetManagerHelper;
31 
32 import java.util.concurrent.Callable;
33 import java.util.function.Function;
34 
35 public class TestViewHelpers {
36     private static final String TAG = "TestViewHelpers";
37 
38     /**
39      * Finds a widget provider which can fit on the home screen.
40      *
41      * @param test               test suite.
42      * @param hasConfigureScreen if true, a provider with a config screen is returned.
43      */
findWidgetProvider(AbstractLauncherUiTest test, final boolean hasConfigureScreen)44     public static LauncherAppWidgetProviderInfo findWidgetProvider(AbstractLauncherUiTest test,
45             final boolean hasConfigureScreen) {
46         LauncherAppWidgetProviderInfo info =
47                 test.getOnUiThread(new Callable<LauncherAppWidgetProviderInfo>() {
48                     @Override
49                     public LauncherAppWidgetProviderInfo call() throws Exception {
50                         ComponentName cn = new ComponentName(getInstrumentation().getContext(),
51                                 hasConfigureScreen ? AppWidgetWithConfig.class
52                                         : AppWidgetNoConfig.class);
53                         Log.d(TAG, "findWidgetProvider componentName=" + cn.flattenToString());
54                         return new WidgetManagerHelper(getTargetContext())
55                                 .findProvider(cn, Process.myUserHandle());
56                     }
57                 });
58         if (info == null) {
59             throw new IllegalArgumentException("No valid widget provider");
60         }
61         return info;
62     }
63 
findChildView(ViewGroup parent, Function<View, Boolean> condition)64     public static View findChildView(ViewGroup parent, Function<View, Boolean> condition) {
65         for (int i = 0; i < parent.getChildCount(); ++i) {
66             final View child = parent.getChildAt(i);
67             if (condition.apply(child)) return child;
68         }
69         return null;
70     }
71 }
72