Merge "Force the adapter to rebind cards with a toggle." into qt-dev

This commit is contained in:
Fan Zhang
2019-04-23 22:33:16 +00:00
committed by Android (Google) Code Review
5 changed files with 236 additions and 2 deletions

View File

@@ -0,0 +1,95 @@
/*
* Copyright (C) 2019 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.homepage.contextualcards;
import static com.google.common.truth.Truth.assertThat;
import android.net.Uri;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.ArrayList;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class ContextualCardsDiffCallbackTest {
private static final Uri TEST_SLICE_URI = Uri.parse("content://test/test");
private ContextualCardsDiffCallback mDiffCallback;
private List<ContextualCard> mOldCards;
private List<ContextualCard> mNewCards;
@Before
public void setUp() {
mOldCards = new ArrayList<>();
mNewCards = new ArrayList<>();
mOldCards.add(getContextualCard("test1"));
mNewCards.add(getContextualCard("test1"));
mNewCards.add(getContextualCard("test2"));
mDiffCallback = new ContextualCardsDiffCallback(mOldCards, mNewCards);
}
@Test
public void getOldListSize_oneCard_returnOne() {
assertThat(mDiffCallback.getOldListSize()).isEqualTo(1);
}
@Test
public void getNewListSize_twoCards_returnTwo() {
assertThat(mDiffCallback.getNewListSize()).isEqualTo(2);
}
@Test
public void areItemsTheSame_sameItems_returnTrue() {
assertThat(mDiffCallback.areItemsTheSame(0, 0)).isTrue();
}
@Test
public void areItemsTheSame_differentItems_returnFalse() {
mOldCards.add(getContextualCard("test3"));
assertThat(mDiffCallback.areItemsTheSame(1, 1)).isFalse();
}
@Test
public void areContentsTheSame_sameContents_returnTrue() {
assertThat(mDiffCallback.areContentsTheSame(0, 0)).isTrue();
}
@Test
public void areContentsTheSame_sliceWithToggle_returnFalse() {
final ContextualCard card = getContextualCard("test1").mutate()
.setHasInlineAction(true).build();
mNewCards.add(0, card);
assertThat(mDiffCallback.areContentsTheSame(0, 0)).isFalse();
}
private ContextualCard getContextualCard(String name) {
return new ContextualCard.Builder()
.setName(name)
.setRankingScore(0.5)
.setCardType(ContextualCard.CardType.SLICE)
.setSliceUri(TEST_SLICE_URI)
.build();
}
}

View File

@@ -0,0 +1,103 @@
/*
* Copyright (C) 2019 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.homepage.contextualcards;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import android.content.Context;
import android.net.Uri;
import androidx.slice.Slice;
import androidx.slice.SliceProvider;
import androidx.slice.widget.SliceLiveData;
import com.android.settings.homepage.contextualcards.deviceinfo.EmergencyInfoSlice;
import com.android.settings.wifi.slice.ContextualWifiSlice;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class EligibleCardCheckerTest {
private static final Uri TEST_SLICE_URI = Uri.parse("content://test/test");
private Context mContext;
private EligibleCardChecker mEligibleCardChecker;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mEligibleCardChecker =
spy(new EligibleCardChecker(mContext, getContextualCard(TEST_SLICE_URI)));
SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS);
}
@Test
public void isSliceToggleable_cardWithToggle_returnTrue() {
final ContextualWifiSlice wifiSlice = new ContextualWifiSlice(mContext);
final Slice slice = wifiSlice.getSlice();
assertThat(mEligibleCardChecker.isSliceToggleable(slice)).isTrue();
}
@Test
public void isSliceToggleable_cardWithoutToggle_returnFalse() {
final EmergencyInfoSlice emergencyInfoSlice = new EmergencyInfoSlice(mContext);
final Slice slice = emergencyInfoSlice.getSlice();
assertThat(mEligibleCardChecker.isSliceToggleable(slice)).isFalse();
}
@Test
public void isCardEligibleToDisplay_toggleSlice_hasInlineActionShouldBeTrue() {
final ContextualWifiSlice wifiSlice = new ContextualWifiSlice(mContext);
final Slice slice = wifiSlice.getSlice();
doReturn(slice).when(mEligibleCardChecker).bindSlice(any(Uri.class));
mEligibleCardChecker.isCardEligibleToDisplay(getContextualCard(TEST_SLICE_URI));
assertThat(mEligibleCardChecker.mCard.hasInlineAction()).isTrue();
}
@Test
public void isCardEligibleToDisplay_notToggleSlice_hasInlineActionShouldBeFalse() {
final EmergencyInfoSlice emergencyInfoSlice = new EmergencyInfoSlice(mContext);
final Slice slice = emergencyInfoSlice.getSlice();
doReturn(slice).when(mEligibleCardChecker).bindSlice(any(Uri.class));
mEligibleCardChecker.isCardEligibleToDisplay(getContextualCard(TEST_SLICE_URI));
assertThat(mEligibleCardChecker.mCard.hasInlineAction()).isFalse();
}
private ContextualCard getContextualCard(Uri sliceUri) {
return new ContextualCard.Builder()
.setName("test_card")
.setRankingScore(0.5)
.setCardType(ContextualCard.CardType.SLICE)
.setSliceUri(sliceUri)
.build();
}
}