aboutsummaryrefslogtreecommitdiff
path: root/shadows/framework/src/main/java/org/robolectric/shadows/ShadowDevicePolicyResourcesManager.java
blob: 70249de3c071caeba9949f21b1619e57a305f61f (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
package org.robolectric.shadows;

import static org.robolectric.util.reflector.Reflector.reflector;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.admin.DevicePolicyResourcesManager;
import android.os.Build.VERSION_CODES;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.util.reflector.Direct;
import org.robolectric.util.reflector.ForType;

/** Shadow for {@link DevicePolicyResourcesManager}. */
@Implements(
    value = DevicePolicyResourcesManager.class,
    minSdk = VERSION_CODES.TIRAMISU,
    // turn off shadowOf generation (new API)
    isInAndroidSdk = false)
public class ShadowDevicePolicyResourcesManager {

  @RealObject DevicePolicyResourcesManager realDevicePolicyResourcesManager;
  private final Map<String, String> stringMappings = new HashMap<>();

  /**
   * Override string returned by the resource identified by {@code stringId}. Reset the override by
   * providing null as the {@code vaNlue}.
   */
  public void setString(@NonNull String stringId, String value) {
    stringMappings.put(stringId, value);
  }

  @Implementation
  @Nullable
  protected String getString(
      @NonNull String stringId, @NonNull Supplier<String> defaultStringLoader) {
    String value = stringMappings.get(stringId);
    if (value != null) {
      return value;
    }

    return reflector(DevicePolicyResourcesManagerReflector.class, realDevicePolicyResourcesManager)
        .getString(stringId, defaultStringLoader);
  }

  @ForType(DevicePolicyResourcesManager.class)
  interface DevicePolicyResourcesManagerReflector {
    @Direct
    String getString(@NonNull String stringId, @NonNull Supplier<String> defaultStringLoader);
  }
}