Add conditional cards to the new homepage.
Make existing Settings Conditions show up in the new homepage as custom view. Bug: 113451905 Test: robotests Change-Id: Ic49089f95ff04442a887cda97481e945136f7209
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* Copyright (C) 2018 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.conditional;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.homepage.HomepageCardUpdateListener;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
public class ConditionHomepageCardControllerTest {
|
||||
|
||||
@Mock
|
||||
private ConditionManager mConditionManager;
|
||||
@Mock
|
||||
private HomepageCardUpdateListener mListener;
|
||||
private Context mContext;
|
||||
private ConditionHomepageCardController mController;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
mController = spy(new ConditionHomepageCardController(mContext));
|
||||
ReflectionHelpers.setField(mController, "mConditionManager", mConditionManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onStart_shouldStartMonitoring() {
|
||||
mController.onStart();
|
||||
|
||||
verify(mConditionManager).startMonitoringStateChange();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onStop_shouldStopMonitoring() {
|
||||
mController.onStop();
|
||||
|
||||
verify(mConditionManager).stopMonitoringStateChange();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onConditionsChanged_listenerIsSet_shouldUpdateData() {
|
||||
final FakeConditionalCard fakeConditionalCard = new FakeConditionalCard(mContext);
|
||||
final List<ConditionalCard> conditionalCards = new ArrayList<>();
|
||||
conditionalCards.add(fakeConditionalCard);
|
||||
when(mConditionManager.getDisplayableCards()).thenReturn(conditionalCards);
|
||||
mController.setHomepageCardUpdateListener(mListener);
|
||||
|
||||
mController.onConditionsChanged();
|
||||
|
||||
verify(mController).onDataUpdated(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onConditionsChanged_listenerNotSet_shouldNotUpdateData() {
|
||||
final FakeConditionalCard fakeConditionalCard = new FakeConditionalCard(mContext);
|
||||
final List<ConditionalCard> conditionalCards = new ArrayList<>();
|
||||
conditionalCards.add(fakeConditionalCard);
|
||||
when(mConditionManager.getDisplayableCards()).thenReturn(conditionalCards);
|
||||
|
||||
mController.onConditionsChanged();
|
||||
|
||||
verify(mController, never()).onDataUpdated(any());
|
||||
}
|
||||
|
||||
private class FakeConditionalCard implements ConditionalCard {
|
||||
|
||||
private final Context mContext;
|
||||
|
||||
public FakeConditionalCard(Context context) {
|
||||
mContext = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getId() {
|
||||
return 100;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getActionText() {
|
||||
return "action_text_test";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMetricsConstant() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Drawable getIcon() {
|
||||
return mContext.getDrawable(R.drawable.ic_do_not_disturb_on_24dp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getTitle() {
|
||||
return "title_text_test";
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getSummary() {
|
||||
return "summary_text_test";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright (C) 2018 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.conditional;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.homepage.ControllerRendererPool;
|
||||
import com.android.settings.homepage.HomepageCard;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
public class ConditionHomepageCardRendererTest {
|
||||
|
||||
@Mock
|
||||
private ControllerRendererPool mControllerRendererPool;
|
||||
@Mock
|
||||
private ConditionHomepageCardController mController;
|
||||
private Context mContext;
|
||||
private ConditionHomepageCardRenderer mRenderer;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
mRenderer = new ConditionHomepageCardRenderer(mContext, mControllerRendererPool);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindView_shouldSetListener() {
|
||||
final int viewType = mRenderer.getViewType();
|
||||
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 View card = view.findViewById(R.id.content);
|
||||
when(mControllerRendererPool.getController(mContext,
|
||||
HomepageCard.CardType.CONDITIONAL)).thenReturn(mController);
|
||||
|
||||
mRenderer.bindView(viewHolder, getHomepageCard());
|
||||
|
||||
assertThat(card).isNotNull();
|
||||
assertThat(card.hasOnClickListeners()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void viewClick_shouldInvokeControllerPrimaryClick() {
|
||||
final int viewType = mRenderer.getViewType();
|
||||
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 View card = view.findViewById(R.id.content);
|
||||
when(mControllerRendererPool.getController(mContext,
|
||||
HomepageCard.CardType.CONDITIONAL)).thenReturn(mController);
|
||||
|
||||
mRenderer.bindView(viewHolder, getHomepageCard());
|
||||
|
||||
assertThat(card).isNotNull();
|
||||
card.performClick();
|
||||
|
||||
verify(mController).onPrimaryClick(any(HomepageCard.class));
|
||||
}
|
||||
|
||||
private HomepageCard getHomepageCard() {
|
||||
ConditionCard conditionCard = ((ConditionCard.Builder) new ConditionCard.Builder()
|
||||
.setConditionId(123)
|
||||
.setMetricsConstant(1)
|
||||
.setActionText("test_action")
|
||||
.setName("test_name")
|
||||
.setCardType(HomepageCard.CardType.CONDITIONAL)
|
||||
.setTitleText("test_title")
|
||||
.setSummaryText("test_summary")
|
||||
.setIconDrawable(mContext.getDrawable(R.drawable.ic_do_not_disturb_on_24dp))
|
||||
.setIsHalfWidth(true))
|
||||
.build();
|
||||
return conditionCard;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user