1 /*
2  * Copyright (C) 2021 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.applications;
18 
19 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
20 import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29 
30 import android.content.Context;
31 import android.os.Bundle;
32 
33 import com.android.settings.applications.appcompat.UserAspectRatioAppsPreferenceController;
34 import com.android.settings.testutils.XmlTestUtils;
35 import com.android.settings.testutils.shadow.ShadowUserManager;
36 import com.android.settings.widget.PreferenceCategoryController;
37 import com.android.settingslib.core.AbstractPreferenceController;
38 import com.android.settingslib.drawer.CategoryKey;
39 
40 import org.junit.Before;
41 import org.junit.Ignore;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.robolectric.RobolectricTestRunner;
45 import org.robolectric.RuntimeEnvironment;
46 import org.robolectric.annotation.Config;
47 import org.robolectric.shadows.androidx.fragment.FragmentController;
48 
49 import java.util.ArrayList;
50 import java.util.List;
51 
52 @RunWith(RobolectricTestRunner.class)
53 public class AppDashboardFragmentTest {
54 
55     private Context mContext;
56     private AppDashboardFragment mFragment;
57 
58     @Before
setUp()59     public void setUp() {
60         mContext = RuntimeEnvironment.application;
61         mFragment = new AppDashboardFragment();
62     }
63 
64     @Test
testCategory_isApps()65     public void testCategory_isApps() {
66         assertThat(mFragment.getCategoryKey()).isEqualTo(CategoryKey.CATEGORY_APPS);
67     }
68 
69     @Test
70     @Config(shadows = ShadowUserManager.class)
testPreferenceControllers_existInPreferenceScreen()71     public void testPreferenceControllers_existInPreferenceScreen() {
72         final List<String> preferenceScreenKeys = XmlTestUtils.getKeysFromPreferenceXml(mContext,
73                 mFragment.getPreferenceScreenResId());
74         final List<String> preferenceKeys = new ArrayList<>();
75 
76         for (AbstractPreferenceController controller : mFragment.createPreferenceControllers(
77                 mContext)) {
78             preferenceKeys.add(controller.getPreferenceKey());
79         }
80 
81         assertThat(preferenceScreenKeys).containsAtLeastElementsIn(preferenceKeys);
82     }
83 
84     @Ignore("b/313578776")
85     @Test
86     @Config(shadows = ShadowUserManager.class)
testAdvancedAppsCategory()87     public void testAdvancedAppsCategory() {
88         AppDashboardFragment fragment = FragmentController.of(new AppDashboardFragment(),
89                 new Bundle()).create().get();
90         UserAspectRatioAppsPreferenceController controller =
91                 mock(UserAspectRatioAppsPreferenceController.class);
92         final PreferenceCategoryController advancedController =
93                 fragment.getAdvancedAppsPreferenceCategoryController();
94         advancedController.setChildren(List.of(controller));
95 
96         when(controller.getAvailabilityStatus()).thenReturn(AVAILABLE);
97         assertTrue(advancedController.isAvailable());
98 
99         when(controller.getAvailabilityStatus()).thenReturn(CONDITIONALLY_UNAVAILABLE);
100         assertFalse(advancedController.isAvailable());
101     }
102 }
103