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