Reduce log spam from TestableResources

Bug: 277364068
Test: Run WindowMagnificationControllerTest in IDE, watch logcat
Change-Id: I47dce1b2cc81f99aa20255b17b444e3f03142a46
This commit is contained in:
David Saff
2023-04-07 15:36:25 -04:00
parent f304ffc940
commit c8c7200f01

View File

@@ -59,7 +59,8 @@ public class TestableResources {
* Since resource ids are unique there is a single addOverride that will override the value
* whenever it is gotten regardless of which method is used (i.e. getColor or getDrawable).
* </p>
* @param id The resource id to be overridden
*
* @param id The resource id to be overridden
* @param value The value of the resource, null to cause a {@link Resources.NotFoundException}
* when gotten.
*/
@@ -74,28 +75,33 @@ public class TestableResources {
* cause a {@link Resources.NotFoundException} whereas removing the override will actually
* switch back to returning the default/real value of the resource.
* </p>
* @param id
*/
public void removeOverride(int id) {
mOverrides.remove(id);
}
private Object answer(InvocationOnMock invocationOnMock) throws Throwable {
try {
int id = invocationOnMock.getArgument(0);
int index = mOverrides.indexOfKey(id);
if (index >= 0) {
Object value = mOverrides.valueAt(index);
if (value == null) throw new Resources.NotFoundException();
return value;
// Only try to override methods with an integer first argument
if (invocationOnMock.getArguments().length > 0) {
Object argument = invocationOnMock.getArgument(0);
if (argument instanceof Integer) {
try {
int id = (Integer)argument;
int index = mOverrides.indexOfKey(id);
if (index >= 0) {
Object value = mOverrides.valueAt(index);
if (value == null) throw new Resources.NotFoundException();
return value;
}
} catch (Resources.NotFoundException e) {
// Let through NotFoundException.
throw e;
} catch (Throwable t) {
// Generic catching for the many things that can go wrong, fall back to
// the real implementation.
Log.i(TAG, "Falling back to default resources call " + t);
}
}
} catch (Resources.NotFoundException e) {
// Let through NotFoundException.
throw e;
} catch (Throwable t) {
// Generic catching for the many things that can go wrong, fall back to
// the real implementation.
Log.i(TAG, "Falling back to default resources call " + t);
}
return invocationOnMock.callRealMethod();
}