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 androidx.test.InstrumentationRegistry.getInstrumentation; 19 20 import static com.android.launcher3.tapl.TestHelpers.getLauncherInMyProcess; 21 22 import android.content.ComponentName; 23 import android.content.pm.ActivityInfo; 24 import android.util.Log; 25 26 import androidx.annotation.Nullable; 27 import androidx.test.InstrumentationRegistry; 28 import androidx.test.uiautomator.UiDevice; 29 30 import com.android.systemui.shared.system.PackageManagerWrapper; 31 32 import org.junit.Assert; 33 import org.junit.rules.TestRule; 34 import org.junit.runner.Description; 35 import org.junit.runners.model.Statement; 36 37 import java.util.ArrayList; 38 39 /** 40 * Test rule which executes a shell command at the start of the test. 41 */ 42 public class ShellCommandRule implements TestRule { 43 44 private final String mCmd; 45 private final String mRevertCommand; 46 private final boolean mCheckSuccess; 47 private final Runnable mAdditionalChecks; 48 ShellCommandRule(String cmd, @Nullable String revertCommand, boolean checkSuccess, Runnable additionalChecks)49 public ShellCommandRule(String cmd, @Nullable String revertCommand, boolean checkSuccess, 50 Runnable additionalChecks) { 51 mCmd = cmd; 52 mRevertCommand = revertCommand; 53 mCheckSuccess = checkSuccess; 54 mAdditionalChecks = additionalChecks; 55 } 56 ShellCommandRule(String cmd, @Nullable String revertCommand)57 public ShellCommandRule(String cmd, @Nullable String revertCommand) { 58 this(cmd, revertCommand, false, null); 59 } 60 61 @Override apply(Statement base, Description description)62 public Statement apply(Statement base, Description description) { 63 return new Statement() { 64 @Override 65 public void evaluate() throws Throwable { 66 final String result = 67 UiDevice.getInstance(getInstrumentation()).executeShellCommand(mCmd); 68 if (mCheckSuccess) { 69 Assert.assertTrue( 70 "Failed command: " + mCmd + ", result: " + result, 71 "Success".equals(result.replaceAll("\\s", ""))); 72 } 73 if (mAdditionalChecks != null) mAdditionalChecks.run(); 74 try { 75 base.evaluate(); 76 } finally { 77 if (mRevertCommand != null) { 78 final String revertResult = UiDevice.getInstance( 79 getInstrumentation()).executeShellCommand( 80 mRevertCommand); 81 if (mCheckSuccess) { 82 Assert.assertTrue( 83 "Failed command: " + mRevertCommand 84 + ", result: " + revertResult, 85 "Success".equals(result.replaceAll("\\s", ""))); 86 } 87 } 88 } 89 } 90 }; 91 } 92 93 /** 94 * Grants the launcher permission to bind widgets. 95 */ 96 public static ShellCommandRule grantWidgetBind() { 97 return new ShellCommandRule("appwidget grantbind --package " 98 + InstrumentationRegistry.getTargetContext().getPackageName(), null); 99 } 100 101 /** 102 * Sets the target launcher as default launcher. 103 */ 104 public static ShellCommandRule setDefaultLauncher() { 105 final ActivityInfo launcher = getLauncherInMyProcess(); 106 Log.d("b/187080582", "Launcher: " + new ComponentName(launcher.packageName, launcher.name) 107 .flattenToString()); 108 return new ShellCommandRule(getLauncherCommand(launcher), null, true, () -> 109 Assert.assertEquals("Setting default launcher failed", 110 new ComponentName(launcher.packageName, launcher.name) 111 .flattenToString(), 112 PackageManagerWrapper.getInstance().getHomeActivities(new ArrayList<>()) 113 .flattenToString())); 114 } 115 116 public static String getLauncherCommand(ActivityInfo launcher) { 117 return "cmd package set-home-activity " + 118 new ComponentName(launcher.packageName, launcher.name).flattenToString(); 119 } 120 121 /** 122 * Disables heads up notification for the duration of the test 123 */ 124 public static ShellCommandRule disableHeadsUpNotification() { 125 return new ShellCommandRule("settings put global heads_up_notifications_enabled 0", 126 "settings put global heads_up_notifications_enabled 1"); 127 } 128 } 129