1/* 2 * Copyright (C) 2017 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 17package com.android.settingslib.development; 18 19import static com.google.common.truth.Truth.assertThat; 20import static org.mockito.Mockito.atLeastOnce; 21import static org.mockito.Mockito.doReturn; 22import static org.mockito.Mockito.verify; 23 24import android.arch.lifecycle.LifecycleOwner; 25import android.os.SystemProperties; 26import android.support.v7.preference.ListPreference; 27import android.support.v7.preference.Preference; 28import android.support.v7.preference.PreferenceScreen; 29 30import com.android.settingslib.SettingsLibRobolectricTestRunner; 31import com.android.settingslib.core.lifecycle.Lifecycle; 32 33import org.junit.Before; 34import org.junit.Test; 35import org.junit.runner.RunWith; 36import org.mockito.Mock; 37import org.mockito.MockitoAnnotations; 38import org.robolectric.RuntimeEnvironment; 39 40@RunWith(SettingsLibRobolectricTestRunner.class) 41public class LogpersistPreferenceControllerTest { 42 43 private LifecycleOwner mLifecycleOwner; 44 private Lifecycle mLifecycle; 45 46 @Mock 47 private ListPreference mListPreference; 48 @Mock 49 private PreferenceScreen mPreferenceScreen; 50 51 private AbstractLogpersistPreferenceController mController; 52 53 @Before 54 public void setUp() { 55 MockitoAnnotations.initMocks(this); 56 SystemProperties.set("ro.debuggable", "1"); 57 mLifecycleOwner = () -> mLifecycle; 58 mLifecycle = new Lifecycle(mLifecycleOwner); 59 mController = new AbstractLogpersistPreferenceController(RuntimeEnvironment.application, 60 mLifecycle) { 61 @Override 62 public void showConfirmationDialog(Preference preference) {} 63 64 @Override 65 public void dismissConfirmationDialog() {} 66 67 @Override 68 public boolean isConfirmationDialogShowing() { 69 return false; 70 } 71 }; 72 73 doReturn(mListPreference).when(mPreferenceScreen) 74 .findPreference(mController.getPreferenceKey()); 75 76 mController.displayPreference(mPreferenceScreen); 77 } 78 79 @Test 80 public void testAvailable() { 81 SystemProperties.set("ro.debuggable", ""); 82 assertThat(mController.isAvailable()).isFalse(); 83 SystemProperties.set("ro.debuggable", "1"); 84 assertThat(mController.isAvailable()).isTrue(); 85 SystemProperties.set("ro.debuggable", "0"); 86 assertThat(mController.isAvailable()).isFalse(); 87 } 88 89 @Test 90 public void testUpdateLogpersistValues_null() { 91 SystemProperties.set( 92 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY, 93 AbstractLogpersistPreferenceController.SELECT_LOGPERSIST_PROPERTY_SERVICE); 94 mController.updateLogpersistValues(); 95 verify(mListPreference, atLeastOnce()).setValue("all"); 96 } 97 98 @Test 99 public void testUpdateLogpersistValues_all() { 100 SystemProperties.set( 101 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY, 102 AbstractLogpersistPreferenceController.SELECT_LOGPERSIST_PROPERTY_SERVICE); 103 SystemProperties.set( 104 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY_BUFFER, 105 "all"); 106 mController.updateLogpersistValues(); 107 verify(mListPreference, atLeastOnce()).setValue("all"); 108 } 109 110 @Test 111 public void testUpdateLogpersistValues_defaultSecurityKernel() { 112 SystemProperties.set( 113 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY, 114 AbstractLogpersistPreferenceController.SELECT_LOGPERSIST_PROPERTY_SERVICE); 115 SystemProperties.set( 116 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY_BUFFER, 117 "default,security,kernel"); 118 mController.updateLogpersistValues(); 119 verify(mListPreference, atLeastOnce()).setValue("default,security,kernel"); 120 } 121 122 @Test 123 public void testUpdateLogpersistValues_kernel() { 124 SystemProperties.set( 125 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY, 126 AbstractLogpersistPreferenceController.SELECT_LOGPERSIST_PROPERTY_SERVICE); 127 SystemProperties.set( 128 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY_BUFFER, 129 "kernel"); 130 mController.updateLogpersistValues(); 131 verify(mListPreference, atLeastOnce()).setValue("kernel"); 132 } 133 134 @Test 135 public void testUpdateLogpersistValues_mainSecuritykernel() { 136 SystemProperties.set( 137 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY, 138 AbstractLogpersistPreferenceController.SELECT_LOGPERSIST_PROPERTY_SERVICE); 139 SystemProperties.set( 140 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY_BUFFER, 141 "main,security,kernel"); 142 mController.updateLogpersistValues(); 143 verify(mListPreference, atLeastOnce()).setValue("all"); 144 } 145} 146 147