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 ;
4648import android .provider .MediaStore ;
4749import android .util .Pair ;
4850
51+ import androidx .annotation .Nullable ;
4952import androidx .test .filters .SdkSuppress ;
5053import androidx .test .runner .AndroidJUnit4 ;
5154
5255import org .junit .After ;
56+ import org .junit .AfterClass ;
5357import org .junit .Before ;
58+ import org .junit .BeforeClass ;
5459import org .junit .Test ;
5560import org .junit .runner .RunWith ;
5661
5762import java .io .File ;
63+ import java .io .IOException ;
5864import java .util .ArrayList ;
5965import java .util .Collections ;
6066import java .util .List ;
@@ -77,7 +83,35 @@ public class CloudPhotoPickerTest extends PhotoPickerBaseTest {
7783
7884 private static final String CLOUD_ID1 = "CLOUD_ID1" ;
7985 private static final String CLOUD_ID2 = "CLOUD_ID2" ;
86+ private static boolean sCloudMediaPreviouslyEnabled ;
87+ private static String sPreviouslyAllowedCloudProviders ;
88+ @ Nullable
89+ private static String sPreviouslySetCloudProvider ;
90+
91+ @ BeforeClass
92+ public static void setUpBeforeClass () throws IOException {
93+ // Store the current CMP configs, so that we can reset them at the end of the test.
94+ sCloudMediaPreviouslyEnabled = isCloudMediaEnabled ();
95+ if (sCloudMediaPreviouslyEnabled ) {
96+ sPreviouslyAllowedCloudProviders = getAllowedProvidersDeviceConfig ();
97+ }
98+ sPreviouslySetCloudProvider = getCurrentCloudProvider ();
99+
100+ // This is a self-instrumentation test, so both "target" package name and "own" package name
101+ // should be the same (android.photopicker.cts).
102+ enableCloudMediaAndSetAllowedCloudProviders (sTargetPackageName );
103+ }
80104
105+ @ AfterClass
106+ public static void tearDownClass () throws Exception {
107+ // Reset CloudMedia configs.
108+ if (sCloudMediaPreviouslyEnabled ) {
109+ enableCloudMediaAndSetAllowedCloudProviders (sPreviouslyAllowedCloudProviders );
110+ } else {
111+ disableCloudMediaAndClearAllowedCloudProviders ();
112+ }
113+ setCloudProvider (sPreviouslySetCloudProvider );
114+ }
81115 @ Before
82116 public void setUp () throws Exception {
83117 super .setUp ();
@@ -93,10 +127,7 @@ public void setUp() throws Exception {
93127 mCloudPrimaryMediaGenerator .setMediaCollectionId (COLLECTION_1 );
94128 mCloudSecondaryMediaGenerator .setMediaCollectionId (COLLECTION_1 );
95129
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 );
130+ setCloudProvider (null );
100131 }
101132
102133 @ After
@@ -109,7 +140,7 @@ public void tearDown() throws Exception {
109140 }
110141 mUriList .clear ();
111142 if (mCloudPrimaryMediaGenerator != null ) {
112- setCloudProvider (mContext , null );
143+ setCloudProvider (null );
113144 }
114145 }
115146
@@ -209,7 +240,7 @@ public void testSupportedProviders() throws Exception {
209240
210241 @ Test
211242 public void testProviderSwitchSuccess () throws Exception {
212- setCloudProvider (mContext , CloudProviderPrimary .AUTHORITY );
243+ setCloudProvider (CloudProviderPrimary .AUTHORITY );
213244 assertThat (MediaStore .isCurrentCloudMediaProviderAuthority (mContext .getContentResolver (),
214245 CloudProviderPrimary .AUTHORITY )).isTrue ();
215246
@@ -223,7 +254,7 @@ public void testProviderSwitchSuccess() throws Exception {
223254
224255 containsExcept (mediaIds , CLOUD_ID1 , CLOUD_ID2 );
225256
226- setCloudProvider (mContext , CloudProviderSecondary .AUTHORITY );
257+ setCloudProvider (CloudProviderSecondary .AUTHORITY );
227258 assertThat (MediaStore .isCurrentCloudMediaProviderAuthority (mContext .getContentResolver (),
228259 CloudProviderPrimary .AUTHORITY )).isFalse ();
229260
@@ -235,11 +266,11 @@ public void testProviderSwitchSuccess() throws Exception {
235266
236267 @ Test
237268 public void testProviderSwitchFailure () throws Exception {
238- setCloudProvider (mContext , CloudProviderNoIntentFilter .AUTHORITY );
269+ setCloudProvider (CloudProviderNoIntentFilter .AUTHORITY );
239270 assertThat (MediaStore .isCurrentCloudMediaProviderAuthority (mContext .getContentResolver (),
240271 CloudProviderPrimary .AUTHORITY )).isFalse ();
241272
242- setCloudProvider (mContext , CloudProviderNoPermission .AUTHORITY );
273+ setCloudProvider (CloudProviderNoPermission .AUTHORITY );
243274 assertThat (MediaStore .isCurrentCloudMediaProviderAuthority (mContext .getContentResolver (),
244275 CloudProviderPrimary .AUTHORITY )).isFalse ();
245276 }
@@ -326,7 +357,7 @@ public void testCloudEventNotification() throws Exception {
326357 assertThat (mediaIds ).containsNoneIn (Collections .singletonList (CLOUD_ID1 ));
327358
328359 // Now set the cloud provider and verify that notification succeeds
329- setCloudProvider (mContext , CloudProviderPrimary .AUTHORITY );
360+ setCloudProvider (CloudProviderPrimary .AUTHORITY );
330361 assertThat (MediaStore .isCurrentCloudMediaProviderAuthority (mContext .getContentResolver (),
331362 CloudProviderPrimary .AUTHORITY )).isTrue ();
332363
@@ -348,18 +379,18 @@ public void testCloudEventNotification() throws Exception {
348379
349380 @ Test
350381 @ SdkSuppress (minSdkVersion = Build .VERSION_CODES .TIRAMISU )
351- public void testStorageManagerKnowsCloudProvider () {
382+ public void testStorageManagerKnowsCloudProvider () throws Exception {
352383 final StorageManager storageManager = mContext .getSystemService (StorageManager .class );
353384
354- setCloudProvider (mContext , CloudProviderPrimary .AUTHORITY );
385+ setCloudProvider (CloudProviderPrimary .AUTHORITY );
355386 assertThat (storageManager .getCloudMediaProvider ())
356387 .isEqualTo (CloudProviderPrimary .AUTHORITY );
357388
358- setCloudProvider (mContext , CloudProviderSecondary .AUTHORITY );
389+ setCloudProvider (CloudProviderSecondary .AUTHORITY );
359390 assertThat (storageManager .getCloudMediaProvider ())
360391 .isEqualTo (CloudProviderSecondary .AUTHORITY );
361392
362- setCloudProvider (mContext , null );
393+ setCloudProvider (null );
363394 assertThat (storageManager .getCloudMediaProvider ()).isNull ();
364395 }
365396
0 commit comments