1 /*
2  * Copyright (C) 2021 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.vpn2;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.mockito.Mockito.spy;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.Context;
25 import android.content.res.Resources;
26 import android.util.AttributeSet;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 
30 import androidx.preference.PreferenceViewHolder;
31 import androidx.test.core.app.ApplicationProvider;
32 import androidx.test.ext.junit.runners.AndroidJUnit4;
33 
34 import com.android.settings.testutils.ResourcesUtils;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 
40 @RunWith(AndroidJUnit4.class)
41 public class VpnInfoPreferenceTest {
42 
43     private Context mContext;
44     private Resources mResources;
45     private VpnInfoPreference mVpnInfoPreference;
46     private AttributeSet mAttrs;
47     private PreferenceViewHolder mHolder;
48     private View mWarningButton;
49 
50     @Before
setUp()51     public void setUp() {
52         mContext = spy(ApplicationProvider.getApplicationContext());
53         mResources = spy(mContext.getResources());
54         when(mContext.getResources()).thenReturn(mResources);
55 
56         final int helpUrlId = ResourcesUtils.getResourcesId(
57                 mContext, "string", "help_url_insecure_vpn");
58         when(mResources.getString(helpUrlId)).thenReturn("https://www.google.com/");
59 
60         mVpnInfoPreference = new VpnInfoPreference(mContext, mAttrs);
61         LayoutInflater inflater = mContext.getSystemService(LayoutInflater.class);
62 
63         // The VpnInfoPreference is a RestrictedPreference, which is a TwoTargetPreference
64         final int layoutId = ResourcesUtils.getResourcesId(
65                 mContext, "layout", "preference_two_target");
66         View view = inflater.inflate(
67                 layoutId,
68                 null /* root */, false /* attachToRoot */);
69         mHolder = spy(PreferenceViewHolder.createInstanceForTests(view));
70         final int warningButtonId = ResourcesUtils.getResourcesId(
71                 mContext, "id", "warning_button");
72         mWarningButton = spy(new View(mContext));
73         when(mWarningButton.getId()).thenReturn(warningButtonId);
74         when(mHolder.findViewById(warningButtonId)).thenReturn(mWarningButton);
75     }
76 
77     @Test
onBindViewHolder_notInsecureVpn_iconInvisible()78     public void onBindViewHolder_notInsecureVpn_iconInvisible() {
79         mVpnInfoPreference.setInsecureVpn(false);
80 
81         mVpnInfoPreference.onBindViewHolder(mHolder);
82 
83         verify(mWarningButton).setVisibility(View.GONE);
84         verify(mWarningButton).setEnabled(false);
85     }
86 
87     @Test
onBindViewHolder_emptyUrl_iconInvisible()88     public void onBindViewHolder_emptyUrl_iconInvisible() {
89         final int helpUrlId = ResourcesUtils.getResourcesId(
90                 mContext, "string", "help_url_insecure_vpn");
91         when(mResources.getString(helpUrlId)).thenReturn("");
92         VpnInfoPreference vpnInfoPreference = new VpnInfoPreference(mContext, mAttrs);
93 
94         vpnInfoPreference.setInsecureVpn(true);
95 
96         vpnInfoPreference.onBindViewHolder(mHolder);
97 
98         verify(mWarningButton).setVisibility(View.GONE);
99         verify(mWarningButton).setEnabled(false);
100     }
101 
102     @Test
onBindViewHolder_insecureVpn_iconVisible()103     public void onBindViewHolder_insecureVpn_iconVisible() {
104         mVpnInfoPreference.setInsecureVpn(true);
105 
106         mVpnInfoPreference.onBindViewHolder(mHolder);
107 
108         verify(mWarningButton).setVisibility(View.VISIBLE);
109         verify(mWarningButton).setEnabled(true);
110     }
111 
112     @Test
onBindViewHolder_dividerInvisible()113     public void onBindViewHolder_dividerInvisible() {
114         mVpnInfoPreference.onBindViewHolder(mHolder);
115 
116         final int dividerId = ResourcesUtils.getResourcesId(mContext, "id", "two_target_divider");
117         final View divider = mHolder.findViewById(dividerId);
118         assertEquals(View.GONE, divider.getVisibility());
119     }
120 }
121