1 /*
2  * Copyright (C) 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.photopicker.cts;
18 
19 import static android.photopicker.cts.PhotoPickerBaseTest.INVALID_CLOUD_PROVIDER;
20 import static android.photopicker.cts.PhotoPickerCloudUtils.NAMESPACE_MEDIAPROVIDER;
21 import static android.photopicker.cts.PhotoPickerCloudUtils.NAMESPACE_STORAGE_NATIVE_BOOT;
22 import static android.photopicker.cts.PhotoPickerCloudUtils.disableCloudMediaAndClearAllowedCloudProviders;
23 import static android.photopicker.cts.PhotoPickerCloudUtils.enableCloudMediaAndSetAllowedCloudProviders;
24 import static android.photopicker.cts.PhotoPickerCloudUtils.getAllowedProvidersDeviceConfig;
25 import static android.photopicker.cts.PhotoPickerCloudUtils.getCurrentCloudProvider;
26 import static android.photopicker.cts.PhotoPickerCloudUtils.isCloudMediaEnabled;
27 import static android.photopicker.cts.PhotoPickerCloudUtils.setCloudProvider;
28 
29 import android.util.Log;
30 
31 import androidx.annotation.NonNull;
32 import androidx.annotation.Nullable;
33 import androidx.test.uiautomator.UiDevice;
34 
35 import java.io.IOException;
36 
37 /**
38  * Provides helper functions to save current device state and restore the state after running tests.
39  */
40 public class DeviceStatePreserver {
41     private static final String TAG = DeviceStatePreserver.class.getSimpleName();
42 
43     private UiDevice mUiDevice;
44 
45     // Previous state in storage_native_boot namespace
46     private static boolean sIsCloudEnabledStorageNativeBoot;
47     @Nullable
48     private static String sAllowedCloudProvidersStorageNativeBoot;
49 
50     // Previous state in mediaprovider namespace
51     private static boolean sIsCloudEnabledMediaProvider;
52     @Nullable
53     private static String sAllowedCloudProvidersMediaProvider;
54 
55     @Nullable
56     private static String sPreviouslySetCloudProvider;
57 
DeviceStatePreserver(@onNull UiDevice device)58     public DeviceStatePreserver(@NonNull UiDevice device) {
59         mUiDevice = device;
60     }
61 
62     /**
63      * Saves current cloud provider related device config flags from
64      * {@link PhotoPickerCloudUtils#NAMESPACE_STORAGE_NATIVE_BOOT} and
65      * {@link PhotoPickerCloudUtils#NAMESPACE_MEDIAPROVIDER}.
66      * Also saves the current cloud provider.
67      */
saveCurrentCloudProviderState()68     public void saveCurrentCloudProviderState() throws IOException {
69         // Save flags in storage_native_boot namespace
70         sIsCloudEnabledStorageNativeBoot = isCloudMediaEnabled(NAMESPACE_STORAGE_NATIVE_BOOT);
71         if (sIsCloudEnabledStorageNativeBoot) {
72             sAllowedCloudProvidersStorageNativeBoot =
73                     getAllowedProvidersDeviceConfig(NAMESPACE_STORAGE_NATIVE_BOOT);
74         }
75 
76         // Save flags in mediaprovider namespace
77         sIsCloudEnabledMediaProvider = isCloudMediaEnabled(NAMESPACE_MEDIAPROVIDER);
78         if (sIsCloudEnabledMediaProvider) {
79             sAllowedCloudProvidersMediaProvider =
80                     getAllowedProvidersDeviceConfig(NAMESPACE_MEDIAPROVIDER);
81         }
82 
83         // Save current cloud provider.
84         try {
85             sPreviouslySetCloudProvider = getCurrentCloudProvider(mUiDevice);
86         } catch (RuntimeException e) {
87             Log.e(TAG, "Could not get previously set cloud provider", e);
88             sPreviouslySetCloudProvider = INVALID_CLOUD_PROVIDER;
89         }
90     }
91 
92     /**
93      * Restores the current cloud provider state previously saved using
94      * {@link DeviceStatePreserver#saveCurrentCloudProviderState()}.
95      */
restoreCloudProviderState()96     public void restoreCloudProviderState() throws Exception {
97         // Restore flags in storage_native_boot namespace
98         if (sIsCloudEnabledStorageNativeBoot) {
99             enableCloudMediaAndSetAllowedCloudProviders(
100                     NAMESPACE_STORAGE_NATIVE_BOOT, sAllowedCloudProvidersStorageNativeBoot);
101         } else {
102             disableCloudMediaAndClearAllowedCloudProviders(NAMESPACE_STORAGE_NATIVE_BOOT);
103         }
104 
105         // Restore flags in mediaprovider namespace
106         if (sIsCloudEnabledMediaProvider) {
107             enableCloudMediaAndSetAllowedCloudProviders(
108                     NAMESPACE_MEDIAPROVIDER, sAllowedCloudProvidersMediaProvider);
109         } else {
110             disableCloudMediaAndClearAllowedCloudProviders(NAMESPACE_MEDIAPROVIDER);
111         }
112 
113         // Restore previously set cloud provider.
114         setCloudProvider(mUiDevice, sPreviouslySetCloudProvider);
115     }
116 }
117