• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 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.common;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 
22 import androidx.preference.Preference;
23 
24 import java.util.HashSet;
25 import java.util.Set;
26 
27 /**
28  * Concrete {@link PreferenceController} with methods for verifying behavior in tests.
29  */
30 public class FakePreferenceController extends PreferenceController<Preference> {
31 
32     @AvailabilityStatus
33     private int mAvailabilityStatus;
34     private int mCheckInitializedCallCount;
35     private int mOnCreateInternalCallCount;
36     private int mOnStartInternalCallCount;
37     private int mOnResumeInternalCallCount;
38     private int mOnPauseInternalCallCount;
39     private int mOnStopInternalCallCount;
40     private int mOnDestroyInternalCallCount;
41     private int mUpdateStateCallCount;
42     private Preference mUpdateStateArg;
43     private int mHandlePreferenceChangedCallCount;
44     private Preference mHandlePreferenceChangedPreferenceArg;
45     private Object mHandlePreferenceChangedValueArg;
46     private int mHandlePreferenceClickedCallCount;
47     private Preference mHandlePreferenceClickedArg;
48     private boolean mAllIgnoresUxRestrictions = false;
49     private Set<String> mPreferencesIgnoringUxRestrictions = new HashSet<>();
50 
FakePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)51     public FakePreferenceController(Context context, String preferenceKey,
52             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
53         super(context, preferenceKey, fragmentController, uxRestrictions);
54         mAvailabilityStatus = super.getAvailabilityStatus();
55     }
56 
57     @Override
getPreferenceType()58     protected Class<Preference> getPreferenceType() {
59         return Preference.class;
60     }
61 
62     @Override
checkInitialized()63     protected void checkInitialized() {
64         mCheckInitializedCallCount++;
65     }
66 
getCheckInitializedCallCount()67     int getCheckInitializedCallCount() {
68         return mCheckInitializedCallCount;
69     }
70 
71     @Override
72     @AvailabilityStatus
getAvailabilityStatus()73     protected int getAvailabilityStatus() {
74         return mAvailabilityStatus;
75     }
76 
setAvailabilityStatus(@vailabilityStatus int availabilityStatus)77     void setAvailabilityStatus(@AvailabilityStatus int availabilityStatus) {
78         mAvailabilityStatus = availabilityStatus;
79     }
80 
81     @Override
onCreateInternal()82     protected void onCreateInternal() {
83         mOnCreateInternalCallCount++;
84     }
85 
getOnCreateInternalCallCount()86     int getOnCreateInternalCallCount() {
87         return mOnCreateInternalCallCount;
88     }
89 
90     @Override
onStartInternal()91     protected void onStartInternal() {
92         mOnStartInternalCallCount++;
93     }
94 
getOnStartInternalCallCount()95     int getOnStartInternalCallCount() {
96         return mOnStartInternalCallCount;
97     }
98 
99     @Override
onResumeInternal()100     protected void onResumeInternal() {
101         mOnResumeInternalCallCount++;
102     }
103 
getOnResumeInternalCallCount()104     int getOnResumeInternalCallCount() {
105         return mOnResumeInternalCallCount;
106     }
107 
108     @Override
onPauseInternal()109     protected void onPauseInternal() {
110         mOnPauseInternalCallCount++;
111     }
112 
getOnPauseInternalCallCount()113     int getOnPauseInternalCallCount() {
114         return mOnPauseInternalCallCount;
115     }
116 
117     @Override
onStopInternal()118     protected void onStopInternal() {
119         mOnStopInternalCallCount++;
120     }
121 
getOnStopInternalCallCount()122     int getOnStopInternalCallCount() {
123         return mOnStopInternalCallCount;
124     }
125 
126     @Override
onDestroyInternal()127     protected void onDestroyInternal() {
128         mOnDestroyInternalCallCount++;
129     }
130 
getOnDestroyInternalCallCount()131     int getOnDestroyInternalCallCount() {
132         return mOnDestroyInternalCallCount;
133     }
134 
135     @Override
updateState(Preference preference)136     protected void updateState(Preference preference) {
137         mUpdateStateArg = preference;
138         mUpdateStateCallCount++;
139     }
140 
getUpdateStateArg()141     Preference getUpdateStateArg() {
142         return mUpdateStateArg;
143     }
144 
getUpdateStateCallCount()145     int getUpdateStateCallCount() {
146         return mUpdateStateCallCount;
147     }
148 
149     @Override
handlePreferenceChanged(Preference preference, Object newValue)150     protected boolean handlePreferenceChanged(Preference preference, Object newValue) {
151         mHandlePreferenceChangedCallCount++;
152         mHandlePreferenceChangedPreferenceArg = preference;
153         mHandlePreferenceChangedValueArg = newValue;
154         return super.handlePreferenceChanged(preference, newValue);
155     }
156 
getHandlePreferenceChangedCallCount()157     int getHandlePreferenceChangedCallCount() {
158         return mHandlePreferenceChangedCallCount;
159     }
160 
getHandlePreferenceChangedPreferenceArg()161     Preference getHandlePreferenceChangedPreferenceArg() {
162         return mHandlePreferenceChangedPreferenceArg;
163     }
164 
getHandlePreferenceChangedValueArg()165     Object getHandlePreferenceChangedValueArg() {
166         return mHandlePreferenceChangedValueArg;
167     }
168 
169     @Override
handlePreferenceClicked(Preference preference)170     protected boolean handlePreferenceClicked(Preference preference) {
171         mHandlePreferenceClickedCallCount++;
172         mHandlePreferenceClickedArg = preference;
173         return super.handlePreferenceClicked(preference);
174     }
175 
getHandlePreferenceClickedCallCount()176     int getHandlePreferenceClickedCallCount() {
177         return mHandlePreferenceClickedCallCount;
178     }
179 
getHandlePreferenceClickedArg()180     Preference getHandlePreferenceClickedArg() {
181         return mHandlePreferenceClickedArg;
182     }
183 
184     @Override
isUxRestrictionsIgnored(boolean allIgnores, Set preferencesThatIgnore)185     protected boolean isUxRestrictionsIgnored(boolean allIgnores, Set preferencesThatIgnore) {
186         return super.isUxRestrictionsIgnored(mAllIgnoresUxRestrictions,
187                 mPreferencesIgnoringUxRestrictions);
188     }
189 
setUxRestrictionsIgnoredConfig(boolean allIgnore, Set preferencesThatIgnore)190     protected void setUxRestrictionsIgnoredConfig(boolean allIgnore, Set preferencesThatIgnore) {
191         mAllIgnoresUxRestrictions = allIgnore;
192         mPreferencesIgnoringUxRestrictions = preferencesThatIgnore;
193     }
194 
195 }
196