1 /* 2 * Copyright (C) 2022 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 17 package android.server.wm; 18 19 import android.app.Activity; 20 import android.app.ActivityOptions; 21 import android.app.WindowConfiguration; 22 import android.content.Intent; 23 import android.util.Log; 24 import android.util.Pair; 25 26 import androidx.test.platform.app.InstrumentationRegistry; 27 28 import com.android.compatibility.common.util.PackageUtil; 29 import com.android.compatibility.common.util.SystemUtil; 30 31 import org.junit.rules.ExternalResource; 32 import org.junit.rules.TestRule; 33 import org.junit.runner.Description; 34 import org.junit.runners.model.Statement; 35 36 /** 37 * Rule for using setRequestedOrientation API in tests. 38 */ 39 public class SetRequestedOrientationRule implements TestRule { 40 @Override apply(Statement base, Description description)41 public Statement apply(Statement base, 42 Description description) { 43 return new Statement() { 44 @Override 45 public void evaluate() throws Throwable { 46 final DisableFixedToUserRotationRule mDisableFixedToUserRotationRule = 47 new DisableFixedToUserRotationRule(); 48 mDisableFixedToUserRotationRule.before(); 49 try { 50 try (IgnoreOrientationRequestSession session = 51 new IgnoreOrientationRequestSession(false /* don' ignore */)) { 52 base.evaluate(); 53 } 54 } finally { 55 mDisableFixedToUserRotationRule.after(); 56 } 57 } 58 }; 59 } 60 61 /** 62 * Builds launch args to launch Activity in fullscreen windowing mode. 63 * 64 * Activities which request orientation need to be in fullscreen windowing mode. 65 */ 66 public static <T extends Activity> Pair<Intent, ActivityOptions> buildFullScreenLaunchArgs( 67 Class<T> klass) { 68 final Intent intent = new Intent( 69 InstrumentationRegistry.getInstrumentation().getTargetContext(), 70 klass); 71 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 72 73 final ActivityOptions options = ActivityOptions.makeBasic(); 74 options.setLaunchWindowingMode(WindowConfiguration.WINDOWING_MODE_FULLSCREEN); 75 76 return Pair.create(intent, options); 77 } 78 79 public static class DisableFixedToUserRotationRule extends ExternalResource { 80 private static final String TAG = "DisableFixToUserRotationRule"; 81 private static final String COMMAND = "cmd window fixed-to-user-rotation "; 82 83 private final boolean mSupportsRotation; 84 85 private String mOriginalValue; 86 87 public DisableFixedToUserRotationRule() { 88 mSupportsRotation = PackageUtil.supportsRotation(); 89 } 90 91 @Override 92 protected void before() throws Throwable { 93 if (!mSupportsRotation) { 94 return; 95 } 96 mOriginalValue = SystemUtil.runShellCommand(COMMAND); 97 executeShellCommandAndPrint(COMMAND + "disabled"); 98 } 99 100 @Override 101 protected void after() { 102 if (!mSupportsRotation) { 103 return; 104 } 105 executeShellCommandAndPrint(COMMAND + mOriginalValue); 106 } 107 108 private void executeShellCommandAndPrint(String cmd) { 109 Log.i(TAG, "Command: " + cmd + " Output: " + SystemUtil.runShellCommand(cmd)); 110 } 111 } 112 } 113