• 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 android.bluetooth.BluetoothDevice.BOND_NONE;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.Matchers.eq;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.spy;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30 
31 import android.content.Context;
32 import android.os.Bundle;
33 import android.view.MenuInflater;
34 import android.view.MenuItem;
35 
36 import androidx.fragment.app.Fragment;
37 import androidx.fragment.app.FragmentManager;
38 import androidx.fragment.app.FragmentTransaction;
39 import androidx.preference.PreferenceScreen;
40 
41 import com.android.settings.R;
42 import com.android.settings.testutils.FakeFeatureFactory;
43 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
44 import com.android.settingslib.bluetooth.LocalBluetoothManager;
45 
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 import org.mockito.Answers;
50 import org.mockito.ArgumentCaptor;
51 import org.mockito.Mock;
52 import org.mockito.MockitoAnnotations;
53 import org.robolectric.RobolectricTestRunner;
54 import org.robolectric.RuntimeEnvironment;
55 import org.robolectric.fakes.RoboMenu;
56 
57 @RunWith(RobolectricTestRunner.class)
58 public class BluetoothDeviceDetailsFragmentTest {
59 
60     private static final String TEST_ADDRESS = "55:66:77:88:99:AA";
61 
62     private BluetoothDeviceDetailsFragment mFragment;
63     private Context mContext;
64     private RoboMenu mMenu;
65     private MenuInflater mInflater;
66     private FragmentTransaction mFragmentTransaction;
67 
68     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
69     private CachedBluetoothDevice mCachedDevice;
70     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
71     private LocalBluetoothManager mLocalManager;
72     @Mock
73     private PreferenceScreen mPreferenceScreen;
74 
75     @Before
setUp()76     public void setUp() {
77         MockitoAnnotations.initMocks(this);
78         mContext = spy(RuntimeEnvironment.application);
79         FakeFeatureFactory.setupForTest();
80 
81         mFragment = spy(BluetoothDeviceDetailsFragment.newInstance(TEST_ADDRESS));
82         doReturn(mLocalManager).when(mFragment).getLocalBluetoothManager(any());
83         doReturn(mCachedDevice).when(mFragment).getCachedDevice(any());
84         doReturn(mPreferenceScreen).when(mFragment).getPreferenceScreen();
85 
86         FragmentManager fragmentManager = mock(FragmentManager.class);
87         when(mFragment.getFragmentManager()).thenReturn(fragmentManager);
88         mFragmentTransaction = mock(FragmentTransaction.class);
89         when(fragmentManager.beginTransaction()).thenReturn(mFragmentTransaction);
90 
91         when(mCachedDevice.getAddress()).thenReturn(TEST_ADDRESS);
92         Bundle args = new Bundle();
93         args.putString(BluetoothDeviceDetailsFragment.KEY_DEVICE_ADDRESS, TEST_ADDRESS);
94         mFragment.setArguments(args);
95         mFragment.onAttach(mContext);
96 
97         mMenu = new RoboMenu(mContext);
98         mInflater = new MenuInflater(mContext);
99     }
100 
101     @Test
verifyOnAttachResult()102     public void verifyOnAttachResult() {
103         assertThat(mFragment.mDeviceAddress).isEqualTo(TEST_ADDRESS);
104         assertThat(mFragment.mManager).isEqualTo(mLocalManager);
105         assertThat(mFragment.mCachedDevice).isEqualTo(mCachedDevice);
106     }
107 
108     @Test
getTitle_displayEditTitle()109     public void getTitle_displayEditTitle() {
110         mFragment.onCreateOptionsMenu(mMenu, mInflater);
111 
112         final MenuItem item = mMenu.getItem(0);
113 
114         assertThat(item.getTitle()).isEqualTo(mContext.getString(R.string.bluetooth_rename_button));
115     }
116 
117     @Test
editMenu_clicked_showDialog()118     public void editMenu_clicked_showDialog() {
119         mFragment.onCreateOptionsMenu(mMenu, mInflater);
120         final MenuItem item = mMenu.getItem(0);
121         ArgumentCaptor<Fragment> captor = ArgumentCaptor.forClass(Fragment.class);
122 
123         mFragment.onOptionsItemSelected(item);
124 
125         assertThat(item.getItemId())
126             .isEqualTo(BluetoothDeviceDetailsFragment.EDIT_DEVICE_NAME_ITEM_ID);
127         verify(mFragmentTransaction).add(captor.capture(), eq(RemoteDeviceNameDialogFragment.TAG));
128         RemoteDeviceNameDialogFragment dialog = (RemoteDeviceNameDialogFragment) captor.getValue();
129         assertThat(dialog).isNotNull();
130     }
131 
132     @Test
finishFragmentIfNecessary_deviceIsBondNone_finishFragment()133     public void finishFragmentIfNecessary_deviceIsBondNone_finishFragment() {
134         when(mCachedDevice.getBondState()).thenReturn(BOND_NONE);
135 
136         mFragment.finishFragmentIfNecessary();
137 
138         verify(mFragment).finish();
139     }
140 }
141