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.anyBoolean;
23 import static org.mockito.ArgumentMatchers.anyInt;
24 import static org.mockito.ArgumentMatchers.anyString;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 
29 import android.view.View;
30 import android.widget.Button;
31 
32 import androidx.fragment.app.FragmentManager;
33 import androidx.fragment.app.FragmentTransaction;
34 
35 import com.android.settings.R;
36 import com.android.settingslib.widget.ActionButtonsPreference;
37 
38 import org.junit.Ignore;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.ArgumentCaptor;
42 import org.robolectric.RobolectricTestRunner;
43 import org.robolectric.RuntimeEnvironment;
44 
45 @RunWith(RobolectricTestRunner.class)
46 @Ignore
47 public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsControllerTestBase {
48     private BluetoothDetailsButtonsController mController;
49     private ActionButtonsPreference mButtonsPref;
50     private Button mConnectButton;
51     private Button mForgetButton;
52 
53     @Override
setUp()54     public void setUp() {
55         super.setUp();
56         final View buttons = View.inflate(
57                 RuntimeEnvironment.application,
58                 com.android.settingslib.widget.preference.actionbuttons.R.layout.settingslib_action_buttons,
59                 null /* parent */);
60         mConnectButton = buttons.findViewById(com.android.settingslib.widget.preference.actionbuttons.R.id.button2);
61         mForgetButton = buttons.findViewById(com.android.settingslib.widget.preference.actionbuttons.R.id.button1);
62         mController =
63                 new BluetoothDetailsButtonsController(mContext, mFragment, mCachedDevice,
64                         mLifecycle);
65         mButtonsPref = createMock();
66         when(mButtonsPref.getKey()).thenReturn(mController.getPreferenceKey());
67         when(mButtonsPref.setButton2OnClickListener(any(View.OnClickListener.class)))
68                 .thenAnswer(invocation -> {
69                     final Object[] args = invocation.getArguments();
70                     mConnectButton.setOnClickListener((View.OnClickListener) args[0]);
71                     return mButtonsPref;
72                 });
73         when(mButtonsPref.setButton1OnClickListener(any(View.OnClickListener.class)))
74                 .thenAnswer(invocation -> {
75                     final Object[] args = invocation.getArguments();
76                     mForgetButton.setOnClickListener((View.OnClickListener) args[0]);
77                     return mButtonsPref;
78                 });
79         mScreen.addPreference(mButtonsPref);
80         setupDevice(mDeviceConfig);
81         when(mCachedDevice.isBusy()).thenReturn(false);
82     }
83 
84     @Test
connected()85     public void connected() {
86         showScreen(mController);
87 
88         verify(mButtonsPref).setButton2Text(R.string.bluetooth_device_context_disconnect);
89         verify(mButtonsPref).setButton1Text(R.string.forget);
90     }
91 
92     @Test
clickOnDisconnect()93     public void clickOnDisconnect() {
94         showScreen(mController);
95         mConnectButton.callOnClick();
96 
97         verify(mCachedDevice).disconnect();
98     }
99 
100     @Test
clickOnConnect()101     public void clickOnConnect() {
102         when(mCachedDevice.isConnected()).thenReturn(false);
103         showScreen(mController);
104 
105         verify(mButtonsPref).setButton2Text(R.string.bluetooth_device_context_connect);
106 
107         mConnectButton.callOnClick();
108         verify(mCachedDevice).connect();
109     }
110 
111     @Test
becomeDisconnected()112     public void becomeDisconnected() {
113         showScreen(mController);
114         // By default we start out with the device connected.
115         verify(mButtonsPref).setButton2Text(R.string.bluetooth_device_context_disconnect);
116 
117         // Now make the device appear to have changed to disconnected.
118         when(mCachedDevice.isConnected()).thenReturn(false);
119         mController.onDeviceAttributesChanged();
120         verify(mButtonsPref).setButton2Text(R.string.bluetooth_device_context_connect);
121 
122         // Click the button and make sure that connect (not disconnect) gets called.
123         mConnectButton.callOnClick();
124         verify(mCachedDevice).connect();
125     }
126 
127     @Test
becomeConnected()128     public void becomeConnected() {
129         // Start out with the device disconnected.
130         when(mCachedDevice.isConnected()).thenReturn(false);
131         showScreen(mController);
132 
133         verify(mButtonsPref).setButton2Text(R.string.bluetooth_device_context_connect);
134 
135 
136         // Now make the device appear to have changed to connected.
137         when(mCachedDevice.isConnected()).thenReturn(true);
138         mController.onDeviceAttributesChanged();
139         verify(mButtonsPref).setButton2Text(R.string.bluetooth_device_context_disconnect);
140 
141         // Click the button and make sure that disconnect (not connect) gets called.
142         mConnectButton.callOnClick();
143         verify(mCachedDevice).disconnect();
144     }
145 
146     @Test
forgetDialog()147     public void forgetDialog() {
148         showScreen(mController);
149         FragmentManager fragmentManager = mock(FragmentManager.class);
150         when(mFragment.getFragmentManager()).thenReturn(fragmentManager);
151         FragmentTransaction ft = mock(FragmentTransaction.class);
152         when(fragmentManager.beginTransaction()).thenReturn(ft);
153         mForgetButton.callOnClick();
154 
155         ArgumentCaptor<ForgetDeviceDialogFragment> dialogCaptor =
156                 ArgumentCaptor.forClass(ForgetDeviceDialogFragment.class);
157         verify(ft).add(dialogCaptor.capture(), anyString());
158 
159         ForgetDeviceDialogFragment dialogFragment = dialogCaptor.getValue();
160         assertThat(dialogFragment).isNotNull();
161     }
162 
163     @Test
startsOutBusy()164     public void startsOutBusy() {
165         when(mCachedDevice.isBusy()).thenReturn(true);
166         showScreen(mController);
167 
168         verify(mButtonsPref).setButton2Text(R.string.bluetooth_device_context_disconnect);
169         verify(mButtonsPref).setButton2Enabled(false);
170         verify(mButtonsPref).setButton1Text(R.string.forget);
171 
172         // Now pretend the device became non-busy.
173         when(mCachedDevice.isBusy()).thenReturn(false);
174         mController.onDeviceAttributesChanged();
175 
176         verify(mButtonsPref).setButton2Enabled(true);
177     }
178 
179     @Test
becomesBusy()180     public void becomesBusy() {
181         showScreen(mController);
182         verify(mButtonsPref).setButton2Enabled(true);
183 
184         // Now pretend the device became busy.
185         when(mCachedDevice.isBusy()).thenReturn(true);
186         mController.onDeviceAttributesChanged();
187 
188         verify(mButtonsPref).setButton2Enabled(false);
189     }
190 
createMock()191     private ActionButtonsPreference createMock() {
192         final ActionButtonsPreference pref = mock(ActionButtonsPreference.class);
193         when(pref.setButton1Text(anyInt())).thenReturn(pref);
194         when(pref.setButton1Icon(anyInt())).thenReturn(pref);
195         when(pref.setButton1Enabled(anyBoolean())).thenReturn(pref);
196         when(pref.setButton1Visible(anyBoolean())).thenReturn(pref);
197         when(pref.setButton1OnClickListener(any(View.OnClickListener.class))).thenReturn(pref);
198 
199         when(pref.setButton2Text(anyInt())).thenReturn(pref);
200         when(pref.setButton2Icon(anyInt())).thenReturn(pref);
201         when(pref.setButton2Enabled(anyBoolean())).thenReturn(pref);
202         when(pref.setButton2Visible(anyBoolean())).thenReturn(pref);
203         when(pref.setButton2OnClickListener(any(View.OnClickListener.class))).thenReturn(pref);
204 
205         return pref;
206     }
207 }
208