1 package com.android.documentsui.files; 2 3 import static com.google.common.truth.Truth.assertThat; 4 5 import static junit.framework.Assert.assertEquals; 6 import static junit.framework.Assert.assertTrue; 7 8 import static org.mockito.Mockito.mock; 9 import static org.mockito.Mockito.when; 10 11 import android.content.ClipData; 12 import android.content.Context; 13 import android.content.Intent; 14 import android.content.QuickViewConstants; 15 import android.content.pm.PackageManager; 16 17 import androidx.test.InstrumentationRegistry; 18 import androidx.test.filters.SmallTest; 19 import androidx.test.runner.AndroidJUnit4; 20 21 import com.android.documentsui.base.DocumentInfo; 22 import com.android.documentsui.testing.TestEnv; 23 import com.android.documentsui.testing.TestPackageManager; 24 import com.android.documentsui.testing.TestProvidersAccess; 25 import com.android.documentsui.testing.TestResources; 26 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 31 import java.util.Arrays; 32 import java.util.HashSet; 33 import java.util.Set; 34 35 @SmallTest 36 @RunWith(AndroidJUnit4.class) 37 public class QuickViewIntentBuilderTest { 38 39 private static String mTargetPackageName; 40 private Context mContext = mock(Context.class); 41 private PackageManager mPm; 42 private TestEnv mEnv; 43 private TestResources mRes; 44 45 @Before setUp()46 public void setUp() { 47 mTargetPackageName = 48 InstrumentationRegistry.getInstrumentation().getTargetContext().getPackageName(); 49 mPm = TestPackageManager.create(); 50 when(mContext.getPackageManager()).thenReturn(mPm); 51 mEnv = TestEnv.create(); 52 mRes = TestResources.create(); 53 54 mRes.setQuickViewerPackage(mTargetPackageName); 55 } 56 57 @Test testSetsNoFeatures_InArchiveDocument()58 public void testSetsNoFeatures_InArchiveDocument() { 59 QuickViewIntentBuilder builder = 60 new QuickViewIntentBuilder( 61 mContext, mRes, TestEnv.FILE_IN_ARCHIVE, mEnv.archiveModel, false); 62 63 Intent intent = builder.build(); 64 65 String[] features = intent.getStringArrayExtra(Intent.EXTRA_QUICK_VIEW_FEATURES); 66 assertEquals(0, features.length); 67 } 68 69 @Test testSetsFullFeatures_RegularDocument()70 public void testSetsFullFeatures_RegularDocument() { 71 QuickViewIntentBuilder builder = 72 new QuickViewIntentBuilder(mContext, mRes, TestEnv.FILE_JPG, mEnv.model, false); 73 74 Intent intent = builder.build(); 75 76 Set<String> features = new HashSet<>( 77 Arrays.asList(intent.getStringArrayExtra(Intent.EXTRA_QUICK_VIEW_FEATURES))); 78 79 assertEquals("Unexpected features set: " + features, 6, features.size()); 80 assertTrue(features.contains(QuickViewConstants.FEATURE_VIEW)); 81 assertTrue(features.contains(QuickViewConstants.FEATURE_EDIT)); 82 assertTrue(features.contains(QuickViewConstants.FEATURE_DELETE)); 83 assertTrue(features.contains(QuickViewConstants.FEATURE_SEND)); 84 assertTrue(features.contains(QuickViewConstants.FEATURE_DOWNLOAD)); 85 assertTrue(features.contains(QuickViewConstants.FEATURE_PRINT)); 86 } 87 88 @Test testPickerFeatures_RegularDocument()89 public void testPickerFeatures_RegularDocument() { 90 91 QuickViewIntentBuilder builder = 92 new QuickViewIntentBuilder(mContext, mRes, TestEnv.FILE_JPG, mEnv.model, true); 93 94 Intent intent = builder.build(); 95 96 Set<String> features = new HashSet<>( 97 Arrays.asList(intent.getStringArrayExtra(Intent.EXTRA_QUICK_VIEW_FEATURES))); 98 99 assertEquals("Unexpected features set: " + features, 1, features.size()); 100 assertTrue(features.contains(QuickViewConstants.FEATURE_VIEW)); 101 } 102 103 @Test testBuild()104 public void testBuild() { 105 mEnv.model.reset(); 106 DocumentInfo previewDoc = mEnv.model.createFile("a.png", 0); 107 mEnv.model.createFile("b.png", 0); 108 mEnv.model.createFile("c.png", 0); 109 mEnv.model.update(); 110 111 QuickViewIntentBuilder builder = 112 new QuickViewIntentBuilder(mContext, mRes, previewDoc, mEnv.model, true); 113 114 Intent intent = builder.build(); 115 ClipData clip = intent.getClipData(); 116 assertThat(clip.getItemAt(intent.getIntExtra(Intent.EXTRA_INDEX, -1)).getUri()) 117 .isEqualTo(previewDoc.getDocumentUri()); 118 assertThat(clip.getItemCount()).isEqualTo(3); 119 } 120 121 @Test testBuild_excludeFolder()122 public void testBuild_excludeFolder() { 123 mEnv.model.reset(); 124 mEnv.model.createFolder("folder"); 125 mEnv.model.createFolder("does not count"); 126 mEnv.model.createFile("a.png", 0); 127 DocumentInfo previewDoc = mEnv.model.createFile("b.png", 0); 128 mEnv.model.createFile("c.png", 0); 129 mEnv.model.update(); 130 131 QuickViewIntentBuilder builder = 132 new QuickViewIntentBuilder(mContext, mRes, previewDoc, mEnv.model, true); 133 134 Intent intent = builder.build(); 135 ClipData clip = intent.getClipData(); 136 assertThat(clip.getItemAt(intent.getIntExtra(Intent.EXTRA_INDEX, -1)).getUri()) 137 .isEqualTo(previewDoc.getDocumentUri()); 138 assertThat(clip.getItemCount()).isEqualTo(3); 139 } 140 141 @Test testBuild_twoProfiles_containsOnlyPreviewDocument()142 public void testBuild_twoProfiles_containsOnlyPreviewDocument() { 143 mEnv.model.reset(); 144 mEnv.model.createDocumentForUser("a.txt", "text/plain", 0, 145 TestProvidersAccess.OtherUser.USER_ID); 146 DocumentInfo previewDoc = mEnv.model.createFile("b.png", 0); 147 mEnv.model.createFile("c.png", 0); 148 mEnv.model.update(); 149 150 QuickViewIntentBuilder builder = 151 new QuickViewIntentBuilder(mContext, mRes, previewDoc, mEnv.model, true); 152 153 Intent intent = builder.build(); 154 ClipData clip = intent.getClipData(); 155 assertThat(clip.getItemAt(intent.getIntExtra(Intent.EXTRA_INDEX, -1)).getUri()) 156 .isEqualTo(previewDoc.getDocumentUri()); 157 assertThat(clip.getItemCount()).isEqualTo(1); 158 } 159 } 160