• 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 package com.android.settings.dashboard;
17 
18 import android.content.Context;
19 import android.os.Bundle;
20 import android.support.v7.preference.Preference;
21 import android.support.v7.preference.PreferenceManager;
22 import android.support.v7.preference.PreferenceScreen;
23 
24 import com.android.settings.SettingsRobolectricTestRunner;
25 import com.android.settings.TestConfig;
26 import com.android.settings.core.PreferenceController;
27 import com.android.settings.core.instrumentation.VisibilityLoggerMixin;
28 import com.android.settings.overlay.FeatureFactory;
29 import com.android.settings.testutils.FakeFeatureFactory;
30 import com.android.settingslib.drawer.DashboardCategory;
31 import com.android.settingslib.drawer.Tile;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Answers;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.robolectric.annotation.Config;
40 import org.robolectric.shadows.ShadowApplication;
41 import org.robolectric.util.ReflectionHelpers;
42 
43 import java.util.ArrayList;
44 import java.util.List;
45 
46 import static com.google.common.truth.Truth.assertThat;
47 import static org.mockito.Matchers.any;
48 import static org.mockito.Matchers.anyString;
49 import static org.mockito.Matchers.eq;
50 import static org.mockito.Mockito.mock;
51 import static org.mockito.Mockito.never;
52 import static org.mockito.Mockito.verify;
53 import static org.mockito.Mockito.when;
54 
55 @RunWith(SettingsRobolectricTestRunner.class)
56 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
57 public class DashboardFragmentTest {
58 
59     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
60     private Context mContext;
61     @Mock
62     private DashboardCategory mDashboardCategory;
63     @Mock
64     private FakeFeatureFactory mFakeFeatureFactory;
65     @Mock
66     private ProgressiveDisclosureMixin mDisclosureMixin;
67     private TestFragment mTestFragment;
68 
69     @Before
setUp()70     public void setUp() {
71         MockitoAnnotations.initMocks(this);
72         FakeFeatureFactory.setupForTest(mContext);
73         mFakeFeatureFactory = (FakeFeatureFactory) FeatureFactory.getFactory(mContext);
74         mDashboardCategory.tiles = new ArrayList<>();
75         mDashboardCategory.tiles.add(new Tile());
76         mTestFragment = new TestFragment(ShadowApplication.getInstance().getApplicationContext());
77         when(mFakeFeatureFactory.dashboardFeatureProvider.getProgressiveDisclosureMixin(
78                 any(Context.class), eq(mTestFragment), any(Bundle.class)))
79                 .thenReturn(mDisclosureMixin);
80         when(mFakeFeatureFactory.dashboardFeatureProvider.getTilesForCategory(anyString()))
81                 .thenReturn(mDashboardCategory);
82         mTestFragment.onAttach(ShadowApplication.getInstance().getApplicationContext());
83         when(mContext.getPackageName()).thenReturn("TestPackage");
84     }
85 
86     @Test
testPreferenceControllerGetterSetter_shouldAddAndGetProperly()87     public void testPreferenceControllerGetterSetter_shouldAddAndGetProperly() {
88         final TestPreferenceController controller = new TestPreferenceController(mContext);
89         mTestFragment.addPreferenceController(controller);
90 
91         final TestPreferenceController retrievedController = mTestFragment.getPreferenceController
92                 (TestPreferenceController.class);
93 
94         assertThat(controller).isSameAs(retrievedController);
95     }
96 
97     @Test
displayTilesAsPreference_shouldAddTilesWithIntent()98     public void displayTilesAsPreference_shouldAddTilesWithIntent() {
99         when(mFakeFeatureFactory.dashboardFeatureProvider.getTilesForCategory(anyString()))
100                 .thenReturn(mDashboardCategory);
101         when(mFakeFeatureFactory.dashboardFeatureProvider.getDashboardKeyForTile(any(Tile.class)))
102                 .thenReturn("test_key");
103         mTestFragment.onCreatePreferences(new Bundle(), "rootKey");
104 
105         verify(mDisclosureMixin).addPreference(any(PreferenceScreen.class),
106                 any(Preference.class));
107     }
108 
109     @Test
displayTilesAsPreference_shouldNotAddTilesWithoutIntent()110     public void displayTilesAsPreference_shouldNotAddTilesWithoutIntent() {
111         mTestFragment.onCreatePreferences(new Bundle(), "rootKey");
112 
113         verify(mTestFragment.mScreen, never()).addPreference(any(Preference.class));
114     }
115 
116     @Test
displayTilesAsPreference_withEmptyCategory_shouldNotAddTiles()117     public void displayTilesAsPreference_withEmptyCategory_shouldNotAddTiles() {
118         mDashboardCategory.tiles = null;
119         mTestFragment.onCreatePreferences(new Bundle(), "rootKey");
120 
121         verify(mTestFragment.mScreen, never()).addPreference(any(Preference.class));
122     }
123 
124     @Test
onAttach_shouldCreatePlaceholderPreferenceController()125     public void onAttach_shouldCreatePlaceholderPreferenceController() {
126         final PreferenceController controller = mTestFragment.getPreferenceController(
127                 DashboardTilePlaceholderPreferenceController.class);
128 
129         assertThat(controller).isNotNull();
130     }
131 
132     @Test
updateState_skipUnavailablePrefs()133     public void updateState_skipUnavailablePrefs() {
134         final List<PreferenceController> preferenceControllers = mTestFragment.mControllers;
135         final PreferenceController mockController1 = mock(PreferenceController.class);
136         final PreferenceController mockController2 = mock(PreferenceController.class);
137         preferenceControllers.add(mockController1);
138         preferenceControllers.add(mockController2);
139         when(mockController1.isAvailable()).thenReturn(false);
140         when(mockController2.isAvailable()).thenReturn(true);
141 
142         mTestFragment.onAttach(ShadowApplication.getInstance().getApplicationContext());
143         mTestFragment.onResume();
144 
145         verify(mockController1, never()).getPreferenceKey();
146         verify(mockController2).getPreferenceKey();
147     }
148 
149     public static class TestPreferenceController extends PreferenceController {
150 
TestPreferenceController(Context context)151         public TestPreferenceController(Context context) {
152             super(context);
153         }
154 
155         @Override
handlePreferenceTreeClick(Preference preference)156         public boolean handlePreferenceTreeClick(Preference preference) {
157             return false;
158         }
159 
160         @Override
isAvailable()161         public boolean isAvailable() {
162             return false;
163         }
164 
165         @Override
getPreferenceKey()166         public String getPreferenceKey() {
167             return null;
168         }
169 
170         @Override
updateNonIndexableKeys(List<String> keys)171         public void updateNonIndexableKeys(List<String> keys) {
172 
173         }
174     }
175 
176     public static class TestFragment extends DashboardFragment {
177 
178         private final PreferenceManager mPreferenceManager;
179         private final Context mContext;
180         private final List<PreferenceController> mControllers;
181 
182         public final PreferenceScreen mScreen;
183 
TestFragment(Context context)184         public TestFragment(Context context) {
185             mContext = context;
186             mPreferenceManager = mock(PreferenceManager.class);
187             mScreen = mock(PreferenceScreen.class);
188             mControllers = new ArrayList<>();
189 
190             when(mPreferenceManager.getContext()).thenReturn(mContext);
191             ReflectionHelpers.setField(
192                     this, "mVisibilityLoggerMixin", mock(VisibilityLoggerMixin.class));
193         }
194 
195         @Override
getContext()196         public Context getContext() {
197             return mContext;
198         }
199 
200         @Override
getMetricsCategory()201         public int getMetricsCategory() {
202             return 0;
203         }
204 
205         @Override
getPreferenceScreen()206         public PreferenceScreen getPreferenceScreen() {
207             return mScreen;
208         }
209 
210         @Override
getLogTag()211         protected String getLogTag() {
212             return "TEST_FRAG";
213         }
214 
215         @Override
getPreferenceScreenResId()216         protected int getPreferenceScreenResId() {
217             return 0;
218         }
219 
220         @Override
getPreferenceControllers(Context context)221         protected List<PreferenceController> getPreferenceControllers(Context context) {
222             return mControllers;
223         }
224 
225         @Override
getPreferenceManager()226         public PreferenceManager getPreferenceManager() {
227             return mPreferenceManager;
228         }
229     }
230 
231 }
232