Home | History | Annotate | Download | only in bluetooth
      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.ArgumentMatchers.any;
     21 import static org.mockito.Mockito.doReturn;
     22 import static org.mockito.Mockito.never;
     23 import static org.mockito.Mockito.spy;
     24 import static org.mockito.Mockito.verify;
     25 import static org.mockito.Mockito.when;
     26 
     27 import android.app.Activity;
     28 import android.app.AlertDialog;
     29 
     30 import com.android.settings.testutils.FakeFeatureFactory;
     31 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     32 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
     33 
     34 import org.junit.Before;
     35 import org.junit.Test;
     36 import org.junit.runner.RunWith;
     37 import org.mockito.Answers;
     38 import org.mockito.Mock;
     39 import org.mockito.MockitoAnnotations;
     40 import org.robolectric.Robolectric;
     41 import org.robolectric.shadows.ShadowDialog;
     42 
     43 @RunWith(SettingsRobolectricTestRunner.class)
     44 public class ForgetDeviceDialogFragmentTest {
     45 
     46     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     47     private CachedBluetoothDevice mCachedDevice;
     48 
     49     private ForgetDeviceDialogFragment mFragment;
     50     private Activity mActivity;
     51     private AlertDialog mDialog;
     52 
     53     @Before
     54     public void setUp() {
     55         MockitoAnnotations.initMocks(this);
     56         FakeFeatureFactory.setupForTest();
     57         String deviceAddress = "55:66:77:88:99:AA";
     58         when(mCachedDevice.getAddress()).thenReturn(deviceAddress);
     59         mFragment = spy(ForgetDeviceDialogFragment.newInstance(deviceAddress));
     60         doReturn(mCachedDevice).when(mFragment).getDevice(any());
     61         mActivity = Robolectric.setupActivity(Activity.class);
     62         mActivity.getFragmentManager().beginTransaction().add(mFragment, null).commit();
     63         mDialog = (AlertDialog) ShadowDialog.getLatestDialog();
     64     }
     65 
     66     @Test
     67     public void cancelDialog() {
     68         mDialog.getButton(AlertDialog.BUTTON_NEGATIVE).performClick();
     69         verify(mCachedDevice, never()).unpair();
     70         assertThat(mActivity.isFinishing()).isFalse();
     71     }
     72 
     73     @Test
     74     public void confirmDialog() {
     75         mDialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick();
     76         verify(mCachedDevice).unpair();
     77         assertThat(mActivity.isFinishing()).isTrue();
     78     }
     79 }
     80