1 /* 2 * Copyright (C) 2016 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.mockito.AdditionalMatchers.not; 20 import static org.mockito.Mockito.*; 21 22 import android.content.Context; 23 import android.content.Context; 24 import android.test.AndroidTestCase; 25 import android.test.suitebuilder.annotation.SmallTest; 26 import android.text.TextUtils; 27 28 import com.android.internal.net.LegacyVpnInfo; 29 import com.android.internal.net.VpnProfile; 30 import com.android.settings.R; 31 import com.android.settings.vpn2.VpnSettings; 32 33 import java.util.Collections; 34 import java.util.HashMap; 35 import java.util.List; 36 import java.util.Map; 37 38 import org.mockito.ArgumentCaptor; 39 import org.mockito.Mock; 40 import org.mockito.MockitoAnnotations; 41 import org.mockito.compat.ArgumentMatcher; 42 43 public class PreferenceListTest extends AndroidTestCase { 44 private static final String TAG = "PreferenceListTest"; 45 46 @Mock VpnSettings mSettings; 47 48 final Map<String, LegacyVpnPreference> mLegacyMocks = new HashMap<>(); 49 final Map<AppVpnInfo, AppPreference> mAppMocks = new HashMap<>(); 50 51 @Override 52 public void setUp() throws Exception { 53 MockitoAnnotations.initMocks(this); 54 55 mLegacyMocks.clear(); 56 mAppMocks.clear(); 57 58 doAnswer(invocation -> { 59 final String key = ((VpnProfile)(invocation.getArguments()[0])).key; 60 if (!mLegacyMocks.containsKey(key)) { 61 mLegacyMocks.put(key, mock(LegacyVpnPreference.class)); 62 } 63 return mLegacyMocks.get(key); 64 }).when(mSettings).findOrCreatePreference(any(VpnProfile.class), anyBoolean()); 65 66 doAnswer(invocation -> { 67 final AppVpnInfo key = (AppVpnInfo)(invocation.getArguments()[0]); 68 if (!mAppMocks.containsKey(key)) { 69 mAppMocks.put(key, mock(AppPreference.class)); 70 } 71 return mAppMocks.get(key); 72 }).when(mSettings).findOrCreatePreference(any(AppVpnInfo.class)); 73 74 doNothing().when(mSettings).setShownPreferences(any()); 75 doReturn(true).when(mSettings).canAddPreferences(); 76 } 77 78 @SmallTest 79 public void testNothingShownByDefault() { 80 final VpnSettings.UpdatePreferences updater = new VpnSettings.UpdatePreferences(mSettings); 81 updater.run(); 82 83 verify(mSettings, never()).findOrCreatePreference(any(VpnProfile.class), anyBoolean()); 84 assertEquals(0, mLegacyMocks.size()); 85 assertEquals(0, mAppMocks.size()); 86 } 87 88 @SmallTest 89 public void testDisconnectedLegacyVpnShown() { 90 final VpnProfile vpnProfile = new VpnProfile("test-disconnected"); 91 92 final VpnSettings.UpdatePreferences updater = new VpnSettings.UpdatePreferences(mSettings); 93 updater.legacyVpns( 94 /* vpnProfiles */ Collections.<VpnProfile>singletonList(vpnProfile), 95 /* connectedLegacyVpns */ Collections.<String, LegacyVpnInfo>emptyMap(), 96 /* lockdownVpnKey */ null); 97 updater.run(); 98 99 verify(mSettings, times(1)).findOrCreatePreference(any(VpnProfile.class), eq(true)); 100 assertEquals(1, mLegacyMocks.size()); 101 assertEquals(0, mAppMocks.size()); 102 } 103 104 @SmallTest 105 public void testConnectedLegacyVpnShownIfDeleted() { 106 final LegacyVpnInfo connectedLegacyVpn =new LegacyVpnInfo(); 107 connectedLegacyVpn.key = "test-connected"; 108 109 final VpnSettings.UpdatePreferences updater = new VpnSettings.UpdatePreferences(mSettings); 110 updater.legacyVpns( 111 /* vpnProfiles */ Collections.<VpnProfile>emptyList(), 112 /* connectedLegacyVpns */ new HashMap<String, LegacyVpnInfo>() {{ 113 put(connectedLegacyVpn.key, connectedLegacyVpn); 114 }}, 115 /* lockdownVpnKey */ null); 116 updater.run(); 117 118 verify(mSettings, times(1)).findOrCreatePreference(any(VpnProfile.class), eq(false)); 119 assertEquals(1, mLegacyMocks.size()); 120 assertEquals(0, mAppMocks.size()); 121 } 122 123 @SmallTest 124 public void testConnectedLegacyVpnShownExactlyOnce() { 125 final VpnProfile vpnProfile = new VpnProfile("test-no-duplicates"); 126 final LegacyVpnInfo connectedLegacyVpn = new LegacyVpnInfo(); 127 connectedLegacyVpn.key = new String(vpnProfile.key); 128 129 final VpnSettings.UpdatePreferences updater = new VpnSettings.UpdatePreferences(mSettings); 130 updater.legacyVpns( 131 /* vpnProfiles */ Collections.<VpnProfile>singletonList(vpnProfile), 132 /* connectedLegacyVpns */ new HashMap<String, LegacyVpnInfo>() {{ 133 put(connectedLegacyVpn.key, connectedLegacyVpn); 134 }}, 135 /* lockdownVpnKey */ null); 136 updater.run(); 137 138 final ArgumentMatcher<VpnProfile> equalsFake = new ArgumentMatcher<VpnProfile>() { 139 @Override 140 public boolean matchesObject(final Object arg) { 141 if (arg == vpnProfile) return true; 142 if (arg == null) return false; 143 return TextUtils.equals(((VpnProfile) arg).key, vpnProfile.key); 144 } 145 }; 146 147 // The VPN profile should have been used to create a preference and set up at laest once 148 // with update=true to fill in all the fields. 149 verify(mSettings, atLeast(1)).findOrCreatePreference(argThat(equalsFake), eq(true)); 150 151 // ...But no other VPN profile key should ever have been passed in. 152 verify(mSettings, never()).findOrCreatePreference(not(argThat(equalsFake)), anyBoolean()); 153 154 // And so we should still have exactly 1 preference created. 155 assertEquals(1, mLegacyMocks.size()); 156 assertEquals(0, mAppMocks.size()); 157 } 158 } 159