1 /* 2 * Copyright (C) 2020 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.documentsui; 18 19 import static com.android.documentsui.base.Providers.AUTHORITY_STORAGE; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import android.app.Activity; 24 import android.app.Instrumentation; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.net.Uri; 28 import android.os.SystemClock; 29 import android.provider.DocumentsContract; 30 31 import androidx.test.filters.SmallTest; 32 import androidx.test.platform.app.InstrumentationRegistry; 33 import androidx.test.rule.ActivityTestRule; 34 35 import com.android.documentsui.base.DocumentInfo; 36 import com.android.documentsui.picker.PickActivity; 37 import com.android.documentsui.testing.TestProvidersAccess; 38 import com.android.documentsui.ui.TestDialogController; 39 import com.android.documentsui.util.VersionUtils; 40 import com.android.modules.utils.build.SdkLevel; 41 42 import com.google.common.collect.Lists; 43 44 import org.junit.Before; 45 import org.junit.Rule; 46 import org.junit.Test; 47 import org.junit.runner.RunWith; 48 import org.junit.runners.Parameterized; 49 import org.junit.runners.Parameterized.Parameter; 50 import org.junit.runners.Parameterized.Parameters; 51 52 @SmallTest 53 @RunWith(Parameterized.class) 54 public class PickActivityTest { 55 56 private static final String RESULT_EXTRA = "test_result_extra"; 57 private static final String RESULT_DATA = "123321"; 58 59 private Context mTargetContext; 60 private Intent mIntentGetContent; 61 private TestDialogController mTestDialogs; 62 private TestConfigStore mTestConfigStore; 63 64 @Parameter(0) 65 public boolean isPrivateSpaceEnabled; 66 67 /** 68 * Parametrize values for {@code isPrivateSpaceEnabled} to run all the tests twice once with 69 * private space flag enabled and once with it disabled. 70 */ 71 @Parameters(name = "privateSpaceEnabled={0}") data()72 public static Iterable<?> data() { 73 return Lists.newArrayList(true, false); 74 } 75 76 @Rule 77 public final ActivityTestRule<PickActivity> mRule = 78 new ActivityTestRule<>(PickActivity.class, false, false); 79 80 @Before setUp()81 public void setUp() throws Exception { 82 mTargetContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); 83 84 mIntentGetContent = new Intent(Intent.ACTION_GET_CONTENT); 85 mIntentGetContent.addCategory(Intent.CATEGORY_OPENABLE); 86 mIntentGetContent.setType("*/*"); 87 Uri hintUri = DocumentsContract.buildRootUri(AUTHORITY_STORAGE, "primary"); 88 mIntentGetContent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, hintUri); 89 90 mTestDialogs = new TestDialogController(); 91 mTestConfigStore = new TestConfigStore(); 92 93 isPrivateSpaceEnabled = SdkLevel.isAtLeastS() && isPrivateSpaceEnabled; 94 } 95 96 @Test testOnDocumentPicked()97 public void testOnDocumentPicked() { 98 DocumentInfo doc = new DocumentInfo(); 99 doc.userId = TestProvidersAccess.USER_ID; 100 doc.authority = "authority"; 101 doc.documentId = "documentId"; 102 103 PickActivity pickActivity = mRule.launchActivity(mIntentGetContent); 104 pickActivity.mState.configStore = mTestConfigStore; 105 if (isPrivateSpaceEnabled) { 106 mTestConfigStore.enablePrivateSpaceInPhotoPicker(); 107 pickActivity.mState.canForwardToProfileIdMap.put(TestProvidersAccess.USER_ID, true); 108 } else { 109 pickActivity.mState.canShareAcrossProfile = true; 110 } 111 pickActivity.onDocumentPicked(doc); 112 SystemClock.sleep(3000); 113 114 Instrumentation.ActivityResult result = mRule.getActivityResult(); 115 assertThat(pickActivity.isFinishing()).isTrue(); 116 assertThat(result.getResultCode()).isEqualTo(Activity.RESULT_OK); 117 assertThat(result.getResultData().getData()).isEqualTo(doc.getDocumentUri()); 118 } 119 120 @Test testOnDocumentPicked_otherUser()121 public void testOnDocumentPicked_otherUser() { 122 if (VersionUtils.isAtLeastR()) { 123 DocumentInfo doc = new DocumentInfo(); 124 doc.userId = TestProvidersAccess.OtherUser.USER_ID; 125 doc.authority = "authority"; 126 doc.documentId = "documentId"; 127 128 PickActivity pickActivity = mRule.launchActivity(mIntentGetContent); 129 pickActivity.mState.configStore = mTestConfigStore; 130 if (isPrivateSpaceEnabled) { 131 mTestConfigStore.enablePrivateSpaceInPhotoPicker(); 132 pickActivity.mState.canForwardToProfileIdMap.put(TestProvidersAccess.USER_ID, true); 133 pickActivity.mState.canForwardToProfileIdMap.put( 134 TestProvidersAccess.OtherUser.USER_ID, true); 135 } else { 136 pickActivity.mState.canShareAcrossProfile = true; 137 } 138 pickActivity.onDocumentPicked(doc); 139 SystemClock.sleep(3000); 140 141 Instrumentation.ActivityResult result = mRule.getActivityResult(); 142 assertThat(result.getResultCode()).isEqualTo(Activity.RESULT_OK); 143 assertThat(result.getResultData().getData()).isEqualTo(doc.getDocumentUri()); 144 } 145 } 146 147 @Test testOnDocumentPicked_otherUserDoesNotReturn()148 public void testOnDocumentPicked_otherUserDoesNotReturn() { 149 DocumentInfo doc = new DocumentInfo(); 150 doc.userId = TestProvidersAccess.OtherUser.USER_ID; 151 doc.authority = "authority"; 152 doc.documentId = "documentId"; 153 154 PickActivity pickActivity = mRule.launchActivity(mIntentGetContent); 155 pickActivity.mState.configStore = mTestConfigStore; 156 if (isPrivateSpaceEnabled) { 157 mTestConfigStore.enablePrivateSpaceInPhotoPicker(); 158 pickActivity.mState.canForwardToProfileIdMap.put(TestProvidersAccess.USER_ID, true); 159 } else { 160 pickActivity.mState.canShareAcrossProfile = false; 161 } 162 pickActivity.getInjector().dialogs = mTestDialogs; 163 pickActivity.onDocumentPicked(doc); 164 SystemClock.sleep(3000); 165 166 assertThat(pickActivity.isFinishing()).isFalse(); 167 mTestDialogs.assertActionNotAllowedShown(); 168 } 169 } 170