• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.localepicker;
18 
19 import static com.android.settings.localepicker.LocaleDialogFragment.ARG_DIALOG_TYPE;
20 import static com.android.settings.localepicker.LocaleDialogFragment.ARG_TARGET_LOCALE;
21 import static com.android.settings.localepicker.LocaleDialogFragment.DIALOG_CONFIRM_SYSTEM_DEFAULT;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.verify;
28 
29 import android.os.Bundle;
30 import android.window.OnBackInvokedDispatcher;
31 
32 import androidx.appcompat.app.AlertDialog;
33 import androidx.fragment.app.FragmentActivity;
34 import androidx.fragment.app.FragmentManager;
35 import androidx.fragment.app.FragmentTransaction;
36 
37 import com.android.internal.app.LocaleStore;
38 import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
39 
40 import org.junit.Before;
41 import org.junit.Rule;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.mockito.junit.MockitoJUnit;
46 import org.mockito.junit.MockitoRule;
47 import org.robolectric.Robolectric;
48 import org.robolectric.RobolectricTestRunner;
49 import org.robolectric.annotation.Config;
50 import org.robolectric.shadows.ShadowLooper;
51 
52 import java.util.Locale;
53 
54 @RunWith(RobolectricTestRunner.class)
55 @Config(shadows = {ShadowAlertDialogCompat.class})
56 public class LocaleDialogFragmentTest {
57 
58     @Rule
59     public final MockitoRule mMockitoRule = MockitoJUnit.rule();
60 
61     @Mock
62     private OnBackInvokedDispatcher mOnBackInvokedDispatcher;
63 
64     private FragmentActivity mActivity;
65     private LocaleDialogFragment mDialogFragment;
66 
67     @Before
setUp()68     public void setUp() throws Exception {
69         mActivity = Robolectric.setupActivity(FragmentActivity.class);
70         mDialogFragment = LocaleDialogFragment.newInstance();
71         LocaleStore.LocaleInfo localeInfo = LocaleStore.getLocaleInfo(Locale.ENGLISH);
72         Bundle args = new Bundle();
73         args.putInt(ARG_DIALOG_TYPE, DIALOG_CONFIRM_SYSTEM_DEFAULT);
74         args.putSerializable(ARG_TARGET_LOCALE, localeInfo);
75         mDialogFragment.setArguments(args);
76         FragmentManager fragmentManager = mActivity.getSupportFragmentManager();
77         FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
78         fragmentTransaction.add(mDialogFragment, null);
79         fragmentTransaction.commit();
80         ShadowLooper.idleMainLooper();
81     }
82 
83     @Test
onCreateDialog_onBackInvokedCallbackIsRegistered()84     public void onCreateDialog_onBackInvokedCallbackIsRegistered() {
85         mDialogFragment.setBackDispatcher(mOnBackInvokedDispatcher);
86         mDialogFragment.onCreateDialog(null);
87 
88         verify(mOnBackInvokedDispatcher).registerOnBackInvokedCallback(
89                 eq(OnBackInvokedDispatcher.PRIORITY_DEFAULT), any());
90     }
91 
92     @Test
onBackInvoked_dialogIsStillDisplaying()93     public void onBackInvoked_dialogIsStillDisplaying() {
94         mDialogFragment.setBackDispatcher(mOnBackInvokedDispatcher);
95         AlertDialog alertDialog = (AlertDialog) mDialogFragment.onCreateDialog(null);
96         alertDialog.show();
97         assertThat(alertDialog).isNotNull();
98         assertThat(alertDialog.isShowing()).isTrue();
99 
100         mOnBackInvokedDispatcher.registerOnBackInvokedCallback(
101                 eq(OnBackInvokedDispatcher.PRIORITY_DEFAULT), any());
102 
103         mDialogFragment.getBackInvokedCallback().onBackInvoked();
104 
105         assertThat(alertDialog.isShowing()).isTrue();
106 
107     }
108 }
109