• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import static android.os.Build.VERSION_CODES.N;
4 import static com.google.common.truth.Truth.assertThat;
5 import static org.robolectric.RuntimeEnvironment.application;
6 import static org.robolectric.Shadows.shadowOf;
7 
8 import android.content.Context;
9 import android.os.Parcel;
10 import android.os.UserHandle;
11 import android.os.UserManager;
12 import android.os.storage.StorageManager;
13 import android.os.storage.StorageVolume;
14 import androidx.test.ext.junit.runners.AndroidJUnit4;
15 import java.io.File;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.robolectric.annotation.Config;
20 
21 /** Unit tests for {@link ShadowStorageManager}. */
22 @RunWith(AndroidJUnit4.class)
23 public class ShadowStorageManagerTest {
24 
25   private StorageManager storageManager;
26 
27   @Before
setUp()28   public void setUp() {
29     storageManager = (StorageManager) application.getSystemService(Context.STORAGE_SERVICE);
30   }
31 
32   @Test
getVolumeList()33   public void getVolumeList() {
34     assertThat(shadowOf(storageManager).getVolumeList()).isNotNull();
35   }
36 
37   @Test
38   @Config(minSdk = N)
getStorageVolumes()39   public void getStorageVolumes() {
40     File file1 = new File("/storage/sdcard");
41     shadowOf(storageManager).addStorageVolume(buildAndGetStorageVolume(file1, "sd card"));
42     assertThat(shadowOf(storageManager).getStorageVolumes()).isNotNull();
43   }
44 
45   @Test
46   @Config(minSdk = N)
getStorageVolume()47   public void getStorageVolume() {
48     File file1 = new File("/storage/internal");
49     File file2 = new File("/storage/sdcard");
50     shadowOf(storageManager).addStorageVolume(buildAndGetStorageVolume(file1, "internal"));
51     assertThat(shadowOf(storageManager).getStorageVolume(file1)).isNotNull();
52     assertThat(shadowOf(storageManager).getStorageVolume(file2)).isNull();
53   }
54 
55   @Test
56   @Config(minSdk = N)
isFileEncryptedNativeOrEmulated()57   public void isFileEncryptedNativeOrEmulated() {
58     shadowOf(storageManager).setFileEncryptedNativeOrEmulated(true);
59     assertThat(StorageManager.isFileEncryptedNativeOrEmulated()).isTrue();
60   }
61 
62   @Test
63   @Config(minSdk = N)
isUserKeyUnlocked()64   public void isUserKeyUnlocked() {
65     shadowOf(application.getSystemService(UserManager.class)).setUserUnlocked(true);
66     assertThat(StorageManager.isUserKeyUnlocked(0)).isTrue();
67   }
68 
buildAndGetStorageVolume(File file, String description)69   private StorageVolume buildAndGetStorageVolume(File file, String description) {
70     Parcel parcel = Parcel.obtain();
71     parcel.writeInt(0);
72     UserHandle userHandle = new UserHandle(parcel);
73     StorageVolumeBuilder storageVolumeBuilder =
74         new StorageVolumeBuilder("volume", file, description, userHandle, "mounted");
75     return storageVolumeBuilder.build();
76   }
77 }
78