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.eq; 22 import static org.mockito.Mockito.doReturn; 23 import static org.mockito.Mockito.mock; 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.Fragment; 29 import android.app.FragmentManager; 30 import android.app.FragmentTransaction; 31 import android.content.Context; 32 import android.os.Bundle; 33 import android.view.MenuInflater; 34 import android.view.MenuItem; 35 36 import com.android.settings.R; 37 import com.android.settings.testutils.FakeFeatureFactory; 38 import com.android.settings.testutils.SettingsRobolectricTestRunner; 39 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 40 import com.android.settingslib.bluetooth.LocalBluetoothManager; 41 42 import org.junit.Before; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 import org.mockito.Answers; 46 import org.mockito.ArgumentCaptor; 47 import org.mockito.Mock; 48 import org.mockito.MockitoAnnotations; 49 import org.robolectric.RuntimeEnvironment; 50 import org.robolectric.fakes.RoboMenu; 51 52 @RunWith(SettingsRobolectricTestRunner.class) 53 public class BluetoothDeviceDetailsFragmentTest { 54 55 private BluetoothDeviceDetailsFragment mFragment; 56 private Context mContext; 57 58 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 59 private CachedBluetoothDevice mCachedDevice; 60 61 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 62 private LocalBluetoothManager mLocalManager; 63 64 @Before setUp()65 public void setUp() { 66 MockitoAnnotations.initMocks(this); 67 mContext = spy(RuntimeEnvironment.application); 68 FakeFeatureFactory.setupForTest(); 69 70 String deviceAddress = "55:66:77:88:99:AA"; 71 mFragment = spy(BluetoothDeviceDetailsFragment.newInstance(deviceAddress)); 72 doReturn(mLocalManager).when(mFragment).getLocalBluetoothManager(any()); 73 doReturn(mCachedDevice).when(mFragment).getCachedDevice(any()); 74 75 when(mCachedDevice.getAddress()).thenReturn(deviceAddress); 76 Bundle args = new Bundle(); 77 args.putString(BluetoothDeviceDetailsFragment.KEY_DEVICE_ADDRESS, deviceAddress); 78 mFragment.setArguments(args); 79 mFragment.onAttach(mContext); 80 } 81 82 @Test renameControlGetsAdded()83 public void renameControlGetsAdded() { 84 RoboMenu menu = new RoboMenu(mContext); 85 MenuInflater inflater = new MenuInflater(mContext); 86 mFragment.onCreateOptionsMenu(menu, inflater); 87 MenuItem item = menu.getItem(0); 88 assertThat(item.getTitle()).isEqualTo(mContext.getString(R.string.bluetooth_rename_button)); 89 assertThat(item.getIcon()).isEqualTo(mContext.getDrawable(R.drawable.ic_mode_edit)); 90 } 91 92 @Test renameControlClicked()93 public void renameControlClicked() { 94 RoboMenu menu = new RoboMenu(mContext); 95 MenuInflater inflater = new MenuInflater(mContext); 96 mFragment.onCreateOptionsMenu(menu, inflater); 97 MenuItem item = menu.getItem(0); 98 assertThat(item.getItemId()) 99 .isEqualTo(BluetoothDeviceDetailsFragment.EDIT_DEVICE_NAME_ITEM_ID); 100 101 FragmentManager fragmentManager = mock(FragmentManager.class); 102 when(mFragment.getFragmentManager()).thenReturn(fragmentManager); 103 FragmentTransaction ft = mock(FragmentTransaction.class); 104 when(fragmentManager.beginTransaction()).thenReturn(ft); 105 106 ArgumentCaptor<Fragment> captor = ArgumentCaptor.forClass(Fragment.class); 107 mFragment.onOptionsItemSelected(item); 108 verify(ft).add(captor.capture(), eq(RemoteDeviceNameDialogFragment.TAG)); 109 RemoteDeviceNameDialogFragment dialog = (RemoteDeviceNameDialogFragment) captor.getValue(); 110 assertThat(dialog).isNotNull(); 111 } 112 } 113