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.widget;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.verify;
23 
24 import android.content.Context;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.widget.CheckBox;
28 import android.widget.LinearLayout;
29 
30 import androidx.preference.Preference.OnPreferenceChangeListener;
31 import androidx.preference.PreferenceViewHolder;
32 
33 import com.android.settings.R;
34 
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.robolectric.RobolectricTestRunner;
39 import org.robolectric.RuntimeEnvironment;
40 
41 @RunWith(RobolectricTestRunner.class)
42 public class MasterCheckBoxPreferenceTest {
43 
44     private Context mContext;
45     private MasterCheckBoxPreference mPreference;
46 
47     @Before
setUp()48     public void setUp() {
49         mContext = RuntimeEnvironment.application;
50         mPreference = new MasterCheckBoxPreference(mContext);
51     }
52 
53     @Test
createNewPreference_shouldSetLayout()54     public void createNewPreference_shouldSetLayout() {
55         assertThat(mPreference.getWidgetLayoutResource())
56                 .isEqualTo(R.layout.preference_widget_master_checkbox);
57     }
58 
59     @Test
setChecked_shouldUpdateCheckBoxCheckedState()60     public void setChecked_shouldUpdateCheckBoxCheckedState() {
61         final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
62                 LayoutInflater.from(mContext).inflate(
63                   R.layout.preference_widget_master_checkbox, null));
64         final CheckBox checkBox = (CheckBox) holder.findViewById(R.id.checkboxWidget);
65         mPreference.onBindViewHolder(holder);
66 
67         mPreference.setChecked(true);
68         assertThat(checkBox.isChecked()).isTrue();
69 
70         mPreference.setChecked(false);
71         assertThat(checkBox.isChecked()).isFalse();
72     }
73 
74     @Test
setEnabled_shouldUpdateCheckBoxEnabledState()75     public void setEnabled_shouldUpdateCheckBoxEnabledState() {
76         final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
77                 LayoutInflater.from(mContext).inflate(
78                   R.layout.preference_widget_master_checkbox, null));
79         final CheckBox checkBox = (CheckBox) holder.findViewById(R.id.checkboxWidget);
80         mPreference.onBindViewHolder(holder);
81 
82         mPreference.setEnabled(true);
83         assertThat(checkBox.isEnabled()).isTrue();
84 
85         mPreference.setEnabled(false);
86         assertThat(checkBox.isEnabled()).isFalse();
87     }
88 
89     @Test
setCheckboxEnabled_shouldOnlyUpdateCheckBoxEnabledState()90     public void setCheckboxEnabled_shouldOnlyUpdateCheckBoxEnabledState() {
91         final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
92                 LayoutInflater.from(mContext).inflate(
93                         R.layout.preference_widget_master_checkbox, null));
94         final CheckBox checkBox = (CheckBox) holder.findViewById(R.id.checkboxWidget);
95         mPreference.onBindViewHolder(holder);
96 
97         mPreference.setCheckBoxEnabled(false);
98         assertThat(mPreference.isEnabled()).isTrue();
99         assertThat(checkBox.isEnabled()).isFalse();
100 
101         mPreference.setCheckBoxEnabled(true);
102         assertThat(mPreference.isEnabled()).isTrue();
103         assertThat(checkBox.isEnabled()).isTrue();
104     }
105 
106     @Test
onBindViewHolder_shouldSetCheckboxEnabledState()107     public void onBindViewHolder_shouldSetCheckboxEnabledState() {
108         final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
109                 LayoutInflater.from(mContext).inflate(
110                         R.layout.preference_widget_master_checkbox, null));
111         final CheckBox checkBox = (CheckBox) holder.findViewById(R.id.checkboxWidget);
112 
113         mPreference.setCheckBoxEnabled(false);
114         mPreference.onBindViewHolder(holder);
115 
116         assertThat(mPreference.isEnabled()).isTrue();
117         assertThat(checkBox.isEnabled()).isFalse();
118 
119         mPreference.setCheckBoxEnabled(true);
120         assertThat(mPreference.isEnabled()).isTrue();
121         assertThat(checkBox.isEnabled()).isTrue();
122     }
123 
124     @Test
clickWidgetView_shouldToggleCheckBox()125     public void clickWidgetView_shouldToggleCheckBox() {
126         final LayoutInflater inflater = LayoutInflater.from(mContext);
127         final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
128                 inflater.inflate(R.layout.preference_two_target, null));
129         final LinearLayout widgetView = holder.itemView.findViewById(android.R.id.widget_frame);
130         assertThat(widgetView).isNotNull();
131 
132         inflater.inflate(R.layout.preference_widget_master_checkbox, widgetView, true);
133         final CheckBox checkBox = (CheckBox) holder.findViewById(R.id.checkboxWidget);
134         mPreference.onBindViewHolder(holder);
135 
136         widgetView.performClick();
137         assertThat(checkBox.isChecked()).isTrue();
138 
139         widgetView.performClick();
140         assertThat(checkBox.isChecked()).isFalse();
141     }
142 
143     @Test
clickWidgetView_shouldNotToggleCheckBoxIfDisabled()144     public void clickWidgetView_shouldNotToggleCheckBoxIfDisabled() {
145         final LayoutInflater inflater = LayoutInflater.from(mContext);
146         final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
147                 inflater.inflate(R.layout.preference_two_target, null));
148         final LinearLayout widgetView = holder.itemView.findViewById(android.R.id.widget_frame);
149         assertThat(widgetView).isNotNull();
150 
151         inflater.inflate(R.layout.preference_widget_master_checkbox, widgetView, true);
152         final CheckBox checkBox = (CheckBox) holder.findViewById(R.id.checkboxWidget);
153         mPreference.onBindViewHolder(holder);
154         mPreference.setEnabled(false);
155 
156         widgetView.performClick();
157         assertThat(checkBox.isChecked()).isFalse();
158     }
159 
160     @Test
clickWidgetView_shouldNotifyPreferenceChanged()161     public void clickWidgetView_shouldNotifyPreferenceChanged() {
162         final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
163                 LayoutInflater.from(mContext).inflate(R.layout.preference_two_target, null));
164         final View widgetView = holder.findViewById(android.R.id.widget_frame);
165         final OnPreferenceChangeListener listener = mock(OnPreferenceChangeListener.class);
166         mPreference.setOnPreferenceChangeListener(listener);
167         mPreference.onBindViewHolder(holder);
168 
169         mPreference.setChecked(false);
170         widgetView.performClick();
171         verify(listener).onPreferenceChange(mPreference, true);
172 
173         mPreference.setChecked(true);
174         widgetView.performClick();
175         verify(listener).onPreferenceChange(mPreference, false);
176     }
177 
178     @Test
onBindViewHolder_checkBoxShouldHaveContentDescription()179     public void onBindViewHolder_checkBoxShouldHaveContentDescription() {
180         final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
181             LayoutInflater.from(mContext)
182                 .inflate(R.layout.preference_widget_master_checkbox, null));
183         final CheckBox checkBox = (CheckBox) holder.findViewById(R.id.checkboxWidget);
184         final String label = "TestButton";
185         mPreference.setTitle(label);
186 
187         mPreference.onBindViewHolder(holder);
188 
189         assertThat(checkBox.getContentDescription()).isEqualTo(label);
190     }
191 }
192