1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package android.testing;
16 
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.when;
19 import static org.mockito.Mockito.withSettings;
20 
21 import android.content.Context;
22 import android.content.res.Configuration;
23 import android.content.res.Resources;
24 import android.util.Log;
25 import android.util.SparseArray;
26 
27 import org.mockito.invocation.InvocationOnMock;
28 
29 import java.util.Arrays;
30 
31 /**
32  * Provides a version of Resources that defaults to all existing resources, but can have ids
33  * changed to return specific values.
34  * <p>
35  * TestableResources are lazily initialized, be sure to call
36  * {@link TestableContext#ensureTestableResources} before your tested code has an opportunity
37  * to cache {@link Context#getResources}.
38  * </p>
39  */
40 public class TestableResources {
41 
42     private static final String TAG = "TestableResources";
43     private final Resources mResources;
44     private final SparseArray<Object> mOverrides = new SparseArray<>();
45 
46     /** Creates a TestableResources instance that calls through to the given real Resources. */
TestableResources(Resources realResources)47     public TestableResources(Resources realResources) {
48         mResources = mock(Resources.class, withSettings()
49                 .spiedInstance(realResources)
50                 .defaultAnswer(this::answer));
51     }
52 
53     /**
54      * Gets the implementation of Resources that will return overridden values when called.
55      */
getResources()56     public Resources getResources() {
57         return mResources;
58     }
59 
60     /**
61      * Sets a configuration for {@link #getResources()} to return to allow custom configs to
62      * be set and tested.
63      *
64      * @param configuration the configuration to return from resources.
65      */
overrideConfiguration(Configuration configuration)66     public void overrideConfiguration(Configuration configuration) {
67         when(mResources.getConfiguration()).thenReturn(configuration);
68     }
69 
70     /**
71      * Sets the return value for the specified resource id.
72      * <p>
73      * Since resource ids are unique there is a single addOverride that will override the value
74      * whenever it is gotten regardless of which method is used (i.e. getColor or getDrawable).
75      * </p>
76      *
77      * @param id    The resource id to be overridden
78      * @param value The value of the resource, null to cause a {@link Resources.NotFoundException}
79      *              when gotten.
80      */
addOverride(int id, Object value)81     public void addOverride(int id, Object value) {
82         mOverrides.put(id, value);
83     }
84 
85     /**
86      * Removes the override for the specified id.
87      * <p>
88      * This should be called over addOverride(id, null), because specifying a null value will
89      * cause a {@link Resources.NotFoundException} whereas removing the override will actually
90      * switch back to returning the default/real value of the resource.
91      * </p>
92      */
removeOverride(int id)93     public void removeOverride(int id) {
94         mOverrides.remove(id);
95     }
96 
answer(InvocationOnMock invocationOnMock)97     private Object answer(InvocationOnMock invocationOnMock) throws Throwable {
98         // Only try to override methods with an integer first argument
99         if (invocationOnMock.getArguments().length > 0) {
100             Object argument = invocationOnMock.getArgument(0);
101             if (argument instanceof Integer) {
102                 try {
103                     int id = (Integer)argument;
104                     int index = mOverrides.indexOfKey(id);
105                     if (index >= 0) {
106                         Object value = mOverrides.valueAt(index);
107                         if (value == null) throw new Resources.NotFoundException();
108                         // Support for Resources.getString(resId, Object... formatArgs)
109                         if (value instanceof String
110                                 && invocationOnMock.getMethod().getName().equals("getString")
111                                 && invocationOnMock.getArguments().length > 1) {
112                             value = String.format(mResources.getConfiguration().getLocales().get(0),
113                                     (String) value,
114                                     Arrays.copyOfRange(invocationOnMock.getArguments(), 1,
115                                             invocationOnMock.getArguments().length));
116                         }
117                         return value;
118                     }
119                 } catch (Resources.NotFoundException e) {
120                     // Let through NotFoundException.
121                     throw e;
122                 } catch (Throwable t) {
123                     // Generic catching for the many things that can go wrong, fall back to
124                     // the real implementation.
125                     Log.i(TAG, "Falling back to default resources call " + t);
126                 }
127             }
128         }
129         return invocationOnMock.callRealMethod();
130     }
131 }
132