• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.mockito.Matchers.any;
21 import static org.mockito.Matchers.anyBoolean;
22 import static org.mockito.Matchers.anyInt;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25 
26 import android.content.Context;
27 import android.support.v7.preference.PreferenceViewHolder;
28 import android.view.View;
29 import android.widget.Button;
30 
31 import com.android.settings.R;
32 import com.android.settings.testutils.SettingsRobolectricTestRunner;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.robolectric.RuntimeEnvironment;
38 
39 @RunWith(SettingsRobolectricTestRunner.class)
40 public class ActionButtonPreferenceTest {
41 
42     private Context mContext;
43     private View mRootView;
44     private ActionButtonPreference mPref;
45     private PreferenceViewHolder mHolder;
46 
47     @Before
setUp()48     public void setUp() {
49         mContext = RuntimeEnvironment.application;
50         mRootView = View.inflate(mContext, R.layout.two_action_buttons, null /* parent */);
51         mHolder = PreferenceViewHolder.createInstanceForTests(mRootView);
52         mPref = new ActionButtonPreference(mContext);
53     }
54 
55     @Test
setVisibility_shouldUpdateButtonVisibility()56     public void setVisibility_shouldUpdateButtonVisibility() {
57         mPref.setButton1Visible(false).setButton2Visible(false);
58         mPref.onBindViewHolder(mHolder);
59 
60         assertThat(mRootView.findViewById(R.id.button1_positive).getVisibility())
61                 .isEqualTo(View.INVISIBLE);
62         assertThat(mRootView.findViewById(R.id.button1_negative).getVisibility())
63                 .isEqualTo(View.INVISIBLE);
64 
65         assertThat(mRootView.findViewById(R.id.button2_positive).getVisibility())
66                 .isEqualTo(View.INVISIBLE);
67         assertThat(mRootView.findViewById(R.id.button2_negative).getVisibility())
68                 .isEqualTo(View.INVISIBLE);
69 
70         mPref.setButton1Visible(true).setButton2Visible(true);
71         mPref.onBindViewHolder(mHolder);
72 
73         assertThat(mRootView.findViewById(R.id.button1_positive).getVisibility())
74                 .isEqualTo(View.VISIBLE);
75         assertThat(mRootView.findViewById(R.id.button1_negative).getVisibility())
76                 .isEqualTo(View.INVISIBLE);
77         assertThat(mRootView.findViewById(R.id.button2_positive).getVisibility())
78                 .isEqualTo(View.VISIBLE);
79         assertThat(mRootView.findViewById(R.id.button2_negative).getVisibility())
80                 .isEqualTo(View.INVISIBLE);
81     }
82 
83     @Test
setPositiveNegative_shouldHideOppositeButton()84     public void setPositiveNegative_shouldHideOppositeButton() {
85         mPref.setButton1Positive(true).setButton2Positive(false);
86         mPref.onBindViewHolder(mHolder);
87 
88         assertThat(mRootView.findViewById(R.id.button1_positive).getVisibility())
89                 .isEqualTo(View.VISIBLE);
90         assertThat(mRootView.findViewById(R.id.button1_negative).getVisibility())
91                 .isEqualTo(View.INVISIBLE);
92         assertThat(mRootView.findViewById(R.id.button2_positive).getVisibility())
93                 .isEqualTo(View.INVISIBLE);
94         assertThat(mRootView.findViewById(R.id.button2_negative).getVisibility())
95                 .isEqualTo(View.VISIBLE);
96     }
97 
98     @Test
setEnabled_shouldEnableButton()99     public void setEnabled_shouldEnableButton() {
100         mPref.setButton1Enabled(true).setButton2Enabled(false);
101         mPref.onBindViewHolder(mHolder);
102 
103         assertThat(mRootView.findViewById(R.id.button1_positive).isEnabled()).isTrue();
104         assertThat(mRootView.findViewById(R.id.button1_negative).isEnabled()).isTrue();
105         assertThat(mRootView.findViewById(R.id.button2_positive).isEnabled()).isFalse();
106         assertThat(mRootView.findViewById(R.id.button2_negative).isEnabled()).isFalse();
107     }
108 
109     @Test
setText()110     public void setText() {
111         mPref.setButton1Text(R.string.settings_label);
112         mPref.onBindViewHolder(mHolder);
113 
114         assertThat(((Button) mRootView.findViewById(R.id.button1_positive)).getText())
115                 .isEqualTo(mContext.getText(R.string.settings_label));
116         assertThat(((Button) mRootView.findViewById(R.id.button1_negative)).getText())
117                 .isEqualTo(mContext.getText(R.string.settings_label));
118     }
119 
createMock()120     public static ActionButtonPreference createMock() {
121         final ActionButtonPreference pref = mock(ActionButtonPreference.class);
122         when(pref.setButton1Text(anyInt())).thenReturn(pref);
123         when(pref.setButton1Positive(anyBoolean())).thenReturn(pref);
124         when(pref.setButton1Enabled(anyBoolean())).thenReturn(pref);
125         when(pref.setButton1Visible(anyBoolean())).thenReturn(pref);
126         when(pref.setButton1OnClickListener(any(View.OnClickListener.class))).thenReturn(pref);
127 
128         when(pref.setButton2Text(anyInt())).thenReturn(pref);
129         when(pref.setButton2Positive(anyBoolean())).thenReturn(pref);
130         when(pref.setButton2Enabled(anyBoolean())).thenReturn(pref);
131         when(pref.setButton2Visible(anyBoolean())).thenReturn(pref);
132         when(pref.setButton2OnClickListener(any(View.OnClickListener.class))).thenReturn(pref);
133         return pref;
134     }
135 }
136