• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.externalstorage;
18 
19 import static com.android.externalstorage.ExternalStorageProvider.AUTHORITY;
20 import static com.android.externalstorage.ExternalStorageProvider.getPathFromDocId;
21 
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24 import static org.mockito.Mockito.atLeast;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.verify;
27 
28 import android.content.pm.ProviderInfo;
29 
30 import androidx.test.InstrumentationRegistry;
31 import androidx.test.runner.AndroidJUnit4;
32 
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 
36 @RunWith(AndroidJUnit4.class)
37 public class ExternalStorageProviderTest {
38     @Test
onCreate_shouldUpdateVolumes()39     public void onCreate_shouldUpdateVolumes() throws Exception {
40         ExternalStorageProvider externalStorageProvider = new ExternalStorageProvider();
41         ExternalStorageProvider spyProvider = spy(externalStorageProvider);
42         ProviderInfo providerInfo = new ProviderInfo();
43         providerInfo.authority = AUTHORITY;
44         providerInfo.grantUriPermissions = true;
45         providerInfo.exported = true;
46 
47         InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
48             @Override
49             public void run() {
50                 spyProvider.attachInfoForTesting(
51                         InstrumentationRegistry.getTargetContext(), providerInfo);
52             }
53         });
54 
55         verify(spyProvider, atLeast(1)).updateVolumes();
56     }
57 
58     @Test
testGetPathFromDocId()59     public void testGetPathFromDocId() throws Exception {
60         final String root = "root";
61         final String path = "abc/def/ghi";
62         String docId = root + ":" + path;
63         assertEquals(getPathFromDocId(docId), path);
64 
65         docId = root + ":" + path + "/";
66         assertEquals(getPathFromDocId(docId), path);
67 
68         docId = root + ":";
69         assertTrue(getPathFromDocId(docId).isEmpty());
70     }
71 }
72