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