Skip to content

Commit b27076c

Browse files
Shivam GargAndroid Build Cherrypicker Worker
authored andcommitted
Recreate setCloudProvider() funtion inorder to pass permission checks
before updating cloud Provider Recreate setCloudProvider() function in PhotoPickerBaseTest class inorder to pass permission checks, added in MediaProvider class while updating the cloud provider. Gave necessary shell permissions to update cloudProvider through shell. Also modified previously present setCloudProvider() function call in all the CTS tests. Bug: b/267327327 Test: Try running ` atest cts/tests/PhotoPicker/src/android/photopicker/cts ` and see if all the testcases passed. (cherry picked from https://googleplex-android-review.googlesource.com/q/commit:68c16c3314c088896170296fb18e3ea0b7625df1) Merged-In: Ie381710835f0f55dd02df221718b821967d7a37f Change-Id: Ie381710835f0f55dd02df221718b821967d7a37f
1 parent be38d7d commit b27076c

5 files changed

Lines changed: 140 additions & 34 deletions

File tree

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

Lines changed: 46 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;
@@ -46,15 +48,19 @@
4648
import android.provider.MediaStore;
4749
import android.util.Pair;
4850

51+
import androidx.annotation.Nullable;
4952
import androidx.test.filters.SdkSuppress;
5053
import androidx.test.runner.AndroidJUnit4;
5154

5255
import org.junit.After;
56+
import org.junit.AfterClass;
5357
import org.junit.Before;
58+
import org.junit.BeforeClass;
5459
import org.junit.Test;
5560
import org.junit.runner.RunWith;
5661

5762
import java.io.File;
63+
import java.io.IOException;
5864
import java.util.ArrayList;
5965
import java.util.Collections;
6066
import 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

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

Lines changed: 14 additions & 8 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;
@@ -44,6 +43,8 @@
4443
import android.os.Build;
4544
import android.photopicker.cts.cloudproviders.CloudProviderPrimary;
4645

46+
47+
import androidx.annotation.Nullable;
4748
import androidx.test.filters.SdkSuppress;
4849
import androidx.test.uiautomator.UiObject;
4950

@@ -54,6 +55,8 @@
5455
import org.junit.Ignore;
5556
import org.junit.Test;
5657

58+
import java.io.IOException;
59+
5760
/**
5861
* Photo Picker Banner Tests for common flows.
5962
*/
@@ -64,35 +67,38 @@ public class PhotoPickerBannersTest extends PhotoPickerBaseTest {
6467

6568
private static boolean sCloudMediaPreviouslyEnabled;
6669
private static String sPreviouslyAllowedCloudProviders;
70+
@Nullable
71+
private static String sPreviouslySetCloudProvider;
6772
private Uri mLocalMediaFileUri;
6873

6974
@BeforeClass
70-
public static void setUpBeforeClass() {
75+
public static void setUpBeforeClass() throws IOException {
7176
// Store the current CMP configs, so that we can reset them at the end of the test.
7277
sCloudMediaPreviouslyEnabled = isCloudMediaEnabled();
7378
if (sCloudMediaPreviouslyEnabled) {
7479
sPreviouslyAllowedCloudProviders = getAllowedProvidersDeviceConfig();
7580
}
81+
sPreviouslySetCloudProvider = getCurrentCloudProvider();
7682

7783
// Override the allowed cloud providers config to enable the banners.
7884
enableCloudMediaAndSetAllowedCloudProviders(sTargetPackageName);
7985
}
8086

8187
@AfterClass
82-
public static void tearDownClass() {
88+
public static void tearDownClass() throws Exception {
8389
// Reset CloudMedia configs.
8490
if (sCloudMediaPreviouslyEnabled) {
8591
enableCloudMediaAndSetAllowedCloudProviders(sPreviouslyAllowedCloudProviders);
8692
} else {
8793
disableCloudMediaAndClearAllowedCloudProviders();
8894
}
95+
setCloudProvider(sPreviouslySetCloudProvider);
8996
}
9097

9198
@Before
9299
public void setUp() throws Exception {
93100
super.setUp();
94-
95-
setCloudProvider(mContext, /* authority */ null);
101+
setCloudProvider(/* authority */ null);
96102

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

113119
deleteMedia(mLocalMediaFileUri, mContext);
114120

115-
setCloudProvider(mContext, /* authority */ null);
121+
setCloudProvider(/* authority */ null);
116122
}
117123

118124
@Test
@@ -153,7 +159,7 @@ public void testChooseAppBannerOnActionButtonClick() throws Exception {
153159

154160
private void setCloudMediaInfoForChooseAppBanner() throws Exception {
155161
// 1. Set a non-null cloud provider and launch the photo picker.
156-
setCloudProvider(mContext, CloudProviderPrimary.AUTHORITY);
162+
setCloudProvider(CloudProviderPrimary.AUTHORITY);
157163
launchPickerActivity();
158164

159165
// 2. Wait for the banner controller construction.
@@ -167,7 +173,7 @@ private void setCloudMediaInfoForChooseAppBanner() throws Exception {
167173
verifySettingsActivityIsVisible();
168174

169175
// 3b. Set the cloud provider to None.
170-
setCloudProvider(mContext, /* authority */ null);
176+
setCloudProvider(/* authority */ null);
171177

172178
// 3c. Go back to the picker.
173179
sDevice.pressBack();

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,26 @@
2020
import android.content.Context;
2121
import android.content.Intent;
2222
import android.content.pm.PackageManager;
23+
import android.util.Log;
2324

25+
import androidx.annotation.Nullable;
2426
import androidx.test.InstrumentationRegistry;
2527
import androidx.test.uiautomator.UiDevice;
2628

2729

30+
31+
2832
import org.junit.Assume;
2933
import org.junit.Before;
3034

35+
import java.io.IOException;
36+
3137
/**
3238
* Photo Picker Base class for Photo Picker tests. This includes common setup methods
3339
* required for all Photo Picker tests.
3440
*/
3541
public class PhotoPickerBaseTest {
42+
private static final String TAG = "PhotoPickerBaseTest";
3643
public static int REQUEST_CODE = 42;
3744
private static final Instrumentation sInstrumentation =
3845
InstrumentationRegistry.getInstrumentation();
@@ -75,4 +82,38 @@ static boolean isHardwareSupported() {
7582
&& !pm.hasSystemFeature(pm.FEATURE_LEANBACK)
7683
&& !pm.hasSystemFeature(pm.FEATURE_AUTOMOTIVE);
7784
}
85+
86+
protected static void setCloudProvider(@Nullable String authority) throws Exception {
87+
if (authority == null) {
88+
sDevice.executeShellCommand(
89+
"content call --uri content://media/ --method set_cloud_provider --extra"
90+
+ " cloud_provider:n:null");
91+
} else {
92+
sDevice.executeShellCommand(
93+
"content call --uri content://media/ --method set_cloud_provider --extra"
94+
+ " cloud_provider:s:"
95+
+ authority);
96+
}
97+
}
98+
99+
protected static String getCurrentCloudProvider() throws IOException {
100+
final String out =
101+
sDevice.executeShellCommand(
102+
"content call --uri content://media/ --method get_cloud_provider");
103+
return extractCloudProvider(out);
104+
}
105+
106+
private static String extractCloudProvider(String out) {
107+
if (out == null) {
108+
Log.d(TAG, "Failed request to get current cloud provider");
109+
return null;
110+
}
111+
String cloudprovider = (out.split("=")[1]);
112+
cloudprovider = cloudprovider.substring(0, cloudprovider.length() - 3);
113+
if (cloudprovider.equals("null")) {
114+
return null;
115+
}
116+
return cloudprovider;
117+
}
78118
}
119+

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static android.Manifest.permission.READ_DEVICE_CONFIG;
2020
import static android.Manifest.permission.WRITE_ALLOWLISTED_DEVICE_CONFIG;
2121
import static android.Manifest.permission.WRITE_DEVICE_CONFIG;
22-
import static android.photopicker.cts.PickerProviderMediaGenerator.setCloudProvider;
2322
import static android.photopicker.cts.PickerProviderMediaGenerator.syncCloudProvider;
2423
import static android.photopicker.cts.util.PhotoPickerUiUtils.findAddButton;
2524
import static android.photopicker.cts.util.PhotoPickerUiUtils.findItemList;
@@ -87,7 +86,7 @@ public static ClipData fetchPickerMedia(GetResultActivity activity, UiDevice uiD
8786
public static void initCloudProviderWithImage(
8887
Context context, PickerProviderMediaGenerator.MediaGenerator mediaGenerator,
8988
String authority, Pair<String, String>... mediaPairs) throws Exception {
90-
setCloudProvider(context, authority);
89+
PhotoPickerBaseTest.setCloudProvider(authority);
9190
assertThat(MediaStore.isCurrentCloudMediaProviderAuthority(context.getContentResolver(),
9291
authority)).isTrue();
9392

0 commit comments

Comments
 (0)