• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 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.car.settings.bluetooth;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.app.AlertDialog;
22 import android.content.Context;
23 import android.view.inputmethod.EditorInfo;
24 import android.view.inputmethod.InputMethodManager;
25 import android.widget.EditText;
26 
27 import androidx.annotation.Nullable;
28 import androidx.annotation.StringRes;
29 
30 import com.android.car.settings.R;
31 import com.android.car.settings.testutils.BaseTestActivity;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.robolectric.Robolectric;
37 import org.robolectric.RobolectricTestRunner;
38 import org.robolectric.RuntimeEnvironment;
39 import org.robolectric.Shadows;
40 import org.robolectric.shadows.ShadowDialog;
41 
42 /** Unit test for {@link BluetoothRenameDialogFragment}. */
43 @RunWith(RobolectricTestRunner.class)
44 public class BluetoothRenameDialogFragmentTest {
45 
46     private TestBluetoothRenameDialogFragment mFragment;
47     private AlertDialog mDialog;
48 
49     @Before
setUp()50     public void setUp() {
51         BaseTestActivity activity = Robolectric.setupActivity(BaseTestActivity.class);
52         mFragment = new TestBluetoothRenameDialogFragment();
53         mFragment.show(activity.getSupportFragmentManager(), /* tag= */ null);
54         mDialog = (AlertDialog) ShadowDialog.getLatestDialog();
55     }
56 
57     @Test
initialTextIsCurrentDeviceName()58     public void initialTextIsCurrentDeviceName() {
59         EditText editText = getEditText();
60 
61         assertThat(editText.getText().toString()).isEqualTo(mFragment.getDeviceName());
62     }
63 
64     @Test
softInputShown()65     public void softInputShown() {
66         InputMethodManager imm =
67                 (InputMethodManager) RuntimeEnvironment.application.getSystemService(
68                         Context.INPUT_METHOD_SERVICE);
69         assertThat(Shadows.shadowOf(imm).isSoftInputVisible()).isTrue();
70     }
71 
72     @Test
noUserInput_positiveButtonDisabled()73     public void noUserInput_positiveButtonDisabled() {
74         assertThat(mDialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled()).isFalse();
75     }
76 
77     @Test
userInput_positiveButtonEnabled()78     public void userInput_positiveButtonEnabled() {
79         EditText editText = getEditText();
80         editText.append("1234");
81 
82         assertThat(mDialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled()).isTrue();
83     }
84 
85     @Test
userInput_emptyName_positiveButtonDisabled()86     public void userInput_emptyName_positiveButtonDisabled() {
87         EditText editText = getEditText();
88         editText.setText("");
89 
90         assertThat(mDialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled()).isFalse();
91     }
92 
93     @Test
nameUpdatedByCode_positiveButtonDisabled()94     public void nameUpdatedByCode_positiveButtonDisabled() {
95         EditText editText = getEditText();
96         editText.append("1234");
97 
98         mFragment.updateDeviceName();
99 
100         assertThat(mDialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled()).isFalse();
101     }
102 
103     @Test
editorDoneAction_dismissesDialog()104     public void editorDoneAction_dismissesDialog() {
105         EditText editText = getEditText();
106 
107         editText.onEditorAction(EditorInfo.IME_ACTION_DONE);
108 
109         assertThat(mDialog.isShowing()).isFalse();
110     }
111 
112     @Test
editorDoneAction_setsDeviceName()113     public void editorDoneAction_setsDeviceName() {
114         EditText editText = getEditText();
115         String editStr = "1234";
116         String expectedName = mFragment.getDeviceName() + editStr;
117 
118         editText.append(editStr);
119         editText.onEditorAction(EditorInfo.IME_ACTION_DONE);
120 
121         assertThat(mFragment.getDeviceName()).isEqualTo(expectedName);
122     }
123 
124     @Test
editorDoneAction_emptyName_doesNotSetDeviceName()125     public void editorDoneAction_emptyName_doesNotSetDeviceName() {
126         EditText editText = getEditText();
127         String expectedName = mFragment.getDeviceName();
128         String editStr = "";
129 
130         editText.setText(editStr);
131         editText.onEditorAction(EditorInfo.IME_ACTION_DONE);
132 
133         assertThat(mFragment.getDeviceName()).isEqualTo(expectedName);
134     }
135 
136     @Test
positiveButtonClicked_setsDeviceName()137     public void positiveButtonClicked_setsDeviceName() {
138         EditText editText = getEditText();
139         String editStr = "1234";
140         String expectedName = mFragment.getDeviceName() + editStr;
141 
142         editText.append(editStr);
143         mDialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick();
144 
145         assertThat(mFragment.getDeviceName()).isEqualTo(expectedName);
146     }
147 
getEditText()148     private EditText getEditText() {
149         return mDialog.findViewById(R.id.textbox);
150     }
151 
152     /** Concrete impl of {@link BluetoothRenameDialogFragment} for testing. */
153     public static class TestBluetoothRenameDialogFragment extends BluetoothRenameDialogFragment {
154 
155         private String mSetDeviceNameArg = "Device Name";
156 
157         @Override
158         @StringRes
getDialogTitle()159         protected int getDialogTitle() {
160             return R.string.bt_rename_dialog_title;
161         }
162 
163         @Nullable
164         @Override
getDeviceName()165         protected String getDeviceName() {
166             return mSetDeviceNameArg;
167         }
168 
169         @Override
setDeviceName(String deviceName)170         protected void setDeviceName(String deviceName) {
171             mSetDeviceNameArg = deviceName;
172         }
173     }
174 }
175