aboutsummaryrefslogtreecommitdiff
path: root/robolectric/src/test/java/org/robolectric/shadows/ShadowStorageManagerTest.java
blob: ad410660e51b448da4daaf206424c3e9646ee0f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package org.robolectric.shadows;

import static android.os.Build.VERSION_CODES.N;
import static android.os.Build.VERSION_CODES.TIRAMISU;
import static android.os.Build.VERSION_CODES.UPSIDE_DOWN_CAKE;
import static com.google.common.truth.Truth.assertThat;
import static org.robolectric.RuntimeEnvironment.getApplication;
import static org.robolectric.Shadows.shadowOf;

import android.content.Context;
import android.os.Parcel;
import android.os.UserHandle;
import android.os.UserManager;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.io.File;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;

/** Unit tests for {@link ShadowStorageManager}. */
@RunWith(AndroidJUnit4.class)
public class ShadowStorageManagerTest {

  private final String internalStorage = "/storage/internal";
  private final String sdcardStorage = "/storage/sdcard";

  private StorageManager storageManager;

  @Before
  public void setUp() {
    storageManager = (StorageManager) getApplication().getSystemService(Context.STORAGE_SERVICE);
  }

  @Test
  public void getVolumeList() {
    assertThat(shadowOf(storageManager).getVolumeList()).isNotNull();
  }

  @Test
  @Config(minSdk = N)
  public void getStorageVolumes() {
    File file1 = new File(sdcardStorage);
    shadowOf(storageManager).addStorageVolume(buildAndGetStorageVolume(file1, "sd card"));
    assertThat(shadowOf(storageManager).getStorageVolumes()).isNotNull();
  }

  @Test
  @Config(minSdk = N)
  public void getStorageVolumesHaveDifferentUUID() {
    File file1 = new File(sdcardStorage);
    File file2 = new File(internalStorage);

    shadowOf(storageManager).addStorageVolume(buildAndGetStorageVolume(file1, "sd card"));
    shadowOf(storageManager).addStorageVolume(buildAndGetStorageVolume(file2, "internal"));

    List<StorageVolume> volumeList = shadowOf(storageManager).getStorageVolumes();
    assertThat(volumeList).hasSize(2);
    StorageVolume storage1 = volumeList.get(0);
    StorageVolume storage2 = volumeList.get(1);
    assertThat(storage1.getUuid()).isNotEqualTo(storage2.getUuid());
  }

  @Test
  @Config(minSdk = N)
  public void getStorageVolume() {
    File file1 = new File(internalStorage);
    File file2 = new File(sdcardStorage);
    File file3 = new File(internalStorage, "test_folder");
    File file4 = new File(sdcardStorage, "test_folder");
    shadowOf(storageManager).addStorageVolume(buildAndGetStorageVolume(file1, "internal"));
    assertThat(shadowOf(storageManager).getStorageVolume(file1)).isNotNull();
    assertThat(shadowOf(storageManager).getStorageVolume(file2)).isNull();
    assertThat(shadowOf(storageManager).getStorageVolume(file3)).isNotNull();
    assertThat(shadowOf(storageManager).getStorageVolume(file4)).isNull();
  }

  @Test
  @Config(minSdk = N, maxSdk = TIRAMISU)
  public void isFileEncryptedNativeOrEmulated() {
    shadowOf(storageManager).setFileEncryptedNativeOrEmulated(true);
    // Use reflection, as this method is planned to be removed from StorageManager in V.
    assertThat(
            (boolean)
                ReflectionHelpers.callStaticMethod(
                    StorageManager.class, "isFileEncryptedNativeOrEmulated"))
        .isTrue();
  }

  @Test
  @Config(minSdk = N, maxSdk = UPSIDE_DOWN_CAKE)
  public void isUserKeyUnlocked() {
    shadowOf(getApplication().getSystemService(UserManager.class)).setUserUnlocked(true);
    // Use reflection, as this method is planned to be removed from StorageManager in V.
    assertThat(
            (boolean)
                ReflectionHelpers.callStaticMethod(
                    StorageManager.class,
                    "isUserKeyUnlocked",
                    ReflectionHelpers.ClassParameter.from(int.class, 0)))
        .isTrue();
  }

  private StorageVolume buildAndGetStorageVolume(File file, String description) {
    Parcel parcel = Parcel.obtain();
    parcel.writeInt(0);
    parcel.setDataPosition(0);
    UserHandle userHandle = new UserHandle(parcel);
    StorageVolumeBuilder storageVolumeBuilder =
        new StorageVolumeBuilder(
            "volume" + " " + description, file, description, userHandle, "mounted");
    return storageVolumeBuilder.build();
  }
}