• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.notification;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.verify;
22 
23 import android.content.Context;
24 import android.support.v7.preference.Preference;
25 import android.support.v7.preference.PreferenceViewHolder;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.widget.LinearLayout;
29 import android.widget.Switch;
30 
31 import com.android.settings.R;
32 import com.android.settings.testutils.SettingsRobolectricTestRunner;
33 import com.android.settingslib.RestrictedLockUtils;
34 
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.robolectric.RuntimeEnvironment;
39 
40 @RunWith(SettingsRobolectricTestRunner.class)
41 public class NotificationAppPreferenceTest {
42 
43     private Context mContext;
44 
45     @Before
setUp()46     public void setUp() {
47         mContext = RuntimeEnvironment.application;
48     }
49 
50     @Test
createNewPreference_shouldSetLayout()51     public void createNewPreference_shouldSetLayout() {
52         final NotificationAppPreference preference = new NotificationAppPreference(mContext);
53         assertThat(preference.getWidgetLayoutResource()).isEqualTo(
54                 R.layout.preference_widget_master_switch);
55     }
56 
57     @Test
setChecked_shouldUpdateButtonCheckedState()58     public void setChecked_shouldUpdateButtonCheckedState() {
59         final NotificationAppPreference preference = new NotificationAppPreference(mContext);
60         final LayoutInflater inflater = LayoutInflater.from(mContext);
61         final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
62                 inflater.inflate(R.layout.preference_app, null));
63         final LinearLayout widgetView = holder.itemView.findViewById(android.R.id.widget_frame);
64         inflater.inflate(R.layout.preference_widget_master_switch, widgetView, true);
65         final Switch toggle = (Switch) holder.findViewById(R.id.switchWidget);
66         preference.onBindViewHolder(holder);
67 
68         preference.setChecked(true);
69         assertThat(toggle.isChecked()).isTrue();
70 
71         preference.setChecked(false);
72         assertThat(toggle.isChecked()).isFalse();
73     }
74 
75     @Test
setSwitchEnabled_shouldUpdateButtonEnabledState()76     public void setSwitchEnabled_shouldUpdateButtonEnabledState() {
77         final NotificationAppPreference preference = new NotificationAppPreference(mContext);
78         final LayoutInflater inflater = LayoutInflater.from(mContext);
79         final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
80                 inflater.inflate(R.layout.preference_app, null));
81         final LinearLayout widgetView = holder.itemView.findViewById(android.R.id.widget_frame);
82         inflater.inflate(R.layout.preference_widget_master_switch, widgetView, true);
83         final Switch toggle = (Switch) holder.findViewById(R.id.switchWidget);
84         preference.onBindViewHolder(holder);
85 
86         preference.setSwitchEnabled(true);
87         assertThat(toggle.isEnabled()).isTrue();
88 
89         preference.setSwitchEnabled(false);
90         assertThat(toggle.isEnabled()).isFalse();
91     }
92 
93     @Test
setSwitchEnabled_shouldUpdateButtonEnabledState_beforeViewBound()94     public void setSwitchEnabled_shouldUpdateButtonEnabledState_beforeViewBound() {
95         final NotificationAppPreference preference = new NotificationAppPreference(mContext);
96         final LayoutInflater inflater = LayoutInflater.from(mContext);
97         final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
98                 inflater.inflate(R.layout.preference_app, null));
99         final LinearLayout widgetView = holder.itemView.findViewById(android.R.id.widget_frame);
100         inflater.inflate(R.layout.preference_widget_master_switch, widgetView, true);
101         final Switch toggle = (Switch) holder.findViewById(R.id.switchWidget);
102 
103         preference.setSwitchEnabled(false);
104         preference.onBindViewHolder(holder);
105         assertThat(toggle.isEnabled()).isFalse();
106     }
107 
108     @Test
clickWidgetView_shouldToggleButton()109     public void clickWidgetView_shouldToggleButton() {
110         final NotificationAppPreference preference = new NotificationAppPreference(mContext);
111         final LayoutInflater inflater = LayoutInflater.from(mContext);
112         final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
113                 inflater.inflate(R.layout.preference_app, null));
114         final LinearLayout widgetView = holder.itemView.findViewById(android.R.id.widget_frame);
115         assertThat(widgetView).isNotNull();
116 
117         inflater.inflate(R.layout.preference_widget_master_switch, widgetView, true);
118         final Switch toggle = (Switch) holder.findViewById(R.id.switchWidget);
119         preference.onBindViewHolder(holder);
120 
121         widgetView.performClick();
122         assertThat(toggle.isChecked()).isTrue();
123 
124         widgetView.performClick();
125         assertThat(toggle.isChecked()).isFalse();
126     }
127 
128     @Test
clickWidgetView_shouldNotToggleButtonIfDisabled()129     public void clickWidgetView_shouldNotToggleButtonIfDisabled() {
130         final NotificationAppPreference preference = new NotificationAppPreference(mContext);
131         final LayoutInflater inflater = LayoutInflater.from(mContext);
132         final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
133                 inflater.inflate(R.layout.preference_app, null));
134         final LinearLayout widgetView = holder.itemView.findViewById(android.R.id.widget_frame);
135         assertThat(widgetView).isNotNull();
136 
137         inflater.inflate(R.layout.preference_widget_master_switch, widgetView, true);
138         final Switch toggle = (Switch) holder.findViewById(R.id.switchWidget);
139         preference.onBindViewHolder(holder);
140         toggle.setEnabled(false);
141 
142         widgetView.performClick();
143         assertThat(toggle.isChecked()).isFalse();
144     }
145 
146     @Test
clickWidgetView_shouldNotifyPreferenceChanged()147     public void clickWidgetView_shouldNotifyPreferenceChanged() {
148         final NotificationAppPreference preference = new NotificationAppPreference(mContext);
149         final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
150                 LayoutInflater.from(mContext).inflate(R.layout.preference_app, null));
151         final View widgetView = holder.findViewById(android.R.id.widget_frame);
152         final Preference.OnPreferenceChangeListener
153                 listener = mock(Preference.OnPreferenceChangeListener.class);
154         preference.setOnPreferenceChangeListener(listener);
155         preference.onBindViewHolder(holder);
156 
157         preference.setChecked(false);
158         widgetView.performClick();
159         verify(listener).onPreferenceChange(preference, true);
160 
161         preference.setChecked(true);
162         widgetView.performClick();
163         verify(listener).onPreferenceChange(preference, false);
164     }
165 
166     @Test
setDisabledByAdmin_hasEnforcedAdmin_shouldDisableButton()167     public void setDisabledByAdmin_hasEnforcedAdmin_shouldDisableButton() {
168         final NotificationAppPreference preference = new NotificationAppPreference(mContext);
169         final LayoutInflater inflater = LayoutInflater.from(mContext);
170         final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
171                 inflater.inflate(R.layout.preference_app, null));
172         final LinearLayout widgetView = holder.itemView.findViewById(android.R.id.widget_frame);
173         inflater.inflate(R.layout.preference_widget_master_switch, widgetView, true);
174         final Switch toggle = (Switch) holder.findViewById(R.id.switchWidget);
175         toggle.setEnabled(true);
176         preference.onBindViewHolder(holder);
177 
178         preference.setDisabledByAdmin(mock(RestrictedLockUtils.EnforcedAdmin.class));
179         assertThat(toggle.isEnabled()).isFalse();
180     }
181 
182     @Test
setDisabledByAdmin_noEnforcedAdmin_shouldEnableButton()183     public void setDisabledByAdmin_noEnforcedAdmin_shouldEnableButton() {
184         final NotificationAppPreference preference = new NotificationAppPreference(mContext);
185         final LayoutInflater inflater = LayoutInflater.from(mContext);
186         final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
187                 inflater.inflate(R.layout.preference_app, null));
188         final LinearLayout widgetView = holder.itemView.findViewById(android.R.id.widget_frame);
189         inflater.inflate(R.layout.preference_widget_master_switch, widgetView, true);
190         final Switch toggle = (Switch) holder.findViewById(R.id.switchWidget);
191         toggle.setEnabled(false);
192         preference.onBindViewHolder(holder);
193 
194         preference.setDisabledByAdmin(null);
195         assertThat(toggle.isEnabled()).isTrue();
196     }
197 
198     @Test
onBindViewHolder_toggleButtonShouldHaveContentDescription()199     public void onBindViewHolder_toggleButtonShouldHaveContentDescription() {
200         final NotificationAppPreference preference = new NotificationAppPreference(mContext);
201         final LayoutInflater inflater = LayoutInflater.from(mContext);
202         final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
203                 inflater.inflate(R.layout.preference_app, null));
204         final LinearLayout widgetView = holder.itemView.findViewById(android.R.id.widget_frame);
205         inflater.inflate(R.layout.preference_widget_master_switch, widgetView, true);
206         final Switch toggle = (Switch) holder.findViewById(R.id.switchWidget);
207         final String label = "TestButton";
208         preference.setTitle(label);
209 
210         preference.onBindViewHolder(holder);
211 
212         assertThat(toggle.getContentDescription()).isEqualTo(label);
213     }
214 }
215