Skip to content

Commit 9edffd8

Browse files
Treehugger RobotGerrit Code Review
authored andcommitted
Merge changes from topic "cherrypicker-L24800030000105414:N08800030001900878" into android14-tests-dev
* changes: Make CtsPhotoPickerTests backward compatible Add --user to content commands Recreate setCloudProvider() funtion inorder to pass permission checks before updating cloud Provider Recreate setCloudProvider() funtion inorder to pass permission checks before updating cloud Provider
2 parents 753c801 + a5fe0d8 commit 9edffd8

6 files changed

Lines changed: 196 additions & 41 deletions

File tree

tests/PhotoPicker/src/android/photopicker/cts/ActionUserSelectImagesForAppTest.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import static android.photopicker.cts.PhotoPickerCloudUtils.isCloudMediaEnabled;
2727
import static android.photopicker.cts.PhotoPickerCloudUtils.selectAndAddPickerMedia;
2828
import static android.photopicker.cts.PickerProviderMediaGenerator.getMediaGenerator;
29-
import static android.photopicker.cts.PickerProviderMediaGenerator.setCloudProvider;
3029
import static android.photopicker.cts.util.PhotoPickerFilesUtils.createImagesAndGetUris;
3130

3231
import static com.android.compatibility.common.util.SystemUtil.runWithShellPermissionIdentity;
@@ -49,6 +48,7 @@
4948
import android.photopicker.cts.util.PhotoPickerFilesUtils;
5049
import android.photopicker.cts.util.UiAssertionUtils;
5150
import android.provider.MediaStore;
51+
import android.util.Log;
5252
import android.util.Pair;
5353

5454
import androidx.annotation.Nullable;
@@ -61,41 +61,54 @@
6161
import org.junit.Test;
6262
import org.junit.runner.RunWith;
6363

64+
import java.io.IOException;
6465
import java.util.ArrayList;
6566
import java.util.List;
6667

