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