| Jason Monk | 77f1b05 | 2017-05-02 14:22:21 -0400 | [diff] [blame] | 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.withSettings; |
| 19 | |
| 20 | import android.content.Context; |
| 21 | import android.content.res.Resources; |
| 22 | import android.util.Log; |
| 23 | import android.util.SparseArray; |
| 24 | |
| 25 | import org.mockito.invocation.InvocationOnMock; |
| 26 | |
| 27 | /** |
| 28 | * Provides a version of Resources that defaults to all existing resources, but can have ids |
| 29 | * changed to return specific values. |
| 30 | * <p> |
| 31 | * TestableResources are lazily initialized, be sure to call |
| 32 | * {@link TestableContext#ensureTestableResources} before your tested code has an opportunity |
| 33 | * to cache {@link Context#getResources}. |
| 34 | * </p> |
| 35 | */ |
| 36 | public class TestableResources { |
| 37 | |
| 38 | private static final String TAG = "TestableResources"; |
| 39 | private final Resources mResources; |
| 40 | private final SparseArray<Object> mOverrides = new SparseArray<>(); |
| 41 | |
| Adrian Roos | e1856fe | 2017-11-24 19:39:12 +0100 | [diff] [blame] | 42 | /** Creates a TestableResources instance that calls through to the given real Resources. */ |
| 43 | public TestableResources(Resources realResources) { |
| Jason Monk | 77f1b05 | 2017-05-02 14:22:21 -0400 | [diff] [blame] | 44 | mResources = mock(Resources.class, withSettings() |
| 45 | .spiedInstance(realResources) |
| 46 | .defaultAnswer(this::answer)); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Gets the implementation of Resources that will return overridden values when called. |
| 51 | */ |
| 52 | public Resources getResources() { |
| 53 | return mResources; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Sets the return value for the specified resource id. |
| 58 | * <p> |
| 59 | * Since resource ids are unique there is a single addOverride that will override the value |
| 60 | * whenever it is gotten regardless of which method is used (i.e. getColor or getDrawable). |
| 61 | * </p> |
| 62 | * @param id The resource id to be overridden |
| 63 | * @param value The value of the resource, null to cause a {@link Resources.NotFoundException} |
| 64 | * when gotten. |
| 65 | */ |
| 66 | public void addOverride(int id, Object value) { |
| 67 | mOverrides.put(id, value); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Removes the override for the specified id. |
| 72 | * <p> |
| 73 | * This should be called over addOverride(id, null), because specifying a null value will |
| 74 | * cause a {@link Resources.NotFoundException} whereas removing the override will actually |
| 75 | * switch back to returning the default/real value of the resource. |
| 76 | * </p> |
| 77 | * @param id |
| 78 | */ |
| 79 | public void removeOverride(int id) { |
| 80 | mOverrides.remove(id); |
| 81 | } |
| 82 | |
| 83 | private Object answer(InvocationOnMock invocationOnMock) throws Throwable { |
| 84 | try { |
| 85 | int id = invocationOnMock.getArgument(0); |
| 86 | int index = mOverrides.indexOfKey(id); |
| 87 | if (index >= 0) { |
| 88 | Object value = mOverrides.valueAt(index); |
| 89 | if (value == null) throw new Resources.NotFoundException(); |
| 90 | return value; |
| 91 | } |
| 92 | } catch (Resources.NotFoundException e) { |
| 93 | // Let through NotFoundException. |
| 94 | throw e; |
| 95 | } catch (Throwable t) { |
| 96 | // Generic catching for the many things that can go wrong, fall back to |
| 97 | // the real implementation. |
| 98 | Log.i(TAG, "Falling back to default resources call " + t); |
| 99 | } |
| 100 | return invocationOnMock.callRealMethod(); |
| 101 | } |
| 102 | } |