• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.content.res.Resources;
20 import android.util.SparseArray;
21 import android.util.SparseBooleanArray;
22 
23 import androidx.annotation.BoolRes;
24 import androidx.annotation.NonNull;
25 import androidx.annotation.PluralsRes;
26 import androidx.annotation.StringRes;
27 
28 import com.android.documentsui.R;
29 import com.android.documentsui.files.QuickViewIntentBuilder;
30 
31 import org.mockito.Mockito;
32 
33 import javax.annotation.Nullable;
34 
35 /**
36  * Abstract to avoid having to implement unnecessary Activity stuff.
37  * Instances are created using {@link #create()}.
38  */
39 public abstract class TestResources extends Resources {
40 
41     public SparseBooleanArray bools;
42     public SparseArray<String> strings;
43     public SparseArray<String> plurals;
44 
TestResources()45     public TestResources() {
46         super(ClassLoader.getSystemClassLoader());
47     }
48 
create()49     public static TestResources create() {
50         TestResources res = Mockito.mock(
51                 TestResources.class, Mockito.CALLS_REAL_METHODS);
52         res.bools = new SparseBooleanArray();
53         res.strings = new SparseArray<>();
54         res.plurals = new SparseArray<>();
55 
56         // quick view package can be set via system property on debug builds.
57         // unfortunately that interfers with testing. For that reason we have
58         // this little hack....QuickViewIntentBuilder will check for this value
59         // and ignore
60         res.setQuickViewerPackage(QuickViewIntentBuilder.IGNORE_DEBUG_PROP);
61         res.setDefaultDocumentsUri(TestProvidersAccess.DOWNLOADS.getUri().toString());
62         return res;
63     }
64 
setQuickViewerPackage(String packageName)65     public void setQuickViewerPackage(String packageName) {
66         strings.put(R.string.trusted_quick_viewer_package, packageName);
67     }
68 
setDefaultDocumentsUri(String uri)69     public void setDefaultDocumentsUri(String uri) {
70         strings.put(R.string.default_root_uri, uri);
71     }
72 
73     @Override
getBoolean(@oolRes int id)74     public final boolean getBoolean(@BoolRes int id) throws NotFoundException {
75         return bools.get(id);
76     }
77 
78     @Override
getString(@tringRes int id)79     public final @Nullable String getString(@StringRes int id) throws NotFoundException {
80         return strings.get(id);
81     }
82 
83     @Override
84     @NonNull
getString( @tringRes int id, Object... formatArgs)85     public final String getString(
86             @StringRes int id, Object... formatArgs) throws NotFoundException {
87         final String raw = getString(id);
88         return String.format(raw, formatArgs);
89     }
90 
91     @Override
getQuantityString(@luralsRes int id, int size)92     public final @Nullable String getQuantityString(@PluralsRes int id, int size) {
93         return plurals.get(id);
94     }
95 
96     @Override
getQuantityString(@luralsRes int id, int size, Object... args)97     public final @Nullable String getQuantityString(@PluralsRes int id, int size, Object... args) {
98         String format = getQuantityString(id, size);
99         if (format != null) {
100             return String.format(format, args);
101         }
102 
103         return null;
104     }
105 
106     @Override
getText(@tringRes int resId)107     public final CharSequence getText(@StringRes int resId) {
108         return getString(resId);
109     }
110 }
111