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.util.rule; 17 18 import android.app.Activity; 19 import android.app.Application; 20 import android.app.Application.ActivityLifecycleCallbacks; 21 import android.os.Bundle; 22 23 import androidx.test.InstrumentationRegistry; 24 25 import com.android.launcher3.Launcher; 26 import com.android.launcher3.Workspace.ItemOperator; 27 28 import org.junit.rules.TestRule; 29 import org.junit.runner.Description; 30 import org.junit.runners.model.Statement; 31 32 import java.util.concurrent.Callable; 33 34 /** 35 * Test rule to get the current Launcher activity. 36 */ 37 public class LauncherActivityRule implements TestRule { 38 39 private Launcher mActivity; 40 41 @Override apply(Statement base, Description description)42 public Statement apply(Statement base, Description description) { 43 return new MyStatement(base); 44 } 45 getActivity()46 public Launcher getActivity() { 47 return mActivity; 48 } 49 itemExists(final ItemOperator op)50 public Callable<Boolean> itemExists(final ItemOperator op) { 51 return () -> { 52 Launcher launcher = getActivity(); 53 if (launcher == null) { 54 return false; 55 } 56 return launcher.getWorkspace().getFirstMatch(op) != null; 57 }; 58 } 59 60 private class MyStatement extends Statement implements ActivityLifecycleCallbacks { 61 62 private final Statement mBase; 63 MyStatement(Statement base)64 public MyStatement(Statement base) { 65 mBase = base; 66 } 67 68 @Override evaluate()69 public void evaluate() throws Throwable { 70 Application app = (Application) 71 InstrumentationRegistry.getTargetContext().getApplicationContext(); 72 app.registerActivityLifecycleCallbacks(this); 73 try { 74 mBase.evaluate(); 75 } finally { 76 app.unregisterActivityLifecycleCallbacks(this); 77 } 78 } 79 80 @Override onActivityCreated(Activity activity, Bundle bundle)81 public void onActivityCreated(Activity activity, Bundle bundle) { 82 if (activity instanceof Launcher) { 83 mActivity = (Launcher) activity; 84 } 85 } 86 87 @Override onActivityStarted(Activity activity)88 public void onActivityStarted(Activity activity) { 89 if (activity instanceof Launcher) { 90 mActivity.getRotationHelper().forceAllowRotationForTesting(true); 91 } 92 } 93 94 @Override onActivityResumed(Activity activity)95 public void onActivityResumed(Activity activity) { 96 } 97 98 @Override onActivityPaused(Activity activity)99 public void onActivityPaused(Activity activity) { 100 } 101 102 @Override onActivityStopped(Activity activity)103 public void onActivityStopped(Activity activity) { 104 } 105 106 @Override onActivitySaveInstanceState(Activity activity, Bundle bundle)107 public void onActivitySaveInstanceState(Activity activity, Bundle bundle) { 108 } 109 110 @Override onActivityDestroyed(Activity activity)111 public void onActivityDestroyed(Activity activity) { 112 if (activity == mActivity) { 113 mActivity = null; 114 } 115 } 116 } 117 } 118