1 /*
2  * Copyright 2023 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.hardware.camera2.cts;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 
23 import android.Manifest;
24 import android.app.Instrumentation;
25 import android.content.Context;
26 import android.content.pm.PackageManager;
27 import android.hardware.camera2.CameraManager;
28 import android.os.SystemProperties;
29 import android.platform.test.annotations.AppModeFull;
30 import android.util.Log;
31 
32 import androidx.test.InstrumentationRegistry;
33 import androidx.test.runner.AndroidJUnit4;
34 
35 import com.android.cts.install.lib.Install;
36 import com.android.cts.install.lib.TestApp;
37 import com.android.cts.install.lib.Uninstall;
38 import com.android.window.flags.Flags;
39 
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 
46 /**
47  * Test AndroidManifest properties.
48  */
49 @RunWith(AndroidJUnit4.class)
50 public class PropertyTest {
51     private static PackageManager sPackageManager;
52     private static final String TAG = "PropertyTest";
53     private static final String PROPERTY_APP1_PACKAGE_NAME =
54             "com.android.camera2.cts.propertytestapp1";
55     private static final String PROPERTY_APP2_PACKAGE_NAME =
56             "com.android.camera2.cts.propertytestapp2";
57     private static final TestApp PROPERTY_APP1 =
58             new TestApp("CameraCtsPropertyTestApp1", PROPERTY_APP1_PACKAGE_NAME, 30,
59                     false, "CameraCtsPropertyTestApp1.apk");
60     private static final TestApp PROPERTY_APP2 =
61             new TestApp("CameraCtsPropertyTestApp2", PROPERTY_APP2_PACKAGE_NAME, 30,
62                     false, "CameraCtsPropertyTestApp2.apk");
63 
64     private CameraManager mCameraManager;
65     private Instrumentation mInstrumentation;
66     private Context mContext;
67     private PackageManager mPackageManager;
68 
adoptShellPermissions()69     private static void adoptShellPermissions() {
70         InstrumentationRegistry
71                 .getInstrumentation()
72                 .getUiAutomation()
73                 .adoptShellPermissionIdentity(
74                         Manifest.permission.INSTALL_PACKAGES, Manifest.permission.DELETE_PACKAGES);
75     }
76 
dropShellPermissions()77     private static void dropShellPermissions() {
78         InstrumentationRegistry
79                 .getInstrumentation()
80                 .getUiAutomation()
81                 .dropShellPermissionIdentity();
82     }
83 
84     @BeforeClass
setupClass()85     public static void setupClass() throws Exception {
86         sPackageManager = InstrumentationRegistry
87                 .getInstrumentation()
88                 .getContext()
89                 .getPackageManager();
90         adoptShellPermissions();
91         Uninstall.packages(PROPERTY_APP1_PACKAGE_NAME);
92         Uninstall.packages(PROPERTY_APP2_PACKAGE_NAME);
93         Install.single(PROPERTY_APP1).commit();
94         Install.single(PROPERTY_APP2).commit();
95         dropShellPermissions();
96     }
97 
98     @AfterClass
teardownClass()99     public static void teardownClass() throws Exception {
100         adoptShellPermissions();
101         Uninstall.packages(PROPERTY_APP1_PACKAGE_NAME);
102         Uninstall.packages(PROPERTY_APP2_PACKAGE_NAME);
103         dropShellPermissions();
104     }
105 
106     @Before
setup()107     public void setup() throws Exception {
108         mInstrumentation = InstrumentationRegistry.getInstrumentation();
109         mContext = mInstrumentation.getContext();
110         mPackageManager = mContext.getPackageManager();
111     }
112 
113     @Test
114     @AppModeFull
testLandscapeToPortraitEnabled()115     public void testLandscapeToPortraitEnabled() {
116         if (SystemProperties.getBoolean(CameraManager.LANDSCAPE_TO_PORTRAIT_PROP, false)) {
117             if (Flags.cameraCompatForFreeform()) {
118                 // `App1` has the override enabled.
119                 Log.i(TAG, "System property enabled, testing getRotationOverride");
120                 assertEquals("getRotationOverride should return"
121                                 + " ROTATION_OVERRIDE_OVERRIDE_TO_PORTRAIT",
122                         CameraManager.getRotationOverrideInternal(mContext, mPackageManager,
123                                 PROPERTY_APP1_PACKAGE_NAME),
124                         CameraManager.ROTATION_OVERRIDE_OVERRIDE_TO_PORTRAIT);
125             } else {
126                 // `App1` has the override enabled.
127                 Log.i(TAG, "System property enabled, testing shouldOverrideToPortrait");
128                 assertTrue("shouldOverrideToPortrait should return true",
129                         CameraManager.shouldOverrideToPortrait(mPackageManager,
130                                 PROPERTY_APP1_PACKAGE_NAME));
131             }
132         } else {
133             Log.i(TAG, "LANDSCAPE_TO_PORTRAIT_PROP System property disabled.");
134         }
135     }
136 
137     @Test
138     @AppModeFull
testLandscapeToPortraitDisabled()139     public void testLandscapeToPortraitDisabled() {
140         if (SystemProperties.getBoolean(CameraManager.LANDSCAPE_TO_PORTRAIT_PROP, false)) {
141             if (Flags.cameraCompatForFreeform()) {
142                 // `App2` has the override disabled.
143                 Log.i(TAG, "System property enabled, testing getRotationOverride");
144                 assertEquals("getRotationOverride should return ROTATION_OVERRIDE_NONE",
145                         CameraManager.getRotationOverrideInternal(mContext, mPackageManager,
146                                 PROPERTY_APP2_PACKAGE_NAME), CameraManager.ROTATION_OVERRIDE_NONE);
147             } else {
148                 // `App2` has the override disabled.
149                 Log.i(TAG, "System property enabled, testing shouldOverrideToPortrait");
150                 assertFalse("shouldOverrideToPortrait should return false",
151                         CameraManager.shouldOverrideToPortrait(mPackageManager,
152                                 PROPERTY_APP2_PACKAGE_NAME));
153             }
154         } else {
155             Log.i(TAG, "LANDSCAPE_TO_PORTRAIT_PROP System property enabled.");
156         }
157     }
158 }
159