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 com.android.providers.media;
18 
19 import static java.util.Objects.requireNonNull;
20 
21 import android.util.Pair;
22 
23 import androidx.annotation.NonNull;
24 import androidx.annotation.Nullable;
25 
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.concurrent.Executor;
31 
32 /**
33  * {@link ConfigStore} implementation that does not attempt to read the "real" configs (usually
34  * stored to {@link android.provider.DeviceConfig}) and instead simply returns default values.
35  */
36 public class TestConfigStore implements ConfigStore {
37     private boolean mCloudMediaInPhotoPickerEnabled = false;
38     private boolean mPrivateSpaceEnabled = false;
39 
40     private boolean mPickerChoiceManagedSelectionEnabled = false;
41     private List<String> mAllowedCloudProviderPackages = Collections.emptyList();
42     private @Nullable String mDefaultCloudProviderPackage = null;
43     private List<Pair<Executor, Runnable>> mObservers = new ArrayList<>();
44 
enableCloudMediaFeatureAndSetAllowedCloudProviderPackages(String... providers)45     public void enableCloudMediaFeatureAndSetAllowedCloudProviderPackages(String... providers) {
46         mAllowedCloudProviderPackages = Arrays.asList(providers);
47         mCloudMediaInPhotoPickerEnabled = true;
48         notifyObservers();
49     }
50 
51     /**
52      * Enables private space flag for PhotoPicker in test config
53      */
enablePrivateSpaceInPhotoPicker()54     public void enablePrivateSpaceInPhotoPicker() {
55         mPrivateSpaceEnabled = true;
56         notifyObservers();
57     }
58 
59     /**
60      * Disables private space flag for PhotoPicker in test config
61      */
disablePrivateSpaceInPhotoPicker()62     public void disablePrivateSpaceInPhotoPicker() {
63         mPrivateSpaceEnabled = false;
64         notifyObservers();
65     }
66     @Override
isPrivateSpaceInPhotoPickerEnabled()67    public boolean isPrivateSpaceInPhotoPickerEnabled() {
68         return mPrivateSpaceEnabled;
69     }
70 
enableCloudMediaFeature()71     public void enableCloudMediaFeature() {
72         mCloudMediaInPhotoPickerEnabled = true;
73         notifyObservers();
74     }
75 
clearAllowedCloudProviderPackagesAndDisableCloudMediaFeature()76     public void clearAllowedCloudProviderPackagesAndDisableCloudMediaFeature() {
77         mAllowedCloudProviderPackages = Collections.emptyList();
78         disableCloudMediaFeature();
79         notifyObservers();
80     }
81 
disableCloudMediaFeature()82     public void disableCloudMediaFeature() {
83         mCloudMediaInPhotoPickerEnabled = false;
84         notifyObservers();
85     }
86 
87     /**
88      * Enables pickerChoiceManagedSelection flag in the test config.
89      */
enablePickerChoiceManagedSelectionEnabled()90     public void enablePickerChoiceManagedSelectionEnabled() {
91         mPickerChoiceManagedSelectionEnabled = true;
92     }
93 
94     @Override
getAllowedCloudProviderPackages()95     public @NonNull List<String> getAllowedCloudProviderPackages() {
96         return mAllowedCloudProviderPackages;
97     }
98 
setAllowedCloudProviderPackages(String... providers)99     public void setAllowedCloudProviderPackages(String... providers) {
100         if (providers.length == 0) {
101             mAllowedCloudProviderPackages = Collections.emptyList();
102         } else {
103             mAllowedCloudProviderPackages = Arrays.asList(providers);
104         }
105         notifyObservers();
106     }
107 
108     @Override
isCloudMediaInPhotoPickerEnabled()109     public boolean isCloudMediaInPhotoPickerEnabled() {
110         return mCloudMediaInPhotoPickerEnabled && !mAllowedCloudProviderPackages.isEmpty();
111     }
112 
setDefaultCloudProviderPackage(@onNull String packageName)113     public void setDefaultCloudProviderPackage(@NonNull String packageName) {
114         requireNonNull(packageName, "null packageName is not allowed. "
115                 + "Consider clearDefaultCloudProviderPackage()");
116         mDefaultCloudProviderPackage = packageName;
117     }
118 
clearDefaultCloudProviderPackage()119     public void clearDefaultCloudProviderPackage() {
120         mDefaultCloudProviderPackage = null;
121     }
122 
123     @Nullable
124     @Override
getDefaultCloudProviderPackage()125     public String getDefaultCloudProviderPackage() {
126         return mDefaultCloudProviderPackage;
127     }
128 
129     @NonNull
130     @Override
getTranscodeCompatManifest()131     public List<String> getTranscodeCompatManifest() {
132         return Collections.emptyList();
133     }
134 
135     @NonNull
136     @Override
getTranscodeCompatStale()137     public List<String> getTranscodeCompatStale() {
138         return Collections.emptyList();
139     }
140 
141     @Override
isPickerChoiceManagedSelectionEnabled()142     public boolean isPickerChoiceManagedSelectionEnabled() {
143         return mPickerChoiceManagedSelectionEnabled;
144     }
145 
146     @Override
addOnChangeListener(@onNull Executor executor, @NonNull Runnable listener)147     public void addOnChangeListener(@NonNull Executor executor, @NonNull Runnable listener) {
148         Pair p = Pair.create(executor, listener);
149         mObservers.add(p);
150     }
151 
152 
153     /**
154      * Runs all subscribers to the TestConfigStore.
155      */
notifyObservers()156     private void notifyObservers() {
157         for (Pair<Executor, Runnable> observer: mObservers) {
158             // Run tasks in a synchronous manner to avoid test flakes.
159             observer.second.run();
160         }
161     }
162 }
163