6768
/** PhotoPicker tests for {@link MediaStore#ACTION_USER_SELECT_IMAGES_FOR_APP} intent. */
6869
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.UPSIDE_DOWN_CAKE, codeName = "UpsideDownCake")
6970
@RunWith(AndroidJUnit4.class)
7071
public class ActionUserSelectImagesForAppTest extends PhotoPickerBaseTest {
71-
72+
private static final String TAG = ActionUserSelectImagesForAppTest.class.getSimpleName();
7273
private static boolean sCloudMediaPreviouslyEnabled;
7374
@Nullable
7475
private static String sPreviouslyAllowedCloudProviders;
76+
@Nullable
77+
private static String sPreviouslySetCloudProvider;
7578

7679
@BeforeClass
77-
public static void setUpClass() {
80+
public static void setUpClass() throws IOException {
7881
// Store the current Cloud-Media feature configs which we will override during the test,
7982
// and will need to restore after the test finished.
8083
sCloudMediaPreviouslyEnabled = isCloudMediaEnabled();
8184
if (sCloudMediaPreviouslyEnabled) {
8285
sPreviouslyAllowedCloudProviders = getAllowedProvidersDeviceConfig();
8386
}
87+
sPreviouslySetCloudProvider = getCurrentCloudProvider();
88+
89+
try {
90+
sPreviouslySetCloudProvider = getCurrentCloudProvider();
91+
} catch (RuntimeException e) {
92+
Log.e(TAG, "Could not get previously set cloud provider", e);
93+
sPreviouslySetCloudProvider = INVALID_CLOUD_PROVIDER;
94+
}
8495

8596
// Override the allowed cloud providers config to enable the banners
8697
// (this is a self-instrumenting test, so "target" package name and "own" package name are
8798
// same: android.photopicker.cts).
8899
enableCloudMediaAndSetAllowedCloudProviders(sTargetPackageName);
100+
89101
}
90102

91103
@AfterClass
92-
public static void tearDownClass() {
104+
public static void tearDownClass() throws Exception {
93105
// Restore Cloud-Media feature configs.
94106
if (sCloudMediaPreviouslyEnabled) {
95107
enableCloudMediaAndSetAllowedCloudProviders(sPreviouslyAllowedCloudProviders);
96108
} else {
97109
disableCloudMediaAndClearAllowedCloudProviders();
98110
}
111+
setCloudProvider(sPreviouslySetCloudProvider);
99112
}
100113

101114
@After
@@ -212,7 +225,7 @@ public void testNoCloudContent() throws Exception {
212225
PhotoPickerFilesUtils.deleteMedia(uri, mContext);
213226
}
214227
uriList.clear();
215-
setCloudProvider(mContext, null);
228+
setCloudProvider(null);
216229
}
217230
}
218231

tests/PhotoPicker/src/android/photopicker/cts/CloudPhotoPickerTest.java

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818

1919
import static android.photopicker.cts.PhotoPickerCloudUtils.addImage;
2020
import static android.photopicker.cts.PhotoPickerCloudUtils.containsExcept;
21+
import static android.photopicker.cts.PhotoPickerCloudUtils.disableCloudMediaAndClearAllowedCloudProviders;
2122
import static android.photopicker.cts.PhotoPickerCloudUtils.enableCloudMediaAndSetAllowedCloudProviders;
2223
import static android.photopicker.cts.PhotoPickerCloudUtils.extractMediaIds;
24+
import static android.photopicker.cts.PhotoPickerCloudUtils.getAllowedProvidersDeviceConfig;
25+
import static android.photopicker.cts.PhotoPickerCloudUtils.isCloudMediaEnabled;
2326
import static android.photopicker.cts.PickerProviderMediaGenerator.MediaGenerator;
24-
import static android.photopicker.cts.PickerProviderMediaGenerator.setCloudProvider;
2527
import static android.photopicker.cts.PickerProviderMediaGenerator.syncCloudProvider;
2628
import static android.photopicker.cts.util.PhotoPickerFilesUtils.createImagesAndGetUris;
2729
import static android.photopicker.cts.util.PhotoPickerFilesUtils.deleteMedia;
@@ -44,17 +46,22 @@
4446
import android.photopicker.cts.cloudproviders.CloudProviderPrimary;
4547
import android.photopicker.cts.cloudproviders.CloudProviderSecondary;
4648
import android.provider.MediaStore;
49+
import android.util.Log;
4750
import android.util.Pair;
4851

52+
import androidx.annotation.Nullable;
4953
import androidx.test.filters.SdkSuppress;
5054
import androidx.test.runner.AndroidJUnit4;
5155

5256
import org.junit.After;
57+
import org.junit.AfterClass;
5358
import org.junit.Before;
59+
import org.junit.BeforeClass;
5460
import org.junit.Test;
5561
import org.junit.runner.RunWith;
5662

5763
import java.io.File;
64+
import java.io.IOException;
5865
import java.util.ArrayList;
5966
import java.util.Collections;
6067
import java.util.List;
@@ -66,6 +73,7 @@
6673
@RunWith(AndroidJUnit4.class)
6774
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.S)
6875
public 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

tests/PhotoPicker/src/android/photopicker/cts/PhotoPickerBannersTest.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static android.photopicker.cts.PhotoPickerCloudUtils.enableCloudMediaAndSetAllowedCloudProviders;
2121
import static android.photopicker.cts.PhotoPickerCloudUtils.getAllowedProvidersDeviceConfig;
2222
import static android.photopicker.cts.PhotoPickerCloudUtils.isCloudMediaEnabled;
23-
import static android.photopicker.cts.PickerProviderMediaGenerator.setCloudProvider;
2423
import static android.photopicker.cts.util.PhotoPickerFilesUtils.createImage;
2524
import static android.photopicker.cts.util.PhotoPickerFilesUtils.deleteMedia;
2625
import static android.photopicker.cts.util.PhotoPickerUiUtils.TIMEOUT;
@@ -43,7 +42,10 @@
4342
import android.net.Uri;
4443
import android.os.Build;
4544
import android.photopicker.cts.cloudproviders.CloudProviderPrimary;
45+
import android.util.Log;
4646

