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 com.google.common.truth.Truth.assertThat; 20 21 import android.content.Context; 22 import android.util.AttributeSet; 23 24 import androidx.test.core.app.ApplicationProvider; 25 import androidx.test.ext.junit.runners.AndroidJUnit4; 26 27 import com.android.settings.testutils.ResourcesUtils; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 33 /** Unittest for ManageablePreference */ 34 @RunWith(AndroidJUnit4.class) 35 public class ManageablePreferenceTest { 36 private static final int STATE_DISCONNECTED = 0; 37 38 private Context mContext = ApplicationProvider.getApplicationContext(); 39 private TestManageablePreference mManageablePreference; 40 41 public class TestManageablePreference extends ManageablePreference { TestManageablePreference(Context context, AttributeSet attrs)42 public TestManageablePreference(Context context, AttributeSet attrs) { 43 super(context, attrs); 44 } 45 } 46 47 @Before setUp()48 public void setUp() { 49 mManageablePreference = new TestManageablePreference(mContext, null); 50 } 51 52 @Test setAlwaysOnVpn_summaryUpdatedCorrectly()53 public void setAlwaysOnVpn_summaryUpdatedCorrectly() { 54 mManageablePreference.setAlwaysOn(true); 55 final String alwaysOnString = ResourcesUtils.getResourcesString( 56 mContext, "vpn_always_on_summary_active"); 57 // Setting always-on should automatically call UpdateSummary 58 assertThat(mManageablePreference.getSummary()).isEqualTo(alwaysOnString); 59 } 60 61 @Test setInsecure_summaryUpdatedCorrectly()62 public void setInsecure_summaryUpdatedCorrectly() { 63 mManageablePreference.setInsecureVpn(true); 64 final String insecureString = ResourcesUtils.getResourcesString( 65 mContext, "vpn_insecure_summary"); 66 // Setting isInsecure should automatically call UpdateSummary 67 assertThat(mManageablePreference.getSummary().toString()).isEqualTo(insecureString); 68 } 69 70 @Test setState_accuratelySet()71 public void setState_accuratelySet() { 72 mManageablePreference.setState(STATE_DISCONNECTED); 73 assertThat(mManageablePreference.getState()).isEqualTo(STATE_DISCONNECTED); 74 } 75 76 @Test setState_summaryUpdatedCorrectly()77 public void setState_summaryUpdatedCorrectly() { 78 mManageablePreference.setState(STATE_DISCONNECTED); 79 // Setting the state should automatically call UpdateSummary 80 // Fetch the appropriate string from the array resource 81 int id = ResourcesUtils.getResourcesId(mContext, "array", "vpn_states"); 82 String[] states = mContext.getResources().getStringArray(id); 83 String disconnectedStr = states[STATE_DISCONNECTED]; 84 assertThat(mManageablePreference.getSummary()).isEqualTo(disconnectedStr); 85 } 86 } 87