• 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.widget;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.robolectric.RuntimeEnvironment.application;
21 
22 import android.content.Context;
23 import android.graphics.drawable.ColorDrawable;
24 import android.widget.TextView;
25 
26 import com.android.settings.R;
27 import com.android.settings.testutils.SettingsRobolectricTestRunner;
28 
29 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
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.RuntimeEnvironment;
35 
36 @RunWith(SettingsRobolectricTestRunner.class)
37 public class SwitchBarTest {
38 
39     private static final int COLOR_BACKGROUND = 1;
40     private static final int COLOR_BACKGROUND_ACTIVATED = 2;
41 
42     private Context mContext;
43     private SwitchBar mBar;
44 
45     @Before
setUp()46     public void setUp() {
47         mContext = RuntimeEnvironment.application;
48         mBar = new SwitchBar(application, Robolectric.buildAttributeSet()
49                 .addAttribute(R.attr.switchBarBackgroundColor, String.valueOf(COLOR_BACKGROUND))
50                 .addAttribute(R.attr.switchBarBackgroundActivatedColor,
51                         String.valueOf(COLOR_BACKGROUND_ACTIVATED))
52                 .build());
53     }
54 
55     @Test
cycleChecked_defaultLabel_shouldUpdateTextAndBackground()56     public void cycleChecked_defaultLabel_shouldUpdateTextAndBackground() {
57         final int defaultOnText = R.string.switch_on_text;
58         final int defaultOffText = R.string.switch_off_text;
59 
60         assertThat(((TextView) mBar.findViewById(R.id.switch_text)).getText())
61                 .isEqualTo(mContext.getString(defaultOffText));
62         assertThat(mBar.getBackground()).isEqualTo(new ColorDrawable(COLOR_BACKGROUND));
63 
64         mBar.setChecked(true);
65 
66         assertThat(mBar.getBackground()).isEqualTo(new ColorDrawable(COLOR_BACKGROUND_ACTIVATED));
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(mBar.getBackground()).isEqualTo(new ColorDrawable(COLOR_BACKGROUND));
78         assertThat(((TextView) mBar.findViewById(R.id.switch_text)).getText())
79                 .isEqualTo(mContext.getString(offText));
80 
81         mBar.setChecked(true);
82         assertThat(mBar.getBackground()).isEqualTo(new ColorDrawable(COLOR_BACKGROUND_ACTIVATED));
83         assertThat(((TextView) mBar.findViewById(R.id.switch_text)).getText())
84                 .isEqualTo(mContext.getString(onText));
85     }
86 
87     @Test
disabledByAdmin_shouldDelegateToRestrictedIcon()88     public void disabledByAdmin_shouldDelegateToRestrictedIcon() {
89         mBar.setDisabledByAdmin(new EnforcedAdmin());
90         assertThat(mBar.getDelegatingView().getId()).isEqualTo(R.id.restricted_icon);
91     }
92 
93     @Test
notDisabledByAdmin_shouldDelegateToSwitch()94     public void notDisabledByAdmin_shouldDelegateToSwitch() {
95         mBar.setDisabledByAdmin(null);
96         assertThat(mBar.getDelegatingView().getId()).isEqualTo(R.id.switch_widget);
97     }
98 }
99