47+
48+
import androidx.annotation.Nullable;
4749
import androidx.test.filters.SdkSuppress;
4850
import androidx.test.uiautomator.UiObject;
4951

@@ -54,45 +56,56 @@
5456
import org.junit.Ignore;
5557
import org.junit.Test;
5658

59+
import java.io.IOException;
60+
5761
/**
5862
* Photo Picker Banner Tests for common flows.
5963
*/
6064
// TODO(b/195009187): Enabling the banners requires setting allowed_cloud_providers device config.
6165
// We currently can't do this in R.
6266
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.S)
6367
public class PhotoPickerBannersTest extends PhotoPickerBaseTest {
64-
68+
private static final String TAG = PhotoPickerBannersTest.class.getSimpleName();
6569
private static boolean sCloudMediaPreviouslyEnabled;
6670
private static String sPreviouslyAllowedCloudProviders;
71+
@Nullable
72+
private static String sPreviouslySetCloudProvider;
6773
private Uri mLocalMediaFileUri;
6874

6975
@BeforeClass
70-
public static void setUpBeforeClass() {
76+
public static void setUpBeforeClass() throws IOException {
7177
// Store the current CMP configs, so that we can reset them at the end of the test.
7278
sCloudMediaPreviouslyEnabled = isCloudMediaEnabled();
7379
if (sCloudMediaPreviouslyEnabled) {
7480
sPreviouslyAllowedCloudProviders = getAllowedProvidersDeviceConfig();
7581
}
7682

83+
try {
84+
sPreviouslySetCloudProvider = getCurrentCloudProvider();
85+
} catch (RuntimeException e) {
86+
Log.e(TAG, "Could not get previously set cloud provider", e);
87+
sPreviouslySetCloudProvider = INVALID_CLOUD_PROVIDER;
88+
}
89+
7790
// Override the allowed cloud providers config to enable the banners.
7891
enableCloudMediaAndSetAllowedCloudProviders(sTargetPackageName);
7992
}
8093

8194
@AfterClass
82-
public static void tearDownClass() {
95+
public static void tearDownClass() throws Exception {
8396
// Reset CloudMedia configs.
8497
if (sCloudMediaPreviouslyEnabled) {
8598
enableCloudMediaAndSetAllowedCloudProviders(sPreviouslyAllowedCloudProviders);
8699
} else {
87100
disableCloudMediaAndClearAllowedCloudProviders();
88101
}
102+
setCloudProvider(sPreviouslySetCloudProvider);
89103
}
90104

91105
@Before
92106
public void setUp() throws Exception {
93107
super.setUp();
94-
95-
setCloudProvider(mContext, /* authority */ null);
108+
setCloudProvider(/* authority */ null);
96109

97110
// Create a local media file because if there's no media items for the picker grids,
98111
// the recycler view gets hidden along with the banners.
@@ -112,7 +125,7 @@ public void tearDown() throws Exception {
112125

113126
deleteMedia(mLocalMediaFileUri, mContext);
114127

115-
setCloudProvider(mContext, /* authority */ null);
128+
setCloudProvider(/* authority */ null);
116129
}
117130

118131
@Test
@@ -153,7 +166,7 @@ public void testChooseAppBannerOnActionButtonClick() throws Exception {
153166

154167
private void setCloudMediaInfoForChooseAppBanner() throws Exception {
155168
// 1. Set a non-null cloud provider and launch the photo picker.
156-
setCloudProvider(mContext, CloudProviderPrimary.AUTHORITY);
169+
setCloudProvider(CloudProviderPrimary.AUTHORITY);
157170
launchPickerActivity();
158171

159172
// 2. Wait for the banner controller construction.
@@ -167,7 +180,7 @@ private void setCloudMediaInfoForChooseAppBanner() throws Exception {
167180
verifySettingsActivityIsVisible();
168181

169182
// 3b. Set the cloud provider to None.
170-
setCloudProvider(mContext, /* authority */ null);
183+
setCloudProvider(/* authority */ null);
171184

172185
// 3c. Go back to the picker.
173186
sDevice.pressBack();

0 commit comments

Comments
 (0)