• 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.bluetooth;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.ArgumentMatchers.anyString;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.never;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 
29 import android.content.DialogInterface;
30 import android.widget.Button;
31 import android.widget.EditText;
32 
33 import androidx.appcompat.app.AlertDialog;
34 import androidx.fragment.app.FragmentActivity;
35 
36 import com.android.settings.R;
37 import com.android.settings.testutils.FakeFeatureFactory;
38 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
39 
40 import org.junit.Before;
41 import org.junit.Ignore;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Answers;
45 import org.mockito.Mock;
46 import org.mockito.MockitoAnnotations;
47 import org.robolectric.RobolectricTestRunner;
48 import org.robolectric.shadows.ShadowDialog;
49 import org.robolectric.shadows.androidx.fragment.FragmentController;
50 
51 @RunWith(RobolectricTestRunner.class)
52 public class RemoteDeviceNameDialogFragmentTest {
53 
54     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
55     private CachedBluetoothDevice mCachedDevice;
56 
57     private RemoteDeviceNameDialogFragment mFragment;
58 
59     @Before
setUp()60     public void setUp() {
61         MockitoAnnotations.initMocks(this);
62         FakeFeatureFactory.setupForTest();
63 
64         String deviceAddress = "55:66:77:88:99:AA";
65         when(mCachedDevice.getAddress()).thenReturn(deviceAddress);
66         when(mCachedDevice.getIdentityAddress()).thenReturn(deviceAddress);
67         mFragment = spy(RemoteDeviceNameDialogFragment.newInstance(mCachedDevice));
68         doReturn(mCachedDevice).when(mFragment).getDevice(any());
69     }
70 
71     /**
72      * Helper method to set the mock device's name and show the dialog.
73      *
74      * @param deviceName what name to set
75      * @return the dialog created
76      */
startDialog(String deviceName)77     AlertDialog startDialog(String deviceName) {
78         when(mCachedDevice.getName()).thenReturn(deviceName);
79         FragmentController.setupFragment(mFragment, FragmentActivity.class, 0 /* containerViewId */,
80                 null /* bundle */);
81         return (AlertDialog) ShadowDialog.getLatestDialog();
82     }
83 
84     @Ignore
85     @Test
deviceNameDisplayIsCorrect()86     public void deviceNameDisplayIsCorrect() {
87         String deviceName = "ABC Corp Headphones";
88         AlertDialog dialog = startDialog(deviceName);
89         EditText editText = dialog.findViewById(R.id.edittext);
90         assertThat(editText.getText().toString()).isEqualTo(deviceName);
91 
92         // Make sure that the "rename" button isn't enabled since the text hasn't changed yet, but
93         // the "cancel" button should be enabled.
94         Button positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
95         assertThat(positiveButton.isEnabled()).isFalse();
96         Button negativeButton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
97         assertThat(negativeButton.isEnabled()).isTrue();
98     }
99 
100     @Ignore
101     @Test
deviceNameEditSucceeds()102     public void deviceNameEditSucceeds() {
103         String deviceNameInitial = "ABC Corp Headphones";
104         String deviceNameModified = "My Headphones";
105         AlertDialog dialog = startDialog(deviceNameInitial);
106 
107         // Before modifying the text the "rename" button should be disabled but the cancel button
108         // should be enabled.
109         Button positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
110         Button negativeButton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
111         assertThat(negativeButton.isEnabled()).isTrue();
112         assertThat(positiveButton.isEnabled()).isFalse();
113 
114         // Once we modify the text, the positive button should be clickable, and clicking it should
115         // cause a call to change the name.
116         EditText editText = dialog.findViewById(R.id.edittext);
117         editText.setText(deviceNameModified);
118         assertThat(positiveButton.isEnabled()).isTrue();
119         positiveButton.performClick();
120         verify(mCachedDevice).setName(deviceNameModified);
121     }
122 
123     @Ignore
124     @Test
deviceNameEditThenCancelDoesntRename()125     public void deviceNameEditThenCancelDoesntRename() {
126         String deviceNameInitial = "ABC Corp Headphones";
127         String deviceNameModified = "My Headphones";
128         AlertDialog dialog = startDialog(deviceNameInitial);
129 
130         // Modifying the text but then hitting cancel should not cause the name to change.
131         Button negativeButton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
132         assertThat(negativeButton.isEnabled()).isTrue();
133         EditText editText = dialog.findViewById(R.id.edittext);
134         editText.setText(deviceNameModified);
135         negativeButton.performClick();
136         verify(mCachedDevice, never()).setName(anyString());
137     }
138 }
139