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

import android.content.res.Resources;
import android.util.LongSparseArray;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import org.robolectric.shadows.ShadowLegacyResourcesImpl.ShadowLegacyThemeImpl;

abstract public class ShadowResourcesImpl {

  public static class Picker extends ResourceModeShadowPicker<ShadowResourcesImpl> {

    public Picker() {
      super(ShadowLegacyResourcesImpl.class, ShadowArscResourcesImpl.class,
          ShadowArscResourcesImpl.class);
    }
  }

  private static List<LongSparseArray<?>> resettableArrays;

  public static void reset() {
    if (resettableArrays == null) {
      resettableArrays = obtainResettableArrays();
    }
    for (LongSparseArray<?> sparseArray : resettableArrays) {
      sparseArray.clear();
    }
  }

  private static List<LongSparseArray<?>> obtainResettableArrays() {
    List<LongSparseArray<?>> resettableArrays = new ArrayList<>();
    Field[] allFields = Resources.class.getDeclaredFields();
    for (Field field : allFields) {
      if (Modifier.isStatic(field.getModifiers()) && field.getType().equals(LongSparseArray.class)) {
        field.setAccessible(true);
        try {
          LongSparseArray<?> longSparseArray = (LongSparseArray<?>) field.get(null);
          if (longSparseArray != null) {
            resettableArrays.add(longSparseArray);
          }
        } catch (IllegalAccessException e) {
          throw new RuntimeException(e);
        }
      }
    }
    return resettableArrays;
  }

  abstract public static class ShadowThemeImpl {
    public static class Picker extends ResourceModeShadowPicker<ShadowThemeImpl> {

      public Picker() {
        super(ShadowLegacyThemeImpl.class, null, null);
      }
    }
  }
}