Insert dynamic dashboard tiles into optional placeholders

Insteads of inserting tile onto screen using absolute priority values,
now each page can have a placeholder preference, and at run time we will
add dynamic dashboard tiles to placeholder's place.

Bug: 32827787
Test: RunSettingsRoboTests
Change-Id: I1fe9e11dce4eb6fb4a9b56af05a2b8e5cdae00d2
This commit is contained in:
Fan Zhang
2016-11-11 13:23:21 -08:00
parent 3b4d72721d
commit 9dc9c6174c
10 changed files with 223 additions and 29 deletions

View File

@@ -77,7 +77,7 @@ public class DashboardFeatureProviderImplTest {
tile.metaData = new Bundle();
tile.metaData.putString(SettingsActivity.META_DATA_KEY_FRAGMENT_CLASS, "HI");
tile.priority = 10;
mImpl.bindPreferenceToTile(mActivity, preference, tile, "123");
mImpl.bindPreferenceToTile(mActivity, preference, tile, "123", Preference.DEFAULT_ORDER);
assertThat(preference.getTitle()).isEqualTo(tile.title);
assertThat(preference.getSummary()).isEqualTo(tile.summary);
@@ -95,7 +95,9 @@ public class DashboardFeatureProviderImplTest {
tile.metaData = new Bundle();
tile.priority = 10;
tile.intent = new Intent();
mImpl.bindPreferenceToTile(mActivity, preference, tile, "123");
tile.intent.setComponent(new ComponentName("pkg", "class"));
mImpl.bindPreferenceToTile(mActivity, preference, tile, "123", Preference.DEFAULT_ORDER);
assertThat(preference.getFragment()).isNull();
assertThat(preference.getOnPreferenceClickListener()).isNotNull();
@@ -112,11 +114,12 @@ public class DashboardFeatureProviderImplTest {
tile.userHandle.add(mock(UserHandle.class));
tile.userHandle.add(mock(UserHandle.class));
tile.intent = new Intent();
tile.intent.setComponent(new ComponentName("pkg", "class"));
when(mActivity.getApplicationContext().getSystemService(Context.USER_SERVICE))
.thenReturn(mUserManager);
mImpl.bindPreferenceToTile(mActivity, preference, tile, "123");
mImpl.bindPreferenceToTile(mActivity, preference, tile, "123", Preference.DEFAULT_ORDER);
preference.getOnPreferenceClickListener().onPreferenceClick(null);
verify(mActivity).getFragmentManager();
@@ -129,9 +132,23 @@ public class DashboardFeatureProviderImplTest {
final Tile tile = new Tile();
tile.intent = new Intent();
tile.intent.setComponent(new ComponentName("pkg", "class"));
mImpl.bindPreferenceToTile(mActivity, preference, tile, null /* key */);
mImpl.bindPreferenceToTile(mActivity, preference, tile, null /* key */
, Preference.DEFAULT_ORDER);
assertThat(preference.getKey()).isNotNull();
assertThat(preference.getOrder()).isEqualTo(Preference.DEFAULT_ORDER);
}
@Test
public void bindPreference_withBaseOrder_shouldOffsetPriority() {
final int baseOrder = 100;
final Preference preference = new Preference(
ShadowApplication.getInstance().getApplicationContext());
final Tile tile = new Tile();
tile.metaData = new Bundle();
tile.priority = 10;
mImpl.bindPreferenceToTile(mActivity, preference, tile, "123", baseOrder);
assertThat(preference.getOrder()).isEqualTo(-tile.priority + baseOrder);
}
}

View File

@@ -42,9 +42,6 @@ import org.robolectric.shadows.ShadowApplication;
import java.util.ArrayList;
import java.util.List;
import java.util.ArrayList;
import java.util.List;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
@@ -122,19 +119,29 @@ public class DashboardFragmentTest {
verify(mTestFragment.mScreen, never()).addPreference(any(Preference.class));
}
@Test
public void onAttach_shouldCreatePlaceholderPreferenceController() {
final PreferenceController controller = mTestFragment.getPreferenceController(
DashboardTilePlaceholderPreferenceController.class);
assertThat(controller).isNotNull();
}
@Test
public void updateState_skipUnavailablePrefs() {
List<PreferenceController> preferenceControllers = mTestFragment.mControllers;
preferenceControllers.add(mock(PreferenceController.class));
preferenceControllers.add(mock(PreferenceController.class));
when(preferenceControllers.get(0).isAvailable()).thenReturn(false);
when(preferenceControllers.get(1).isAvailable()).thenReturn(true);
final List<PreferenceController> preferenceControllers = mTestFragment.mControllers;
final PreferenceController mockController1 = mock(PreferenceController.class);
final PreferenceController mockController2 = mock(PreferenceController.class);
preferenceControllers.add(mockController1);
preferenceControllers.add(mockController2);
when(mockController1.isAvailable()).thenReturn(false);
when(mockController2.isAvailable()).thenReturn(true);
mTestFragment.onAttach(ShadowApplication.getInstance().getApplicationContext());
mTestFragment.onResume();
verify(mTestFragment.mControllers.get(0), never()).getPreferenceKey();
verify(mTestFragment.mControllers.get(1)).getPreferenceKey();
verify(mockController1, never()).getPreferenceKey();
verify(mockController2).getPreferenceKey();
}
public static class TestPreferenceController extends PreferenceController {

View File

@@ -0,0 +1,74 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.dashboard;
import android.content.Context;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class DashboardTilePlaceholderPreferenceControllerTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
@Mock(answer = RETURNS_DEEP_STUBS)
private PreferenceScreen mScreen;
private DashboardTilePlaceholderPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mController = new DashboardTilePlaceholderPreferenceController(mContext);
}
@Test
public void display_hasPlaceholderPref_shouldUseOrderFromPlaceholder() {
final int baseOrder = 15;
when(mScreen.findPreference(anyString()).getOrder()).thenReturn(baseOrder);
mController.displayPreference(mScreen);
assertThat(mController.getOrder()).isEqualTo(baseOrder);
}
@Test
public void display_noPlaceholderPref_shouldUseDefaultOrder() {
when(mScreen.findPreference(anyString())).thenReturn(null);
mController.displayPreference(mScreen);
assertThat(mController.getOrder()).isEqualTo(Preference.DEFAULT_ORDER);
}
}