Remove mIsHalfWidth field from ContextualCard

mIsHalfWidth in ContextualCard is too generic to identify its
responsibility, it is used to hold the value of is_support_half from
database and used to decide the card width. Also, it limits the
relationship of CardType-to-Renderer not to be extended to one-to-many.

To deal with this, we replaced mIsHalfWidth with mViewType to implement
one-to-many relationship of CardType-to-Renderer, and removed all
related logic of mIsHalfWidth.

Bug: 121303357
Test: robotests, visual

Change-Id: I03e14392272194424f317d11bf9d0d794a6133f4
This commit is contained in:
Mill Chen
2019-01-09 18:54:31 +08:00
parent 46285fe08f
commit 1c3ed19a09
34 changed files with 187 additions and 228 deletions

View File

@@ -18,17 +18,16 @@ package com.android.settings.homepage.contextualcards;
import static com.google.common.truth.Truth.assertThat;
import com.android.settings.homepage.contextualcards.ContextualCardLookupTable
.ControllerRendererMapping;
import com.android.settings.homepage.contextualcards.ContextualCardLookupTable.ControllerRendererMapping;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import java.util.ArrayList;
import java.util.List;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class ContextualCardLookupTableTest {
@@ -98,18 +97,4 @@ public class ContextualCardLookupTableTest {
ContextualCardLookupTable.getCardRendererClassByViewType(UNSUPPORTED_VIEW_TYPE);
}
@Test
public void getRendererClassByCardType_hasSupportedCardType_shouldGetCorrespondingRenderer() {
for (ControllerRendererMapping mapping : ContextualCardLookupTable.LOOKUP_TABLE) {
assertThat(ContextualCardLookupTable.getCardRendererClassByCardType(mapping.mCardType))
.isEqualTo(mapping.mRendererClass);
}
}
@Test
public void getCardRendererClassByCardType_hasUnsupportedCardType_shouldAlwaysGetNull() {
assertThat(ContextualCardLookupTable.getCardRendererClassByCardType(UNSUPPORTED_CARD_TYPE))
.isNull();
}
}

View File

