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