Tony Mantler | c48ba6a | 2017-07-31 10:58:53 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.settingslib.development; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
Tony Mantler | c48ba6a | 2017-07-31 10:58:53 -0700 | [diff] [blame] | 20 | import static org.mockito.Mockito.atLeastOnce; |
| 21 | import static org.mockito.Mockito.doReturn; |
| 22 | import static org.mockito.Mockito.verify; |
| 23 | |
Aurimas Liutikas | fd52c14 | 2018-04-17 09:50:46 -0700 | [diff] [blame] | 24 | import androidx.lifecycle.LifecycleOwner; |
Tony Mantler | c48ba6a | 2017-07-31 10:58:53 -0700 | [diff] [blame] | 25 | import android.os.SystemProperties; |
Aurimas Liutikas | fd52c14 | 2018-04-17 09:50:46 -0700 | [diff] [blame] | 26 | import androidx.preference.ListPreference; |
| 27 | import androidx.preference.Preference; |
| 28 | import androidx.preference.PreferenceScreen; |
Tony Mantler | c48ba6a | 2017-07-31 10:58:53 -0700 | [diff] [blame] | 29 | |
| 30 | import com.android.settingslib.SettingsLibRobolectricTestRunner; |
Tony Mantler | c48ba6a | 2017-07-31 10:58:53 -0700 | [diff] [blame] | 31 | import com.android.settingslib.core.lifecycle.Lifecycle; |
| 32 | |
| 33 | import org.junit.Before; |
| 34 | import org.junit.Test; |
| 35 | import org.junit.runner.RunWith; |
| 36 | import org.mockito.Mock; |
| 37 | import org.mockito.MockitoAnnotations; |
| 38 | import org.robolectric.RuntimeEnvironment; |
Tony Mantler | c48ba6a | 2017-07-31 10:58:53 -0700 | [diff] [blame] | 39 | |
| 40 | @RunWith(SettingsLibRobolectricTestRunner.class) |
Tony Mantler | c48ba6a | 2017-07-31 10:58:53 -0700 | [diff] [blame] | 41 | public class LogpersistPreferenceControllerTest { |
| 42 | |
Fan Zhang | 0cc6164 | 2018-01-16 14:31:51 -0800 | [diff] [blame] | 43 | private LifecycleOwner mLifecycleOwner; |
Tony Mantler | ece840b | 2017-11-10 13:16:59 -0800 | [diff] [blame] | 44 | private Lifecycle mLifecycle; |
Tony Mantler | c48ba6a | 2017-07-31 10:58:53 -0700 | [diff] [blame] | 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"); |
Fan Zhang | 0cc6164 | 2018-01-16 14:31:51 -0800 | [diff] [blame] | 57 | mLifecycleOwner = () -> mLifecycle; |
| 58 | mLifecycle = new Lifecycle(mLifecycleOwner); |
Tony Mantler | c48ba6a | 2017-07-31 10:58:53 -0700 | [diff] [blame] | 59 | mController = new AbstractLogpersistPreferenceController(RuntimeEnvironment.application, |
| 60 | mLifecycle) { |
| 61 | @Override |
Tony Mantler | 265e7fb | 2017-08-09 14:18:19 -0700 | [diff] [blame] | 62 | public void showConfirmationDialog(Preference preference) {} |
Tony Mantler | c48ba6a | 2017-07-31 10:58:53 -0700 | [diff] [blame] | 63 | |
Tony Mantler | 265e7fb | 2017-08-09 14:18:19 -0700 | [diff] [blame] | 64 | @Override |
| 65 | public void dismissConfirmationDialog() {} |
| 66 | |
| 67 | @Override |
| 68 | public boolean isConfirmationDialogShowing() { |
| 69 | return false; |
Tony Mantler | c48ba6a | 2017-07-31 10:58:53 -0700 | [diff] [blame] | 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 | |