@@ -16,6 +16,9 @@
package com.android.settings.homepage.contextualcards;
import static com.android.settings.homepage.contextualcards.slices.SliceContextualCardRenderer.VIEW_TYPE_FULL_WIDTH;
import static com.android.settings.homepage.contextualcards.slices.SliceContextualCardRenderer.VIEW_TYPE_HALF_WIDTH;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyMap;
@@ -222,7 +225,7 @@ public class ContextualCardManagerTest {
assertThat(result).hasSize(5);
for (ContextualCard card : result) {
assertThat(card.isHalfWidth()).isFalse();
assertThat(card.getViewType()).isEqualTo(VIEW_TYPE_FULL_WIDTH);
}
}
@@ -242,7 +245,7 @@ public class ContextualCardManagerTest {
assertThat(result).hasSize(5);
for (ContextualCard card : result) {
assertThat(card.isHalfWidth()).isFalse();
assertThat(card.getViewType()).isEqualTo(VIEW_TYPE_FULL_WIDTH);
}
}
@@ -257,14 +260,16 @@ public class ContextualCardManagerTest {
);
final List<ContextualCard> twoConsecutiveSuggestionCards = buildCategoriedCards(
getContextualCardList(), categories);
final List<Boolean> expectedValues = Arrays.asList(false, false, true, true, false);
final List<Integer> expectedValues = Arrays.asList(VIEW_TYPE_FULL_WIDTH,
VIEW_TYPE_FULL_WIDTH, VIEW_TYPE_HALF_WIDTH, VIEW_TYPE_HALF_WIDTH,
VIEW_TYPE_FULL_WIDTH);
final List<ContextualCard> result = mManager.assignCardWidth(
twoConsecutiveSuggestionCards);
assertThat(result).hasSize(5);
for (int i = 0; i < result.size(); i++) {
assertThat(result.get(i).isHalfWidth()).isEqualTo(expectedValues.get(i));
assertThat(result.get(i).getViewType()).isEqualTo(expectedValues.get(i));
}
}
@@ -285,7 +290,7 @@ public class ContextualCardManagerTest {
assertThat(result).hasSize(5);
for (ContextualCard card : result) {
assertThat(card.isHalfWidth()).isFalse();
assertThat(card.getViewType()).isEqualTo(VIEW_TYPE_FULL_WIDTH);
}
}
@@ -300,14 +305,16 @@ public class ContextualCardManagerTest {
);
final List<ContextualCard> threeConsecutiveSuggestionCards = buildCategoriedCards(
getContextualCardList(), categories);
final List<Boolean> expectedValues = Arrays.asList(false, true, true, false, false);
final List<Integer> expectedValues = Arrays.asList(VIEW_TYPE_FULL_WIDTH,
VIEW_TYPE_HALF_WIDTH, VIEW_TYPE_HALF_WIDTH, VIEW_TYPE_FULL_WIDTH,
VIEW_TYPE_FULL_WIDTH);
final List<ContextualCard> result = mManager.assignCardWidth(
threeConsecutiveSuggestionCards);
assertThat(result).hasSize(5);
for (int i = 0; i < result.size(); i++) {
assertThat(result.get(i).isHalfWidth()).isEqualTo(expectedValues.get(i));
assertThat(result.get(i).getViewType()).isEqualTo(expectedValues.get(i));
}
}
@@ -322,14 +329,16 @@ public class ContextualCardManagerTest {
);
final List<ContextualCard> fourConsecutiveSuggestionCards = buildCategoriedCards(
getContextualCardList(), categories);
final List<Boolean> expectedValues = Arrays.asList(false, true, true, true, true);
final List<Integer> expectedValues = Arrays.asList(VIEW_TYPE_FULL_WIDTH,
VIEW_TYPE_HALF_WIDTH, VIEW_TYPE_HALF_WIDTH, VIEW_TYPE_HALF_WIDTH,
VIEW_TYPE_HALF_WIDTH);
final List<ContextualCard> result = mManager.assignCardWidth(
fourConsecutiveSuggestionCards);
assertThat(result).hasSize(5);
for (int i = 0; i < result.size(); i++) {
assertThat(result.get(i).isHalfWidth()).isEqualTo(expectedValues.get(i));
assertThat(result.get(i).getViewType()).isEqualTo(expectedValues.get(i));
}
}
@@ -338,6 +347,7 @@ public class ContextualCardManagerTest {
.setName(TEST_SLICE_NAME)
.setCardType(ContextualCard.CardType.SLICE)
.setSliceUri(Uri.parse(sliceUri))
.setViewType(VIEW_TYPE_FULL_WIDTH)
.build();
}
@@ -356,28 +366,33 @@ public class ContextualCardManagerTest {
.setName("test_wifi")
.setCardType(ContextualCard.CardType.SLICE)
.setSliceUri(CustomSliceRegistry.CONTEXTUAL_WIFI_SLICE_URI)
.setViewType(VIEW_TYPE_FULL_WIDTH)
.build());
cards.add(new ContextualCard.Builder()
.setName("test_flashlight")
.setCardType(ContextualCard.CardType.SLICE)
.setSliceUri(
Uri.parse("content://com.android.settings.test.slices/action/flashlight"))
.setViewType(VIEW_TYPE_FULL_WIDTH)
.build());
cards.add(new ContextualCard.Builder()
.setName("test_connected")
.setCardType(ContextualCard.CardType.SLICE)
.setSliceUri(CustomSliceRegistry.BLUETOOTH_DEVICES_SLICE_URI)
.setViewType(VIEW_TYPE_FULL_WIDTH)
.build());
cards.add(new ContextualCard.Builder()
.setName("test_gesture")
.setCardType(ContextualCard.CardType.SLICE)
.setSliceUri(Uri.parse(
"content://com.android.settings.test.slices/action/gesture_pick_up"))
.setViewType(VIEW_TYPE_FULL_WIDTH)
.build());
cards.add(new ContextualCard.Builder()
.setName("test_battery")
.setCardType(ContextualCard.CardType.SLICE)
.setSliceUri(CustomSliceRegistry.BATTERY_INFO_SLICE_URI)
.setViewType(VIEW_TYPE_FULL_WIDTH)
.build());
return cards;
}

