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 org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.spy;
28 import static org.mockito.Mockito.verify;
29 
30 import android.app.settings.SettingsEnums;
31 import android.content.Context;
32 import android.os.Bundle;
33 
34 import androidx.test.annotation.UiThreadTest;
35 import androidx.test.core.app.ApplicationProvider;
36 
37 import com.android.internal.app.LocaleStore;
38 import com.android.settings.testutils.FakeFeatureFactory;
39 import com.android.settings.testutils.ResourcesUtils;
40 
41 import org.junit.Before;
42 import org.junit.Rule;
43 import org.junit.Test;
44 import org.mockito.junit.MockitoJUnit;
45 import org.mockito.junit.MockitoRule;
46 
47 import java.util.Locale;
48 
49 @UiThreadTest
50 public class LocaleDialogFragmentTest {
51     @Rule
52     public final MockitoRule mockito = MockitoJUnit.rule();
53 
54     private Context mContext;
55     private LocaleListEditor mLocaleListEditor;
56     private LocaleDialogFragment mDialogFragment;
57     private FakeFeatureFactory mFeatureFactory;
58 
59     @Before
setUp()60     public void setUp() throws Exception {
61         mContext = ApplicationProvider.getApplicationContext();
62         mDialogFragment = new LocaleDialogFragment();
63         mLocaleListEditor = spy(new LocaleListEditor());
64         mFeatureFactory = FakeFeatureFactory.setupForTest();
65     }
66 
setArgument(int type)67     private void setArgument(int type) {
68         LocaleStore.LocaleInfo localeInfo = LocaleStore.getLocaleInfo(Locale.ENGLISH);
69         Bundle args = new Bundle();
70         args.putInt(ARG_DIALOG_TYPE, type);
71         args.putSerializable(ARG_TARGET_LOCALE, localeInfo);
72         mDialogFragment.setArguments(args);
73     }
74 
75     @Test
getDialogContent_confirmSystemDefault_has2ButtonText()76     public void getDialogContent_confirmSystemDefault_has2ButtonText() {
77         setArgument(DIALOG_CONFIRM_SYSTEM_DEFAULT);
78         LocaleDialogFragment.LocaleDialogController controller =
79                 mDialogFragment.getLocaleDialogController(mContext, mDialogFragment,
80                         mLocaleListEditor);
81 
82         LocaleDialogFragment.LocaleDialogController.DialogContent dialogContent =
83                 controller.getDialogContent();
84 
85         assertEquals(ResourcesUtils.getResourcesString(
86                         mContext, "button_label_confirmation_of_system_locale_change"),
87                 dialogContent.mPositiveButton);
88         assertEquals(ResourcesUtils.getResourcesString(mContext, "cancel"),
89                 dialogContent.mNegativeButton);
90     }
91 
92     @Test
getDialogContent_unavailableLocale_has1ButtonText()93     public void getDialogContent_unavailableLocale_has1ButtonText() {
94         setArgument(LocaleDialogFragment.DIALOG_NOT_AVAILABLE_LOCALE);
95         LocaleDialogFragment.LocaleDialogController controller =
96                 mDialogFragment.getLocaleDialogController(mContext, mDialogFragment,
97                         mLocaleListEditor);
98 
99         LocaleDialogFragment.LocaleDialogController.DialogContent dialogContent =
100                 controller.getDialogContent();
101 
102         assertEquals(ResourcesUtils.getResourcesString(mContext, "okay"),
103                 dialogContent.mPositiveButton);
104         assertTrue(dialogContent.mNegativeButton.isEmpty());
105     }
106 
107     @Test
getMetricsCategory_systemLocaleChange()108     public void getMetricsCategory_systemLocaleChange() {
109         setArgument(DIALOG_CONFIRM_SYSTEM_DEFAULT);
110         int result = mDialogFragment.getMetricsCategory();
111 
112         assertEquals(SettingsEnums.DIALOG_SYSTEM_LOCALE_CHANGE, result);
113     }
114 
115     @Test
getMetricsCategory_unavailableLocale()116     public void getMetricsCategory_unavailableLocale() {
117         setArgument(LocaleDialogFragment.DIALOG_NOT_AVAILABLE_LOCALE);
118         int result = mDialogFragment.getMetricsCategory();
119 
120         assertEquals(SettingsEnums.DIALOG_SYSTEM_LOCALE_UNAVAILABLE, result);
121     }
122 }
123