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.app.settings.SettingsEnums;
27 import android.content.DialogInterface;
28 import android.content.Intent;
29 import android.os.Bundle;
30 
31 import androidx.appcompat.app.AlertDialog;
32 import androidx.fragment.app.FragmentActivity;
33 
34 import com.android.settings.SettingsActivity;
35 import com.android.settings.bluetooth.BluetoothPairingDetail;
36 import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
37 
38 import org.junit.Before;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.junit.MockitoJUnit;
43 import org.mockito.junit.MockitoRule;
44 import org.robolectric.Robolectric;
45 import org.robolectric.RobolectricTestRunner;
46 import org.robolectric.annotation.Config;
47 import org.robolectric.shadows.ShadowLooper;
48 
49 /** Tests for {@link HearingAidDialogFragment}. */
50 @RunWith(RobolectricTestRunner.class)
51 @Config(shadows = {
52         com.android.settings.testutils.shadow.ShadowFragment.class,
53         ShadowAlertDialogCompat.class,
54 })
55 public class HearingAidDialogFragmentTest {
56 
57     @Rule
58     public MockitoRule mocks = MockitoJUnit.rule();
59 
60     private FragmentActivity mActivity;
61     private HearingAidDialogFragment mFragment;
62 
63     @Before
setUpTestFragment()64     public void setUpTestFragment() {
65         mFragment = spy(HearingAidDialogFragment.newInstance());
66         mActivity = Robolectric.setupActivity(FragmentActivity.class);
67         when(mFragment.getActivity()).thenReturn(mActivity);
68     }
69 
70     @Test
onCreateDialog_dialogExist()71     public void onCreateDialog_dialogExist() {
72         final Dialog dialog = mFragment.onCreateDialog(Bundle.EMPTY);
73 
74         assertThat(dialog).isNotNull();
75     }
76 
77     @Test
dialogPositiveButtonClick_intentToExpectedClass()78     public void dialogPositiveButtonClick_intentToExpectedClass() {
79         final AlertDialog dialog = (AlertDialog) mFragment.onCreateDialog(Bundle.EMPTY);
80         dialog.show();
81 
82         dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
83         ShadowLooper.idleMainLooper();
84 
85         final Intent intent = shadowOf(mActivity).getNextStartedActivity();
86         assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT))
87                 .isEqualTo(BluetoothPairingDetail.class.getName());
88     }
89 
90     @Test
dialogNegativeButtonClick_dismissDialog()91     public void dialogNegativeButtonClick_dismissDialog() {
92         final AlertDialog dialog = (AlertDialog) mFragment.onCreateDialog(Bundle.EMPTY);
93         dialog.show();
94 
95         dialog.getButton(DialogInterface.BUTTON_NEGATIVE).performClick();
96         ShadowLooper.idleMainLooper();
97 
98         assertThat(dialog.isShowing()).isFalse();
99     }
100 
101     @Test
getMetricsCategory_returnsCorrectCategory()102     public void getMetricsCategory_returnsCorrectCategory() {
103         assertThat(mFragment.getMetricsCategory()).isEqualTo(
104                 SettingsEnums.DIALOG_ACCESSIBILITY_HEARINGAID);
105     }
106 }
107