View File

@@ -105,33 +105,4 @@ public class ControllerRendererPoolTest {
assertThat(renderer).isNull();
assertThat(mPool.getRenderers()).isEmpty();
}
@Test
public void getRenderer_hasSupportedCardTypeAndWidth_shouldReturnCorrespondingRenderer() {
ContextualCardLookupTable.LOOKUP_TABLE.stream().forEach(mapping -> assertThat(
mPool.getRendererByCardType(mContext, mLifecycleOwner,
mapping.mCardType).getClass()).isEqualTo(mapping.mRendererClass));
}
@Test
public void getRenderer_hasSupportedCardTypeAndWidth_shouldHaveDistinctRenderersInPool() {
final long count = ContextualCardLookupTable.LOOKUP_TABLE.stream().map(
mapping -> mapping.mRendererClass).distinct().count();
ContextualCardLookupTable.LOOKUP_TABLE.stream().forEach(
mapping -> mPool.getRendererByCardType(mContext, mLifecycleOwner,
mapping.mCardType));
assertThat(mPool.getRenderers()).hasSize((int) count);
}
@Test
public void getRenderer_hasUnsupportedCardType_shouldReturnNullAndPoolIsEmpty() {
final ContextualCardRenderer renderer = mPool.getRendererByCardType(mContext,
mLifecycleOwner,
UNSUPPORTED_CARD_TYPE);
assertThat(renderer).isNull();
assertThat(mPool.getRenderers()).isEmpty();
}
}

View File

@@ -119,7 +119,8 @@ public class ConditionContextualCardControllerTest {
assertThat(conditionalCards).hasSize(3);
assertThat(conditionalCards.get(CardType.CONDITIONAL)).hasSize(1);
assertThat(conditionalCards.get(CardType.CONDITIONAL).get(0).isHalfWidth()).isFalse();
assertThat(conditionalCards.get(CardType.CONDITIONAL).get(0).getViewType()).isEqualTo(
ConditionContextualCardRenderer.VIEW_TYPE_FULL_WIDTH);
assertThat(conditionalCards.get(CardType.CONDITIONAL_HEADER)).isEmpty();
assertThat(conditionalCards.get(CardType.CONDITIONAL_FOOTER)).isNotEmpty();
}
@@ -145,7 +146,8 @@ public class ConditionContextualCardControllerTest {
assertThat(conditionalCards).hasSize(3);
assertThat(conditionalCards.get(CardType.CONDITIONAL)).hasSize(2);
for (ContextualCard card : conditionalCards.get(CardType.CONDITIONAL)) {
assertThat(card.isHalfWidth()).isTrue();
assertThat(card.getViewType()).isEqualTo(
ConditionContextualCardRenderer.VIEW_TYPE_HALF_WIDTH);
}
assertThat(conditionalCards.get(CardType.CONDITIONAL_HEADER)).isEmpty();
assertThat(conditionalCards.get(CardType.CONDITIONAL_FOOTER)).isNotEmpty();
@@ -197,7 +199,7 @@ public class ConditionContextualCardControllerTest {
.setName("test_name" + i)
.setTitleText("test_title" + i)
.setSummaryText("test_summary" + i)
.setIsHalfWidth(true)
.setViewType(ConditionContextualCardRenderer.VIEW_TYPE_HALF_WIDTH)
.build());
}
return conditionCards;

View File

