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.robolectric.RuntimeEnvironment.application;
22 
23 import android.content.Context;
24 import android.graphics.drawable.ColorDrawable;
25 import android.widget.TextView;
26 
27 import com.android.settings.R;
28 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
29 
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.robolectric.Robolectric;
34 import org.robolectric.RobolectricTestRunner;
35 import org.robolectric.RuntimeEnvironment;
36 
37 @RunWith(RobolectricTestRunner.class)
38 public class SwitchBarTest {
39 
40     private static final int COLOR_BACKGROUND = 1;
41     private static final int COLOR_BACKGROUND_ACTIVATED = 2;
42 
43     private Context mContext;
44     private SwitchBar mBar;
45 
46     @Before
setUp()47     public void setUp() {
48         mContext = RuntimeEnvironment.application;
49         mBar = new SwitchBar(application, Robolectric.buildAttributeSet()
50                 .addAttribute(R.attr.switchBarBackgroundColor, String.valueOf(COLOR_BACKGROUND))
51                 .addAttribute(R.attr.switchBarBackgroundActivatedColor,
52                         String.valueOf(COLOR_BACKGROUND_ACTIVATED))
53                 .build());
54     }
55 
56     @Test
cycleChecked_defaultLabel_shouldUpdateTextAndBackground()57     public void cycleChecked_defaultLabel_shouldUpdateTextAndBackground() {
58         final int defaultOnText = R.string.switch_on_text;
59         final int defaultOffText = R.string.switch_off_text;
60 
61         assertThat(((TextView) mBar.findViewById(R.id.switch_text)).getText())
62                 .isEqualTo(mContext.getString(defaultOffText));
63 
64         mBar.setChecked(true);
65 
66         assertThat(mBar.getBackground()).isInstanceOf(ColorDrawable.class);
67         assertThat(((TextView) mBar.findViewById(R.id.switch_text)).getText())
68                 .isEqualTo(mContext.getString(defaultOnText));
69     }
70 
71     @Test
cycleChecked_customLabel_shouldUpdateTextAndBackground()72     public void cycleChecked_customLabel_shouldUpdateTextAndBackground() {
73         final int onText = R.string.master_clear_progress_text;
74         final int offText = R.string.manage_space_text;
75 
76         mBar.setSwitchBarText(onText, offText);
77         assertThat(((TextView) mBar.findViewById(R.id.switch_text)).getText())
78                 .isEqualTo(mContext.getString(offText));
79 
80         mBar.setChecked(true);
81         assertThat(mBar.getBackground()).isInstanceOf(ColorDrawable.class);
82 
83         assertThat(((TextView) mBar.findViewById(R.id.switch_text)).getText())
84                 .isEqualTo(mContext.getString(onText));
85     }
86 
87     @Test
setCheck_customLabelWithStringType_shouldUpdateTextAndBackground()88     public void setCheck_customLabelWithStringType_shouldUpdateTextAndBackground() {
89         final String onText = mContext.getString(
90                 R.string.accessibility_service_master_switch_title);
91         final String offText = mContext.getString(
92                 R.string.accessibility_service_master_switch_title);
93         final TextView switchBarTextView = ((TextView) mBar.findViewById(R.id.switch_text));
94 
95         mBar.setSwitchBarText(onText, offText);
96 
97         assertThat(switchBarTextView.getText()).isEqualTo(offText);
98 
99         mBar.setChecked(true);
100 
101         assertThat(mBar.getBackground()).isInstanceOf(ColorDrawable.class);
102         assertThat(switchBarTextView.getText()).isEqualTo(onText);
103     }
104 
105     @Test
disabledByAdmin_shouldDelegateToRestrictedIcon()106     public void disabledByAdmin_shouldDelegateToRestrictedIcon() {
107         mBar.setDisabledByAdmin(new EnforcedAdmin());
108         assertThat(mBar.getDelegatingView().getId()).isEqualTo(R.id.restricted_icon);
109     }
110 
111     @Test
notDisabledByAdmin_shouldDelegateToSwitch()112     public void notDisabledByAdmin_shouldDelegateToSwitch() {
113         mBar.setDisabledByAdmin(null);
114         assertThat(mBar.getDelegatingView().getId()).isEqualTo(R.id.switch_widget);
115     }
116 
117     @Test
performClick_shouldIsCheckedValueChange()118     public void performClick_shouldIsCheckedValueChange() {
119         boolean isChecked = mBar.isChecked();
120         mBar.performClick();
121         assertThat(mBar.isChecked()).isEqualTo(!isChecked);
122     }
123 }
124