2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.android.settings.dashboard;
18 import static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.ArgumentMatchers.nullable;
21 import static org.mockito.Matchers.eq;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.never;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
27 import android.content.ComponentName;
28 import android.content.Context;
29 import android.content.Intent;
30 import android.os.Bundle;
31 import android.support.v7.preference.Preference;
32 import android.support.v7.preference.PreferenceManager;
33 import android.support.v7.preference.PreferenceScreen;
35 import com.android.settings.testutils.SettingsRobolectricTestRunner;
36 import com.android.settings.TestConfig;
37 import com.android.settings.core.PreferenceControllerMixin;
38 import com.android.settings.core.instrumentation.VisibilityLoggerMixin;
39 import com.android.settings.overlay.FeatureFactory;
40 import com.android.settings.testutils.FakeFeatureFactory;
41 import com.android.settingslib.core.AbstractPreferenceController;
42 import com.android.settingslib.drawer.DashboardCategory;
43 import com.android.settingslib.drawer.Tile;
44 import com.android.settingslib.drawer.TileUtils;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 import org.mockito.Answers;
50 import org.mockito.Mock;
51 import org.mockito.MockitoAnnotations;
52 import org.robolectric.annotation.Config;
53 import org.robolectric.shadows.ShadowApplication;
54 import org.robolectric.util.ReflectionHelpers;
56 import java.util.ArrayList;
57 import java.util.List;
59 @RunWith(SettingsRobolectricTestRunner.class)
60 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
61 public class DashboardFragmentTest {
63 @Mock(answer = Answers.RETURNS_DEEP_STUBS)
64 private Context mContext;
66 private DashboardCategory mDashboardCategory;
68 private FakeFeatureFactory mFakeFeatureFactory;
70 private ProgressiveDisclosureMixin mDisclosureMixin;
71 private TestFragment mTestFragment;
75 MockitoAnnotations.initMocks(this);
76 FakeFeatureFactory.setupForTest(mContext);
77 mFakeFeatureFactory = (FakeFeatureFactory) FeatureFactory.getFactory(mContext);
78 mDashboardCategory.tiles = new ArrayList<>();
79 mDashboardCategory.tiles.add(new Tile());
80 mTestFragment = new TestFragment(ShadowApplication.getInstance().getApplicationContext());
81 when(mFakeFeatureFactory.dashboardFeatureProvider.getProgressiveDisclosureMixin(
82 nullable(Context.class), eq(mTestFragment), nullable(Bundle.class)))
83 .thenReturn(mDisclosureMixin);
84 when(mFakeFeatureFactory.dashboardFeatureProvider
85 .getTilesForCategory(nullable(String.class)))
86 .thenReturn(mDashboardCategory);
87 mTestFragment.onAttach(ShadowApplication.getInstance().getApplicationContext());
88 when(mContext.getPackageName()).thenReturn("TestPackage");
92 public void testPreferenceControllerGetterSetter_shouldAddAndGetProperly() {
93 final TestPreferenceController controller = new TestPreferenceController(mContext);
94 mTestFragment.addPreferenceController(controller);
96 final TestPreferenceController retrievedController = mTestFragment.getPreferenceController
97 (TestPreferenceController.class);
99 assertThat(controller).isSameAs(retrievedController);
103 public void displayTilesAsPreference_shouldAddTilesWithIntent() {
104 when(mFakeFeatureFactory.dashboardFeatureProvider
105 .getTilesForCategory(nullable(String.class)))
106 .thenReturn(mDashboardCategory);
107 when(mFakeFeatureFactory.dashboardFeatureProvider
108 .getDashboardKeyForTile(nullable(Tile.class)))
109 .thenReturn("test_key");
110 mTestFragment.onCreatePreferences(new Bundle(), "rootKey");
112 verify(mDisclosureMixin).addPreference(nullable(PreferenceScreen.class),
113 nullable(Preference.class));
117 public void displayTilesAsPreference_shouldNotAddTilesWithoutIntent() {
118 mTestFragment.onCreatePreferences(new Bundle(), "rootKey");
120 verify(mTestFragment.mScreen, never()).addPreference(nullable(Preference.class));
124 public void displayTilesAsPreference_withEmptyCategory_shouldNotAddTiles() {
125 mDashboardCategory.tiles = null;
126 mTestFragment.onCreatePreferences(new Bundle(), "rootKey");
128 verify(mTestFragment.mScreen, never()).addPreference(nullable(Preference.class));
132 public void onAttach_shouldCreatePlaceholderPreferenceController() {
133 final AbstractPreferenceController controller = mTestFragment.getPreferenceController(
134 DashboardTilePlaceholderPreferenceController.class);
136 assertThat(controller).isNotNull();
140 public void updateState_skipUnavailablePrefs() {
141 final List<AbstractPreferenceController> preferenceControllers = mTestFragment.mControllers;
142 final AbstractPreferenceController mockController1 =
143 mock(AbstractPreferenceController.class);
144 final AbstractPreferenceController mockController2 =
145 mock(AbstractPreferenceController.class);
146 preferenceControllers.add(mockController1);
147 preferenceControllers.add(mockController2);
148 when(mockController1.isAvailable()).thenReturn(false);
149 when(mockController2.isAvailable()).thenReturn(true);
151 mTestFragment.onAttach(ShadowApplication.getInstance().getApplicationContext());
152 mTestFragment.onResume();
154 verify(mockController1, never()).getPreferenceKey();
155 verify(mockController2).getPreferenceKey();
159 public void tintTileIcon_hasMetadata_shouldReturnIconTintableMetadata() {
160 final Tile tile = new Tile();
161 final Bundle metaData = new Bundle();
162 tile.metaData = metaData;
164 metaData.putBoolean(TileUtils.META_DATA_PREFERENCE_ICON_TINTABLE, false);
165 assertThat(mTestFragment.tintTileIcon(tile)).isFalse();
167 metaData.putBoolean(TileUtils.META_DATA_PREFERENCE_ICON_TINTABLE, true);
168 assertThat(mTestFragment.tintTileIcon(tile)).isTrue();
173 public void tintTileIcon_noMetadata_shouldReturnPackageNameCheck() {
174 final Tile tile = new Tile();
175 final Intent intent = new Intent();
176 tile.intent = intent;
178 intent.setComponent(new ComponentName(
179 ShadowApplication.getInstance().getApplicationContext().getPackageName(),
181 assertThat(mTestFragment.tintTileIcon(tile)).isFalse();
183 intent.setComponent(new ComponentName("OtherPackage", "TestClass"));
184 assertThat(mTestFragment.tintTileIcon(tile)).isTrue();
187 public static class TestPreferenceController extends AbstractPreferenceController
188 implements PreferenceControllerMixin {
190 public TestPreferenceController(Context context) {
195 public boolean handlePreferenceTreeClick(Preference preference) {
200 public boolean isAvailable() {
205 public String getPreferenceKey() {
210 public void updateNonIndexableKeys(List<String> keys) {
215 public static class TestFragment extends DashboardFragment {
217 private final PreferenceManager mPreferenceManager;
218 private final Context mContext;
219 private final List<AbstractPreferenceController> mControllers;
221 public final PreferenceScreen mScreen;
223 public TestFragment(Context context) {
225 mPreferenceManager = mock(PreferenceManager.class);
226 mScreen = mock(PreferenceScreen.class);
227 mControllers = new ArrayList<>();
229 when(mPreferenceManager.getContext()).thenReturn(mContext);
230 ReflectionHelpers.setField(
231 this, "mVisibilityLoggerMixin", mock(VisibilityLoggerMixin.class));
235 public Context getContext() {
240 public int getMetricsCategory() {
245 public PreferenceScreen getPreferenceScreen() {
250 protected String getLogTag() {
255 protected int getPreferenceScreenResId() {
260 protected List<AbstractPreferenceController> getPreferenceControllers(Context context) {
265 public PreferenceManager getPreferenceManager() {
266 return mPreferenceManager;