Home | History | Annotate | Download | only in testutils
      1 /*
      2  * Copyright (C) 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.car.settings.testutils;
     18 
     19 import static org.robolectric.shadow.api.Shadow.directlyOn;
     20 
     21 import android.annotation.ColorRes;
     22 import android.annotation.DimenRes;
     23 import android.annotation.Nullable;
     24 import android.content.res.Resources;
     25 
     26 import org.robolectric.annotation.Implementation;
     27 import org.robolectric.annotation.Implements;
     28 import org.robolectric.annotation.RealObject;
     29 import org.robolectric.shadows.ShadowResources;
     30 
     31 /**
     32  * Overrides ShadowResources to return a dummy value if resources are not found.
     33  *
     34  * Reason for this is the Design Support Library has moved to prebuilts but robolectric is unable
     35  * to pull resources from binaries at this time.
     36  * TODO: Delete this shadow when robolectric supports binary resource loading
     37  */
     38 @Implements(value = Resources.class, inheritImplementationMethods = true)
     39 public class CarSettingsShadowResources extends ShadowResources {
     40     @RealObject
     41     public Resources realResources;
     42 
     43     @Implementation
     44     public int getDimensionPixelSize(@DimenRes int id) {
     45         try {
     46             return directlyOn(realResources, Resources.class).getDimensionPixelSize(id);
     47         } catch (Resources.NotFoundException e1) {
     48             return 0;
     49         }
     50     }
     51 
     52     @Implementation
     53     public int getDimensionPixelOffset(@DimenRes int id) {
     54         try {
     55             return directlyOn(realResources, Resources.class).getDimensionPixelOffset(id);
     56         } catch (Resources.NotFoundException e1) {
     57             return 0;
     58         }
     59     }
     60 
     61     @Implementation
     62     public int getColor(@ColorRes int id, @Nullable Resources.Theme theme) {
     63         try {
     64             return directlyOn(realResources, Resources.class).getColor(id, theme);
     65         } catch (Resources.NotFoundException e1) {
     66             return 0;
     67         }
     68     }
     69 }
     70 
     71