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