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 android.os.Process.myUserHandle; 19 20 import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; 21 22 import static com.android.launcher3.util.TestUtil.getOnUiThread; 23 24 import android.app.Instrumentation; 25 import android.content.ComponentName; 26 import android.util.Log; 27 import android.view.View; 28 import android.view.ViewGroup; 29 30 import com.android.launcher3.testcomponent.AppWidgetNoConfig; 31 import com.android.launcher3.testcomponent.AppWidgetWithConfig; 32 import com.android.launcher3.widget.LauncherAppWidgetProviderInfo; 33 import com.android.launcher3.widget.WidgetManagerHelper; 34 35 import java.util.function.Function; 36 37 public class TestViewHelpers { 38 private static final String TAG = "TestViewHelpers"; 39 40 /** 41 * Finds a widget provider which can fit on the home screen. 42 * 43 * @param hasConfigureScreen if true, a provider with a config screen is returned. 44 */ findWidgetProvider(boolean hasConfigureScreen)45 public static LauncherAppWidgetProviderInfo findWidgetProvider(boolean hasConfigureScreen) { 46 LauncherAppWidgetProviderInfo info = getOnUiThread(() -> { 47 Instrumentation i = getInstrumentation(); 48 ComponentName cn = new ComponentName(i.getContext(), 49 hasConfigureScreen ? AppWidgetWithConfig.class : AppWidgetNoConfig.class); 50 Log.d(TAG, "findWidgetProvider componentName=" + cn.flattenToString()); 51 return new WidgetManagerHelper(i.getTargetContext()).findProvider(cn, myUserHandle()); 52 }); 53 if (info == null) { 54 throw new IllegalArgumentException("No valid widget provider"); 55 } 56 return info; 57 } 58 findChildView(ViewGroup parent, Function<View, Boolean> condition)59 public static View findChildView(ViewGroup parent, Function<View, Boolean> condition) { 60 for (int i = 0; i < parent.getChildCount(); ++i) { 61 final View child = parent.getChildAt(i); 62 if (condition.apply(child)) return child; 63 } 64 return null; 65 } 66 } 67