@@ -65,36 +65,40 @@ public class ConditionContextualCardRendererTest {
@Test
public void bindView_shouldSetListener() {
final int viewType = mRenderer.getViewType(false /* isHalfWidth */);
final RecyclerView recyclerView = new RecyclerView(mActivity);
recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
final View view = LayoutInflater.from(mActivity).inflate(viewType, recyclerView, false);
final RecyclerView.ViewHolder viewHolder = mRenderer.createViewHolder(view);
final View card = view.findViewById(R.id.content);
final ContextualCard card = buildConditionContextualCard();
final View view = LayoutInflater.from(mActivity).inflate(card.getViewType(), recyclerView,
false);
final RecyclerView.ViewHolder viewHolder = mRenderer.createViewHolder(view,
card.getViewType());
final View cardView = view.findViewById(R.id.content);
when(mControllerRendererPool.getController(mActivity,
ContextualCard.CardType.CONDITIONAL)).thenReturn(mController);
mRenderer.bindView(viewHolder, buildConditionContextualCard());
mRenderer.bindView(viewHolder, card);
assertThat(card).isNotNull();
assertThat(card.hasOnClickListeners()).isTrue();
assertThat(cardView).isNotNull();
assertThat(cardView.hasOnClickListeners()).isTrue();
}
@Test
public void viewClick_shouldInvokeControllerPrimaryClick() {
final int viewType = mRenderer.getViewType(false /* isHalfWidth */);
final RecyclerView recyclerView = new RecyclerView(mActivity);
recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
final View view = LayoutInflater.from(mActivity).inflate(viewType, recyclerView, false);
final RecyclerView.ViewHolder viewHolder = mRenderer.createViewHolder(view);
final View card = view.findViewById(R.id.content);
final ContextualCard card = buildConditionContextualCard();
final View view = LayoutInflater.from(mActivity).inflate(card.getViewType(), recyclerView,
false);
final RecyclerView.ViewHolder viewHolder = mRenderer.createViewHolder(view,
card.getViewType());
final View cardView = view.findViewById(R.id.content);
when(mControllerRendererPool.getController(mActivity,
ContextualCard.CardType.CONDITIONAL)).thenReturn(mController);
mRenderer.bindView(viewHolder, buildConditionContextualCard());
mRenderer.bindView(viewHolder, card);
assertThat(card).isNotNull();
card.performClick();
assertThat(cardView).isNotNull();
cardView.performClick();
verify(mController).onPrimaryClick(any(ContextualCard.class));
}
@@ -108,7 +112,7 @@ public class ConditionContextualCardRendererTest {
.setTitleText("test_title")
.setSummaryText("test_summary")
.setIconDrawable(mActivity.getDrawable(R.drawable.ic_do_not_disturb_on_24dp))
.setIsHalfWidth(true)
.setViewType(ConditionContextualCardRenderer.VIEW_TYPE_FULL_WIDTH)
.build();
}
}

View File

