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