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