• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.accessibility;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.spy;
22 import static org.mockito.Mockito.when;
23 import static org.robolectric.Shadows.shadowOf;
24 
25 import android.app.Dialog;
26 import android.content.DialogInterface;
27 import android.content.Intent;
28 import android.os.Bundle;
29 
30 import androidx.appcompat.app.AlertDialog;
31 import androidx.fragment.app.FragmentActivity;
32 
33 import com.android.settings.SettingsActivity;
34 import com.android.settings.bluetooth.BluetoothPairingDetail;
35 import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
36 
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.junit.MockitoJUnit;
42 import org.mockito.junit.MockitoRule;
43 import org.robolectric.Robolectric;
44 import org.robolectric.RobolectricTestRunner;
45 import org.robolectric.annotation.Config;
46 
47 
48 @RunWith(RobolectricTestRunner.class)
49 @Config(shadows = ShadowAlertDialogCompat.class)
50 public class HearingAidDialogFragmentTest {
51 
52     @Rule
53     public MockitoRule mocks = MockitoJUnit.rule();
54 
55     private FragmentActivity mActivity;
56     private HearingAidDialogFragment mFragment;
57 
58     @Before
setUpTestFragment()59     public void setUpTestFragment() {
60         mFragment = spy(HearingAidDialogFragment.newInstance());
61         mActivity = Robolectric.setupActivity(FragmentActivity.class);
62         when(mFragment.getActivity()).thenReturn(mActivity);
63     }
64 
65     @Test
onCreateDialog_dialogExist()66     public void onCreateDialog_dialogExist() {
67         final Dialog dialog = mFragment.onCreateDialog(Bundle.EMPTY);
68 
69         assertThat(dialog).isNotNull();
70     }
71 
72     @Test
dialogPositiveButtonClick_intentToExpectedClass()73     public void dialogPositiveButtonClick_intentToExpectedClass() {
74         final AlertDialog dialog = (AlertDialog) mFragment.onCreateDialog(Bundle.EMPTY);
75         dialog.show();
76 
77         dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
78 
79         final Intent intent = shadowOf(mActivity).getNextStartedActivity();
80         assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT))
81                 .isEqualTo(BluetoothPairingDetail.class.getName());
82     }
83 
84     @Test
dialogNegativeButtonClick_dismissDialog()85     public void dialogNegativeButtonClick_dismissDialog() {
86         final AlertDialog dialog = (AlertDialog) mFragment.onCreateDialog(Bundle.EMPTY);
87         dialog.show();
88 
89         dialog.getButton(DialogInterface.BUTTON_NEGATIVE).performClick();
90 
91         assertThat(dialog.isShowing()).isFalse();
92     }
93 }
94