1 /* 2 * Copyright (C) 2019 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.settings.notification.app; 18 19 import static android.app.NotificationManager.IMPORTANCE_DEFAULT; 20 import static android.app.NotificationManager.IMPORTANCE_LOW; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import static org.mockito.Mockito.mock; 25 import static org.mockito.Mockito.spy; 26 import static org.mockito.Mockito.times; 27 import static org.mockito.Mockito.verify; 28 29 import android.content.Context; 30 import android.graphics.drawable.Drawable; 31 import android.view.ContextThemeWrapper; 32 import android.view.LayoutInflater; 33 import android.view.View; 34 import android.view.ViewGroup; 35 import android.widget.TextView; 36 37 import androidx.preference.PreferenceViewHolder; 38 39 import com.android.settings.R; 40 41 import org.junit.Before; 42 import org.junit.Test; 43 import org.junit.runner.RunWith; 44 import org.robolectric.RobolectricTestRunner; 45 import org.robolectric.RuntimeEnvironment; 46 47 @RunWith(RobolectricTestRunner.class) 48 public class ImportancePreferenceTest { 49 50 private Context mContext; 51 52 @Before setUp()53 public void setUp() { 54 Context context = spy(RuntimeEnvironment.application.getApplicationContext()); 55 mContext = new ContextThemeWrapper(context, R.style.Theme_Settings); 56 } 57 58 @Test createNewPreference_shouldSetLayout()59 public void createNewPreference_shouldSetLayout() { 60 final ImportancePreference preference = new ImportancePreference(mContext); 61 assertThat(preference.getLayoutResource()).isEqualTo( 62 R.layout.notif_importance_preference); 63 } 64 65 @Test onBindViewHolder_nonConfigurable()66 public void onBindViewHolder_nonConfigurable() { 67 final ImportancePreference preference = new ImportancePreference(mContext); 68 final LayoutInflater inflater = LayoutInflater.from(mContext); 69 PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests( 70 inflater.inflate(R.layout.notif_importance_preference, null)); 71 Drawable unselected = mock(Drawable.class); 72 Drawable selected = mock(Drawable.class); 73 preference.selectedBackground = selected; 74 preference.unselectedBackground = unselected; 75 76 preference.setConfigurable(false); 77 preference.setImportance(IMPORTANCE_DEFAULT); 78 preference.onBindViewHolder(holder); 79 80 assertThat(holder.itemView.findViewById(R.id.silence).isEnabled()).isFalse(); 81 assertThat(holder.itemView.findViewById(R.id.alert).isEnabled()).isFalse(); 82 83 assertThat(holder.itemView.findViewById(R.id.alert).getBackground()).isEqualTo(selected); 84 assertThat(holder.itemView.findViewById(R.id.silence).getBackground()) 85 .isEqualTo(unselected); 86 87 // other button 88 preference.setImportance(IMPORTANCE_LOW); 89 holder = PreferenceViewHolder.createInstanceForTests( 90 inflater.inflate(R.layout.notif_importance_preference, null)); 91 preference.onBindViewHolder(holder); 92 93 assertThat(holder.itemView.findViewById(R.id.alert).getBackground()).isEqualTo(unselected); 94 assertThat(holder.itemView.findViewById(R.id.silence).getBackground()).isEqualTo(selected); 95 } 96 97 @Test onBindViewHolder_selectButtonAndText()98 public void onBindViewHolder_selectButtonAndText() { 99 final ImportancePreference preference = new ImportancePreference(mContext); 100 final LayoutInflater inflater = LayoutInflater.from(mContext); 101 final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests( 102 inflater.inflate(R.layout.notif_importance_preference, null)); 103 Drawable unselected = mock(Drawable.class); 104 Drawable selected = mock(Drawable.class); 105 preference.selectedBackground = selected; 106 preference.unselectedBackground = unselected; 107 108 preference.setConfigurable(true); 109 preference.setImportance(IMPORTANCE_DEFAULT); 110 111 preference.onBindViewHolder(holder); 112 113 assertThat(holder.itemView.findViewById(R.id.alert).getBackground()).isEqualTo(selected); 114 assertThat(holder.itemView.findViewById(R.id.silence).getBackground()) 115 .isEqualTo(unselected); 116 assertThat(((TextView) holder.itemView.findViewById(R.id.alert_summary)).getText()) 117 .isEqualTo(mContext.getString(R.string.notification_channel_summary_default)); 118 } 119 120 @Test onClick_changesUICallsListener()121 public void onClick_changesUICallsListener() { 122 final ImportancePreference preference = spy(new ImportancePreference(mContext)); 123 final LayoutInflater inflater = LayoutInflater.from(mContext); 124 final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests( 125 inflater.inflate(R.layout.notif_importance_preference, null)); 126 Drawable unselected = mock(Drawable.class); 127 Drawable selected = mock(Drawable.class); 128 preference.selectedBackground = selected; 129 preference.unselectedBackground = unselected; 130 131 preference.setConfigurable(true); 132 preference.setImportance(IMPORTANCE_DEFAULT); 133 preference.onBindViewHolder(holder); 134 135 View silenceButton = holder.itemView.findViewById(R.id.silence); 136 137 silenceButton.callOnClick(); 138 139 assertThat(holder.itemView.findViewById(R.id.alert).getBackground()).isEqualTo(unselected); 140 assertThat(holder.itemView.findViewById(R.id.silence).getBackground()).isEqualTo(selected); 141 142 verify(preference, times(1)).callChangeListener(IMPORTANCE_LOW); 143 } 144 145 @Test setImportanceSummary()146 public void setImportanceSummary() { 147 final ImportancePreference preference = spy(new ImportancePreference(mContext)); 148 final LayoutInflater inflater = LayoutInflater.from(mContext); 149 final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests( 150 inflater.inflate(R.layout.notif_importance_preference, null)); 151 152 preference.setConfigurable(true); 153 preference.setImportance(IMPORTANCE_DEFAULT); 154 preference.onBindViewHolder(holder); 155 156 TextView tv = holder.itemView.findViewById(R.id.silence_summary); 157 158 preference.setDisplayInStatusBar(true); 159 preference.setDisplayOnLockscreen(true); 160 161 preference.setImportanceSummary((ViewGroup) holder.itemView, IMPORTANCE_LOW, true); 162 163 assertThat(tv.getText()).isEqualTo( 164 mContext.getString(R.string.notification_channel_summary_low)); 165 } 166 167 @Test setImportanceSummary_default()168 public void setImportanceSummary_default() { 169 final ImportancePreference preference = spy(new ImportancePreference(mContext)); 170 final LayoutInflater inflater = LayoutInflater.from(mContext); 171 final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests( 172 inflater.inflate(R.layout.notif_importance_preference, null)); 173 174 preference.setConfigurable(true); 175 preference.setImportance(IMPORTANCE_DEFAULT); 176 preference.onBindViewHolder(holder); 177 178 TextView tv = holder.itemView.findViewById(R.id.alert_summary); 179 180 preference.setDisplayInStatusBar(true); 181 preference.setDisplayOnLockscreen(true); 182 183 preference.setImportanceSummary((ViewGroup) holder.itemView, IMPORTANCE_DEFAULT, true); 184 185 assertThat(tv.getText()).isEqualTo( 186 mContext.getString(R.string.notification_channel_summary_default)); 187 } 188 } 189