2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.network;
19 import static android.net.ConnectivityManager.PRIVATE_DNS_MODE_OFF;
20 import static android.net.ConnectivityManager.PRIVATE_DNS_MODE_OPPORTUNISTIC;
21 import static android.net.ConnectivityManager.PRIVATE_DNS_MODE_PROVIDER_HOSTNAME;
22 import static android.provider.Settings.Global.PRIVATE_DNS_MODE;
24 import static com.google.common.truth.Truth.assertThat;
26 import static org.mockito.Mockito.anyInt;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
30 import android.content.ContentResolver;
31 import android.content.Context;
32 import android.content.DialogInterface;
33 import android.net.ConnectivityManager;
34 import android.provider.Settings;
35 import android.view.LayoutInflater;
36 import android.view.View;
37 import android.widget.Button;
38 import android.widget.LinearLayout;
40 import androidx.appcompat.app.AlertDialog;
42 import com.android.settings.R;
43 import com.android.settings.testutils.shadow.ShadowOs;
44 import com.android.settingslib.CustomDialogPreferenceCompat.CustomPreferenceDialogFragment;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 import org.mockito.MockitoAnnotations;
50 import org.robolectric.RobolectricTestRunner;
51 import org.robolectric.RuntimeEnvironment;
52 import org.robolectric.annotation.Config;
53 import org.robolectric.util.ReflectionHelpers;
55 @RunWith(RobolectricTestRunner.class)
56 @Config(shadows = ShadowOs.class)
57 public class PrivateDnsModeDialogPreferenceTest {
59 private static final String HOST_NAME = "dns.example.com";
60 private static final String INVALID_HOST_NAME = "...,";
62 private PrivateDnsModeDialogPreference mPreference;
64 private Context mContext;
65 private Button mSaveButton;
69 MockitoAnnotations.initMocks(this);
71 ReflectionHelpers.setStaticField(android.system.OsConstants.class, "AF_INET", 2);
72 ReflectionHelpers.setStaticField(android.system.OsConstants.class, "AF_INET6", 10);
74 mContext = RuntimeEnvironment.application;
75 mSaveButton = new Button(mContext);
77 final CustomPreferenceDialogFragment fragment = mock(CustomPreferenceDialogFragment.class);
78 final AlertDialog dialog = mock(AlertDialog.class);
79 when(fragment.getDialog()).thenReturn(dialog);
80 when(dialog.getButton(anyInt())).thenReturn(mSaveButton);
82 mPreference = new PrivateDnsModeDialogPreference(mContext);
83 ReflectionHelpers.setField(mPreference, "mFragment", fragment);
85 final LayoutInflater inflater = LayoutInflater.from(mContext);
86 final View view = inflater.inflate(R.layout.private_dns_mode_dialog,
87 new LinearLayout(mContext), false);
89 mPreference.onBindDialogView(view);
93 public void testOnCheckedChanged_dnsModeOff_disableEditText() {
94 mPreference.onCheckedChanged(null, R.id.private_dns_mode_off);
96 assertThat(mPreference.mMode).isEqualTo(PRIVATE_DNS_MODE_OFF);
97 assertThat(mPreference.mEditText.isEnabled()).isFalse();
101 public void testOnCheckedChanged_dnsModeOpportunistic_disableEditText() {
102 mPreference.onCheckedChanged(null, R.id.private_dns_mode_opportunistic);
104 assertThat(mPreference.mMode).isEqualTo(PRIVATE_DNS_MODE_OPPORTUNISTIC);
105 assertThat(mPreference.mEditText.isEnabled()).isFalse();
109 public void testOnCheckedChanged_dnsModeProvider_enableEditText() {
110 mPreference.onCheckedChanged(null, R.id.private_dns_mode_provider);
112 assertThat(mPreference.mMode).isEqualTo(PRIVATE_DNS_MODE_PROVIDER_HOSTNAME);
113 assertThat(mPreference.mEditText.isEnabled()).isTrue();
117 public void testOnBindDialogView_containsCorrectData() {
118 // Don't set settings to the default value ("opportunistic") as that
119 // risks masking failure to read the mode from settings.
120 Settings.Global.putString(mContext.getContentResolver(),
121 PrivateDnsModeDialogPreference.MODE_KEY, PRIVATE_DNS_MODE_OFF);
122 Settings.Global.putString(mContext.getContentResolver(),
123 PrivateDnsModeDialogPreference.HOSTNAME_KEY, HOST_NAME);
125 final LayoutInflater inflater = LayoutInflater.from(mContext);
126 final View view = inflater.inflate(R.layout.private_dns_mode_dialog,
127 new LinearLayout(mContext), false);
128 mPreference.onBindDialogView(view);
130 assertThat(mPreference.mEditText.getText().toString()).isEqualTo(HOST_NAME);
131 assertThat(mPreference.mRadioGroup.getCheckedRadioButtonId()).isEqualTo(
132 R.id.private_dns_mode_off);
136 public void testOnCheckedChanged_switchMode_saveButtonHasCorrectState() {
137 final String[] INVALID_HOST_NAMES = new String[] {
139 "2001:db8::53", // IPv6 string literal
140 "192.168.1.1", // IPv4 string literal
143 for (String invalid : INVALID_HOST_NAMES) {
144 // Set invalid hostname
145 mPreference.mEditText.setText(invalid);
147 mPreference.onCheckedChanged(null, R.id.private_dns_mode_off);
148 assertThat(mSaveButton.isEnabled()).named("off: " + invalid).isTrue();
150 mPreference.onCheckedChanged(null, R.id.private_dns_mode_opportunistic);
151 assertThat(mSaveButton.isEnabled()).named("opportunistic: " + invalid).isTrue();
153 mPreference.onCheckedChanged(null, R.id.private_dns_mode_provider);
154 assertThat(mSaveButton.isEnabled()).named("provider: " + invalid).isFalse();
159 public void testOnClick_positiveButtonClicked_saveData() {
160 // Set the default settings to OFF
161 final ContentResolver contentResolver = mContext.getContentResolver();
162 Settings.Global.putString(contentResolver, PRIVATE_DNS_MODE, PRIVATE_DNS_MODE_OFF);
164 mPreference.mMode = ConnectivityManager.PRIVATE_DNS_MODE_OPPORTUNISTIC;
165 mPreference.onClick(null, DialogInterface.BUTTON_POSITIVE);
167 // Change to OPPORTUNISTIC
168 assertThat(Settings.Global.getString(contentResolver, PRIVATE_DNS_MODE)).isEqualTo(
169 PRIVATE_DNS_MODE_OPPORTUNISTIC);
173 public void testOnClick_negativeButtonClicked_doNothing() {
174 // Set the default settings to OFF
175 final ContentResolver contentResolver = mContext.getContentResolver();
176 Settings.Global.putString(contentResolver, PRIVATE_DNS_MODE, PRIVATE_DNS_MODE_OFF);
178 mPreference.mMode = ConnectivityManager.PRIVATE_DNS_MODE_OPPORTUNISTIC;
179 mPreference.onClick(null, DialogInterface.BUTTON_NEGATIVE);
181 // Still equal to OFF
182 assertThat(Settings.Global.getString(contentResolver,
183 Settings.Global.PRIVATE_DNS_MODE)).isEqualTo(
184 ConnectivityManager.PRIVATE_DNS_MODE_OFF);