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 PrimaryCheckBoxPreferenceTest {
43 
44     private Context mContext;
45     private PrimaryCheckBoxPreference mPreference;
46 
47     @Before
setUp()48     public void setUp() {
49         mContext = RuntimeEnvironment.application;
50         mPreference = new PrimaryCheckBoxPreference(mContext);
51     }
52 
53     @Test
createNewPreference_shouldSetLayout()54     public void createNewPreference_shouldSetLayout() {
55         assertThat(mPreference.getWidgetLayoutResource())
56                 .isEqualTo(R.layout.preference_widget_primary_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_primary_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_primary_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_primary_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_primary_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(
129                         com.android.settingslib.widget.preference.twotarget.R.layout.preference_two_target, null));
130         final LinearLayout widgetView = holder.itemView.findViewById(android.R.id.widget_frame);
131         assertThat(widgetView).isNotNull();
132 
133         inflater.inflate(R.layout.preference_widget_primary_checkbox, widgetView, true);
134         final CheckBox checkBox = (CheckBox) holder.findViewById(R.id.checkboxWidget);
135         mPreference.onBindViewHolder(holder);
136 
137         widgetView.performClick();
138         assertThat(checkBox.isChecked()).isTrue();
139 
140         widgetView.performClick();
141         assertThat(checkBox.isChecked()).isFalse();
142     }
143 
144     @Test
clickWidgetView_shouldNotToggleCheckBoxIfDisabled()145     public void clickWidgetView_shouldNotToggleCheckBoxIfDisabled() {
146         final LayoutInflater inflater = LayoutInflater.from(mContext);
147         final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
148                 inflater.inflate(
149                         com.android.settingslib.widget.preference.twotarget.R.layout.preference_two_target, null));
150         final LinearLayout widgetView = holder.itemView.findViewById(android.R.id.widget_frame);
151         assertThat(widgetView).isNotNull();
152 
153         inflater.inflate(R.layout.preference_widget_primary_checkbox, widgetView, true);
154         final CheckBox checkBox = (CheckBox) holder.findViewById(R.id.checkboxWidget);
155         mPreference.onBindViewHolder(holder);
156         mPreference.setEnabled(false);
157 
158         widgetView.performClick();
159         assertThat(checkBox.isChecked()).isFalse();
160     }
161 
162     @Test
clickWidgetView_shouldNotifyPreferenceChanged()163     public void clickWidgetView_shouldNotifyPreferenceChanged() {
164         final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
165                 LayoutInflater.from(mContext).inflate(
166                         com.android.settingslib.widget.preference.twotarget.R.layout.preference_two_target, null));
167         final View widgetView = holder.findViewById(android.R.id.widget_frame);
168         final OnPreferenceChangeListener listener = mock(OnPreferenceChangeListener.class);
169         mPreference.setOnPreferenceChangeListener(listener);
170         mPreference.onBindViewHolder(holder);
171 
172         mPreference.setChecked(false);
173         widgetView.performClick();
174         verify(listener).onPreferenceChange(mPreference, true);
175 
176         mPreference.setChecked(true);
177         widgetView.performClick();
178         verify(listener).onPreferenceChange(mPreference, false);
179     }
180 
181     @Test
onBindViewHolder_checkBoxShouldHaveContentDescription()182     public void onBindViewHolder_checkBoxShouldHaveContentDescription() {
183         final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
184                 LayoutInflater.from(mContext).inflate(R.layout.preference_widget_primary_checkbox,
185                         null));
186         final CheckBox checkBox = (CheckBox) holder.findViewById(R.id.checkboxWidget);
187         final String label = "TestButton";
188         mPreference.setTitle(label);
189 
190         mPreference.onBindViewHolder(holder);
191 
192         assertThat(checkBox.getContentDescription()).isEqualTo(label);
193     }
194 }
195