1 /* 2 * Copyright (C) 2019 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 package com.android.launcher3.testing; 17 18 import static com.android.launcher3.allapps.AllAppsStore.DEFER_UPDATES_TEST; 19 import static com.android.launcher3.util.Executors.MAIN_EXECUTOR; 20 21 import android.annotation.TargetApi; 22 import android.app.Activity; 23 import android.content.Context; 24 import android.graphics.Insets; 25 import android.os.Build; 26 import android.os.Bundle; 27 import android.view.WindowInsets; 28 29 import com.android.launcher3.DeviceProfile; 30 import com.android.launcher3.InvariantDeviceProfile; 31 import com.android.launcher3.Launcher; 32 import com.android.launcher3.LauncherAppState; 33 import com.android.launcher3.LauncherState; 34 import com.android.launcher3.R; 35 import com.android.launcher3.util.ResourceBasedOverride; 36 import com.android.launcher3.widget.picker.WidgetsFullSheet; 37 38 import java.util.concurrent.ExecutionException; 39 import java.util.function.Function; 40 import java.util.function.Supplier; 41 42 /** 43 * Class to handle requests from tests 44 */ 45 @TargetApi(Build.VERSION_CODES.Q) 46 public class TestInformationHandler implements ResourceBasedOverride { 47 newInstance(Context context)48 public static TestInformationHandler newInstance(Context context) { 49 return Overrides.getObject(TestInformationHandler.class, 50 context, R.string.test_information_handler_class); 51 } 52 53 protected Context mContext; 54 protected DeviceProfile mDeviceProfile; 55 protected LauncherAppState mLauncherAppState; 56 init(Context context)57 public void init(Context context) { 58 mContext = context; 59 mDeviceProfile = InvariantDeviceProfile.INSTANCE. 60 get(context).getDeviceProfile(context); 61 mLauncherAppState = LauncherAppState.getInstanceNoCreate(); 62 } 63 call(String method)64 public Bundle call(String method) { 65 final Bundle response = new Bundle(); 66 switch (method) { 67 case TestProtocol.REQUEST_HOME_TO_ALL_APPS_SWIPE_HEIGHT: { 68 return getLauncherUIProperty(Bundle::putInt, l -> { 69 final float progress = LauncherState.NORMAL.getVerticalProgress(l) 70 - LauncherState.ALL_APPS.getVerticalProgress(l); 71 final float distance = l.getAllAppsController().getShiftRange() * progress; 72 return (int) distance; 73 }); 74 } 75 76 case TestProtocol.REQUEST_IS_LAUNCHER_INITIALIZED: { 77 return getUIProperty(Bundle::putBoolean, t -> isLauncherInitialized(), () -> true); 78 } 79 80 case TestProtocol.REQUEST_FREEZE_APP_LIST: 81 return getLauncherUIProperty(Bundle::putBoolean, l -> { 82 l.getAppsView().getAppsStore().enableDeferUpdates(DEFER_UPDATES_TEST); 83 return true; 84 }); 85 case TestProtocol.REQUEST_UNFREEZE_APP_LIST: 86 return getLauncherUIProperty(Bundle::putBoolean, l -> { 87 l.getAppsView().getAppsStore().disableDeferUpdates(DEFER_UPDATES_TEST); 88 return true; 89 }); 90 91 case TestProtocol.REQUEST_APPS_LIST_SCROLL_Y: { 92 return getLauncherUIProperty(Bundle::putInt, 93 l -> l.getAppsView().getActiveRecyclerView().getCurrentScrollY()); 94 } 95 96 case TestProtocol.REQUEST_WIDGETS_SCROLL_Y: { 97 return getLauncherUIProperty(Bundle::putInt, 98 l -> WidgetsFullSheet.getWidgetsView(l).getCurrentScrollY()); 99 } 100 101 case TestProtocol.REQUEST_WINDOW_INSETS: { 102 return getUIProperty(Bundle::putParcelable, a -> { 103 WindowInsets insets = a.getWindow() 104 .getDecorView().getRootWindowInsets(); 105 return Insets.max( 106 insets.getSystemGestureInsets(), insets.getSystemWindowInsets()); 107 }, this::getCurrentActivity); 108 } 109 110 case TestProtocol.REQUEST_ICON_HEIGHT: { 111 response.putInt(TestProtocol.TEST_INFO_RESPONSE_FIELD, 112 mDeviceProfile.allAppsCellHeightPx); 113 return response; 114 } 115 116 case TestProtocol.REQUEST_MOCK_SENSOR_ROTATION: 117 TestProtocol.sDisableSensorRotation = true; 118 return response; 119 120 default: 121 return null; 122 } 123 } 124 125 protected boolean isLauncherInitialized() { 126 return Launcher.ACTIVITY_TRACKER.getCreatedActivity() == null 127 || LauncherAppState.getInstance(mContext).getModel().isModelLoaded(); 128 } 129 130 protected Activity getCurrentActivity() { 131 return Launcher.ACTIVITY_TRACKER.getCreatedActivity(); 132 } 133 134 /** 135 * Returns the result by getting a Launcher property on UI thread 136 */ 137 public static <T> Bundle getLauncherUIProperty( 138 BundleSetter<T> bundleSetter, Function<Launcher, T> provider) { 139 return getUIProperty(bundleSetter, provider, Launcher.ACTIVITY_TRACKER::getCreatedActivity); 140 } 141 142 /** 143 * Returns the result by getting a generic property on UI thread 144 */ 145 private static <S, T> Bundle getUIProperty( 146 BundleSetter<T> bundleSetter, Function<S, T> provider, Supplier<S> targetSupplier) { 147 try { 148 return MAIN_EXECUTOR.submit(() -> { 149 S target = targetSupplier.get(); 150 if (target == null) { 151 return null; 152 } 153 T value = provider.apply(target); 154 Bundle response = new Bundle(); 155 bundleSetter.set(response, TestProtocol.TEST_INFO_RESPONSE_FIELD, value); 156 return response; 157 }).get(); 158 } catch (ExecutionException | InterruptedException e) { 159 throw new RuntimeException(e); 160 } 161 } 162 163 /** 164 * Generic interface for setting a fiend in bundle 165 * @param <T> the type of value being set 166 */ 167 public interface BundleSetter<T> { 168 169 /** 170 * Sets any generic property to the bundle 171 */ 172 void set(Bundle b, String key, T value); 173 } 174 } 175