@@ -59,15 +59,17 @@ public class ConditionFooterContextualCardRendererTest {
@Test
public void bindView_shouldSetClickListener() {
final int viewType = mRenderer.getViewType(false /* isHalfWidth */);
final RecyclerView recyclerView = new RecyclerView(mContext);
recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
final View view = LayoutInflater.from(mContext).inflate(viewType, recyclerView, false);
final RecyclerView.ViewHolder viewHolder = mRenderer.createViewHolder(view);
final ContextualCard card = generateConditionFooterContextualCard();
final View view = LayoutInflater.from(mContext).inflate(card.getViewType(), recyclerView,
false);
final RecyclerView.ViewHolder viewHolder = mRenderer.createViewHolder(view,
card.getViewType());
when(mControllerRendererPool.getController(mContext,
ContextualCard.CardType.CONDITIONAL_FOOTER)).thenReturn(mController);
mRenderer.bindView(viewHolder, generateConditionFooterContextualCard());
mRenderer.bindView(viewHolder, card);
assertThat(viewHolder.itemView).isNotNull();
assertThat(viewHolder.itemView.hasOnClickListeners()).isTrue();
@@ -75,15 +77,17 @@ public class ConditionFooterContextualCardRendererTest {
@Test
public void bindView_clickView_shouldSetTrueToIsConditionExpanded() {
final int viewType = mRenderer.getViewType(false /* isHalfWidth */);
final RecyclerView recyclerView = new RecyclerView(mContext);
recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
final View view = LayoutInflater.from(mContext).inflate(viewType, recyclerView, false);
final RecyclerView.ViewHolder viewHolder = mRenderer.createViewHolder(view);
final ContextualCard card = generateConditionFooterContextualCard();
final View view = LayoutInflater.from(mContext).inflate(card.getViewType(), recyclerView,
false);
final RecyclerView.ViewHolder viewHolder = mRenderer.createViewHolder(view,
card.getViewType());
when(mControllerRendererPool.getController(mContext,
ContextualCard.CardType.CONDITIONAL_FOOTER)).thenReturn(mController);
mRenderer.bindView(viewHolder, generateConditionFooterContextualCard());
mRenderer.bindView(viewHolder, card);
assertThat(viewHolder.itemView).isNotNull();
viewHolder.itemView.performClick();
@@ -96,6 +100,7 @@ public class ConditionFooterContextualCardRendererTest {
return new ConditionFooterContextualCard.Builder()
.setName("test_condition_footer")
.setRankingScore(-9999.0)
.setViewType(ConditionFooterContextualCardRenderer.VIEW_TYPE)
.build();
}
}

View File

@@ -67,11 +67,13 @@ public class ConditionHeaderContextualCardRendererTest {
@Test
public void bindView_shouldSetClickListener() {
final int viewType = mRenderer.getViewType(false /* isHalfWidth */);
final RecyclerView recyclerView = new RecyclerView(mActivity);
recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
final View view = LayoutInflater.from(mActivity).inflate(viewType, recyclerView, false);
final RecyclerView.ViewHolder viewHolder = mRenderer.createViewHolder(view);
final ContextualCard card = generateConditionHeaderContextualCard();
final View view = LayoutInflater.from(mActivity).inflate(card.getViewType(), recyclerView,
false);
final RecyclerView.ViewHolder viewHolder = mRenderer.createViewHolder(view,
card.getViewType());
when(mControllerRendererPool.getController(mActivity,
ContextualCard.CardType.CONDITIONAL_HEADER)).thenReturn(mController);
@@ -83,11 +85,13 @@ public class ConditionHeaderContextualCardRendererTest {
@Test
public void bindView_clickView_shouldSetTrueToIsConditionExpanded() {
final int viewType = mRenderer.getViewType(false /* isHalfWidth */);
final RecyclerView recyclerView = new RecyclerView(mActivity);
recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
final View view = LayoutInflater.from(mActivity).inflate(viewType, recyclerView, false);
final RecyclerView.ViewHolder viewHolder = mRenderer.createViewHolder(view);
final ContextualCard card = generateConditionHeaderContextualCard();
final View view = LayoutInflater.from(mActivity).inflate(card.getViewType(), recyclerView,
false);
final RecyclerView.ViewHolder viewHolder = mRenderer.createViewHolder(view,
card.getViewType());
when(mControllerRendererPool.getController(mActivity,
ContextualCard.CardType.CONDITIONAL_HEADER)).thenReturn(mController);
@@ -105,6 +109,7 @@ public class ConditionHeaderContextualCardRendererTest {
.setConditionalCards(generateConditionCards(3))
.setName("test_condition_header")
.setRankingScore(-9999.0)
.setViewType(ConditionHeaderContextualCardRenderer.VIEW_TYPE)
.build();
}
@@ -118,7 +123,7 @@ public class ConditionHeaderContextualCardRendererTest {
.setName("test_name" + i)
.setTitleText("test_title" + i)
.setSummaryText("test_summary" + i)
.setIsHalfWidth(true)
.setViewType(ConditionContextualCardRenderer.VIEW_TYPE_HALF_WIDTH)
.build());
}
return conditionCards;

View File

@@ -66,35 +66,38 @@ public class LegacySuggestionContextualCardRendererTest {
@Test
public void bindView_shouldSetListener() {
final int viewType = mRenderer.getViewType(true /* isHalfWidth */);
final RecyclerView recyclerView = new RecyclerView(mActivity);
recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
final View card = LayoutInflater.from(mActivity).inflate(viewType, recyclerView, false);
final RecyclerView.ViewHolder viewHolder = mRenderer.createViewHolder(card);
final ContextualCard card = buildContextualCard();
final View cardView = LayoutInflater.from(mActivity).inflate(card.getViewType(),
recyclerView, false);
final RecyclerView.ViewHolder viewHolder = mRenderer.createViewHolder(cardView,
card.getViewType());
when(mControllerRendererPool.getController(mActivity,
ContextualCard.CardType.LEGACY_SUGGESTION)).thenReturn(mController);
mRenderer.bindView(viewHolder, buildContextualCard());
assertThat(card).isNotNull();
assertThat(card.hasOnClickListeners()).isTrue();
assertThat(cardView).isNotNull();
assertThat(cardView.hasOnClickListeners()).isTrue();
}
@Test
public void viewClick_shouldInvokeControllerPrimaryClick() {
final int viewType = mRenderer.getViewType(true /* isHalfWidth */);
final RecyclerView recyclerView = new RecyclerView(mActivity);
recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
final View card = LayoutInflater.from(mActivity).inflate(viewType, recyclerView, false);
final RecyclerView.ViewHolder viewHolder = mRenderer.createViewHolder(card);
final ContextualCard card = buildContextualCard();
final View cardView = LayoutInflater.from(mActivity).inflate(card.getViewType(),
recyclerView, false);
final RecyclerView.ViewHolder viewHolder = mRenderer.createViewHolder(cardView,
card.getViewType());
when(mControllerRendererPool.getController(mActivity,
ContextualCard.CardType.LEGACY_SUGGESTION)).thenReturn(mController);
mRenderer.bindView(viewHolder, buildContextualCard());
assertThat(card).isNotNull();
card.performClick();
assertThat(cardView).isNotNull();
cardView.performClick();
verify(mController).onPrimaryClick(any(ContextualCard.class));
}
@@ -105,6 +108,7 @@ public class LegacySuggestionContextualCardRendererTest {
.setTitleText("test_title")
.setSummaryText("test_summary")
.setIconDrawable(mActivity.getDrawable(R.drawable.ic_do_not_disturb_on_24dp))
.setViewType(LegacySuggestionContextualCardRenderer.VIEW_TYPE)
.build();
}
}

View File

@@ -16,6 +16,8 @@
package com.android.settings.homepage.contextualcards.slices;
import static com.android.settings.homepage.contextualcards.slices.SliceContextualCardRenderer.VIEW_TYPE_FULL_WIDTH;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
@@ -211,12 +213,12 @@ public class SliceContextualCardRendererTest {
}
private RecyclerView.ViewHolder getSliceViewHolder() {
final int viewType = mRenderer.getViewType(false /* isHalfWidth */);
final RecyclerView recyclerView = new RecyclerView(mActivity);
recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
final View view = LayoutInflater.from(mActivity).inflate(viewType, recyclerView, false);
final View view = LayoutInflater.from(mActivity).inflate(VIEW_TYPE_FULL_WIDTH, recyclerView,
false);
return mRenderer.createViewHolder(view);
return mRenderer.createViewHolder(view, VIEW_TYPE_FULL_WIDTH);
}
private ContextualCard buildContextualCard(Uri sliceUri) {
@@ -224,6 +226,7 @@ public class SliceContextualCardRendererTest {
.setName("test_name")
.setCardType(ContextualCard.CardType.SLICE)
.setSliceUri(sliceUri)
.setViewType(VIEW_TYPE_FULL_WIDTH)
.build();
}
}

View File

@@ -127,7 +127,7 @@ public class SliceFullCardRendererHelperTest {
.setCategory(ContextualCardProto.ContextualCard.Category.SUGGESTION_VALUE)
.setCardType(ContextualCard.CardType.SLICE)
.setSliceUri(TEST_SLICE_URI)
.setIsHalfWidth(false /* isHalfWidth */)
.setViewType(VIEW_TYPE_FULL_WIDTH)
.build();
}

View File

@@ -95,7 +95,7 @@ public class SliceHalfCardRendererHelperTest {
.setCategory(ContextualCardProto.ContextualCard.Category.SUGGESTION_VALUE)
.setCardType(ContextualCard.CardType.SLICE)
.setSliceUri(TEST_SLICE_URI)
.setIsHalfWidth(false /* isHalfWidth */)
.setViewType(VIEW_TYPE_HALF_WIDTH)
.build();
}