1 /*
2  * Copyright (C) 2015 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.base;
18 
19 import static androidx.core.util.Preconditions.checkArgument;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25 
26 import android.content.ContentResolver;
27 import android.content.Context;
28 import android.net.Uri;
29 import android.provider.DocumentsContract;
30 import android.test.AndroidTestCase;
31 
32 import androidx.test.filters.SmallTest;
33 import androidx.test.rule.provider.ProviderTestRule;
34 
35 import com.android.documentsui.InspectorProvider;
36 import com.android.documentsui.testing.TestProvidersAccess;
37 import com.android.documentsui.util.VersionUtils;
38 
39 import org.junit.Before;
40 import org.junit.Rule;
41 import org.junit.Test;
42 
43 import java.util.HashSet;
44 import java.util.Set;
45 
46 @SmallTest
47 public class DocumentInfoTest extends AndroidTestCase {
48 
49     private static final DocumentInfo TEST_DOC
50             = createDocInfo("authority.a", "doc.1", "text/plain");
51     private static final String FOLDER_NAME = "Top";
52     private static final String FILE_NAME = InspectorProvider.OPEN_IN_PROVIDER_TEST;
53 
54     private Context mContext;
55     private ContentResolver mResolver;
56 
57     @Rule
58     private ProviderTestRule mProviderTestRule = new ProviderTestRule.Builder(
59             InspectorProvider.class, InspectorProvider.AUTHORITY).build();
60 
61     @Before
setUp()62     public void setUp() throws Exception {
63         super.setUp();
64 
65         mContext = prepareContentResolverSource();
66         mResolver = mContext.getContentResolver();
67     }
68 
createDocInfo(String authority, String docId, String mimeType)69     private static DocumentInfo createDocInfo(String authority, String docId, String mimeType) {
70         DocumentInfo doc = new DocumentInfo();
71         doc.userId = UserId.DEFAULT_USER;
72         doc.authority = authority;
73         doc.documentId = docId;
74         doc.mimeType = mimeType;
75         doc.deriveFields();
76         return doc;
77     }
78 
prepareContentResolverSource()79     protected Context prepareContentResolverSource() {
80         ContentResolver contentResolver = mProviderTestRule.getResolver();
81         Context context = mock(Context.class);
82         // inject ContentResolver
83         when(context.getContentResolver()).thenReturn(contentResolver);
84         return context;
85     }
86 
87     @Test
testEquals()88     public void testEquals() throws Exception {
89         assertEquals(TEST_DOC, TEST_DOC);
90         assertEquals(TEST_DOC, createDocInfo("authority.a", "doc.1", "text/plain"));
91     }
92 
93     @Test
testEquals_HandlesNulls()94     public void testEquals_HandlesNulls() throws Exception {
95         assertFalse(TEST_DOC.equals(null));
96     }
97 
98     @Test
testEquals_HandlesNullFields()99     public void testEquals_HandlesNullFields() throws Exception {
100         assertFalse(TEST_DOC.equals(new DocumentInfo()));
101         assertFalse(new DocumentInfo().equals(TEST_DOC));
102     }
103 
104     @Test
testNotEquals_differentUser()105     public void testNotEquals_differentUser() throws Exception {
106         DocumentInfo documentInfo1 = createDocInfo("authority.a", "doc.1", "text/plain");
107         DocumentInfo documentInfo2 = createDocInfo("authority.a", "doc.1", "text/plain");
108         documentInfo1.userId = UserId.of(documentInfo2.userId.getIdentifier() + 1);
109         assertFalse(documentInfo1.equals(documentInfo2));
110     }
111 
112     @Test
testNotEquals_differentAuthority()113     public void testNotEquals_differentAuthority() throws Exception {
114         assertFalse(TEST_DOC.equals(createDocInfo("authority.b", "doc.1", "text/plain")));
115     }
116 
117     @Test
testNotEquals_differentDocId()118     public void testNotEquals_differentDocId() throws Exception {
119         assertFalse(TEST_DOC.equals(createDocInfo("authority.a", "doc.2", "text/plain")));
120     }
121 
122     @Test
testNotEquals_differentMimetype()123     public void testNotEquals_differentMimetype() throws Exception {
124         assertFalse(TEST_DOC.equals(createDocInfo("authority.a", "doc.1", "image/png")));
125     }
126 
127     @Test
testFolderMimeTypeFromUri()128     public void testFolderMimeTypeFromUri() throws Exception {
129         final Uri validUri = DocumentsContract.buildDocumentUri(
130                 InspectorProvider.AUTHORITY, FOLDER_NAME);
131 
132         final Set<String> mimeTypes = new HashSet<>();
133         DocumentInfo.addMimeTypes(mResolver, validUri, mimeTypes);
134 
135         assertThat(mimeTypes.size()).isEqualTo(1);
136 
137         assertThat(mimeTypes.contains(DocumentsContract.Document.MIME_TYPE_DIR)).isTrue();
138     }
139 
140     @Test
testFileMimeTypeFromUri()141     public void testFileMimeTypeFromUri() throws Exception {
142         final Uri validUri = DocumentsContract.buildDocumentUri(
143                 InspectorProvider.AUTHORITY, FILE_NAME);
144 
145         final Set<String> mimeTypes = new HashSet<>();
146         DocumentInfo.addMimeTypes(mResolver, validUri, mimeTypes);
147 
148         assertThat(mimeTypes.size()).isEqualTo(1);
149 
150         assertThat(mimeTypes.contains("text/plain")).isTrue();
151     }
152 
153     @Test
testGetTreeDocumentUri_currentUser()154     public void testGetTreeDocumentUri_currentUser() {
155         checkArgument(UserId.CURRENT_USER.equals(TEST_DOC.userId));
156 
157         assertThat(TEST_DOC.getTreeDocumentUri())
158                 .isEqualTo(DocumentsContract.buildTreeDocumentUri(TEST_DOC.authority,
159                         TEST_DOC.documentId));
160     }
161 
162     @Test
testGetTreeDocumentUri_otherUser_shouldHaveDifferentUri()163     public void testGetTreeDocumentUri_otherUser_shouldHaveDifferentUri() {
164         if (VersionUtils.isAtLeastR()) {
165             final DocumentInfo doc = createDocInfo("authority.a", "doc.1", "text/plain");
166             final DocumentInfo otherUserDoc = createDocInfo("authority.a", "doc.1", "text/plain");
167             otherUserDoc.userId = TestProvidersAccess.OtherUser.USER_ID;
168 
169             // Make sure they do not return the same tree uri
170             assertThat(otherUserDoc.getTreeDocumentUri()).isNotEqualTo(doc.getTreeDocumentUri());
171         }
172     }
173 
174     @Test
testGetTreeDocumentUri_otherUser_sameHostAndPath()175     public void testGetTreeDocumentUri_otherUser_sameHostAndPath() {
176         if (VersionUtils.isAtLeastR()) {
177             final DocumentInfo doc = createDocInfo("authority.a", "doc.1", "text/plain");
178             final DocumentInfo otherUserDoc = createDocInfo("authority.a", "doc.1", "text/plain");
179             otherUserDoc.userId = TestProvidersAccess.OtherUser.USER_ID;
180 
181             // They should have same host(authority without user info) and path
182             assertThat(otherUserDoc.getTreeDocumentUri().getHost())
183                     .isEqualTo(doc.getTreeDocumentUri().getHost());
184             assertThat(otherUserDoc.getTreeDocumentUri().getPath())
185                     .isEqualTo(doc.getTreeDocumentUri().getPath());
186         }
187     }
188 
189     @Test
testGetTreeDocumentUri_otherUser_userInfo()190     public void testGetTreeDocumentUri_otherUser_userInfo() {
191         if (VersionUtils.isAtLeastR()) {
192             final DocumentInfo doc = createDocInfo("authority.a", "doc.1", "text/plain");
193             final DocumentInfo otherUserDoc = createDocInfo("authority.a", "doc.1", "text/plain");
194             otherUserDoc.userId = TestProvidersAccess.OtherUser.USER_ID;
195 
196             // Different user info between doc and otherUserDoc
197             assertThat(otherUserDoc.getTreeDocumentUri().getUserInfo())
198                     .isNotEqualTo(doc.getTreeDocumentUri().getUserInfo());
199 
200             // Same user info within otherUserDoc
201             assertThat(otherUserDoc.getTreeDocumentUri().getUserInfo())
202                     .isEqualTo(otherUserDoc.getDocumentUri().getUserInfo());
203         }
204     }
205 }
206