1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17 package com.android.settingslib.widget; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.content.Context; 22 import android.view.LayoutInflater; 23 import android.view.View; 24 import android.widget.ImageView; 25 import android.widget.TextView; 26 27 import org.junit.Before; 28 import org.junit.Rule; 29 import org.junit.Test; 30 import org.junit.rules.ExpectedException; 31 import org.junit.runner.RunWith; 32 import org.robolectric.RobolectricTestRunner; 33 import org.robolectric.RuntimeEnvironment; 34 35 @RunWith(RobolectricTestRunner.class) 36 public class AppEntitiesHeaderControllerTest { 37 38 private static final CharSequence TITLE = "APP_TITLE"; 39 private static final CharSequence SUMMARY = "APP_SUMMARY"; 40 41 @Rule 42 public final ExpectedException thrown = ExpectedException.none(); 43 44 private Context mContext; 45 private View mAppEntitiesHeaderView; 46 private AppEntitiesHeaderController mController; 47 private AppEntityInfo mAppEntityInfo; 48 49 @Before setUp()50 public void setUp() { 51 mContext = RuntimeEnvironment.application; 52 mAppEntitiesHeaderView = LayoutInflater.from(mContext).inflate( 53 R.layout.app_entities_header, null /* root */); 54 mController = AppEntitiesHeaderController.newInstance(mContext, 55 mAppEntitiesHeaderView); 56 mAppEntityInfo = new AppEntityInfo.Builder() 57 .setIcon(mContext.getDrawable(com.android.internal.R.drawable.ic_menu)) 58 .setTitle(TITLE) 59 .setSummary(SUMMARY) 60 .setOnClickListener(v -> { 61 }) 62 .build(); 63 mController.setAppEntity(0, mAppEntityInfo); 64 } 65 66 @Test assert_amountOfMaximumAppsAreThree()67 public void assert_amountOfMaximumAppsAreThree() { 68 assertThat(AppEntitiesHeaderController.MAXIMUM_APPS).isEqualTo(3); 69 } 70 71 @Test setHeaderTitleRes_setTextRes_shouldSetToTitleView()72 public void setHeaderTitleRes_setTextRes_shouldSetToTitleView() { 73 mController.setHeaderTitleRes(R.string.expand_button_title).apply(); 74 final TextView view = mAppEntitiesHeaderView.findViewById(R.id.header_title); 75 76 assertThat(view.getText()).isEqualTo(mContext.getText(R.string.expand_button_title)); 77 } 78 79 @Test setHeaderDetailsRes_setTextRes_shouldSetToDetailsView()80 public void setHeaderDetailsRes_setTextRes_shouldSetToDetailsView() { 81 mController.setHeaderDetailsRes(R.string.expand_button_title).apply(); 82 final TextView view = mAppEntitiesHeaderView.findViewById(R.id.header_details); 83 84 assertThat(view.getText()).isEqualTo(mContext.getText(R.string.expand_button_title)); 85 } 86 87 @Test setHeaderDetails_onlyDetailsTextSet_shouldSetToDetailsView()88 public void setHeaderDetails_onlyDetailsTextSet_shouldSetToDetailsView() { 89 mController.setHeaderDetails(TITLE).apply(); 90 final TextView view = mAppEntitiesHeaderView.findViewById(R.id.header_details); 91 92 assertThat(view.getText()).isEqualTo(TITLE); 93 } 94 95 @Test setHeaderDetails_detailsTextAndResBothSet_shouldSetTextToDetailsView()96 public void setHeaderDetails_detailsTextAndResBothSet_shouldSetTextToDetailsView() { 97 mController.setHeaderDetailsRes(R.string.expand_button_title); 98 mController.setHeaderDetails(TITLE).apply(); 99 final TextView view = mAppEntitiesHeaderView.findViewById(R.id.header_details); 100 101 assertThat(view.getText()).isEqualTo(TITLE); 102 } 103 104 @Test setHeaderDetailsClickListener_setClickListener_detailsViewAttachClickListener()105 public void setHeaderDetailsClickListener_setClickListener_detailsViewAttachClickListener() { 106 mController.setHeaderDetailsClickListener(v -> { 107 }).apply(); 108 final TextView view = mAppEntitiesHeaderView.findViewById(R.id.header_details); 109 110 assertThat(view.hasOnClickListeners()).isTrue(); 111 } 112 113 @Test setAppEntity_indexLessThanZero_shouldThrowArrayIndexOutOfBoundsException()114 public void setAppEntity_indexLessThanZero_shouldThrowArrayIndexOutOfBoundsException() { 115 thrown.expect(ArrayIndexOutOfBoundsException.class); 116 117 mController.setAppEntity(-1, mAppEntityInfo); 118 } 119 120 @Test asetAppEntity_indexGreaterThanMaximum_shouldThrowArrayIndexOutOfBoundsException()121 public void asetAppEntity_indexGreaterThanMaximum_shouldThrowArrayIndexOutOfBoundsException() { 122 thrown.expect(ArrayIndexOutOfBoundsException.class); 123 124 mController.setAppEntity(AppEntitiesHeaderController.MAXIMUM_APPS + 1, mAppEntityInfo); 125 } 126 127 @Test setAppEntity_addAppToIndex0_shouldShowAppView1()128 public void setAppEntity_addAppToIndex0_shouldShowAppView1() { 129 mController.setAppEntity(0, mAppEntityInfo).apply(); 130 final View app1View = mAppEntitiesHeaderView.findViewById(R.id.app1_view); 131 final ImageView appIconView = app1View.findViewById(R.id.app_icon); 132 final TextView appTitle = app1View.findViewById(R.id.app_title); 133 final TextView appSummary = app1View.findViewById(R.id.app_summary); 134 135 assertThat(app1View.getVisibility()).isEqualTo(View.VISIBLE); 136 assertThat(app1View.hasOnClickListeners()).isTrue(); 137 assertThat(appIconView.getDrawable()).isNotNull(); 138 assertThat(appTitle.getText()).isEqualTo(TITLE); 139 assertThat(appSummary.getText()).isEqualTo(SUMMARY); 140 } 141 142 @Test setAppEntity_addAppToIndex1_shouldShowAppView2()143 public void setAppEntity_addAppToIndex1_shouldShowAppView2() { 144 mController.setAppEntity(1, mAppEntityInfo).apply(); 145 final View app2View = mAppEntitiesHeaderView.findViewById(R.id.app2_view); 146 final ImageView appIconView = app2View.findViewById(R.id.app_icon); 147 final TextView appTitle = app2View.findViewById(R.id.app_title); 148 final TextView appSummary = app2View.findViewById(R.id.app_summary); 149 150 assertThat(app2View.getVisibility()).isEqualTo(View.VISIBLE); 151 assertThat(app2View.hasOnClickListeners()).isTrue(); 152 assertThat(appIconView.getDrawable()).isNotNull(); 153 assertThat(appTitle.getText()).isEqualTo(TITLE); 154 assertThat(appSummary.getText()).isEqualTo(SUMMARY); 155 } 156 157 @Test setAppEntity_addAppToIndex2_shouldShowAppView3()158 public void setAppEntity_addAppToIndex2_shouldShowAppView3() { 159 mController.setAppEntity(2, mAppEntityInfo).apply(); 160 final View app3View = mAppEntitiesHeaderView.findViewById(R.id.app3_view); 161 final ImageView appIconView = app3View.findViewById(R.id.app_icon); 162 final TextView appTitle = app3View.findViewById(R.id.app_title); 163 final TextView appSummary = app3View.findViewById(R.id.app_summary); 164 165 assertThat(app3View.getVisibility()).isEqualTo(View.VISIBLE); 166 assertThat(app3View.hasOnClickListeners()).isTrue(); 167 assertThat(appIconView.getDrawable()).isNotNull(); 168 assertThat(appTitle.getText()).isEqualTo(TITLE); 169 assertThat(appSummary.getText()).isEqualTo(SUMMARY); 170 } 171 172 @Test removeAppEntity_removeIndex0_shouldNotShowAppView1()173 public void removeAppEntity_removeIndex0_shouldNotShowAppView1() { 174 mController.setAppEntity(0, mAppEntityInfo) 175 .setAppEntity(1, mAppEntityInfo).apply(); 176 final View app1View = mAppEntitiesHeaderView.findViewById(R.id.app1_view); 177 final View app2View = mAppEntitiesHeaderView.findViewById(R.id.app2_view); 178 179 assertThat(app1View.getVisibility()).isEqualTo(View.VISIBLE); 180 assertThat(app2View.getVisibility()).isEqualTo(View.VISIBLE); 181 182 mController.removeAppEntity(0).apply(); 183 184 assertThat(app1View.getVisibility()).isEqualTo(View.GONE); 185 assertThat(app2View.getVisibility()).isEqualTo(View.VISIBLE); 186 } 187 188 @Test clearAllAppEntities_shouldNotShowAllAppViews()189 public void clearAllAppEntities_shouldNotShowAllAppViews() { 190 mController.setAppEntity(0, mAppEntityInfo) 191 .setAppEntity(1, mAppEntityInfo) 192 .setAppEntity(2, mAppEntityInfo).apply(); 193 final View appViewsContainer = mAppEntitiesHeaderView.findViewById( 194 R.id.app_views_container); 195 final View app1View = mAppEntitiesHeaderView.findViewById(R.id.app1_view); 196 final View app2View = mAppEntitiesHeaderView.findViewById(R.id.app2_view); 197 final View app3View = mAppEntitiesHeaderView.findViewById(R.id.app3_view); 198 199 assertThat(app1View.getVisibility()).isEqualTo(View.VISIBLE); 200 assertThat(app2View.getVisibility()).isEqualTo(View.VISIBLE); 201 assertThat(app3View.getVisibility()).isEqualTo(View.VISIBLE); 202 203 mController.clearAllAppEntities().apply(); 204 205 assertThat(appViewsContainer.getVisibility()).isEqualTo(View.GONE); 206 } 207 208 @Test apply_noAppEntitySet_shouldOnlyShowTitleAndEmptyView()209 public void apply_noAppEntitySet_shouldOnlyShowTitleAndEmptyView() { 210 mController.setHeaderTitleRes(R.string.expand_button_title) 211 .setAppEntity(0, mAppEntityInfo) 212 .setAppEntity(1, mAppEntityInfo) 213 .setAppEntity(2, mAppEntityInfo).apply(); 214 final View titleView = mAppEntitiesHeaderView.findViewById(R.id.header_title); 215 final View detailsView = mAppEntitiesHeaderView.findViewById(R.id.header_details); 216 final View emptyView = mAppEntitiesHeaderView.findViewById(R.id.empty_view); 217 final View appViewsContainer = mAppEntitiesHeaderView.findViewById( 218 R.id.app_views_container); 219 220 mController.clearAllAppEntities().apply(); 221 222 assertThat(titleView.getVisibility()).isEqualTo(View.VISIBLE); 223 assertThat(emptyView.getVisibility()).isEqualTo(View.VISIBLE); 224 225 assertThat(detailsView.getVisibility()).isEqualTo(View.GONE); 226 assertThat(appViewsContainer.getVisibility()).isEqualTo(View.GONE); 227 } 228 } 229