Home | History | Annotate | Download | only in testing
      1 /*
      2  * Copyright (C) 2016 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.documentsui.testing;
     18 
     19 import android.annotation.BoolRes;
     20 import android.annotation.NonNull;
     21 import android.annotation.StringRes;
     22 import android.content.res.Resources;
     23 import android.util.SparseArray;
     24 import android.util.SparseBooleanArray;
     25 
     26 import com.android.documentsui.R;
     27 import com.android.documentsui.files.QuickViewIntentBuilder;
     28 
     29 import org.mockito.Mockito;
     30 
     31 import javax.annotation.Nullable;
     32 
     33 /**
     34  * Abstract to avoid having to implement unnecessary Activity stuff.
     35  * Instances are created using {@link #create()}.
     36  */
     37 public abstract class TestResources extends Resources {
     38 
     39     public SparseBooleanArray bools;
     40     public SparseArray<String> strings;
     41 
     42     public TestResources() {
     43         super(ClassLoader.getSystemClassLoader());
     44     }
     45 
     46     public static TestResources create() {
     47         TestResources res = Mockito.mock(
     48                 TestResources.class, Mockito.CALLS_REAL_METHODS);
     49         res.bools = new SparseBooleanArray();
     50         res.strings = new SparseArray<>();
     51 
     52         res.setProductivityDeviceEnabled(false);
     53 
     54         // quick view package can be set via system property on debug builds.
     55         // unfortunately that interfers with testing. For that reason we have
     56         // this little hack....QuickViewIntentBuilder will check for this value
     57         // and ignore
     58         res.setQuickViewerPackage(QuickViewIntentBuilder.IGNORE_DEBUG_PROP);
     59         return res;
     60     }
     61 
     62     public void setQuickViewerPackage(String packageName) {
     63         strings.put(R.string.trusted_quick_viewer_package, packageName);
     64     }
     65 
     66     public void setProductivityDeviceEnabled(boolean enabled) {
     67         bools.put(R.bool.show_documents_root, enabled);
     68     }
     69 
     70     @Override
     71     public final boolean getBoolean(@BoolRes int id) throws NotFoundException {
     72         return bools.get(id);
     73     }
     74 
     75     @Override
     76     public final @Nullable String getString(@StringRes int id) throws NotFoundException {
     77         return strings.get(id);
     78     }
     79 
     80     @NonNull
     81     public final String getString(
     82             @StringRes int id, Object... formatArgs) throws NotFoundException {
     83         return getString(id);
     84     }
     85 
     86     public final CharSequence getText(@StringRes int resId) {
     87         return getString(resId);
     88     }
     89 }
     90