2 * Copyright (C) 2017 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.conditional;
18 import static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.when;
23 import android.content.Context;
24 import android.support.v7.widget.RecyclerView;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.widget.LinearLayout;
29 import com.android.settings.R;
30 import com.android.settings.TestConfig;
31 import com.android.settings.dashboard.DashboardAdapter;
32 import com.android.settings.testutils.SettingsRobolectricTestRunner;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.robolectric.RuntimeEnvironment;
40 import org.robolectric.annotation.Config;
42 import java.util.ArrayList;
43 import java.util.List;
45 @RunWith(SettingsRobolectricTestRunner.class)
46 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
47 public class ConditionAdapterTest {
49 private Condition mCondition1;
51 private Condition mCondition2;
53 private Context mContext;
54 private ConditionAdapter mConditionAdapter;
55 private List<Condition> mOneCondition;
56 private List<Condition> mTwoConditions;
60 MockitoAnnotations.initMocks(this);
61 mContext = RuntimeEnvironment.application;
62 final CharSequence[] actions = new CharSequence[2];
63 when(mCondition1.getActions()).thenReturn(actions);
64 when(mCondition1.shouldShow()).thenReturn(true);
65 mOneCondition = new ArrayList<>();
66 mOneCondition.add(mCondition1);
67 mTwoConditions = new ArrayList<>();
68 mTwoConditions.add(mCondition1);
69 mTwoConditions.add(mCondition2);
73 public void getItemCount_notExpanded_shouldReturn0() {
74 mConditionAdapter = new ConditionAdapter(mContext, mOneCondition, false);
75 assertThat(mConditionAdapter.getItemCount()).isEqualTo(0);
79 public void getItemCount_expanded_shouldReturnListSize() {
80 mConditionAdapter = new ConditionAdapter(mContext, mOneCondition, true);
81 assertThat(mConditionAdapter.getItemCount()).isEqualTo(1);
83 mConditionAdapter = new ConditionAdapter(mContext, mTwoConditions, true);
84 assertThat(mConditionAdapter.getItemCount()).isEqualTo(2);
88 public void getItemViewType_shouldReturnConditionTile() {
89 mConditionAdapter = new ConditionAdapter(mContext, mTwoConditions, true);
90 assertThat(mConditionAdapter.getItemViewType(0)).isEqualTo(R.layout.condition_tile);
94 public void onBindViewHolder_shouldSetListener() {
95 final View view = LayoutInflater.from(mContext).inflate(
96 R.layout.condition_tile, new LinearLayout(mContext), true);
97 final DashboardAdapter.DashboardItemHolder viewHolder =
98 new DashboardAdapter.DashboardItemHolder(view);
99 mConditionAdapter = new ConditionAdapter(mContext, mOneCondition, true);
101 mConditionAdapter.onBindViewHolder(viewHolder, 0);
102 final View card = view.findViewById(R.id.content);
103 assertThat(card.hasOnClickListeners()).isTrue();
107 public void viewClick_shouldInvokeConditionPrimaryClick() {
108 final View view = LayoutInflater.from(mContext).inflate(
109 R.layout.condition_tile, new LinearLayout(mContext), true);
110 final DashboardAdapter.DashboardItemHolder viewHolder =
111 new DashboardAdapter.DashboardItemHolder(view);
112 mConditionAdapter = new ConditionAdapter(mContext, mOneCondition, true);
114 mConditionAdapter.onBindViewHolder(viewHolder, 0);
115 final View card = view.findViewById(R.id.content);
117 verify(mCondition1).onPrimaryClick();
121 public void onSwiped_nullCondition_shouldNotCrash() {
122 final RecyclerView recyclerView = new RecyclerView(mContext);
123 final View view = LayoutInflater.from(mContext).inflate(
124 R.layout.condition_tile, new LinearLayout(mContext), true);
125 final DashboardAdapter.DashboardItemHolder viewHolder =
126 new DashboardAdapter.DashboardItemHolder(view);
127 mConditionAdapter = new ConditionAdapter(mContext, mOneCondition, true);
128 mConditionAdapter.addDismissHandling(recyclerView);
130 // do not bind viewholder to simulate the null condition scenario
131 mConditionAdapter.mSwipeCallback.onSwiped(viewHolder, 0);