1 /*
2  * Copyright (C) 2021 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.photopicker.cts;
18 
19 import static android.photopicker.cts.PhotoPickerCloudUtils.disableDeviceConfigSync;
20 
21 import android.app.Instrumentation;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.pm.PackageManager;
25 
26 import androidx.annotation.Nullable;
27 import androidx.test.InstrumentationRegistry;
28 import androidx.test.uiautomator.UiDevice;
29 
30 import org.junit.Assume;
31 import org.junit.Before;
32 
33 import java.io.IOException;
34 
35 /**
36  * Photo Picker Base class for Photo Picker tests. This includes common setup methods
37  * required for all Photo Picker tests.
38  */
39 public class PhotoPickerBaseTest {
40     private static final String TAG = "PhotoPickerBaseTest";
41     public static int REQUEST_CODE = 42;
42     protected static final String INVALID_CLOUD_PROVIDER = "Invalid";
43     private static final Instrumentation sInstrumentation =
44             InstrumentationRegistry.getInstrumentation();
45     public static final String sTargetPackageName =
46             sInstrumentation.getTargetContext().getPackageName();
47     protected static final UiDevice sDevice = UiDevice.getInstance(sInstrumentation);
48 
49     protected GetResultActivity mActivity;
50     protected Context mContext;
51 
52     // Do not use org.junit.BeforeClass (b/260380362) or
53     // com.android.bedstead.harrier.annotations.BeforeClass (b/246986339#comment18)
54     // when using DeviceState. Some subclasses of PhotoPickerBaseTest may use DeviceState so avoid
55     // adding either @BeforeClass methods here.
56 
57     @Before
setUp()58     public void setUp() throws Exception {
59         Assume.assumeTrue(isHardwareSupported());
60         disableDeviceConfigSync();
61 
62         final String setSyncDelayCommand =
63                 "device_config put storage pickerdb.default_sync_delay_ms 0";
64         sDevice.executeShellCommand(setSyncDelayCommand);
65 
66         mContext = sInstrumentation.getContext();
67         final Intent intent = new Intent(mContext, GetResultActivity.class);
68         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
69 
70         // Wake up the device and dismiss the keyguard before the test starts
71         sDevice.executeShellCommand("input keyevent KEYCODE_WAKEUP");
72         sDevice.executeShellCommand("wm dismiss-keyguard");
73 
74         mActivity = (GetResultActivity) sInstrumentation.startActivitySync(intent);
75         // Wait for the UI Thread to become idle.
76         sInstrumentation.waitForIdleSync();
77         mActivity.clearResult();
78         sDevice.waitForIdle();
79     }
80 
isHardwareSupported()81     static boolean isHardwareSupported() {
82         // These UI tests are not optimised for Watches, TVs, Auto;
83         // IoT devices do not have a UI to run these UI tests
84         PackageManager pm = sInstrumentation.getContext().getPackageManager();
85         return !pm.hasSystemFeature(pm.FEATURE_EMBEDDED)
86                 && !pm.hasSystemFeature(pm.FEATURE_WATCH)
87                 && !pm.hasSystemFeature(pm.FEATURE_LEANBACK);
88     }
89 
setCloudProvider(@ullable String authority)90     protected static void setCloudProvider(@Nullable String authority) throws Exception {
91         PhotoPickerCloudUtils.setCloudProvider(sDevice, authority);
92     }
93 
getCurrentCloudProvider()94     protected static String getCurrentCloudProvider() throws IOException {
95         return PhotoPickerCloudUtils.getCurrentCloudProvider(sDevice);
96     }
97 }
98 
99