1 /*
2  * Copyright 2019 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.settings.development.compat;
18 
19 import static com.android.internal.compat.OverrideAllowedState.ALLOWED;
20 import static com.android.internal.compat.OverrideAllowedState.DISABLED_NOT_DEBUGGABLE;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.anyLong;
26 import static org.mockito.ArgumentMatchers.anyString;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.spy;
30 import static org.mockito.Mockito.when;
31 
32 import android.compat.Compatibility.ChangeConfig;
33 import android.content.Context;
34 import android.content.pm.ApplicationInfo;
35 import android.content.pm.PackageManager;
36 import android.content.pm.PackageManager.NameNotFoundException;
37 import android.graphics.drawable.Drawable;
38 import android.os.RemoteException;
39 
40 import androidx.preference.Preference;
41 import androidx.preference.PreferenceCategory;
42 import androidx.preference.PreferenceManager;
43 import androidx.preference.PreferenceScreen;
44 import androidx.preference.SwitchPreferenceCompat;
45 import androidx.test.core.app.ApplicationProvider;
46 
47 import com.android.internal.compat.CompatibilityChangeConfig;
48 import com.android.internal.compat.CompatibilityChangeInfo;
49 import com.android.internal.compat.IOverrideValidator;
50 import com.android.internal.compat.IPlatformCompat;
51 import com.android.internal.compat.OverrideAllowedState;
52 import com.android.settings.R;
53 
54 import org.junit.Before;
55 import org.junit.Rule;
56 import org.junit.Test;
57 import org.junit.runner.RunWith;
58 import org.mockito.Mock;
59 import org.mockito.junit.MockitoJUnit;
60 import org.mockito.junit.MockitoRule;
61 import org.robolectric.RobolectricTestRunner;
62 
63 import java.util.ArrayList;
64 import java.util.Arrays;
65 import java.util.HashSet;
66 import java.util.List;
67 import java.util.Set;
68 
69 @RunWith(RobolectricTestRunner.class)
70 public class PlatformCompatDashboardTest {
71     @Rule
72     public final MockitoRule mMockitoRule = MockitoJUnit.rule();
73     private PlatformCompatDashboard mDashboard;
74 
75     @Mock
76     private IPlatformCompat mPlatformCompat;
77     private PreferenceScreen mPreferenceScreen;
78     @Mock
79     private ApplicationInfo mApplicationInfo;
80     @Mock
81     private PreferenceManager mPreferenceManager;
82     @Mock
83     private IOverrideValidator mOverrideValidator;
84     @Mock
85     private PackageManager mPackageManager;
86 
87     private Context mContext;
88     private CompatibilityChangeInfo[] mChanges;
89     private static final String APP_NAME = "foo.bar.baz";
90 
91     @Before
setUp()92     public void setUp() throws RemoteException, NameNotFoundException {
93         mChanges = new CompatibilityChangeInfo[5];
94         mChanges[0] = new CompatibilityChangeInfo(
95                 1L, "Default_Enabled", 0, 0, false, false, "", false);
96         mChanges[1] = new CompatibilityChangeInfo(
97                 2L, "Default_Disabled", 0, 0, true, false, "", false);
98         mChanges[2] = new CompatibilityChangeInfo(
99                 3L, "Enabled_Since_SDK_1_1", -1, 1, false, false, "", false);
100         mChanges[3] = new CompatibilityChangeInfo(
101                 4L, "Enabled_Since_SDK_1_2", -1, 1, false, false, "", false);
102         mChanges[4] = new CompatibilityChangeInfo(
103                 5L, "Enabled_Since_SDK_2", -1, 2, false, false, "", false);
104         when(mPlatformCompat.listUIChanges()).thenReturn(mChanges);
105         when(mPlatformCompat.getOverrideValidator()).thenReturn(mOverrideValidator);
106         // By default, allow any change
107         when(mOverrideValidator.getOverrideAllowedState(anyLong(),anyString()))
108             .thenReturn(new OverrideAllowedState(ALLOWED, -1, -1));
109         mContext = spy(ApplicationProvider.getApplicationContext());
110         mPreferenceManager = new PreferenceManager(mContext);
111         mPreferenceScreen = mPreferenceManager.createPreferenceScreen(mContext);
112         mApplicationInfo.packageName = APP_NAME;
113         mDashboard = spy(new PlatformCompatDashboard());
114         mDashboard.mSelectedApp = APP_NAME;
115         doReturn(mApplicationInfo).when(mDashboard).getApplicationInfo();
116         doReturn(mContext).when(mDashboard).getContext();
117         doReturn(mPlatformCompat).when(mDashboard).getPlatformCompat();
118         doReturn(mPreferenceScreen).when(mDashboard).getPreferenceScreen();
119         doReturn(mPreferenceManager).when(mDashboard).getPreferenceManager();
120         doReturn(mPackageManager).when(mContext).getPackageManager();
121     }
122 
123     @Test
getHelpResource_shouldNotHaveHelpResource()124     public void getHelpResource_shouldNotHaveHelpResource() {
125         assertThat(mDashboard.getHelpResource()).isEqualTo(0);
126     }
127 
128     @Test
getPreferenceScreenResId_shouldBePlatformCompatSettingsResId()129     public void getPreferenceScreenResId_shouldBePlatformCompatSettingsResId() {
130         assertThat(mDashboard.getPreferenceScreenResId())
131                 .isEqualTo(R.xml.platform_compat_settings);
132     }
133 
134     @Test
createAppPreference_targetSdkEquals1_summaryReturnsAppNameAndTargetSdk()135     public void createAppPreference_targetSdkEquals1_summaryReturnsAppNameAndTargetSdk() {
136         mApplicationInfo.targetSdkVersion = 1;
137         Drawable icon = mock(Drawable.class);
138         when(mApplicationInfo.loadIcon(any(PackageManager.class))).thenReturn(icon);
139 
140         Preference appPreference = mDashboard.createAppPreference(mApplicationInfo);
141 
142         assertThat(appPreference.getSummary()).isEqualTo(mContext.getResources().getString(
143                 R.string.platform_compat_selected_app_summary, APP_NAME, 1));
144     }
145 
146     @Test
createPreferenceForChange_defaultEnabledChange_createCheckedEntry()147     public void createPreferenceForChange_defaultEnabledChange_createCheckedEntry() {
148         CompatibilityChangeInfo enabledChange = mChanges[0];
149         CompatibilityChangeConfig config = new CompatibilityChangeConfig(
150                 new ChangeConfig(new HashSet<Long>(Arrays.asList(enabledChange.getId())),
151                         new HashSet<Long>()));
152 
153         Preference enabledPreference = mDashboard.createPreferenceForChange(mContext, enabledChange,
154                 config);
155 
156         SwitchPreferenceCompat enabledSwitchPreference = (SwitchPreferenceCompat) enabledPreference;
157 
158         assertThat(enabledPreference.getSummary()).isEqualTo(mChanges[0].getName());
159         assertThat(enabledPreference instanceof SwitchPreferenceCompat).isTrue();
160         assertThat(enabledSwitchPreference.isChecked()).isTrue();
161         assertThat(enabledSwitchPreference.isEnabled()).isTrue();
162     }
163 
164     @Test
createPreferenceForChange_defaultDisabledChange_createUncheckedEntry()165     public void createPreferenceForChange_defaultDisabledChange_createUncheckedEntry() {
166         CompatibilityChangeInfo disabledChange = mChanges[1];
167         CompatibilityChangeConfig config = new CompatibilityChangeConfig(
168                 new ChangeConfig(new HashSet<Long>(),
169                         new HashSet<Long>(Arrays.asList(disabledChange.getId()))));
170 
171         Preference disabledPreference = mDashboard.createPreferenceForChange(mContext,
172                 disabledChange, config);
173 
174         assertThat(disabledPreference.getSummary()).isEqualTo(mChanges[1].getName());
175         SwitchPreferenceCompat disabledSwitchPreference =
176                 (SwitchPreferenceCompat) disabledPreference;
177         assertThat(disabledSwitchPreference.isChecked()).isFalse();
178         assertThat(disabledSwitchPreference.isEnabled()).isTrue();
179     }
180 
181     @Test
createPreferenceForChange_cannotOverride_createDisabledEntry()182     public void createPreferenceForChange_cannotOverride_createDisabledEntry()
183                     throws RemoteException {
184         CompatibilityChangeInfo enabledChange = mChanges[0];
185         CompatibilityChangeConfig config = new CompatibilityChangeConfig(
186                 new ChangeConfig(new HashSet<Long>(Arrays.asList(enabledChange.getId())),
187                         new HashSet<Long>()));
188         when(mOverrideValidator.getOverrideAllowedState(anyLong(),anyString()))
189             .thenReturn(new OverrideAllowedState(DISABLED_NOT_DEBUGGABLE, -1, -1));
190 
191         Preference preference = mDashboard.createPreferenceForChange(mContext, enabledChange,
192                 config);
193 
194         SwitchPreferenceCompat switchPreference = (SwitchPreferenceCompat) preference;
195 
196         assertThat(preference.getSummary()).isEqualTo(mChanges[0].getName());
197         assertThat(preference instanceof SwitchPreferenceCompat).isTrue();
198         assertThat(switchPreference.isChecked()).isTrue();
199         assertThat(switchPreference.isEnabled()).isFalse();
200     }
201 
202     @Test
createChangeCategoryPreference_enabledAndDisabled_hasTitleAndEntries()203     public void createChangeCategoryPreference_enabledAndDisabled_hasTitleAndEntries() {
204         Set<Long> enabledChanges = new HashSet<>();
205         enabledChanges.add(mChanges[0].getId());
206         enabledChanges.add(mChanges[1].getId());
207         enabledChanges.add(mChanges[2].getId());
208         Set<Long> disabledChanges = new HashSet<>();
209         disabledChanges.add(mChanges[3].getId());
210         disabledChanges.add(mChanges[4].getId());
211         CompatibilityChangeConfig config = new CompatibilityChangeConfig(
212                 new ChangeConfig(enabledChanges, disabledChanges));
213         List<CompatibilityChangeInfo> changesToAdd = new ArrayList<>();
214         for (int i = 0; i < mChanges.length; ++i) {
215             changesToAdd.add(new CompatibilityChangeInfo(mChanges[i].getId(), mChanges[i].getName(),
216                     -1, mChanges[i].getEnableSinceTargetSdk(), mChanges[i].getDisabled(),
217                     mChanges[i].getLoggingOnly(), mChanges[i].getDescription(),
218                     mChanges[i].getOverridable()));
219         }
220 
221         PreferenceCategory category = mDashboard.createChangeCategoryPreference(changesToAdd,
222         config, "foo");
223 
224         assertThat(category.getTitle()).isEqualTo("foo");
225         assertThat(category.getPreferenceCount()).isEqualTo(mChanges.length);
226         for (int i = 0; i < mChanges.length; ++i) {
227             Preference childPreference = category.getPreference(i);
228             assertThat(childPreference instanceof SwitchPreferenceCompat).isTrue();
229         }
230     }
231 }
232