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