1818
1919import static android .photopicker .cts .PhotoPickerCloudUtils .addImage ;
2020import static android .photopicker .cts .PhotoPickerCloudUtils .containsExcept ;
21+ import static android .photopicker .cts .PhotoPickerCloudUtils .disableCloudMediaAndClearAllowedCloudProviders ;
2122import static android .photopicker .cts .PhotoPickerCloudUtils .enableCloudMediaAndSetAllowedCloudProviders ;
2223import static android .photopicker .cts .PhotoPickerCloudUtils .extractMediaIds ;
24+ import static android .photopicker .cts .PhotoPickerCloudUtils .getAllowedProvidersDeviceConfig ;
25+ import static android .photopicker .cts .PhotoPickerCloudUtils .isCloudMediaEnabled ;
2326import static android .photopicker .cts .PickerProviderMediaGenerator .MediaGenerator ;
24- import static android .photopicker .cts .PickerProviderMediaGenerator .setCloudProvider ;
2527import static android .photopicker .cts .PickerProviderMediaGenerator .syncCloudProvider ;
2628import static android .photopicker .cts .util .PhotoPickerFilesUtils .createImagesAndGetUris ;
2729import static android .photopicker .cts .util .PhotoPickerFilesUtils .deleteMedia ;
4446import android .photopicker .cts .cloudproviders .CloudProviderPrimary ;
4547import android .photopicker .cts .cloudproviders .CloudProviderSecondary ;
4648import android .provider .MediaStore ;
49+ import android .util .Log ;
4750import android .util .Pair ;
4851
52+ import androidx .annotation .Nullable ;
4953import androidx .test .filters .SdkSuppress ;
5054import androidx .test .runner .AndroidJUnit4 ;
5155
5256import org .junit .After ;
57+ import org .junit .AfterClass ;
5358import org .junit .Before ;
59+ import org .junit .BeforeClass ;
5460import org .junit .Test ;
5561import org .junit .runner .RunWith ;
5662
5763import java .io .File ;
64+ import java .io .IOException ;
5865import java .util .ArrayList ;
5966import java .util .Collections ;
6067import java .util .List ;
6673@ RunWith (AndroidJUnit4 .class )
6774@ SdkSuppress (minSdkVersion = Build .VERSION_CODES .S )
6875public class CloudPhotoPickerTest extends PhotoPickerBaseTest {
76+ private static final String TAG = CloudPhotoPickerTest .class .getSimpleName ();
6977 private final List <Uri > mUriList = new ArrayList <>();
7078 private MediaGenerator mCloudPrimaryMediaGenerator ;
7179 private MediaGenerator mCloudSecondaryMediaGenerator ;
@@ -77,7 +85,41 @@ public class CloudPhotoPickerTest extends PhotoPickerBaseTest {
7785
7886 private static final String CLOUD_ID1 = "CLOUD_ID1" ;
7987 private static final String CLOUD_ID2 = "CLOUD_ID2" ;
88+ private static boolean sCloudMediaPreviouslyEnabled ;
89+ private static String sPreviouslyAllowedCloudProviders ;
90+ @ Nullable
91+ private static String sPreviouslySetCloudProvider ;
92+
93+ @ BeforeClass
94+ public static void setUpBeforeClass () throws IOException {
95+ // Store the current CMP configs, so that we can reset them at the end of the test.
96+ sCloudMediaPreviouslyEnabled = isCloudMediaEnabled ();
97+ if (sCloudMediaPreviouslyEnabled ) {
98+ sPreviouslyAllowedCloudProviders = getAllowedProvidersDeviceConfig ();
99+ }
100+
101+ try {
102+ sPreviouslySetCloudProvider = getCurrentCloudProvider ();
103+ } catch (RuntimeException e ) {
104+ Log .e (TAG , "Could not get previously set cloud provider" , e );
105+ sPreviouslySetCloudProvider = INVALID_CLOUD_PROVIDER ;
106+ }
80107
108+ // This is a self-instrumentation test, so both "target" package name and "own" package name
109+ // should be the same (android.photopicker.cts).
110+ enableCloudMediaAndSetAllowedCloudProviders (sTargetPackageName );
111+ }
112+
113+ @ AfterClass
114+ public static void tearDownClass () throws Exception {
115+ // Reset CloudMedia configs.
116+ if (sCloudMediaPreviouslyEnabled ) {
117+ enableCloudMediaAndSetAllowedCloudProviders (sPreviouslyAllowedCloudProviders );
118+ } else {
119+ disableCloudMediaAndClearAllowedCloudProviders ();
120+ }
121+ setCloudProvider (sPreviouslySetCloudProvider );
122+ }
81123 @ Before
82124 public void setUp () throws Exception {
83125 super .setUp ();
@@ -93,10 +135,7 @@ public void setUp() throws Exception {
93135 mCloudPrimaryMediaGenerator .setMediaCollectionId (COLLECTION_1 );
94136 mCloudSecondaryMediaGenerator .setMediaCollectionId (COLLECTION_1 );
95137
96- // This is a self-instrumentation test, so both "target" package name and "own" package name
97- // should be the same (android.photopicker.cts).
98- enableCloudMediaAndSetAllowedCloudProviders (sTargetPackageName );
99- setCloudProvider (mContext , null );
138+ setCloudProvider (null );
100139 }
101140
102141 @ After
@@ -109,7 +148,7 @@ public void tearDown() throws Exception {
109148 }
110149 mUriList .clear ();
111150 if (mCloudPrimaryMediaGenerator != null ) {
112- setCloudProvider (mContext , null );
151+ setCloudProvider (null );
113152 }
114153 }
115154
@@ -209,7 +248,7 @@ public void testSupportedProviders() throws Exception {
209248
210249 @ Test
211250 public void testProviderSwitchSuccess () throws Exception {
212- setCloudProvider (mContext , CloudProviderPrimary .AUTHORITY );
251+ setCloudProvider (CloudProviderPrimary .AUTHORITY );
213252 assertThat (MediaStore .isCurrentCloudMediaProviderAuthority (mContext .getContentResolver (),
214253 CloudProviderPrimary .AUTHORITY )).isTrue ();
215254
@@ -223,7 +262,7 @@ public void testProviderSwitchSuccess() throws Exception {
223262
224263 containsExcept (mediaIds , CLOUD_ID1 , CLOUD_ID2 );
225264
226- setCloudProvider (mContext , CloudProviderSecondary .AUTHORITY );
265+ setCloudProvider (CloudProviderSecondary .AUTHORITY );
227266 assertThat (MediaStore .isCurrentCloudMediaProviderAuthority (mContext .getContentResolver (),
228267 CloudProviderPrimary .AUTHORITY )).isFalse ();
229268
@@ -235,11 +274,11 @@ public void testProviderSwitchSuccess() throws Exception {
235274
236275 @ Test
237276 public void testProviderSwitchFailure () throws Exception {
238- setCloudProvider (mContext , CloudProviderNoIntentFilter .AUTHORITY );
277+ setCloudProvider (CloudProviderNoIntentFilter .AUTHORITY );
239278 assertThat (MediaStore .isCurrentCloudMediaProviderAuthority (mContext .getContentResolver (),
240279 CloudProviderPrimary .AUTHORITY )).isFalse ();
241280
242- setCloudProvider (mContext , CloudProviderNoPermission .AUTHORITY );
281+ setCloudProvider (CloudProviderNoPermission .AUTHORITY );
243282 assertThat (MediaStore .isCurrentCloudMediaProviderAuthority (mContext .getContentResolver (),
244283 CloudProviderPrimary .AUTHORITY )).isFalse ();
245284 }
@@ -326,7 +365,7 @@ public void testCloudEventNotification() throws Exception {
326365 assertThat (mediaIds ).containsNoneIn (Collections .singletonList (CLOUD_ID1 ));
327366
328367 // Now set the cloud provider and verify that notification succeeds
329- setCloudProvider (mContext , CloudProviderPrimary .AUTHORITY );
368+ setCloudProvider (CloudProviderPrimary .AUTHORITY );
330369 assertThat (MediaStore .isCurrentCloudMediaProviderAuthority (mContext .getContentResolver (),
331370 CloudProviderPrimary .AUTHORITY )).isTrue ();
332371
@@ -348,18 +387,18 @@ public void testCloudEventNotification() throws Exception {
348387
349388 @ Test
350389 @ SdkSuppress (minSdkVersion = Build .VERSION_CODES .TIRAMISU )
351- public void testStorageManagerKnowsCloudProvider () {
390+ public void testStorageManagerKnowsCloudProvider () throws Exception {
352391 final StorageManager storageManager = mContext .getSystemService (StorageManager .class );
353392
354- setCloudProvider (mContext , CloudProviderPrimary .AUTHORITY );
393+ setCloudProvider (CloudProviderPrimary .AUTHORITY );
355394 assertThat (storageManager .getCloudMediaProvider ())
356395 .isEqualTo (CloudProviderPrimary .AUTHORITY );
357396
358- setCloudProvider (mContext , CloudProviderSecondary .AUTHORITY );
397+ setCloudProvider (CloudProviderSecondary .AUTHORITY );
359398 assertThat (storageManager .getCloudMediaProvider ())
360399 .isEqualTo (CloudProviderSecondary .AUTHORITY );
361400
362- setCloudProvider (mContext , null );
401+ setCloudProvider (null );
363402 assertThat (storageManager .getCloudMediaProvider ()).isNull ();
364403 }
365404
0 commit comments